-
Notifications
You must be signed in to change notification settings - Fork 9
/
run.py
197 lines (155 loc) · 8.46 KB
/
run.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
import time
import torch
import torch.nn.functional as F
from tqdm import tqdm
from models.dataset import DatasetNP
from models.fields import NPullNetwork
import argparse
from pyhocon import ConfigFactory
import os
from shutil import copyfile
import numpy as np
import trimesh
from models.utils import get_root_logger, print_log
import math
import mcubes
import warnings
warnings.filterwarnings("ignore")
class Runner:
def __init__(self, args, conf_path, mode='train'):
self.device = torch.device('cuda')
# Configuration
self.conf_path = conf_path
f = open(self.conf_path)
conf_text = f.read()
f.close()
self.conf = ConfigFactory.parse_string(conf_text)
self.conf['dataset.np_data_name'] = self.conf['dataset.np_data_name']
self.base_exp_dir = self.conf['general.base_exp_dir'] + args.dir
os.makedirs(self.base_exp_dir, exist_ok=True)
self.dataset_np = DatasetNP(self.conf['dataset'], args.dataname)
self.dataname = args.dataname
self.iter_step = 0
# Training parameters
self.maxiter = self.conf.get_int('train.maxiter')
self.save_freq = self.conf.get_int('train.save_freq')
self.report_freq = self.conf.get_int('train.report_freq')
self.val_freq = self.conf.get_int('train.val_freq')
self.batch_size = self.conf.get_int('train.batch_size')
self.learning_rate = self.conf.get_float('train.learning_rate')
self.warm_up_end = self.conf.get_float('train.warm_up_end', default=0.0)
self.eval_num_points = self.conf.get_int('train.eval_num_points')
self.mode = mode
# Networks
self.sdf_network = NPullNetwork(**self.conf['model.sdf_network']).to(self.device)
self.optimizer = torch.optim.Adam(self.sdf_network.parameters(), lr=self.learning_rate)
# Backup codes and configs for debug
if self.mode[:5] == 'train':
self.file_backup()
def train(self):
timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime())
log_file = os.path.join(os.path.join(self.base_exp_dir), f'{timestamp}.log')
logger = get_root_logger(log_file=log_file, name='outs')
self.logger = logger
batch_size = self.batch_size
res_step = self.maxiter - self.iter_step
for iter_i in tqdm(range(res_step)):
self.update_learning_rate_np(iter_i)
points, samples, point_gt = self.dataset_np.np_train_data(batch_size)
samples.requires_grad = True
gradients_sample = self.sdf_network.gradient(samples).squeeze() # 5000x3
sdf_sample = self.sdf_network.sdf(samples) # 5000x1
grad_norm = F.normalize(gradients_sample, dim=1) # 5000x3
sample_moved = samples - grad_norm * sdf_sample # 5000x3
loss_sdf = torch.linalg.norm((points - sample_moved), ord=2, dim=-1).mean()
loss = loss_sdf
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
self.iter_step += 1
if self.iter_step % self.report_freq == 0:
print_log('iter:{:8>d} cd_l1 = {} lr={}'.format(self.iter_step, loss_sdf, self.optimizer.param_groups[0]['lr']), logger=logger)
if self.iter_step % self.val_freq == 0 and self.iter_step!=0:
self.validate_mesh(resolution=256, threshold=args.mcubes_threshold, point_gt=point_gt, iter_step=self.iter_step, logger=logger)
if self.iter_step % self.save_freq == 0 and self.iter_step!=0:
self.save_checkpoint()
def validate_mesh(self, resolution=64, threshold=0.0, point_gt=None, iter_step=0, logger=None):
bound_min = torch.tensor(self.dataset_np.object_bbox_min, dtype=torch.float32)
bound_max = torch.tensor(self.dataset_np.object_bbox_max, dtype=torch.float32)
os.makedirs(os.path.join(self.base_exp_dir, 'outputs'), exist_ok=True)
mesh = self.extract_geometry(bound_min, bound_max, resolution=resolution, threshold=threshold, query_func=lambda pts: -self.sdf_network.sdf(pts))
mesh.export(os.path.join(self.base_exp_dir, 'outputs', '{:0>8d}_{}.ply'.format(self.iter_step,str(threshold))))
def update_learning_rate_np(self, iter_step):
warn_up = self.warm_up_end
max_iter = self.maxiter
init_lr = self.learning_rate
lr = (iter_step / warn_up) if iter_step < warn_up else 0.5 * (math.cos((iter_step - warn_up)/(max_iter - warn_up) * math.pi) + 1)
lr = lr * init_lr
for g in self.optimizer.param_groups:
g['lr'] = lr
def extract_fields(self, bound_min, bound_max, resolution, query_func):
N = 32
X = torch.linspace(bound_min[0], bound_max[0], resolution).split(N)
Y = torch.linspace(bound_min[1], bound_max[1], resolution).split(N)
Z = torch.linspace(bound_min[2], bound_max[2], resolution).split(N)
u = np.zeros([resolution, resolution, resolution], dtype=np.float32)
with torch.no_grad():
for xi, xs in enumerate(X):
for yi, ys in enumerate(Y):
for zi, zs in enumerate(Z):
xx, yy, zz = torch.meshgrid(xs, ys, zs)
pts = torch.cat([xx.reshape(-1, 1), yy.reshape(-1, 1), zz.reshape(-1, 1)], dim=-1)
val = query_func(pts).reshape(len(xs), len(ys), len(zs)).detach().cpu().numpy()
u[xi * N: xi * N + len(xs), yi * N: yi * N + len(ys), zi * N: zi * N + len(zs)] = val
return u
def extract_geometry(self, bound_min, bound_max, resolution, threshold, query_func):
print('Creating mesh with threshold: {}'.format(threshold))
u = self.extract_fields(bound_min, bound_max, resolution, query_func)
vertices, triangles = mcubes.marching_cubes(u, threshold)
b_max_np = bound_max.detach().cpu().numpy()
b_min_np = bound_min.detach().cpu().numpy()
vertices = vertices / (resolution - 1.0) * (b_max_np - b_min_np)[None, :] + b_min_np[None, :]
mesh = trimesh.Trimesh(vertices, triangles)
return mesh
def file_backup(self):
dir_lis = self.conf['general.recording']
os.makedirs(os.path.join(self.base_exp_dir, 'recording'), exist_ok=True)
for dir_name in dir_lis:
cur_dir = os.path.join(self.base_exp_dir, 'recording', dir_name)
os.makedirs(cur_dir, exist_ok=True)
files = os.listdir(dir_name)
for f_name in files:
if f_name[-3:] == '.py':
copyfile(os.path.join(dir_name, f_name), os.path.join(cur_dir, f_name))
copyfile(self.conf_path, os.path.join(self.base_exp_dir, 'recording', 'config.conf'))
def load_checkpoint(self, checkpoint_name):
checkpoint = torch.load(os.path.join(self.base_exp_dir, 'checkpoints', checkpoint_name), map_location=self.device)
print(os.path.join(self.base_exp_dir, 'checkpoints', checkpoint_name))
self.sdf_network.load_state_dict(checkpoint['sdf_network_fine'])
self.iter_step = checkpoint['iter_step']
def save_checkpoint(self):
checkpoint = {
'sdf_network_fine': self.sdf_network.state_dict(),
'iter_step': self.iter_step,
}
os.makedirs(os.path.join(self.base_exp_dir, 'checkpoints'), exist_ok=True)
torch.save(checkpoint, os.path.join(self.base_exp_dir, 'checkpoints', 'ckpt_{:0>6d}.pth'.format(self.iter_step)))
if __name__ == '__main__':
torch.set_default_tensor_type('torch.cuda.FloatTensor')
parser = argparse.ArgumentParser()
parser.add_argument('--conf', type=str, default='./confs/np_srb.conf')
parser.add_argument('--mode', type=str, default='train')
parser.add_argument('--mcubes_threshold', type=float, default=0.0)
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--dir', type=str, default='gargoyle')
parser.add_argument('--dataname', type=str, default='gargoyle')
args = parser.parse_args()
torch.cuda.set_device(args.gpu)
runner = Runner(args, args.conf, args.mode)
if args.mode == 'train':
runner.train()
elif args.mode == 'validate_mesh':
threshs = [-0.001,-0.0025,-0.005,-0.01,-0.02,0.0,0.001,0.0025,0.005,0.01,0.02]
for thresh in threshs:
runner.validate_mesh(resolution=256, threshold=thresh)