def load_world_pointcloud(self, filename): def _transform(xyz, intrinsic, extrinsic): camera = Camera(intrinsic) xyz[:, 1:3] *= -1 xyz = camera.camera2world(extrinsic, xyz) xyz[:, 1:3] *= -1 return xyz filesep = filename.split(os.sep) seqname = filesep[0] fileidx = os.path.splitext(filesep[2])[0] camera_path = self.camera_path % seqname intrinsic_file = camera_path + self.camera_intrinsic_file intrinsic = SynthiaDataset.load_intrinsics(intrinsic_file) extrinsic_file = camera_path + self.camera_extrinsics_file % fileidx extrinsic = SynthiaDataset.load_extrinsics(extrinsic_file) ptc = read_plyfile(self.data_root / filename) xyz, rgbc = ptc[:, :3], ptc[:, 3:] xyz = _transform(xyz, intrinsic, extrinsic) ptc = np.hstack((xyz, rgbc)) center = np.zeros((1, 3)) center = _transform(center, intrinsic, extrinsic)[0] return ptc, center
def load_ply(self, index): if not self.load_whole: filepath = self.data_root / self.data_paths[index] return read_plyfile(filepath), None # readply get 7 chs: xyz-rgb-label else: feats = self.data_dict['data'][index] label = self.data_dict['label'][index] return np.concatenate([feats, label.reshape(-1,1)], axis=-1), None
def handle_process(path): f = Path(path.split(',')[0]) phase_out_path = Path(path.split(',')[1]) pointcloud = read_plyfile(f) # Make sure alpha value is meaningless. assert np.unique(pointcloud[:, -1]).size == 1 # Load label file. label_f = f.parent / (f.stem + '.labels' + f.suffix) if label_f.is_file(): label = read_plyfile(label_f) # Sanity check that the pointcloud and its label has same vertices. assert pointcloud.shape[0] == label.shape[0] assert np.allclose(pointcloud[:, :3], label[:, :3]) else: # Label may not exist in test case. label = np.zeros_like(pointcloud) out_f = phase_out_path / (f.name[:-len(POINTCLOUD_FILE)] + f.suffix) processed = np.hstack((pointcloud[:, :6], np.array([label[:, -1]]).T)) save_point_cloud(processed, out_f, with_label=True, verbose=False)
def generate_meta(voxels_path, split_path, get_area_fn, trainarea, valarea, testarea, data_root='/', check_pc=False): train_file_list = [] val_file_list = [] test_file_list = [] for pointcloud_file in tqdm(glob.glob(osp.join(data_root, voxels_path))): area = get_area_fn(pointcloud_file) if area in trainarea: file_list = train_file_list elif area in valarea: file_list = val_file_list elif area in testarea: file_list = test_file_list else: raise ValueError('Area %s not in the split' % area) # Skip label files. if pointcloud_file.endswith('_label_voxel.ply'): continue # Parse and check if the corresponding label file exists. file_stem, file_ext = osp.splitext(pointcloud_file) file_stem_split = file_stem.split('_') file_stem_split.insert(-1, 'label') pointcloud_label_file = '_'.join(file_stem_split) + file_ext if not osp.isfile(pointcloud_label_file): raise ValueError('Lable file missing for: ' + pointcloud_file) # Check if the pointcloud is empty. if check_pc: pointcloud_data = read_plyfile(pointcloud_file) if not pointcloud_data: print('Skipping empty point cloud: %s.') continue pointcloud_file = osp.relpath(pointcloud_file, data_root) pointcloud_label_file = osp.relpath(pointcloud_label_file, data_root) # Append metadata. file_list.append([pointcloud_file, pointcloud_label_file]) with open(split_path % 'train', 'w') as f: f.write('\n'.join([' '.join(pair) for pair in train_file_list])) with open(split_path % 'val', 'w') as f: f.write('\n'.join([' '.join(pair) for pair in val_file_list])) with open(split_path % 'test', 'w') as f: f.write('\n'.join([' '.join(pair) for pair in test_file_list]))
def load_datafile(self, index): filepath = self.data_root / self.data_paths[index] scene_id = os.path.splitext(self.data_paths[index].split( os.sep)[-1])[0] scene_f = self.config.scannet_alignment_path % (scene_id, scene_id) ptc = read_plyfile(filepath) if os.path.isfile(scene_f): alignment_txt = [ l for l in read_txt(scene_f) if l.startswith('axisAlignment = ') ][0] rot = np.array([float(x) for x in alignment_txt[16:].split() ]).reshape(4, 4) xyz1 = np.hstack((ptc[:, :3], np.ones((ptc.shape[0], 1)))) ptc[:, :3] = (xyz1 @ rot.T)[:, :3] return ptc, None, None
def test_pointcloud(self, pred_dir): print('Running full pointcloud evaluation.') eval_path = os.path.join(pred_dir, 'fulleval') os.makedirs(eval_path, exist_ok=True) # Join room by their area and room id. # Test independently for each room. sys.setrecursionlimit(100000) # Increase recursion limit for k-d tree. hist = np.zeros((self.NUM_LABELS, self.NUM_LABELS)) for i, data_path in enumerate(self.data_paths): room_id = self.get_output_id(i) pred = np.load( os.path.join(pred_dir, 'pred_%04d_%02d.npy' % (i, 0))) # save voxelized pointcloud predictions save_point_cloud(np.hstack( (pred[:, :3], np.array([SCANNET_COLOR_MAP[i] for i in pred[:, -1]]))), f'{eval_path}/{room_id}_voxel.ply', verbose=False) fullply_f = self.data_root / data_path query_pointcloud = read_plyfile(fullply_f) query_xyz = query_pointcloud[:, :3] query_label = query_pointcloud[:, -1] # Run test for each room. pred_tree = spatial.KDTree(pred[:, :3], leafsize=500) _, result = pred_tree.query(query_xyz) ptc_pred = pred[result, 3].astype(int) # Save prediciton in txt format for submission. np.savetxt(f'{eval_path}/{room_id}.txt', ptc_pred, fmt='%i') # Save prediciton in colored pointcloud for visualization. save_point_cloud(np.hstack( (query_xyz, np.array([SCANNET_COLOR_MAP[i] for i in ptc_pred]))), f'{eval_path}/{room_id}.ply', verbose=False) # Evaluate IoU. if self.IGNORE_LABELS is not None: ptc_pred = np.array([self.label_map[x] for x in ptc_pred], dtype=np.int) query_label = np.array( [self.label_map[x] for x in query_label], dtype=np.int) hist += fast_hist(ptc_pred, query_label, self.NUM_LABELS) ious = per_class_iu(hist) * 100 print('mIoU: ' + str(np.nanmean(ious)) + '\n' 'Class names: ' + ', '.join(CLASS_LABELS) + '\n' 'IoU: ' + ', '.join(np.round(ious, 2).astype(str)))
def load_ply(self, index): filepath = self.data_root / self.data_paths[index] return read_plyfile(filepath), None
POINTCLOUD_FILE = '_vh_clean_2.ply' CROP_SIZE = -1 BUGS = { 'train/scene0270_00_*.ply': 50, 'train/scene0270_02_*.ply': 50, 'train/scene0384_00_*.ply': 149, } # Preprocess data. for out_path, in_path in SUBSETS.items(): phase_out_path = SCANNET_OUT_PATH / out_path phase_out_path.mkdir(parents=True, exist_ok=True) for f in (SCANNET_RAW_PATH / in_path).glob('*/*' + POINTCLOUD_FILE): # Load pointcloud file. pointcloud = read_plyfile(f) # Make sure alpha value is meaningless. assert np.unique(pointcloud[:, -1]).size == 1 # Load label file. label_f = f.parent / (f.stem + '.labels' + f.suffix) if label_f.is_file(): label = read_plyfile(label_f) # Sanity check that the pointcloud and its label has same vertices. assert pointcloud.shape[0] == label.shape[0] assert np.allclose(pointcloud[:, :3], label[:, :3]) else: # Label may not exist in test case. label = np.zeros_like(pointcloud) xyz = pointcloud[:, :3] if CROP_SIZE > 0: xyz_range = xyz.max(0) - xyz.min(0) crop_half = CROP_SIZE / 2
xyz = pointcloud[:, :3] pool = ProcessPoolExecutor(max_workers=9) all_points = np.empty((0, 3)) out_f = k / (f.name[:-len(POINTCLOUD_FILE)] + f.suffix) processed = np.hstack((pointcloud[:, :6], np.array([label[:, -1]]).T)) save_point_cloud(processed, out_f, with_label=True, verbose=False) path_list=[] for out_path, in_path in SUBSETS.items(): phase_out_path = SCANNET_OUT_PATH / out_path phase_out_path.mkdir(parents=True, exist_ok=True) for f in (SCANNET_RAW_PATH / in_path).glob('*/*' + POINTCLOUD_FILE): path_list.append(str(f)+','+str(phase_out_path)) print(path_list) pool = ProcessPoolExecutor(max_workers=20) result = list(pool.map(handle_process,path_list)) for i in result: pass """ # Fix bug in the data. for files, bug_index in BUGS.items(): print(files) for f in SCANNET_OUT_PATH.glob(files): pointcloud = read_plyfile(f) bug_mask = pointcloud[:, -1] == bug_index print(f'Fixing {f} bugged label {bug_index} x {bug_mask.sum()}') pointcloud[bug_mask, -1] = 0 save_point_cloud(pointcloud, f, with_label=True, verbose=False)
dtype(point) for dtype, point in zip(python_types, cur_point))) vertices_array = np.array(vertices, dtype=npy_types) el = PlyElement.describe(vertices_array, 'vertex') # Write PlyData([el]).write(filename) # Preprocess data. for out_path, in_path in SUBSETS.items(): phase_out_path = SCANNET_OUT_PATH / out_path phase_out_path.mkdir(parents=True, exist_ok=True) for f in (SCANNET_RAW_PATH / in_path).glob('*/*' + POINTCLOUD_FILE): # Load pointcloud file. out_f = phase_out_path / (f.name[:-len(POINTCLOUD_FILE)] + f.suffix) pointcloud = read_plyfile(f) num_points = pointcloud.shape[0] # Make sure alpha value is meaningless. assert np.unique(pointcloud[:, -1]).size == 1 # Load label. segment_f = f.with_suffix('.0.010000.segs.json') segment_group_f = ( f.parent / f.name[:-len(POINTCLOUD_FILE)]).with_suffix('.aggregation.json') semantic_f = f.parent / (f.stem + '.labels' + f.suffix) if semantic_f.is_file(): # Load semantic label. semantic_label = read_plyfile(semantic_f) # Sanity check that the pointcloud and its label has same vertices. assert num_points == semantic_label.shape[0]