def eval_step(self, data): ''' Performs an evaluation step. Args: data (dict): data dictionary ''' self.model.eval() device = self.device threshold = self.threshold occ = data.get('voxels').to(device) inputs = data.get('inputs').to(device) points = data.get('points_iou') points_occ = data.get('points_iou.occ') with torch.no_grad(): occ_logits = self.model(inputs).squeeze(1) eval_dict = {} # Compute loss occ_hat = torch.sigmoid(occ_logits) loss = F.binary_cross_entropy_with_logits(occ_logits, occ) eval_dict['loss'] = loss.item() # Compute discretized IOU occ_np = (occ >= 0.5).cpu().numpy() occ_hat_np = (occ_hat >= threshold).cpu().numpy() iou_voxels = compute_iou(occ_np, occ_hat_np).mean() eval_dict['iou_voxels'] = iou_voxels # Compute continuous IOU (if possible) if points is not None: voxel_grids = [ VoxelGrid(occ_hat_np_i) for occ_hat_np_i in occ_hat_np ] points_np = points.cpu().numpy() points_occ_np = (points_occ >= 0.5).cpu().numpy() points_occ_hat_np = np.stack( [vg.contains(p) for p, vg in zip(points_np, voxel_grids)]) iou = compute_iou(points_occ_np, points_occ_hat_np).mean() eval_dict['iou'] = iou return eval_dict
pointcloud_out_file = os.path.join(pointcloud_dir, "%s.ply" % modelname) export_pointcloud(pointcloud, pointcloud_out_file) out_file_dict["pointcloud"] = pointcloud_out_file if cfg["generation"]["copy_input"]: # Save inputs if input_type == "img": inputs_path = os.path.join(in_dir, "%s.jpg" % modelname) inputs = tf.squeeze(data["inputs"], axis=0) visualize_data(inputs, "img", inputs_path) out_file_dict["in"] = inputs_path elif input_type == "voxels": inputs_path = os.path.join(in_dir, "%s.off" % modelname) inputs = tf.squeeze(data["inputs"], axis=0) voxel_mesh = VoxelGrid(inputs).to_mesh() voxel_mesh.export(inputs_path) out_file_dict["in"] = inputs_path elif input_type == "pointcloud": inputs_path = os.path.join(in_dir, "%s.ply" % modelname) inputs = tf.squeeze(data["inputs"], axis=0).numpy() export_pointcloud(inputs, inputs_path, False) out_file_dict["in"] = inputs_path # Copy to visualization directory for first vis_n_output samples c_it = model_counter[category_id] if c_it < vis_n_outputs: # Save output files img_name = "%02d.off" % c_it for k, filepath in out_file_dict.items(): ext = os.path.splitext(filepath)[1]