def dump_point_cloud(scene_name, output_dir, points, label=None, weight=None, vis_weight_factor=40): ## dump color point clouds coord = points[:, 0:3] color = points[:, 3:6] pc_util.write_ply_rgb(coord, color, os.path.join(output_dir, scene_name + '_points.ply')) if label is not None: # dump point clouds label color_map = scannet_util.create_color_palette() label_nyu = np.copy(label) label_cnt = Counter(label.reshape(-1)) for val_id, cnt in label_cnt.items(): label_nyu[np.where(label == val_id)] = g_label_ids[val_id] pc_util.write_ply_color( coord, label_nyu, os.path.join(output_dir, scene_name + '_label.obj'), len(color_map), color_map) if weight is not None: # dump weight weight_color = vis_weight_factor * np.expand_dims( weight, axis=-1).astype(np.uint8) weight_color = np.concatenate( (weight_color, weight_color, weight_color), axis=-1) pc_util.write_ply_rgb( coord, weight_color, os.path.join(output_dir, scene_name + '_weight.ply'))
def dump_point_cloud(scene_name, output_dir, coord_img, valid_coord_idx, coord_label=None, coord_weight=None, vis_weight_factor=40): ## dump color point clouds vpt_coord = coord_img[valid_coord_idx, 0:3] vpt_color = coord_img[valid_coord_idx, 3:6] pc_util.write_ply_rgb(vpt_coord, vpt_color, os.path.join(output_dir, scene_name + '_vpt.ply')) if coord_label is not None: # dump point clouds label color_map = scannet_util.create_color_palette() coord_label = np.squeeze(coord_label, axis=-1) coord_label_nyu = np.copy(coord_label) coord_label_cnt = Counter(coord_label.reshape(-1)) for val_id, cnt in coord_label_cnt.items(): coord_label_nyu[np.where( coord_label == val_id)] = g_label_ids[val_id] vpt_label_nyu = coord_label_nyu[valid_coord_idx] pc_util.write_ply_color( vpt_coord, vpt_label_nyu, os.path.join(output_dir, scene_name + '_vpt_label.obj'), len(color_map), color_map) if coord_weight is not None: # dump weight vpt_weight_color = np.squeeze(coord_weight, axis=-1) vpt_weight_color = vis_weight_factor * np.expand_dims( vpt_weight_color, axis=-1).astype(np.uint8) vpt_weight_color = np.concatenate( (vpt_weight_color, vpt_weight_color, vpt_weight_color), axis=-1) vpt_weight_color = vpt_weight_color[valid_coord_idx] pc_util.write_ply_rgb( vpt_coord, vpt_weight_color, os.path.join(output_dir, scene_name + '_vpt_weight.ply'))
print "Warning : dropout is on, with ratio %f" % (DROPOUT_RATIO) if AUGMENT: print "Warning : rotation is on" if N >= 0: xyz, labels, _ = DATA[N] if DROPOUT: # Replicate the input dropout to see the effect of the decimation drop_idx = np.where( np.random.random((xyz.shape[0])) <= DROPOUT_RATIO)[0] xyz = np.delete(xyz, drop_idx, axis=0) labels = np.delete(labels, drop_idx) print "Exporting scene number " + str(N) pc_util.write_ply_color(xyz, labels, OUTPUT_DIR + "/{}_{}.obj".format(SET, N), NUM_CLASSES) else: for i in range(len(DATA)): xyz, labels, _ = DATA[i] mean_x, mean_y, mean_z = np.mean(xyz, axis=0) std_x, std_y, std_z = np.std(xyz, axis=0) print "Exporting scene number " + str(i) # Print some statistics print "Stats: means=%1.2f/%1.2f/%1.2f, std=%1.2f/%1.2f/%1.2f" % ( mean_x, mean_y, mean_z, std_x, std_y, std_z) # You can open this files with meshlab for instance pc_util.write_ply_color(xyz, labels, OUTPUT_DIR + "/{}_{}.obj".format(SET, i), NUM_CLASSES)
# Outputs OUTPUT_DIR = "visu/color" if not os.path.exists(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) if DROPOUT: print "Warning : dropout is on, with ratio %f" % (DROPOUT_RATIO) if AUGMENT: print "Warning : rotation is on" for i in range(N): data, label_data, _ = DATA.next_batch(BATCH_SIZE, AUGMENT, DROPOUT) if (N > 1): print "Exporting batch number " + str(N) for j, scene in enumerate(data): labels = label_data[j] if DROPOUT: # Replicate the input dropout to see the effect of the decimation drop_idx = np.where( np.random.random((scene.shape[0])) <= DROPOUT_RATIO)[0] scene = np.delete(scene, drop_idx, axis=0) labels = np.delete(labels, drop_idx) np.savetxt(OUTPUT_DIR + "/{}_{}_{}.obj".format(SET, "trueColors", j), scene, delimiter=" ") pc_util.write_ply_color( scene[:, 0:3], labels, OUTPUT_DIR + "/{}_{}_{}.obj".format(SET, "labelColors", j), NUM_CLASSES)
def visualize_step(self, phase, iter, gt_data, our_data, eval_dict, inference_switch=False): ''' Performs a visualization step. ''' split_file = os.path.join(self.cfg.config['data']['split'], 'scannetv2_' + phase + '.json') scene_name = read_json(split_file)[gt_data['scan_idx']]['scan'].split( '/')[3] dump_dir = os.path.join(self.cfg.config['log']['vis_path'], '%s_%s_%s' % (phase, iter, scene_name)) if not os.path.exists(dump_dir): os.mkdir(dump_dir) DUMP_CONF_THRESH = self.cfg.config['generation'][ 'dump_threshold'] # Dump boxes with obj prob larger than that. batch_id = 0 '''Predict meshes''' pred_sem_cls = our_data[7]['pred_sem_cls'][batch_id].cpu().numpy() if our_data[5] is not None: meshes = our_data[5] BATCH_PROPOSAL_IDs = our_data[3][0].cpu().numpy() for mesh_data, map_data in zip(meshes, BATCH_PROPOSAL_IDs): str_nums = (map_data[0], map_data[1], pred_sem_cls[map_data[0]]) object_mesh = os.path.join( dump_dir, 'proposal_%d_target_%d_class_%d_mesh.ply' % str_nums) mesh_data.export(object_mesh) else: BATCH_PROPOSAL_IDs = np.empty(0) '''Predict boxes''' est_data = our_data[0] pred_corners_3d_upright_camera = our_data[7][ 'pred_corners_3d_upright_camera'] objectness_prob = our_data[7]['obj_prob'][batch_id] # INPUT point_clouds = gt_data['point_clouds'].cpu().numpy() # NETWORK OUTPUTS seed_xyz = est_data['seed_xyz'].detach().cpu().numpy( ) # (B,num_seed,3) if 'vote_xyz' in est_data: # aggregated_vote_xyz = est_data['aggregated_vote_xyz'].detach().cpu().numpy() # vote_xyz = est_data['vote_xyz'].detach().cpu().numpy() # (B,num_seed,3) aggregated_vote_xyz = est_data['aggregated_vote_xyz'].detach().cpu( ).numpy() box_corners_cam = pred_corners_3d_upright_camera[batch_id] box_corners_depth = flip_axis_to_depth(box_corners_cam) centroid = (np.max(box_corners_depth, axis=1) + np.min(box_corners_depth, axis=1)) / 2. forward_vector = box_corners_depth[:, 1] - box_corners_depth[:, 2] left_vector = box_corners_depth[:, 0] - box_corners_depth[:, 1] up_vector = box_corners_depth[:, 6] - box_corners_depth[:, 2] orientation = np.arctan2(forward_vector[:, 1], forward_vector[:, 0]) forward_size = np.linalg.norm(forward_vector, axis=1) left_size = np.linalg.norm(left_vector, axis=1) up_size = np.linalg.norm(up_vector, axis=1) sizes = np.vstack([forward_size, left_size, up_size]).T box_params = np.hstack([centroid, sizes, orientation[:, np.newaxis]]) # OTHERS pred_mask = eval_dict['pred_mask'] # B,num_proposal pc = point_clouds[batch_id, :, :] # Dump various point clouds pc_util.write_ply(pc, os.path.join(dump_dir, '%06d_pc.ply' % (batch_id))) pc_util.write_ply( seed_xyz[batch_id, :, :], os.path.join(dump_dir, '%06d_seed_pc.ply' % (batch_id))) if 'vote_xyz' in est_data: pc_util.write_ply( est_data['vote_xyz'][batch_id, :, :], os.path.join(dump_dir, '%06d_vgen_pc.ply' % (batch_id))) pc_util.write_ply( aggregated_vote_xyz[batch_id, :, :], os.path.join(dump_dir, '%06d_aggregated_vote_pc.ply' % (batch_id))) pc_util.write_ply( box_params[:, 0:3], os.path.join(dump_dir, '%06d_proposal_pc.ply' % (batch_id))) if np.sum(objectness_prob > DUMP_CONF_THRESH) > 0: pc_util.write_ply( box_params[objectness_prob > DUMP_CONF_THRESH, 0:3], os.path.join(dump_dir, '%06d_confident_proposal_pc.ply' % (batch_id))) # Dump predicted bounding boxes if np.sum(objectness_prob > DUMP_CONF_THRESH) > 0: num_proposal = box_params.shape[0] if len(box_params) > 0: pc_util.write_oriented_bbox( box_params[objectness_prob > DUMP_CONF_THRESH, :], os.path.join(dump_dir, '%06d_pred_confident_bbox.ply' % (batch_id))) pc_util.write_oriented_bbox( box_params[np.logical_and( objectness_prob > DUMP_CONF_THRESH, pred_mask[ batch_id, :] == 1), :], os.path.join( dump_dir, '%06d_pred_confident_nms_bbox.ply' % (batch_id))) pc_util.write_oriented_bbox( box_params[pred_mask[batch_id, :] == 1, :], os.path.join(dump_dir, '%06d_pred_nms_bbox.ply' % (batch_id))) pc_util.write_oriented_bbox( box_params, os.path.join(dump_dir, '%06d_pred_bbox.ply' % (batch_id))) save_path = os.path.join( dump_dir, '%06d_pred_confident_nms_bbox.npz' % (batch_id)) np.savez(save_path, obbs=box_params[np.logical_and( objectness_prob > DUMP_CONF_THRESH, pred_mask[ batch_id, :] == 1), :], proposal_map=BATCH_PROPOSAL_IDs) # Return if it is at inference time. No dumping of groundtruths if inference_switch: return objectness_loss, objectness_label, objectness_mask, object_assignment = \ compute_objectness_loss(est_data, gt_data) # LABELS gt_center = gt_data['center_label'].cpu().numpy() # (B,MAX_NUM_OBJ,3) gt_mask = gt_data['box_label_mask'].cpu().numpy() # B,K2 gt_heading_class = gt_data['heading_class_label'].cpu().numpy() # B,K2 gt_heading_residual = gt_data['heading_residual_label'].cpu().numpy( ) # B,K2 gt_size_class = gt_data['size_class_label'].cpu().numpy() # B,K2 gt_size_residual = gt_data['size_residual_label'].cpu().numpy( ) # B,K2,3 objectness_label = objectness_label.detach().cpu().numpy() # (B,K,) objectness_mask = objectness_mask.detach().cpu().numpy() # (B,K,) if np.sum(objectness_label[batch_id, :]) > 0: pc_util.write_ply( box_params[objectness_label[batch_id, :] > 0, 0:3], os.path.join(dump_dir, '%06d_gt_positive_proposal_pc.ply' % (batch_id))) if np.sum(objectness_mask[batch_id, :]) > 0: pc_util.write_ply( box_params[objectness_mask[batch_id, :] > 0, 0:3], os.path.join(dump_dir, '%06d_gt_mask_proposal_pc.ply' % (batch_id))) pc_util.write_ply( gt_center[batch_id, :, 0:3], os.path.join(dump_dir, '%06d_gt_centroid_pc.ply' % (batch_id))) pc_util.write_ply_color( box_params[:, 0:3], objectness_label[batch_id, :], os.path.join(dump_dir, '%06d_proposal_pc_objectness_label.ply' % (batch_id))) # Dump GT bounding boxes obbs = [] for j in range(gt_center.shape[1]): if gt_mask[batch_id, j] == 0: continue obb = self.cfg.dataset_config.param2obb( gt_center[batch_id, j, 0:3], gt_heading_class[batch_id, j], gt_heading_residual[batch_id, j], gt_size_class[batch_id, j], gt_size_residual[batch_id, j]) obbs.append(obb) if len(obbs) > 0: obbs = np.vstack(tuple(obbs)) # (num_gt_objects, 7) pc_util.write_oriented_bbox( obbs, os.path.join(dump_dir, '%06d_gt_bbox.ply' % (batch_id))) # OPTIONALL, also dump prediction and gt details if 'batch_pred_map_cls' in eval_dict: fout = open( os.path.join(dump_dir, '%06d_pred_map_cls.txt' % (batch_id)), 'w') for t in eval_dict['batch_pred_map_cls'][batch_id]: fout.write(str(t[0]) + ' ') fout.write(",".join([str(x) for x in list(t[1].flatten())])) fout.write(' ' + str(t[2])) fout.write('\n') fout.close() if 'batch_gt_map_cls' in eval_dict: fout = open( os.path.join(dump_dir, '%06d_gt_map_cls.txt' % (batch_id)), 'w') for t in eval_dict['batch_gt_map_cls'][batch_id]: fout.write(str(t[0]) + ' ') fout.write(",".join([str(x) for x in list(t[1].flatten())])) fout.write('\n') fout.close()
def dump_results(end_points, dump_dir, config, inference_switch=False): ''' Dump results. Args: end_points: dict {..., pred_mask} pred_mask is a binary mask array of size (batch_size, num_proposal) computed by running NMS and empty box removal Returns: None ''' if not os.path.exists(dump_dir): os.system('mkdir %s' % (dump_dir)) # INPUT point_clouds = end_points['point_clouds'].cpu().numpy() batch_size = point_clouds.shape[0] # NETWORK OUTPUTS seed_xyz = end_points['seed_xyz'].detach().cpu().numpy() # (B,num_seed,3) if 'vote_xyz' in end_points: aggregated_vote_xyz = end_points['aggregated_vote_xyz'].detach().cpu( ).numpy() vote_xyz = end_points['vote_xyz'].detach().cpu().numpy( ) # (B,num_seed,3) aggregated_vote_xyz = end_points['aggregated_vote_xyz'].detach().cpu( ).numpy() objectness_scores = end_points['objectness_scores'].detach().cpu().numpy( ) # (B,K,2) pred_center = end_points['center'].detach().cpu().numpy() # (B,K,3) pred_heading_class = torch.argmax(end_points['heading_scores'], -1) # B,num_proposal pred_heading_residual = torch.gather( end_points['heading_residuals'], 2, pred_heading_class.unsqueeze(-1)) # B,num_proposal,1 pred_heading_class = pred_heading_class.detach().cpu().numpy( ) # B,num_proposal pred_heading_residual = pred_heading_residual.squeeze( 2).detach().cpu().numpy() # B,num_proposal pred_size_class = torch.argmax(end_points['size_scores'], -1) # B,num_proposal pred_size_residual = torch.gather( end_points['size_residuals'], 2, pred_size_class.unsqueeze(-1).unsqueeze(-1).repeat( 1, 1, 1, 3)) # B,num_proposal,1,3 pred_size_residual = pred_size_residual.squeeze( 2).detach().cpu().numpy() # B,num_proposal,3 # OTHERS pred_mask = end_points['pred_mask'] # B,num_proposal idx_beg = 0 for i in range(batch_size): pc = point_clouds[i, :, :] objectness_prob = softmax(objectness_scores[i, :, :])[:, 1] # (K,) # Dump various point clouds pc_util.write_ply( pc, os.path.join(dump_dir, '%06d_pc.ply' % (idx_beg + i))) pc_util.write_ply( seed_xyz[i, :, :], os.path.join(dump_dir, '%06d_seed_pc.ply' % (idx_beg + i))) if 'vote_xyz' in end_points: pc_util.write_ply( end_points['vote_xyz'][i, :, :], os.path.join(dump_dir, '%06d_vgen_pc.ply' % (idx_beg + i))) pc_util.write_ply( aggregated_vote_xyz[i, :, :], os.path.join(dump_dir, '%06d_aggregated_vote_pc.ply' % (idx_beg + i))) pc_util.write_ply( aggregated_vote_xyz[i, :, :], os.path.join(dump_dir, '%06d_aggregated_vote_pc.ply' % (idx_beg + i))) pc_util.write_ply( pred_center[i, :, 0:3], os.path.join(dump_dir, '%06d_proposal_pc.ply' % (idx_beg + i))) if np.sum(objectness_prob > DUMP_CONF_THRESH) > 0: pc_util.write_ply( pred_center[i, objectness_prob > DUMP_CONF_THRESH, 0:3], os.path.join(dump_dir, '%06d_confident_proposal_pc.ply' % (idx_beg + i))) # Dump predicted bounding boxes if np.sum(objectness_prob > DUMP_CONF_THRESH) > 0: num_proposal = pred_center.shape[1] obbs = [] for j in range(num_proposal): obb = config.param2obb(pred_center[i, j, 0:3], pred_heading_class[i, j], pred_heading_residual[i, j], pred_size_class[i, j], pred_size_residual[i, j]) obbs.append(obb) if len(obbs) > 0: obbs = np.vstack(tuple(obbs)) # (num_proposal, 7) pc_util.write_oriented_bbox( obbs[objectness_prob > DUMP_CONF_THRESH, :], os.path.join( dump_dir, '%06d_pred_confident_bbox.ply' % (idx_beg + i))) pc_util.write_oriented_bbox( obbs[np.logical_and(objectness_prob > DUMP_CONF_THRESH, pred_mask[i, :] == 1), :], os.path.join( dump_dir, '%06d_pred_confident_nms_bbox.ply' % (idx_beg + i))) pc_util.write_oriented_bbox( obbs[pred_mask[i, :] == 1, :], os.path.join(dump_dir, '%06d_pred_nms_bbox.ply' % (idx_beg + i))) pc_util.write_oriented_bbox( obbs, os.path.join(dump_dir, '%06d_pred_bbox.ply' % (idx_beg + i))) # Return if it is at inference time. No dumping of groundtruths if inference_switch: return # LABELS gt_center = end_points['center_label'].cpu().numpy() # (B,MAX_NUM_OBJ,3) gt_mask = end_points['box_label_mask'].cpu().numpy() # B,K2 gt_heading_class = end_points['heading_class_label'].cpu().numpy() # B,K2 gt_heading_residual = end_points['heading_residual_label'].cpu().numpy( ) # B,K2 gt_size_class = end_points['size_class_label'].cpu().numpy() # B,K2 gt_size_residual = end_points['size_residual_label'].cpu().numpy( ) # B,K2,3 objectness_label = end_points['objectness_label'].detach().cpu().numpy( ) # (B,K,) objectness_mask = end_points['objectness_mask'].detach().cpu().numpy( ) # (B,K,) for i in range(batch_size): if np.sum(objectness_label[i, :]) > 0: pc_util.write_ply( pred_center[i, objectness_label[i, :] > 0, 0:3], os.path.join( dump_dir, '%06d_gt_positive_proposal_pc.ply' % (idx_beg + i))) if np.sum(objectness_mask[i, :]) > 0: pc_util.write_ply( pred_center[i, objectness_mask[i, :] > 0, 0:3], os.path.join(dump_dir, '%06d_gt_mask_proposal_pc.ply' % (idx_beg + i))) pc_util.write_ply( gt_center[i, :, 0:3], os.path.join(dump_dir, '%06d_gt_centroid_pc.ply' % (idx_beg + i))) pc_util.write_ply_color( pred_center[i, :, 0:3], objectness_label[i, :], os.path.join( dump_dir, '%06d_proposal_pc_objectness_label.obj' % (idx_beg + i))) # Dump GT bounding boxes obbs = [] for j in range(gt_center.shape[1]): if gt_mask[i, j] == 0: continue obb = config.param2obb(gt_center[i, j, 0:3], gt_heading_class[i, j], gt_heading_residual[i, j], gt_size_class[i, j], gt_size_residual[i, j]) obbs.append(obb) if len(obbs) > 0: obbs = np.vstack(tuple(obbs)) # (num_gt_objects, 7) pc_util.write_oriented_bbox( obbs, os.path.join(dump_dir, '%06d_gt_bbox.ply' % (idx_beg + i))) # OPTIONALL, also dump prediction and gt details if 'batch_pred_map_cls' in end_points: for ii in range(batch_size): fout = open(os.path.join(dump_dir, '%06d_pred_map_cls.txt' % (ii)), 'w') for t in end_points['batch_pred_map_cls'][ii]: fout.write(str(t[0]) + ' ') fout.write(",".join([str(x) for x in list(t[1].flatten())])) fout.write(' ' + str(t[2])) fout.write('\n') fout.close() if 'batch_gt_map_cls' in end_points: for ii in range(batch_size): fout = open(os.path.join(dump_dir, '%06d_gt_map_cls.txt' % (ii)), 'w') for t in end_points['batch_gt_map_cls'][ii]: fout.write(str(t[0]) + ' ') fout.write(",".join([str(x) for x in list(t[1].flatten())])) fout.write('\n') fout.close()
mask = np.sum((scene>=box_min)*(scene<=box_max),axis=1) == 3 return mask def input_dropout(self,Input,dropout_max=0.875): dropout_ratio = np.random.random()*dropout_max drop_index = np.where(np.random.random((Input.shape[0]))<=dropout_ratio)[0] return drop_index def get_total_num_points(self): total = 0 for scene_index in range(len(self)): total += len(self.scene_points_list[scene_index]) return total def __len__(self): return len(self.scene_points_list) if __name__ == '__main__': # Run some tests DATA_PATH = "semantic_data/" data = Dataset(20000) print "There are %i scenes" % (len(data)) Input, labels, weights = data.next_input(False,False) print Input print labels print weights print "Batch shape is " +str(np.shape(data.next_batch(30)[0])) print "Total number of points %i" %(data.get_total_num_points()) print "Exemple num batch %i" %(int(data.get_total_num_points()/(32*20000))) pc_util.write_ply_color(Input, labels, "../visu/test6.obj")
def predict(): """ Load the selected checkpoint and predict the labels Write in the output directories both groundtruth and prediction This enable to visualize side to side the prediction and the true labels, and helps to debug the network """ with tf.device('/gpu:' + str(GPU_INDEX)): pointclouds_pl, labels_pl, _ = MODEL.placeholder_inputs( BATCH_SIZE, NUM_POINT) print tf.shape(pointclouds_pl) is_training_pl = tf.placeholder(tf.bool, shape=()) # simple model pred, _ = MODEL.get_model(pointclouds_pl, is_training_pl, NUM_CLASSES) # Add ops to save and restore all the variables. saver = tf.train.Saver() # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Restore variables from disk. saver.restore(sess, CHECKPOINT) print "Model restored." ops = { 'pointclouds_pl': pointclouds_pl, 'labels_pl': labels_pl, 'is_training_pl': is_training_pl, 'pred': pred } # To add the histograms meta_hist_true = np.zeros(9) meta_hist_pred = np.zeros(9) for idx in range(len(DATASET)): xyz, true_labels, _ = DATASET[idx] # Ground truth print "Exporting scene number " + str(idx) pc_util.write_ply_color( xyz, true_labels, "{}/{}_{}.obj".format(OUTPUT_DIR_GROUNDTRUTH, SET, idx), NUM_CLASSES) # Prediction pred_labels = predict_one_input(sess, ops, idx) # Compute mean IoU iou, update_op = tf.metrics.mean_iou(tf.to_int64(true_labels), tf.to_int64(pred_labels), NUM_CLASSES) sess.run(tf.local_variables_initializer()) update_op.eval(session=sess) print(sess.run(iou)) hist_true, _ = np.histogram(true_labels, range(NUM_CLASSES + 1)) hist_pred, _ = np.histogram(pred_labels, range(NUM_CLASSES + 1)) # update meta histograms meta_hist_true += hist_true meta_hist_pred += hist_pred # print individual histograms print hist_true print hist_pred pc_util.write_ply_color( xyz, pred_labels, "{}/{}_{}.obj".format(OUTPUT_DIR_PREDICTION, SET, idx), NUM_CLASSES) meta_hist_pred = (meta_hist_pred / sum(meta_hist_pred)) * 100 meta_hist_true = (meta_hist_true / sum(meta_hist_true)) * 100 print(LABELS_TEXT) print(meta_hist_true) print(meta_hist_pred)
else: for j, scene in enumerate(data): labels = label_data[j] if PARAMS['use_color']: pc_util.write_ply_true_color( scene[:, 0:3], (255 * scene[:, 3:6]).astype(int), OUTPUT_DIR_BATCHES + "/{}_{}_{}.txt".format(SET, "trueColors", j)) else: pc_util.write_ply_true_color( scene[:, 0:3], np.zeros(scene[:, 0:3].shape), OUTPUT_DIR_BATCHES + "/{}_{}_{}.txt".format(SET, "trueColors", j)) pc_util.write_ply_color( scene[:, 0:3], labels, OUTPUT_DIR_BATCHES + "/{}_{}_{}.txt".format(SET, "labelColors", j), NUM_CLASSES) if GROUP_BY_BATCHES and NBATCH > 0: if PARAMS['use_color']: pc_util.write_ply_true_color( visu_cloud[:, 0:3], (255 * visu_cloud[:, 3:6]).astype(int), OUTPUT_DIR_BATCHES + "/{}_{}.txt".format(SET, "trueColors")) else: pc_util.write_ply_true_color( visu_cloud[:, 0:3], np.zeros(visu_cloud[:, 0:3].shape), OUTPUT_DIR_BATCHES + "/{}_{}.txt".format(SET, "trueColors")) pc_util.write_ply_color( visu_cloud[:, 0:3], visu_labels, OUTPUT_DIR_BATCHES + "/{}_{}.txt".format(SET, "labelColors"), NUM_CLASSES)
def predict(): """ Load the selected checkpoint and predict the labels Write in the output directories both groundtruth and prediction This enable to visualize side to side the prediction and the true labels, and helps to debug the network """ with tf.device('/gpu:' + str(GPU_INDEX)): pointclouds_pl, labels_pl, _ = MODEL.placeholder_inputs( 1, NUM_POINT, hyperparams=PARAMS) print(tf.shape(pointclouds_pl)) is_training_pl = tf.placeholder(tf.bool, shape=()) # simple model pred, _ = MODEL.get_model(pointclouds_pl, is_training_pl, NUM_CLASSES, hyperparams=PARAMS) # Add ops to save and restore all the variables. saver = tf.train.Saver() # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Restore variables from disk. saver.restore(sess, CHECKPOINT) print("Model restored.") ops = { 'pointclouds_pl': pointclouds_pl, 'labels_pl': labels_pl, 'is_training_pl': is_training_pl, 'pred': pred } if EXPORT_FULL_POINT_CLOUDS: OUTPUT_DIR_FULL_PC = os.path.join(OUTPUT_DIR, "full_scenes_predictions") if not os.path.exists(OUTPUT_DIR_FULL_PC): os.mkdir(OUTPUT_DIR_FULL_PC) nscenes = len(DATASET) p = 6 if PARAMS['use_color'] else 3 scene_points = [np.array([]).reshape((0, p)) for i in range(nscenes)] ground_truth = [np.array([]) for i in range(nscenes)] predicted_labels = [np.array([]) for i in range(nscenes)] for i in range(N * nscenes): if i % 100 == 0 and i > 0: print("{} inputs generated".format(i)) f, data, raw_data, true_labels, col, _ = DATASET.next_input( DROPOUT, True, False, predicting=True) if p == 6: raw_data = np.hstack((raw_data, col)) data = np.hstack((data, col)) pred_labels = predict_one_input(sess, ops, data) scene_points[f] = np.vstack((scene_points[f], raw_data)) ground_truth[f] = np.hstack((ground_truth[f], true_labels)) predicted_labels[f] = np.hstack((predicted_labels[f], pred_labels)) filenames = DATASET.get_data_filenames() filenamesForExport = filenames[0:min(len(filenames), MAX_EXPORT)] print("{} point clouds to export".format(len(filenamesForExport))) for f, filename in enumerate(filenamesForExport): print("exporting file {} which has {} points".format( os.path.basename(filename), len(ground_truth[f]))) pc_util.write_ply_color( scene_points[f][:, 0:3], ground_truth[f], OUTPUT_DIR_FULL_PC + "/{}_groundtruth.txt".format(os.path.basename(filename)), NUM_CLASSES) pc_util.write_ply_color( scene_points[f][:, 0:3], predicted_labels[f], OUTPUT_DIR_FULL_PC + "/{}_aggregated.txt".format(os.path.basename(filename)), NUM_CLASSES) np.savetxt(OUTPUT_DIR_FULL_PC + "/{}_pred.txt".format(os.path.basename(filename)), predicted_labels[f].reshape((-1, 1)), delimiter=" ") print("done.") return if not os.path.exists(OUTPUT_DIR_GROUNDTRUTH): os.mkdir(OUTPUT_DIR_GROUNDTRUTH) if not os.path.exists(OUTPUT_DIR_PREDICTION): os.mkdir(OUTPUT_DIR_PREDICTION) # To add the histograms meta_hist_true = np.zeros(9) meta_hist_pred = np.zeros(9) for idx in range(N): data, true_labels, _, _ = DATASET.next_input(DROPOUT, True, False) # Ground truth print("Exporting scene number " + str(idx)) pc_util.write_ply_color( data[:, 0:3], true_labels, OUTPUT_DIR_GROUNDTRUTH + "/{}_{}.txt".format(SET, idx), NUM_CLASSES) # Prediction pred_labels = predict_one_input(sess, ops, data) # Compute mean IoU iou, update_op = tf.metrics.mean_iou(tf.to_int64(true_labels), tf.to_int64(pred_labels), NUM_CLASSES) sess.run(tf.local_variables_initializer()) update_op.eval(session=sess) print(sess.run(iou)) hist_true, _ = np.histogram(true_labels, range(NUM_CLASSES + 1)) hist_pred, _ = np.histogram(pred_labels, range(NUM_CLASSES + 1)) # update meta histograms meta_hist_true += hist_true meta_hist_pred += hist_pred # print individual histograms print(hist_true) print(hist_pred) pc_util.write_ply_color( data[:, 0:3], pred_labels, "{}/{}_{}.txt".format(OUTPUT_DIR_PREDICTION, SET, idx), NUM_CLASSES) meta_hist_pred = (meta_hist_pred / sum(meta_hist_pred)) * 100 meta_hist_true = (meta_hist_true / sum(meta_hist_true)) * 100 print(LABELS_TEXT) print(meta_hist_true) print(meta_hist_pred)