def test_iteration(self): self.couple.separate_points_normal_labels() batchs = self.couple.P1.size(0) self.couple.add_P2P1(*loss.forward_chamfer(self.network, self.couple.P1, self.couple.P2, local_fix=self.fix, distChamfer=self.distChamfer)) loss_val_Deformation_ChamferL2 = loss.chamferL2(self.couple.dist1_P2_P1, self.couple.dist2_P2_P1) loss_val_Reconstruction_L2 = loss.L2(self.couple.P2_P1, self.couple.P2) self.log.update("loss_val_Deformation_ChamferL2", loss_val_Deformation_ChamferL2) self.log.update("loss_val_Reconstruction_L2", loss_val_Reconstruction_L2) print( '\r' + colored('[%d: %d/%d]' % (self.epoch, self.iteration, self.len_dataset_test / (self.opt.batch_size)), 'red') + colored('loss_val_Deformation_ChamferL2: %f' % loss_val_Deformation_ChamferL2.item(), 'yellow'), end='') if self.iteration % 60 == 1 and self.opt.display: self.visualizer.show_pointclouds(points=self.couple.P2[0], Y=self.couple.label2[0], title="val_B") self.visualizer.show_pointclouds(points=self.couple.P1[0], Y=self.couple.label1[0], title="val_A") self.visualizer.show_pointclouds(points=self.couple.P2_P1[0], Y=self.couple.label1[0], title="val_B_reconstructed") # Compute Miou when labels are tranfered from P1 to P2. predicted_target = self.couple.label1.view(-1)[self.couple.idx2_P2_P1].view(batchs, -1) for shape in range(batchs): if self.couple.cat_1 == self.couple.cat_2: target = self.couple.label2[shape].squeeze().data.cpu().numpy() iou_val = miou_shape.miou_shape(predicted_target[shape].squeeze().cpu().numpy(), target, self.dataset_train.part_category[self.couple.cat_1[shape]]) self.log.update("iou_val", iou_val)
def test_iteration(self): label1 = self.P1[:, :, 6].contiguous() label1[0] = label1[0] - torch.min(label1[0]) + 1 self.P1 = self.P1[:, :, :3].contiguous().cuda().float() P2, dist1_P2, dist2_P2, idx1_P2, idx2_P2 = self.forward_chamfer_atlasnet( self.network, self.P1, self.fix, distChamfer=self.distChamfer) loss_val_Deformation_ChamferL2 = loss.chamferL2(dist1_P2, dist2_P2) self.log.update("loss_val_Deformation_ChamferL2", loss_val_Deformation_ChamferL2) print('\r' + colored( '[%d: %d/%d]' % (self.epoch, self.iteration, self.len_dataset_test / (self.opt.batch_size)), 'red') + colored( 'loss_val_Deformation_ChamferL2: %f' % loss_val_Deformation_ChamferL2.item(), 'yellow'), end='') if self.iteration % 60 == 1 and self.opt.display: self.visualizer.show_pointclouds(points=P2[0], title="val_A_reconstructed") self.visualizer.show_pointclouds(points=self.P1[0], Y=label1[0], title="val_A")
def train_iteration(self): self.optimizer.zero_grad() label1 = self.P1[:, :, 6].contiguous() label1[0] = label1[0] - torch.min(label1[0]) + 1 self.P1 = self.P1[:, :, :3].contiguous().cuda().float() P2, dist1_P2, dist2_P2, idx1_P2, idx2_P2 = self.forward_chamfer_atlasnet( self.network, self.P1, self.fix, distChamfer=self.distChamfer) loss_train_Deformation_ChamferL2 = loss.chamferL2(dist1_P2, dist2_P2) loss_train_total = loss_train_Deformation_ChamferL2 loss_train_total.backward() self.log.update("loss_train_Deformation_ChamferL2", loss_train_Deformation_ChamferL2) self.log.update("loss_train_total", loss_train_total) self.optimizer.step() # gradient update # VIZUALIZE if self.iteration % 50 == 1 and self.opt.display: self.visualizer.show_pointclouds(points=P2[0], title="train_A_reconstructed") self.visualizer.show_pointclouds(points=self.P1[0], Y=label1[0], title="train_A") self.print_iteration_stats(loss_train_total)
# Compute NN P2_P0_NN_list = list( map(lambda x: distChamfer(x, P2), points_train_list)) predicted_NN_P2_P0_list = list( map( lambda x, y: x.view(-1)[y[3].view(-1).data.long()].view( 1, -1), labels_train_list, P2_P0_NN_list)) iou_NN_list = list( map( lambda x: miou_shape.miou_shape(x.squeeze().cpu().numpy( ), P2_label, trainer.parts), predicted_NN_P2_P0_list)) predicted_NN_P2_P0_list = torch.cat(predicted_NN_P2_P0_list) # NN NN_chamferL2_list = list( map(lambda x: loss.chamferL2(x[0], x[1]), P2_P0_NN_list)) top_k_idx, top_k_values = min_k(NN_chamferL2_list) add("iou_NN", top_k_idx) # NN + ICP points_train_NN_ICP = ICP.ICP(points_train_list[top_k_idx[0]], P2).unsqueeze(0).float() dist1_NN_tr, dist2_NN_tr, idx1_NN_tr, idx2_NN_tr = distChamfer( points_train_NN_ICP, P2) predicted_P2_NN_tr = labels_train_list[top_k_idx[0]].view(-1)[ idx2_NN_tr.view(-1).data.long()].view(-1) iou_dict["iou_NN_ICP_NN"] = miou_shape.miou_shape( predicted_P2_NN_tr.cpu().numpy(), P2_label, trainer.parts) # NN + ICP + ours P2_P1_ours_1, _, _, _, idx2_P2_P0 = loss.forward_chamfer(
def get_criterion_shape(opt): return_dict = {} my_utils.plant_seeds(randomized_seed=opt.randomize) trainer = t.Trainer(opt) trainer.build_dataset_train_for_matching() trainer.build_dataset_test_for_matching() trainer.build_network() trainer.build_losses() trainer.network.eval() # Load input mesh exist_P2_label = True try: mesh_path = opt.eval_get_criterions_for_shape # Ends in .txt points = np.loadtxt(mesh_path) points = torch.from_numpy(points).float() # Normalization is done before resampling ! P2 = normalize_points.BoundingBox(points[:, :3]) P2_label = points[:, 6].data.cpu().numpy() except: mesh_path = opt.eval_get_criterions_for_shape # Ends in .obj source_mesh_edge = get_shapenet_model.link(mesh_path) P2 = torch.from_numpy(source_mesh_edge.vertices) exist_P2_label = False min_k = Min_k(opt.k_max_eval) max_k = Max_k(opt.k_max_eval) points_train_list = [] point_train_paths = [] labels_train_list = [] iterator_train = trainer.dataloader_train.__iter__() for find_best in range(opt.num_shots_eval): try: points_train, _, _, file_path = iterator_train.next() points_train_list.append( points_train[:, :, :3].contiguous().cuda().float()) point_train_paths.append(file_path) labels_train_list.append( points_train[:, :, 6].contiguous().cuda().float()) except: break # ========Loop on test examples======================== # with torch.no_grad(): P2 = P2[:, :3].unsqueeze(0).contiguous().cuda().float() P2_latent = trainer.network.encode( P2.transpose(1, 2).contiguous(), P2.transpose(1, 2).contiguous()) # Chamfer (P0_P2) P0_P2_list = list( map( lambda x: loss.forward_chamfer(trainer.network, P2, x, local_fix=None, distChamfer=trainer.distChamfer ), points_train_list)) # Compute Chamfer (P2_P0) P2_P0_list = list( map( lambda x: loss.forward_chamfer(trainer.network, x, P2, local_fix=None, distChamfer=trainer.distChamfer ), points_train_list)) predicted_ours_P2_P0_list = list( map(lambda x, y: x.view(-1)[y[4].view(-1).data.long()].view(1, -1), labels_train_list, P2_P0_list)) if exist_P2_label: iou_ours_list = list( map( lambda x: miou_shape.miou_shape(x.squeeze().cpu().numpy( ), P2_label, trainer.parts), predicted_ours_P2_P0_list)) top_k_idx, top_k_values = max_k(iou_ours_list) return_dict["oracle"] = point_train_paths[top_k_idx[0]][0] predicted_ours_P2_P0_list = list( map(lambda x, y: x.view(-1)[y[4].view(-1).data.long()].view(1, -1), labels_train_list, P2_P0_list)) predicted_ours_P2_P0_list = torch.cat(predicted_ours_P2_P0_list) # Compute NN P2_P0_NN_list = list( map(lambda x: loss.distChamfer(x, P2), points_train_list)) predicted_NN_P2_P0_list = list( map(lambda x, y: x.view(-1)[y[3].view(-1).data.long()].view(1, -1), labels_train_list, P2_P0_NN_list)) predicted_NN_P2_P0_list = torch.cat(predicted_NN_P2_P0_list) # NN NN_chamferL2_list = list( map(lambda x: loss.chamferL2(x[0], x[1]), P2_P0_NN_list)) top_k_idx, top_k_values = min_k(NN_chamferL2_list) return_dict["NN_criterion"] = point_train_paths[top_k_idx[0]][0] # Chamfer ours chamfer_list = list( map(lambda x: loss.chamferL2(x[1], x[2]), P2_P0_list)) top_k_idx, top_k_values = min_k(chamfer_list) return_dict["chamfer_criterion"] = point_train_paths[top_k_idx[0]][0] # NN in latent space P0_latent_list = list( map( lambda x: trainer.network.encode( x.transpose(1, 2).contiguous(), x.transpose(1, 2).contiguous()), points_train_list)) cosine_list = list( map(lambda x: loss.cosine(x, P2_latent), P0_latent_list)) top_k_idx, top_k_values = min_k(cosine_list) return_dict["cosine_criterion"] = point_train_paths[top_k_idx[0]][0] # Cycle 2 P0_P2_cycle_list = list( map(lambda x, y: loss.batch_cycle_2(x[0], y[3], 1), P0_P2_list, P2_P0_list)) P0_P2_cycle_list = list( map(lambda x, y: loss.L2(x, y), P0_P2_cycle_list, points_train_list)) P2_P0_cycle_list = list( map(lambda x, y: loss.batch_cycle_2(x[0], y[3], 1), P2_P0_list, P0_P2_list)) P2_P0_cycle_list = list(map(lambda x: loss.L2(x, P2), P2_P0_cycle_list)) # Cycle 2 both sides both_cycle_list = list( map(lambda x, y: x * y, P0_P2_cycle_list, P2_P0_cycle_list)) both_cycle_list = np.power(both_cycle_list, 1.0 / 2.0).tolist() top_k_cycle2_idx, top_k_values = min_k(both_cycle_list) return_dict["cycle_criterion"] = point_train_paths[ top_k_cycle2_idx[0]][0] pprint.pprint(return_dict) return return_dict
def compute_loss_train_Deformation_ChamferL2(self): self.loss_train_Deformation_ChamferL2 = (1 / 2.0) * ( loss.chamferL2(self.dist1_P2_P1, self.dist2_P2_P1) + loss.chamferL2(self.dist1_P1_P2, self.dist2_P1_P2))
map(lambda x, y: x.view(-1)[y[4].view(-1).data.long()].view(1, -1), labels_train_list, P2_P0_list)) iou_ours_list = list( map(lambda x: miou_shape.miou_shape(x.squeeze().cpu().numpy(), P2_label, trainer.parts), predicted_ours_P2_P0_list)) predicted_ours_P2_P0_list = torch.cat(predicted_ours_P2_P0_list) # Compute NN P2_P0_NN_list = list(map(lambda x: distChamfer(x, P2), points_train_list)) predicted_NN_P2_P0_list = list( map(lambda x, y: x.view(-1)[y[3].view(-1).data.long()].view(1, -1), labels_train_list, P2_P0_NN_list)) iou_NN_list = list(map(lambda x: miou_shape.miou_shape(x.squeeze().cpu().numpy(), P2_label, trainer.parts), predicted_NN_P2_P0_list)) predicted_NN_P2_P0_list = torch.cat(predicted_NN_P2_P0_list) # NN NN_chamferL2_list = list(map(lambda x: loss.chamferL2(x[0], x[1]), P2_P0_NN_list)) top_k_idx, top_k_values = min_k(NN_chamferL2_list) add("iou_NN", top_k_idx) # NN + ICP points_train_NN_ICP = ICP.ICP(points_train_list[top_k_idx[0]], P2).unsqueeze(0).float() dist1_NN_tr, dist2_NN_tr, idx1_NN_tr, idx2_NN_tr = distChamfer(points_train_NN_ICP, P2) predicted_P2_NN_tr = labels_train_list[top_k_idx[0]].view(-1)[idx2_NN_tr.view(-1).data.long()].view(-1) iou_dict["iou_NN_ICP_NN"] = miou_shape.miou_shape(predicted_P2_NN_tr.cpu().numpy(), P2_label, trainer.parts) # NN + ICP + ours P2_P1_ours_1, _, _, _, idx2_P2_P0 = loss.forward_chamfer(trainer.network, points_train_NN_ICP, P2, local_fix=None, distChamfer=distChamfer) iou_dict["iou_NN_ICP_ours"] = miou_shape.miou_shape( labels_train_list[top_k_idx[0]].view(-1)[idx2_P2_P0.view(-1).data.long()].view(1, -1).squeeze().cpu().numpy(),