'iter=%d, loss1_forward=%.6f loss1_backward=%.6f loss2_forward=%.6f loss2_backward=%.6f' % (it, np.sqrt(loss1_forward.item() / GV1.shape[0]), np.sqrt(loss1_backward.item() / GV2.shape[0]), np.sqrt(loss2_forward.item() / GV2.shape[0]), np.sqrt(loss2_backward.item() / GV1.shape[0]))) current_loss = loss.item() if save_path != '': torch.save({'func': func, 'optim': optimizer}, save_path) GV1_deformed = func.forward(GV1_device) GV1_deformed = torch.from_numpy(GV1_deformed.data.cpu().numpy()) V1_copy = V1.clone() #Finalize(V1_copy, F1, E1, V2G1, GV1_deformed, 1.0, param_id2) pyDeform.NormalizeByTemplate(V1_copy, param_id1.tolist()) V1_origin = V1_copy.clone() #V1_copy = V1_copy.to(device) func.func = func.func.cpu() V1_copy = func.forward(V1_copy) V1_copy = torch.from_numpy(V1_copy.data.cpu().numpy()) src_to_src = torch.from_numpy( np.array([i for i in range(V1_origin.shape[0])]).astype('int32')) pyDeform.SolveLinear(V1_origin, F1, E1, src_to_src, V1_copy, 1, 1) pyDeform.DenormalizeByTemplate(V1_origin, param_id2.tolist()) pyDeform.SaveMesh(output_path, V1_origin, F1)
reverse_deform = ReverseLossLayer() graph_V = nn.Parameter(graph_V) optimizer = optim.Adam([graph_V], lr=1e-3) pyDeform.NormalizeByTemplate(graph_V_tar, param_id.tolist()) loss_src2tar, loss_tar2src = None, None niter = 10000 prev_loss_src, prev_loss_tar = 1e30, 1e30 for it in range(0, niter): optimizer.zero_grad() loss_src2tar = graph_deform(graph_V, graph_E) loss_tar2src = reverse_deform(graph_V, graph_V_tar) loss = loss_src2tar / graph_V.shape[0] + loss_tar2src / graph_V_tar.shape[0] loss.backward() optimizer.step() if it % 100 == 0: current_loss_src = np.sqrt(loss_src2tar.item() / graph_V.shape[0]) current_loss_tar = np.sqrt(loss_tar2src.item() / graph_V_tar.shape[0]) print('iter=%d, loss_src2tar=%.6f loss_tar2src=%.6f' % (it, np.sqrt(loss_src2tar.item() / graph_V.shape[0]), np.sqrt(loss_tar2src.item() / graph_V_tar.shape[0]))) if prev_loss_src - current_loss_src < 1e-6\ and prev_loss_tar - current_loss_tar < 1e-6: break prev_loss_src, prev_loss_tar = current_loss_src, current_loss_tar Finalize(src_V, src_F, src_E, src_to_graph, graph_V, 1, param_id) pyDeform.SaveMesh(output_path, src_V, src_F)
if i != 0: V1_copy = func.integrate(V1_copy, 0, i * 0.04, device) V1_copy = torch.from_numpy(V1_copy.data.cpu().numpy()) src_to_src = torch.from_numpy(np.array([i for i in range(V1_origin.shape[0])]).astype('int32')) E1 = np.zeros((F1.shape[0] * 3, 2), dtype='int32') F1_numpy = F1.numpy() E1[:F1.shape[0],:] = F1_numpy[:,0:2] E1[F1.shape[0]:F1.shape[0]*2,:] = F1_numpy[:,1:3] E1[F1.shape[0]*2:,0] = F1_numpy[:,2] E1[F1.shape[0]*2:,1] = F1_numpy[:,0] E1 = torch.from_numpy(E1) pyDeform.SolveLinear(V1_deform, F1, E1, src_to_src, V1_copy, 1, 1) pyDeform.DenormalizeByTemplate(V1_deform, param_id2.tolist()) pyDeform.SaveMesh('%s/src-%02d.obj'%(output_path,i), V1_deform, F1) V2_copy = V2.clone() pyDeform.NormalizeByTemplate(V2_copy, param_id2.tolist()) V2_origin = V2_copy.clone() for i in range(26): V2_deform = V2_origin.clone() V2_copy = V2_origin.clone().to(device) if i != 25: V2_copy = func.integrate(V2_copy, 1, i * 0.04, device) V2_copy = torch.from_numpy(V2_copy.data.cpu().numpy()) src_to_src = torch.from_numpy(np.array([i for i in range(V2_origin.shape[0])]).astype('int32')) pyDeform.SolveLinear(V2_deform, F2, E2, src_to_src, V2_copy, 1, 1)