-
Notifications
You must be signed in to change notification settings - Fork 16
/
cage_deformer_3d.py
354 lines (301 loc) · 17.1 KB
/
cage_deformer_3d.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from __future__ import print_function
from pprint import pprint
import traceback
import sys
import shutil
import openmesh as om
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.utils.data
import numpy as np
import pymesh
from pytorch_points.misc import logger
from pytorch_points.network.geo_operations import mean_value_coordinates_3D, edge_vertex_indices
from pytorch_points.utils.pc_utils import load, save_ply, save_pts, center_bounding_box
from pytorch_points.utils.geometry_utils import read_trimesh, write_trimesh, build_gemm, Mesh, get_edge_points, generatePolygon
from pytorch_points.utils.pytorch_utils import weights_init, check_values, save_network, load_network, save_grad, saved_variables, \
clamp_gradient_norm, linear_loss_weight, tolerating_collate, clamp_gradient, fix_network_parameters
import losses
import networks
from common import loadInitCage, build_dataset, crisscross_input, log_outputs, deform_with_MVC
def test(net=None, save_subdir="test"):
opt.phase = "test"
dataset = build_dataset(opt)
if opt.dim == 3:
init_cage_V, init_cage_Fs = loadInitCage([opt.template])
cage_V_t = init_cage_V.transpose(1,2).detach().cuda()
else:
init_cage_V = generatePolygon(0, 0, 1.5, 0, 0, 0, opt.cage_deg)
init_cage_V = torch.tensor([(x, y) for x, y in init_cage_V], dtype=torch.float).unsqueeze(0)
cage_V_t = init_cage_V.transpose(1,2).detach().cuda()
init_cage_Fs = [torch.arange(opt.cage_deg, dtype=torch.int64).view(1,1,-1).cuda()]
if net is None:
# network
net = networks.NetworkFull(opt, dim=opt.dim, bottleneck_size=opt.bottleneck_size,
template_vertices=cage_V_t, template_faces=init_cage_Fs[-1],
).cuda()
net.eval()
load_network(net, opt.ckpt)
else:
net.eval()
print(net)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False, drop_last=False,
collate_fn=tolerating_collate,
num_workers=0,
worker_init_fn=lambda id: np.random.seed(np.random.get_state()[1][0] + id))
test_output_dir = os.path.join(opt.log_dir, save_subdir)
os.makedirs(test_output_dir, exist_ok=True)
with open(os.path.join(test_output_dir, "eval.txt"), "w") as f:
with torch.no_grad():
for i, data in enumerate(dataloader):
data = dataset.uncollate(data)
############# blending ############
# sample 4 different alpha
if opt.blend_style:
num_alpha = 4
blend_alpha = torch.linspace(0, 1, steps=num_alpha, dtype=torch.float32).cuda().reshape(num_alpha, 1)
data["source_shape"] = data["source_shape"].expand(num_alpha, -1, -1).contiguous()
data["target_shape"] = data["target_shape"].expand(num_alpha, -1, -1).contiguous()
else:
blend_alpha = 1.0
data["alpha"] = blend_alpha
###################################
source_shape_t = data["source_shape"].transpose(1,2).contiguous().detach()
target_shape_t = data["target_shape"].transpose(1,2).contiguous().detach()
outputs = net(source_shape_t, target_shape_t, blend_alpha)
deformed = outputs["deformed"]
####################### evaluation ########################
s_filename = os.path.splitext(data["source_file"][0])[0]
t_filename = os.path.splitext(data["target_file"][0])[0]
log_str = "{}/{} {}-{} ".format(i, len(dataloader), s_filename, t_filename)
print(log_str)
f.write(log_str+"\n")
###################### outputs ############################
for b in range(deformed.shape[0]):
if "source_mesh" in data and data["source_mesh"] is not None:
if isinstance(data["source_mesh"][0], str):
source_mesh = om.read_polymesh(data["source_mesh"][0]).points().copy()
source_mesh = dataset.normalize(source_mesh, opt.isV2)
source_mesh = torch.from_numpy(source_mesh.astype(np.float32)).unsqueeze(0).cuda()
deformed = deform_with_MVC(outputs["cage"][b:b+1], outputs["new_cage"][b:b+1],
outputs["cage_face"], source_mesh)
else:
deformed = deform_with_MVC(outputs["cage"][b:b+1], outputs["new_cage"][b:b+1],
outputs["cage_face"], data["source_mesh"])
deformed[b] = center_bounding_box(deformed[b])[0]
if data["source_face"] is not None and data["source_mesh"] is not None:
source_mesh = data["source_mesh"][0].detach().cpu()
source_mesh = center_bounding_box(source_mesh)[0]
source_face = data["source_face"][0].detach().cpu()
tosave = pymesh.form_mesh(vertices=source_mesh, faces=source_face)
pymesh.save_mesh(os.path.join(opt.log_dir, save_subdir, "{}-{}-Sa.obj".format(s_filename, t_filename)),
tosave, use_float=True
)
tosave = pymesh.form_mesh(vertices=deformed[0].detach().cpu(), faces=source_face)
pymesh.save_mesh(os.path.join(opt.log_dir, save_subdir, "{}-{}-Sab-{}.obj".format(s_filename, t_filename, b)),
tosave, use_float=True,
)
elif data["source_face"] is None and isinstance(data["source_mesh"][0], str):
orig_file_path = data["source_mesh"][0]
mesh = om.read_polymesh(orig_file_path)
points_arr = mesh.points()
points_arr[:] = source_mesh[0].detach().cpu().numpy()
om.write_mesh(os.path.join(opt.log_dir, save_subdir, "{}-{}-Sa.obj".format(s_filename, t_filename)), mesh)
points_arr[:] = deformed[0].detach().cpu().numpy()
om.write_mesh(os.path.join(opt.log_dir, save_subdir, "{}-{}-Sab-{}.obj".format(s_filename, t_filename, b)), mesh)
else:
# save to "pts" for rendering
save_pts(os.path.join(opt.log_dir, save_subdir,"{}-{}-Sa.pts".format(s_filename,t_filename)), data["source_shape"][b].detach().cpu())
save_pts(os.path.join(opt.log_dir, save_subdir,"{}-{}-Sab-{}.pts".format(s_filename,t_filename, b)), deformed[0].detach().cpu())
if data["target_face"] is not None and data["target_mesh"] is not None:
data["target_mesh"][0] = center_bounding_box(data["target_mesh"][0])[0]
tosave = pymesh.form_mesh(vertices=data["target_mesh"][0].detach().cpu(), faces=data["target_face"][0].detach().cpu())
pymesh.save_mesh(os.path.join(opt.log_dir, save_subdir, "{}-{}-Sb.obj".format(s_filename, t_filename)),
tosave, use_float=True,
)
elif data["target_face"] is None and isinstance(data["target_mesh"][0], str):
orig_file_path = data["target_mesh"][0]
mesh = om.read_polymesh(orig_file_path)
points_arr = mesh.points()
points_arr[:] = dataset.normalize(points_arr.copy(), opt.isV2)
om.write_mesh(os.path.join(opt.log_dir, save_subdir, "{}-{}-Sb.obj".format(s_filename, t_filename)), mesh)
else:
save_pts(os.path.join(opt.log_dir, save_subdir,"{}-{}-Sb.pts".format(s_filename,t_filename)), data["target_shape"][0].detach().cpu())
outputs["cage"][b] = center_bounding_box(outputs["cage"][b])[0]
outputs["new_cage"][b] = center_bounding_box(outputs["new_cage"][b])[0]
pymesh.save_mesh_raw(
os.path.join(opt.log_dir, save_subdir, "{}-{}-cage1-{}.ply".format(s_filename, t_filename, b)),
outputs["cage"][b].detach().cpu(), outputs["cage_face"][0].detach().cpu(), binary=True)
pymesh.save_mesh_raw(
os.path.join(opt.log_dir, save_subdir, "{}-{}-cage2-{}.ply".format(s_filename, t_filename, b)),
outputs["new_cage"][b].detach().cpu(), outputs["cage_face"][0].detach().cpu(), binary=True)
dataset.render_result(test_output_dir)
def train():
dataset = build_dataset(opt)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batch_size, shuffle=True, drop_last=True,
collate_fn=tolerating_collate,
num_workers=2, worker_init_fn=lambda id: np.random.seed(np.random.get_state()[1][0] + id))
if opt.dim == 3:
# cage (1,N,3)
init_cage_V, init_cage_Fs = loadInitCage([opt.template])
cage_V_t = init_cage_V.transpose(1,2).detach().cuda()
cage_edge_points_list = []
cage_edges_list = []
for F in init_cage_Fs:
mesh = Mesh(vertices=init_cage_V[0], faces=F[0])
build_gemm(mesh, F[0])
cage_edge_points = torch.from_numpy(get_edge_points(mesh)).cuda()
cage_edge_points_list.append(cage_edge_points)
cage_edges_list = [edge_vertex_indices(F[0])]
else:
init_cage_V = generatePolygon(0, 0, 1.5, 0, 0, 0, opt.cage_deg)
init_cage_V = torch.tensor([(x, y) for x, y in init_cage_V], dtype=torch.float).unsqueeze(0)
cage_V_t = init_cage_V.transpose(1,2).detach().cuda()
init_cage_Fs = [torch.arange(opt.cage_deg, dtype=torch.int64).view(1,1,-1).cuda()]
# network
net = networks.NetworkFull(opt, dim=opt.dim, bottleneck_size=opt.bottleneck_size,
template_vertices=cage_V_t, template_faces=init_cage_Fs[-1],
).cuda()
net.apply(weights_init)
if opt.ckpt:
load_network(net, opt.ckpt)
all_losses = losses.AllLosses(opt)
# optimizer
optimizer = torch.optim.Adam([
{"params": net.encoder.parameters()},
{"params": net.nd_decoder.parameters()},
{"params": net.merger.parameters()}], lr=opt.lr)
if opt.full_net:
optimizer.add_param_group({'params': net.nc_decoder.parameters(), 'lr': 0.1*opt.lr})
if opt.optimize_template:
optimizer.add_param_group({'params': net.template_vertices, 'lr': opt.lr})
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, int(opt.nepochs*0.4), gamma=0.1, last_epoch=-1)
# train
net.train()
start_epoch = 0
t = 0
steps_C = 20
steps_D = 20
# train
os.makedirs(opt.log_dir, exist_ok=True)
shutil.copy2(__file__, opt.log_dir)
shutil.copy2(os.path.join(os.path.dirname(__file__), "networks.py"), opt.log_dir)
shutil.copy2(os.path.join(os.path.dirname(__file__), "losses.py"), opt.log_dir)
shutil.copy2(os.path.join(os.path.dirname(__file__), "datasets.py"), opt.log_dir)
shutil.copy2(os.path.join(os.path.dirname(__file__), "common.py"), opt.log_dir)
shutil.copy2(os.path.join(os.path.dirname(__file__), "option.py"), opt.log_dir)
print(net)
log_file = open(os.path.join(opt.log_dir, "training_log.txt"), "a")
log_file.write(str(net)+"\n")
log_interval = max(len(dataloader)//5, 50)
save_interval = max(opt.nepochs//10, 1)
running_avg_loss = -1
with torch.autograd.detect_anomaly():
if opt.epoch:
start_epoch = opt.epoch % opt.nepochs
t += start_epoch*len(dataloader)
for epoch in range(start_epoch, opt.nepochs):
for t_epoch, data in enumerate(dataloader):
warming_up = epoch < opt.warmup_epochs
progress = t_epoch/len(dataloader)+epoch
optimize_C = (t % (steps_C+steps_D)) > steps_D
############# get data ###########
data = dataset.uncollate(data)
data = crisscross_input(data)
if opt.dim == 3:
data["cage_edge_points"] = cage_edge_points_list[-1]
data["cage_edges"] = cage_edges_list[-1]
source_shape, target_shape = data["source_shape"], data["target_shape"]
############# blending ############
if opt.blend_style:
blend_alpha = torch.rand((source_shape.shape[0], 1), dtype=torch.float32).to(device=source_shape.device)
else:
blend_alpha = 1.0
data["alpha"] = blend_alpha
############# run network ###########
optimizer.zero_grad()
# optimizer_C.zero_grad()
# optimizer_D.zero_grad()
source_shape_t = source_shape.transpose(1,2)
target_shape_t = target_shape.transpose(1,2)
outputs = net(source_shape_t, target_shape_t, data["alpha"])
############# get losses ###########
current_loss = all_losses(data, outputs, progress)
loss_sum = torch.sum(torch.stack([v for v in current_loss.values()], dim=0))
if running_avg_loss < 0:
running_avg_loss = loss_sum
else:
running_avg_loss = running_avg_loss + (loss_sum.item() - running_avg_loss)/(t+1)
if (t % log_interval == 0) or (loss_sum > 5*running_avg_loss):
log_str = "warming up {} e {:03d} t {:05d}: {}".format(warming_up, epoch, t,
", ".join(["{} {:.3g}".format(k, v.mean().item()) for k, v in current_loss.items()]))
print(log_str)
log_file.write(log_str+"\n")
log_outputs(opt, t, outputs, data)
if loss_sum > 100*running_avg_loss:
logger.info("loss ({}) > 5*running_average_loss ({}). Skip without update.".format(loss_sum, 5*running_avg_loss))
torch.cuda.empty_cache()
continue
loss_sum.backward()
if epoch < opt.warmup_epochs:
try:
net.nc_decoder.zero_grad()
net.encoder.zero_grad()
except AttributeError:
net.template_vertices.grad.zero_()
if opt.alternate_cd:
optimize_C = (epoch > opt.warmup_epochs) and (epoch % (opt.c_epoch+opt.d_epoch)) > opt.d_epoch
if optimize_C:
net.nd_decoder.zero_grad()
else:
try:
net.encoder.zero_grad()
net.nc_decoder.zero_grad()
except AttributeError:
net.template_vertices.grad.zero_()
clamp_gradient(net, 0.1)
optimizer.step()
if (t + 1) % 500 == 0:
save_network(net, opt.log_dir, network_label="net", epoch_label="latest")
t += 1
if (epoch + 1) % save_interval == 0:
save_network(net, opt.log_dir, network_label="net", epoch_label=epoch)
scheduler.step()
if opt.eval:
try:
test(net=net, save_subdir="epoch_{}".format(epoch))
except Exception as e:
traceback.print_exc(file=sys.stdout)
logger.warn("Failed to run test", str(e))
log_file.close()
save_network(net, opt.log_dir, network_label="net", epoch_label="final")
test(net=net)
if __name__ == "__main__":
from option import BaseOptions
import datetime
import os
parser = BaseOptions()
opt = parser.parse()
# reproducability
torch.manual_seed(24)
torch.backends.cudnn.deterministic = True # type: ignore
torch.backends.cudnn.benchmark = False # type: ignore
np.random.seed(24)
if opt.phase == "train":
if opt.ckpt is not None:
opt.log_dir = os.path.dirname(opt.ckpt)
else:
opt.log_dir = os.path.join(opt.log_dir, "-".join(filter(None, [os.path.basename(__file__)[:-3],
datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S"),
opt.name])))
else:
opt.log_dir = os.path.dirname(opt.ckpt)
if opt.phase == "test":
test(save_subdir=opt.subdir)
else:
os.makedirs(opt.log_dir, exist_ok=True)
log_file = open(os.path.join(opt.log_dir, "training_log.txt"), "a")
parser.print_options(opt, log_file)
log_file.close()
train()