forked from ashawkey/torch-ngp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_sdf.py
63 lines (46 loc) · 2.55 KB
/
main_sdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import torch
import argparse
from sdf.utils import *
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('path', type=str)
parser.add_argument('--test', action='store_true', help="test mode")
parser.add_argument('--workspace', type=str, default='workspace')
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--lr', type=float, default=1e-4, help="initial learning rate")
parser.add_argument('--fp16', action='store_true', help="use amp mixed precision training")
parser.add_argument('--ff', action='store_true', help="use fully-fused MLP")
parser.add_argument('--tcnn', action='store_true', help="use TCNN backend")
opt = parser.parse_args()
print(opt)
seed_everything(opt.seed)
if opt.ff:
assert opt.fp16, "fully-fused mode must be used with fp16 mode"
from sdf.netowrk_ff import SDFNetwork
elif opt.tcnn:
assert opt.fp16, "tcnn mode must be used with fp16 mode"
from sdf.network_tcnn import SDFNetwork
else:
from sdf.netowrk import SDFNetwork
model = SDFNetwork(encoding="hashgrid")
print(model)
if opt.test:
trainer = Trainer('ngp', model, workspace=opt.workspace, fp16=opt.fp16, use_checkpoint='best', eval_interval=1)
trainer.save_mesh(os.path.join(opt.workspace, 'results', 'output.ply'), 1024)
else:
from sdf.provider import SDFDataset
from loss import mape_loss
train_dataset = SDFDataset(opt.path, size=100, num_samples=2**18)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=1, shuffle=True)
valid_dataset = SDFDataset(opt.path, size=1, num_samples=2**18) # just a dummy
valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=1)
criterion = mape_loss # torch.nn.L1Loss()
optimizer = lambda model: torch.optim.Adam([
{'name': 'encoding', 'params': model.encoder.parameters()},
{'name': 'net', 'params': model.backbone.parameters(), 'weight_decay': 1e-6},
], lr=opt.lr, betas=(0.9, 0.99), eps=1e-15)
scheduler = lambda optimizer: optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)
trainer = Trainer('ngp', model, workspace=opt.workspace, optimizer=optimizer, criterion=criterion, ema_decay=0.95, fp16=opt.fp16, lr_scheduler=scheduler, use_checkpoint='latest', eval_interval=1)
trainer.train(train_loader, valid_loader, 20)
# also test
trainer.save_mesh(os.path.join(opt.workspace, 'results', 'output.ply'), 1024)