def main(): args = parse_args() mmcv.check_file_exist(args.prediction_path) cfg = Config.fromfile(args.config) # update data root according to MMDET_DATASETS update_data_root(cfg) if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) cfg.data.test.test_mode = True cfg.data.test.pop('samples_per_gpu', 0) cfg.data.test.pipeline = get_loading_pipeline(cfg.data.train.pipeline) dataset = build_dataset(cfg.data.test) outputs = mmcv.load(args.prediction_path) result_visualizer = ResultVisualizer(args.show, args.wait_time, args.show_score_thr) result_visualizer.evaluate_and_show(dataset, outputs, topk=args.topk, show_dir=args.show_dir)
def _load_semantic_seg_3d(self, results): """Private function to load 3D semantic segmentation annotations. Args: results (dict): Result dict from :obj:`mmdet3d.CustomDataset`. Returns: dict: The dict containing the semantic segmentation annotations. """ pts_semantic_mask_path = results['ann_info']['pts_semantic_mask_path'] if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) try: mask_bytes = self.file_client.get(pts_semantic_mask_path) # add .copy() to fix read-only bug pts_semantic_mask = np.frombuffer(mask_bytes, dtype=self.seg_3d_dtype).copy() except ConnectionError: mmcv.check_file_exist(pts_semantic_mask_path) pts_semantic_mask = np.fromfile(pts_semantic_mask_path, dtype=np.long) results['pts_semantic_mask'] = pts_semantic_mask results['pts_seg_fields'].append('pts_semantic_mask') return results
def evaluate(self, runner, results): tmp_file = osp.join(runner.work_dir, 'temp_0') result_files = results2json(self.dataset, results, tmp_file) if result_files is None: print('Nothing to evaluate.') return for res_type in self.iou_type: if res_type not in ['bbox', 'segm']: raise KeyError( 'invalid iou_type: {} for evaluation'.format(res_type)) if res_type == 'segm': assert runner.model.module.with_mask try: mmcv.check_file_exist(result_files[res_type]) except IndexError: print('No prediction found.') break lvis_eval = LVISEval( self.dataset.lvis, result_files[res_type], iou_type=res_type) lvis_eval.run() lvis_eval.print_results() eval_results = lvis_eval.get_results() metrics = eval_results.keys() for i, metric in enumerate(metrics): if metric.find('AP') == -1: continue key = 'AP_{}/{}'.format(res_type, metric) val = float('{:.3f}'.format(eval_results[metric])) runner.log_buffer.output[key] = val runner.log_buffer.ready = True for res_type in self.iou_type: os.remove(result_files[res_type])
def main(): args = parse_args() mmcv.check_file_exist(args.prediction_path) cfg = Config.fromfile(args.config) if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) cfg.data.test.test_mode = True # import modules from string list. if cfg.get('custom_imports', None): from mmcv.utils import import_modules_from_strings import_modules_from_strings(**cfg['custom_imports']) cfg.data.test.pop('samples_per_gpu', 0) cfg.data.test.pipeline = get_loading_pipeline(cfg.data.train.pipeline) dataset = build_dataset(cfg.data.test) outputs = mmcv.load(args.prediction_path) result_visualizer = ResultVisualizer(args.show, args.wait_time, args.show_score_thr) result_visualizer.evaluate_and_show(dataset, outputs, topk=args.topk, show_dir=args.show_dir)
def test_seg_format_results(): import mmcv from os import path as osp root_path = './tests/data/scannet' ann_file = './tests/data/scannet/scannet_infos.pkl' scannet_dataset = ScanNetSegDataset(data_root=root_path, ann_file=ann_file, test_mode=True) results = [] pred_sem_mask = dict(semantic_mask=torch.tensor([ 13, 5, 1, 2, 6, 2, 13, 1, 14, 2, 0, 0, 5, 5, 3, 0, 1, 14, 0, 0, 0, 18, 6, 15, 13, 0, 2, 4, 0, 3, 16, 6, 13, 5, 13, 0, 0, 0, 0, 1, 7, 3, 19, 12, 8, 0, 11, 0, 0, 1, 2, 13, 17, 1, 1, 1, 6, 2, 13, 19, 4, 17, 0, 14, 1, 7, 2, 1, 7, 2, 0, 5, 17, 5, 0, 0, 3, 6, 5, 11, 1, 13, 13, 2, 3, 1, 0, 13, 19, 1, 14, 5, 3, 1, 13, 1, 2, 3, 2, 1 ]).long()) results.append(pred_sem_mask) result_files, tmp_dir = scannet_dataset.format_results(results) expected_label = np.array([ 16, 6, 2, 3, 7, 3, 16, 2, 24, 3, 1, 1, 6, 6, 4, 1, 2, 24, 1, 1, 1, 36, 7, 28, 16, 1, 3, 5, 1, 4, 33, 7, 16, 6, 16, 1, 1, 1, 1, 2, 8, 4, 39, 14, 9, 1, 12, 1, 1, 2, 3, 16, 34, 2, 2, 2, 7, 3, 16, 39, 5, 34, 1, 24, 2, 8, 3, 2, 8, 3, 1, 6, 34, 6, 1, 1, 4, 7, 6, 12, 2, 16, 16, 3, 4, 2, 1, 16, 39, 2, 24, 6, 4, 2, 16, 2, 3, 4, 3, 2 ]) expected_txt_path = osp.join(tmp_dir.name, 'results', 'scene0000_00.txt') assert np.all(result_files[0]['seg_mask'] == expected_label) mmcv.check_file_exist(expected_txt_path)
def main(): parser = ArgumentParser(description='LVIS Evaluation') parser.add_argument('config', help='config') parser.add_argument('result', help='result file path') parser.add_argument( '--types', type=str, nargs='+', choices=['proposal_fast', 'proposal', 'bbox', 'segm', 'keypoint'], default=['segm'], help='result types') parser.add_argument( '--max_dets', type=int, default=100, help='proposal numbers, only used for recall evaluation') args = parser.parse_args() cfg = mmcv.Config.fromfile(args.config) dataset = build_dataset(cfg.data.test) print('loading result file: {}'.format(args.result)) result_files = dict() for eval_type in args.types: result_json_path = args.result + '.{}.json'.format(eval_type) mmcv.check_file_exist(result_json_path) result_files[eval_type] = result_json_path lvis_eval(result_files, args.types, dataset.lvis, args.max_dets)
def main(): args = parse_args() mmcv.check_file_exist(args.prediction_path) cfg = Config.fromfile(args.config) # replace the ${key} with the value of cfg.key cfg = replace_cfg_vals(cfg) # update data root according to MMDET_DATASETS update_data_root(cfg) if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) cfg.data.test.test_mode = True cfg.data.test.pop('samples_per_gpu', 0) if cfg.data.train.type in ('MultiImageMixDataset', 'ClassBalancedDataset', 'RepeatDataset', 'ConcatDataset'): cfg.data.test.pipeline = get_loading_pipeline( cfg.data.train.dataset.pipeline) else: cfg.data.test.pipeline = get_loading_pipeline(cfg.data.train.pipeline) dataset = build_dataset(cfg.data.test) outputs = mmcv.load(args.prediction_path) result_visualizer = ResultVisualizer(args.show, args.wait_time, args.show_score_thr, args.overlay_gt_pred) result_visualizer.evaluate_and_show(dataset, outputs, topk=args.topk, show_dir=args.show_dir)
def _load_points(self, pts_filename): if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) try: pts_bytes = self.file_client.get(pts_filename) points = np.frombuffer(pts_bytes, dtype=np.float32) except ConnectionError: mmcv.check_file_exist(pts_filename) if pts_filename.endswith('.npy'): points = np.load(pts_filename) else: points = np.fromfile(pts_filename, dtype=np.float32) return points
def lvis_eval(result_files, result_types, lvis, max_dets=100): if result_files is None: print('Nothing to evaluate.') return for res_type in result_types: if res_type not in ['bbox', 'segm']: raise KeyError( 'invalid iou_type: {} for evaluation'.format(res_type)) try: mmcv.check_file_exist(result_files[res_type]) except IndexError: print('No prediction found.') break lvis_eval = LVISEval(lvis, result_files[res_type], iou_type=res_type) lvis_eval.params.max_dets = max_dets lvis_eval.run() lvis_eval.print_results() # Compute per-category AP # from https://github.com/facebookresearch/detectron2/blob/03064eb5bafe4a3e5750cc7a16672daf5afe8435/detectron2/evaluation/coco_evaluation.py#L259-L283 # noqa if False: precisions = lvis_eval.eval['precision'] catIds = lvis.get_cat_ids() # precision has dims (iou, recall, cls, area range, max dets) assert len(catIds) == precisions.shape[2] results_per_category = [] for idx, catId in enumerate(catIds): # area range index 0: all area ranges # max dets index -1: typically 100 per image nm = lvis.load_cats([catId])[0] precision = precisions[:, :, idx, :] precision = precision[precision > -1] ap = np.mean(precision) if precision.size else float('nan') results_per_category.append( ('{}'.format(nm['name']), '{:0.3f}'.format(float(ap * 100)))) N_COLS = min(6, len(results_per_category) * 2) results_flatten = list(itertools.chain(*results_per_category)) headers = ['category', 'AP'] * (N_COLS // 2) results_2d = itertools.zip_longest( *[results_flatten[i::N_COLS] for i in range(N_COLS)]) table_data = [headers] table_data += [result for result in results_2d] table = AsciiTable(table_data) print(table.table) mmcv.dump(results_per_category, 'interp_classwise_ap.pkl')
def _load_points(self, pts_filename): """Private function to load point clouds data. Args: pts_filename (str): Filename of point clouds data. Returns: np.ndarray: An array containing point clouds data. """ if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) try: pts_bytes = self.file_client.get(pts_filename) points = np.frombuffer(pts_bytes, dtype=np.float32) except ConnectionError: mmcv.check_file_exist(pts_filename) if pts_filename.endswith('.npy'): points = np.load(pts_filename) else: points = np.fromfile(pts_filename, dtype=np.float32) return points
def test_show(): import mmcv import tempfile from os import path as osp from mmdet3d.core.bbox import LiDARInstance3DBoxes temp_dir = tempfile.mkdtemp() data_root, ann_file, classes, pts_prefix,\ pipeline, modality, split = _generate_kitti_dataset_config() kitti_dataset = KittiDataset(data_root, ann_file, split=split, modality=modality, pipeline=pipeline) boxes_3d = LiDARInstance3DBoxes( torch.tensor( [[46.1218, -4.6496, -0.9275, 0.5316, 1.4442, 1.7450, 1.1749], [33.3189, 0.1981, 0.3136, 0.5656, 1.2301, 1.7985, 1.5723], [46.1366, -4.6404, -0.9510, 0.5162, 1.6501, 1.7540, 1.3778], [33.2646, 0.2297, 0.3446, 0.5746, 1.3365, 1.7947, 1.5430], [58.9079, 16.6272, -1.5829, 1.5656, 3.9313, 1.4899, 1.5505]])) scores_3d = torch.tensor([0.1815, 0.1663, 0.5792, 0.2194, 0.2780]) labels_3d = torch.tensor([0, 0, 1, 1, 2]) result = dict(boxes_3d=boxes_3d, scores_3d=scores_3d, labels_3d=labels_3d) results = [result] kitti_dataset.show(results, temp_dir) pts_file_path = osp.join(temp_dir, '000000', '000000_points.obj') gt_file_path = osp.join(temp_dir, '000000', '000000_gt.ply') pred_file_path = osp.join(temp_dir, '000000', '000000_pred.ply') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path)
def test_seg_show(): import mmcv import tempfile from os import path as osp tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name root_path = './tests/data/s3dis' ann_file = './tests/data/s3dis/s3dis_infos.pkl' s3dis_dataset = S3DISSegDataset(data_root=root_path, ann_files=ann_file, scene_idxs=[0]) result = dict(semantic_mask=torch.tensor([ 2, 2, 1, 2, 2, 5, 1, 0, 1, 1, 9, 12, 3, 0, 2, 0, 2, 0, 8, 2, 0, 2, 0, 2, 1, 7, 2, 10, 2, 0, 0, 0, 2, 2, 2, 2, 2, 1, 2, 2, 0, 0, 4, 6, 7, 2, 1, 2, 0, 1, 7, 0, 2, 2, 2, 0, 2, 2, 1, 12, 0, 2, 2, 2, 2, 7, 2, 2, 0, 2, 6, 2, 12, 6, 2, 12, 2, 1, 6, 1, 2, 6, 8, 2, 10, 1, 10, 0, 6, 9, 4, 3, 0, 0, 12, 1, 1, 5, 2, 2 ]).long()) results = [result] s3dis_dataset.show(results, temp_dir, show=False) pts_file_path = osp.join(temp_dir, 'Area_1_office_2', 'Area_1_office_2_points.obj') gt_file_path = osp.join(temp_dir, 'Area_1_office_2', 'Area_1_office_2_gt.obj') pred_file_path = osp.join(temp_dir, 'Area_1_office_2', 'Area_1_office_2_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup()
def test_show(): import mmcv import tempfile from os import path as osp from mmdet3d.core.bbox import LiDARInstance3DBoxes temp_dir = tempfile.mkdtemp() data_root = 'tests/data/kitti' ann_file = 'tests/data/kitti/kitti_infos_train.pkl' modality = {'use_lidar': True, 'use_camera': False} split = 'training' file_client_args = dict(backend='disk') point_cloud_range = [0, -40, -3, 70.4, 40, 1] class_names = ['Pedestrian', 'Cyclist', 'Car'] pipeline = [ dict(type='LoadPointsFromFile', load_dim=4, use_dim=4, file_client_args=file_client_args), dict(type='MultiScaleFlipAug3D', img_scale=(1333, 800), pts_scale_ratio=1, flip=False, transforms=[ dict(type='GlobalRotScaleTrans', rot_range=[0, 0], scale_ratio_range=[1., 1.], translation_std=[0, 0, 0]), dict(type='RandomFlip3D'), dict(type='PointsRangeFilter', point_cloud_range=point_cloud_range), dict(type='DefaultFormatBundle3D', class_names=class_names, with_label=False), dict(type='Collect3D', keys=['points']) ]) ] kitti_dataset = KittiDataset(data_root, ann_file, split=split, modality=modality, pipeline=pipeline) boxes_3d = LiDARInstance3DBoxes( torch.tensor( [[46.1218, -4.6496, -0.9275, 0.5316, 1.4442, 1.7450, 1.1749], [33.3189, 0.1981, 0.3136, 0.5656, 1.2301, 1.7985, 1.5723], [46.1366, -4.6404, -0.9510, 0.5162, 1.6501, 1.7540, 1.3778], [33.2646, 0.2297, 0.3446, 0.5746, 1.3365, 1.7947, 1.5430], [58.9079, 16.6272, -1.5829, 1.5656, 3.9313, 1.4899, 1.5505]])) scores_3d = torch.tensor([0.1815, 0.1663, 0.5792, 0.2194, 0.2780]) labels_3d = torch.tensor([0, 0, 1, 1, 2]) result = dict(boxes_3d=boxes_3d, scores_3d=scores_3d, labels_3d=labels_3d) results = [result] kitti_dataset.show(results, temp_dir) pts_file_path = osp.join(temp_dir, '000000', '000000_points.obj') gt_file_path = osp.join(temp_dir, '000000', '000000_gt.ply') pred_file_path = osp.join(temp_dir, '000000', '000000_pred.ply') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path)
def test_show(): root_path = 'tests/data/nuscenes/' ann_file = 'tests/data/nuscenes/nus_infos_mono3d.coco.json' class_names = [ 'car', 'truck', 'trailer', 'bus', 'construction_vehicle', 'bicycle', 'motorcycle', 'pedestrian', 'traffic_cone', 'barrier' ] eval_pipeline = [ dict(type='LoadImageFromFileMono3D'), dict(type='DefaultFormatBundle3D', class_names=class_names, with_label=False), dict(type='Collect3D', keys=['img']) ] nus_dataset = NuScenesMonoDataset(data_root=root_path, ann_file=ann_file, img_prefix='tests/data/nuscenes/', test_mode=True, pipeline=eval_pipeline) results = mmcv.load('tests/data/nuscenes/mono3d_sample_results.pkl') results = [results[0]] # show with eval_pipeline tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name nus_dataset.show(results, temp_dir, show=False) file_name = 'n015-2018-07-18-11-07-57+0800__' \ 'CAM_BACK_LEFT__1531883530447423' img_file_path = osp.join(temp_dir, file_name, f'{file_name}_img.png') gt_file_path = osp.join(temp_dir, file_name, f'{file_name}_gt.png') pred_file_path = osp.join(temp_dir, file_name, f'{file_name}_pred.png') mmcv.check_file_exist(img_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup()
def test_show(): import mmcv import tempfile from os import path as osp from mmdet3d.core.bbox import DepthInstance3DBoxes temp_dir = tempfile.mkdtemp() root_path = './tests/data/sunrgbd' ann_file = './tests/data/sunrgbd/sunrgbd_infos.pkl' sunrgbd_dataset = SUNRGBDDataset(root_path, ann_file) boxes_3d = DepthInstance3DBoxes( torch.tensor( [[1.1500, 4.2614, -1.0669, 1.3219, 2.1593, 1.0267, 1.6473], [-0.9583, 2.1916, -1.0881, 0.6213, 1.3022, 1.6275, -3.0720], [2.5697, 4.8152, -1.1157, 0.5421, 0.7019, 0.7896, 1.6712], [0.7283, 2.5448, -1.0356, 0.7691, 0.9056, 0.5771, 1.7121], [-0.9860, 3.2413, -1.2349, 0.5110, 0.9940, 1.1245, 0.3295]])) scores_3d = torch.tensor( [1.5280e-01, 1.6682e-03, 6.2811e-04, 1.2860e-03, 9.4229e-06]) labels_3d = torch.tensor([0, 0, 0, 0, 0]) result = dict(boxes_3d=boxes_3d, scores_3d=scores_3d, labels_3d=labels_3d) results = [result] sunrgbd_dataset.show(results, temp_dir, show=False) pts_file_path = osp.join(temp_dir, '000001', '000001_points.obj') gt_file_path = osp.join(temp_dir, '000001', '000001_gt.ply') pred_file_path = osp.join(temp_dir, '000001', '000001_pred.ply') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path)
def test_seg_show(): import mmcv import tempfile from os import path as osp tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name root_path = './tests/data/scannet' ann_file = './tests/data/scannet/scannet_infos.pkl' scannet_dataset = ScanNetSegDataset( data_root=root_path, ann_file=ann_file, scene_idxs=[0]) result = dict( semantic_mask=torch.tensor([ 13, 5, 1, 2, 6, 2, 13, 1, 14, 2, 0, 0, 5, 5, 3, 0, 1, 14, 0, 0, 0, 18, 6, 15, 13, 0, 2, 4, 0, 3, 16, 6, 13, 5, 13, 0, 0, 0, 0, 1, 7, 3, 19, 12, 8, 0, 11, 0, 0, 1, 2, 13, 17, 1, 1, 1, 6, 2, 13, 19, 4, 17, 0, 14, 1, 7, 2, 1, 7, 2, 0, 5, 17, 5, 0, 0, 3, 6, 5, 11, 1, 13, 13, 2, 3, 1, 0, 13, 19, 1, 14, 5, 3, 1, 13, 1, 2, 3, 2, 1 ]).long()) results = [result] scannet_dataset.show(results, temp_dir, show=False) pts_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_points.obj') gt_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_gt.obj') pred_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup()
def test_check_file_exist(): mmcv.check_file_exist(__file__) if sys.version_info > (3, 3): with pytest.raises(FileNotFoundError): # noqa mmcv.check_file_exist('no_such_file.txt') else: with pytest.raises(IOError): mmcv.check_file_exist('no_such_file.txt')
def _load_points(self, pts_filename): """Private function to load point clouds data. Args: pts_filename (str): Filename of point clouds data. Returns: np.ndarray: An array containing point clouds data. """ if self._file_client is None: self._file_client = mmcv.FileClient(**self._file_client_args) try: pts_bytes = self._file_client.get(pts_filename) points = np.frombuffer(pts_bytes, dtype=np.float32) except ConnectionError: mmcv.check_file_exist(pts_filename) if pts_filename.endswith(".npy"): points = np.load(pts_filename) else: points = np.fromfile(pts_filename, dtype=np.float32) points = points.reshape(-1, self._load_dim) points = points[:, self._use_dim] attribute_dims = None # TODO attributes if self.shift_height: floor_height = np.percentile(points[:, 2], 0.99) height = points[:, 2] - floor_height points = np.concatenate([points, np.expand_dims(height, 1)], 1) attribute_dims = dict(height=3) points_class = get_points_type(self._coord_type) points = points_class(points, points_dim=points.shape[-1], attribute_dims=attribute_dims) return points
def __init__(self, root_path, split='train'): self.root_dir = root_path self.split = split self.split_dir = osp.join(root_path) self.classes = [ 'cabinet', 'bed', 'chair', 'sofa', 'table', 'door', 'window', 'bookshelf', 'picture', 'counter', 'desk', 'curtain', 'refrigerator', 'showercurtrain', 'toilet', 'sink', 'bathtub', 'garbagebin' ] self.cat2label = {cat: self.classes.index(cat) for cat in self.classes} self.label2cat = {self.cat2label[t]: t for t in self.cat2label} self.cat_ids = np.array( [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36, 39]) self.cat_ids2class = { nyu40id: i for i, nyu40id in enumerate(list(self.cat_ids)) } assert split in ['train', 'val', 'test'] split_file = osp.join(self.root_dir, 'meta_data', f'scannetv2_{split}.txt') mmcv.check_file_exist(split_file) self.sample_id_list = mmcv.list_from_file(split_file)
def __init__(self, root_path, split='train', use_v1=False): self.root_dir = root_path self.split = split self.split_dir = osp.join(root_path, 'sunrgbd_trainval') self.classes = [ 'bed', 'table', 'sofa', 'chair', 'toilet', 'desk', 'dresser', 'night_stand', 'bookshelf', 'bathtub' ] self.cat2label = {cat: self.classes.index(cat) for cat in self.classes} self.label2cat = { label: self.classes[label] for label in range(len(self.classes)) } assert split in ['train', 'val', 'test'] split_file = osp.join(self.split_dir, f'{split}_data_idx.txt') mmcv.check_file_exist(split_file) self.sample_id_list = map(int, mmcv.list_from_file(split_file)) self.image_dir = osp.join(self.split_dir, 'image') self.calib_dir = osp.join(self.split_dir, 'calib') self.depth_dir = osp.join(self.split_dir, 'depth') if use_v1: self.label_dir = osp.join(self.split_dir, 'label_v1') else: self.label_dir = osp.join(self.split_dir, 'label')
def _load_masks_3d(self, results): """Private function to load 3D mask annotations. Args: results (dict): Result dict from :obj:`mmdet3d.CustomDataset`. Returns: dict: The dict containing loaded 3D mask annotations. """ pts_instance_mask_path = results['ann_info']['pts_instance_mask_path'] if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) try: mask_bytes = self.file_client.get(pts_instance_mask_path) pts_instance_mask = np.frombuffer(mask_bytes, dtype=np.int) except ConnectionError: mmcv.check_file_exist(pts_instance_mask_path) pts_instance_mask = np.fromfile(pts_instance_mask_path, dtype=np.long) results['pts_instance_mask'] = pts_instance_mask results['pts_mask_fields'].append('pts_instance_mask') return results
def test_show(): import mmcv from os import path as osp from mmdet3d.core.bbox import LiDARInstance3DBoxes tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name class_names = [ 'car', 'truck', 'trailer', 'bus', 'construction_vehicle', 'bicycle', 'motorcycle', 'pedestrian', 'traffic_cone', 'barrier' ] eval_pipeline = [ dict( type='LoadPointsFromFile', coord_type='LIDAR', load_dim=5, use_dim=5, file_client_args=dict(backend='disk')), dict( type='LoadPointsFromMultiSweeps', sweeps_num=10, file_client_args=dict(backend='disk')), dict( type='DefaultFormatBundle3D', class_names=class_names, with_label=False), dict(type='Collect3D', keys=['points']) ] nus_dataset = NuScenesDataset('tests/data/nuscenes/nus_info.pkl', None, 'tests/data/nuscenes') boxes_3d = LiDARInstance3DBoxes( torch.tensor( [[46.1218, -4.6496, -0.9275, 0.5316, 1.4442, 1.7450, 1.1749], [33.3189, 0.1981, 0.3136, 0.5656, 1.2301, 1.7985, 1.5723], [46.1366, -4.6404, -0.9510, 0.5162, 1.6501, 1.7540, 1.3778], [33.2646, 0.2297, 0.3446, 0.5746, 1.3365, 1.7947, 1.5430], [58.9079, 16.6272, -1.5829, 1.5656, 3.9313, 1.4899, 1.5505]])) scores_3d = torch.tensor([0.1815, 0.1663, 0.5792, 0.2194, 0.2780]) labels_3d = torch.tensor([0, 0, 1, 1, 2]) result = dict(boxes_3d=boxes_3d, scores_3d=scores_3d, labels_3d=labels_3d) results = [dict(pts_bbox=result)] nus_dataset.show(results, temp_dir, show=False, pipeline=eval_pipeline) file_name = 'n015-2018-08-02-17-16-37+0800__LIDAR_TOP__1533201470948018' pts_file_path = osp.join(temp_dir, file_name, f'{file_name}_points.obj') gt_file_path = osp.join(temp_dir, file_name, f'{file_name}_gt.obj') pred_file_path = osp.join(temp_dir, file_name, f'{file_name}_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup()
def test_show(): import mmcv import tempfile from os import path as osp from mmdet3d.core.bbox import DepthInstance3DBoxes tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name root_path = './tests/data/scannet' ann_file = './tests/data/scannet/scannet_infos.pkl' scannet_dataset = ScanNetDataset(root_path, ann_file) boxes_3d = DepthInstance3DBoxes( torch.tensor([[ -2.4053e+00, 9.2295e-01, 8.0661e-02, 2.4054e+00, 2.1468e+00, 8.5990e-01, 0.0000e+00 ], [ -1.9341e+00, -2.0741e+00, 3.0698e-03, 3.2206e-01, 2.5322e-01, 3.5144e-01, 0.0000e+00 ], [ -3.6908e+00, 8.0684e-03, 2.6201e-01, 4.1515e-01, 7.6489e-01, 5.3585e-01, 0.0000e+00 ], [ 2.6332e+00, 8.5143e-01, -4.9964e-03, 3.0367e-01, 1.3448e+00, 1.8329e+00, 0.0000e+00 ], [ 2.0221e-02, 2.6153e+00, 1.5109e-02, 7.3335e-01, 1.0429e+00, 1.0251e+00, 0.0000e+00 ]])) scores_3d = torch.tensor( [1.2058e-04, 2.3012e-03, 6.2324e-06, 6.6139e-06, 6.7965e-05]) labels_3d = torch.tensor([0, 0, 0, 0, 0]) result = dict(boxes_3d=boxes_3d, scores_3d=scores_3d, labels_3d=labels_3d) results = [result] scannet_dataset.show(results, temp_dir, show=False) pts_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_points.obj') gt_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_gt.obj') pred_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup()
def test_show(): import mmcv from os import path as osp from mmdet3d.core.bbox import LiDARInstance3DBoxes tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name root_path = './tests/data/lyft' ann_file = './tests/data/lyft/lyft_infos.pkl' class_names = ('car', 'truck', 'bus', 'emergency_vehicle', 'other_vehicle', 'motorcycle', 'bicycle', 'pedestrian', 'animal') eval_pipeline = [ dict(type='LoadPointsFromFile', coord_type='LIDAR', load_dim=5, use_dim=5, file_client_args=dict(backend='disk')), dict(type='LoadPointsFromMultiSweeps', sweeps_num=10, file_client_args=dict(backend='disk')), dict(type='DefaultFormatBundle3D', class_names=class_names, with_label=False), dict(type='Collect3D', keys=['points']) ] kitti_dataset = LyftDataset(ann_file, None, root_path) boxes_3d = LiDARInstance3DBoxes( torch.tensor( [[46.1218, -4.6496, -0.9275, 0.5316, 1.4442, 1.7450, 1.1749], [33.3189, 0.1981, 0.3136, 0.5656, 1.2301, 1.7985, 1.5723], [46.1366, -4.6404, -0.9510, 0.5162, 1.6501, 1.7540, 1.3778], [33.2646, 0.2297, 0.3446, 0.5746, 1.3365, 1.7947, 1.5430], [58.9079, 16.6272, -1.5829, 1.5656, 3.9313, 1.4899, 1.5505]])) scores_3d = torch.tensor([0.1815, 0.1663, 0.5792, 0.2194, 0.2780]) labels_3d = torch.tensor([0, 0, 1, 1, 2]) result = dict(boxes_3d=boxes_3d, scores_3d=scores_3d, labels_3d=labels_3d) results = [dict(pts_bbox=result)] kitti_dataset.show(results, temp_dir, show=False, pipeline=eval_pipeline) file_name = 'host-a017_lidar1_1236118886901125926' pts_file_path = osp.join(temp_dir, file_name, f'{file_name}_points.obj') gt_file_path = osp.join(temp_dir, file_name, f'{file_name}_gt.obj') pred_file_path = osp.join(temp_dir, file_name, f'{file_name}_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup()
def _fill_trainval_infos(nusc, split_names, out_dir, keyword=None, keyword_action=None, subset_name=None, location=None, test=False, max_sweeps=10): import tools.data_converter.xmuda_splits as splits pkl_dict = {} for split_name in split_names: pkl_dict[split_name] = [] for sample in mmcv.track_iter_progress(nusc.sample): curr_scene_name = nusc.get( 'scene', sample['scene_token'])['name'] # 'scene-0061' # get if the current scene is in train, val or test curr_split = None for split_name in split_names: if curr_scene_name in getattr(splits, split_name): curr_split = split_name break if curr_split is None: continue if subset_name == 'night': if curr_split == 'train': if curr_scene_name in splits.val_night: curr_split = 'val' if subset_name == 'singapore': if curr_split == 'train': if curr_scene_name in splits.val_singapore: curr_split = 'val' # filter for day/night if keyword: scene_description = nusc.get("scene", sample["scene_token"])["description"] if keyword.lower() in scene_description.lower(): if keyword_action == 'exclude': # skip sample continue else: if keyword_action == 'filter': # skip sample continue if location: scene = nusc.get("scene", sample["scene_token"]) if location not in nusc.get("log", scene['log_token'])['location']: continue lidar_token = sample['data']['LIDAR_TOP'] cam_token = sample['data']['CAM_FRONT'] sd_rec = nusc.get('sample_data', sample['data']['LIDAR_TOP']) cs_record = nusc.get('calibrated_sensor', sd_rec['calibrated_sensor_token']) pose_record = nusc.get('ego_pose', sd_rec['ego_pose_token']) lidar_path, boxes_lidar, _ = nusc.get_sample_data(lidar_token) _, boxes_cam, _ = nusc.get_sample_data(cam_token) lidarseg = nusc.get('lidarseg', lidar_token) lidarseg_path = os.path.join(nusc.dataroot, lidarseg['filename']) mmcv.check_file_exist(lidar_path) info = { 'lidar_path': lidar_path, 'lidarseg_path': lidarseg_path, 'token': sample['token'], 'sweeps': [], 'cams': dict(), 'lidar2ego_translation': cs_record['translation'], 'lidar2ego_rotation': cs_record['rotation'], 'ego2global_translation': pose_record['translation'], 'ego2global_rotation': pose_record['rotation'], 'timestamp': sample['timestamp'], } l2e_r = info['lidar2ego_rotation'] l2e_t = info['lidar2ego_translation'] e2g_r = info['ego2global_rotation'] e2g_t = info['ego2global_translation'] l2e_r_mat = Quaternion(l2e_r).rotation_matrix e2g_r_mat = Quaternion(e2g_r).rotation_matrix # obtain 6 image's information per frame camera_types = [ 'CAM_FRONT', 'CAM_FRONT_RIGHT', 'CAM_FRONT_LEFT', 'CAM_BACK', 'CAM_BACK_LEFT', 'CAM_BACK_RIGHT', ] for cam in camera_types: cam_token = sample['data'][cam] cam_path, _, cam_intrinsic = nusc.get_sample_data(cam_token) cam_info = obtain_sensor2top(nusc, cam_token, l2e_t, l2e_r_mat, e2g_t, e2g_r_mat, cam) cam_info.update(cam_intrinsic=cam_intrinsic) info['cams'].update({cam: cam_info}) # obtain sweeps for a single key-frame sd_rec = nusc.get('sample_data', sample['data']['LIDAR_TOP']) sweeps = [] while len(sweeps) < max_sweeps: if not sd_rec['prev'] == '': sweep = obtain_sensor2top(nusc, sd_rec['prev'], l2e_t, l2e_r_mat, e2g_t, e2g_r_mat, 'lidar') sweeps.append(sweep) sd_rec = nusc.get('sample_data', sd_rec['prev']) else: break info['sweeps'] = sweeps # obtain annotation if not test: # full range # annotations = [ # nusc.get('sample_annotation', token) # for token in sample['anns'] # ] # inside camera valid_box_tokens = [box.token for box in boxes_cam] boxes_lidar_filter = [ box for box in boxes_lidar if box.token in valid_box_tokens ] annotations = [ nusc.get('sample_annotation', token) for token in valid_box_tokens ] locs = np.array([b.center for b in boxes_lidar_filter]).reshape(-1, 3) dims = np.array([b.wlh for b in boxes_lidar_filter]).reshape(-1, 3) rots = np.array([ b.orientation.yaw_pitch_roll[0] for b in boxes_lidar_filter ]).reshape(-1, 1) # velocity = np.array( # [nusc.box_velocity(token)[:2] for token in sample['anns']]) velocity = np.array( [nusc.box_velocity(token)[:2] for token in valid_box_tokens]) valid_flag = np.array( [(anno['num_lidar_pts'] + anno['num_radar_pts']) > 0 for anno in annotations], dtype=bool).reshape(-1) # convert velo from global to lidar for i in range(len(boxes_lidar_filter)): velo = np.array([*velocity[i], 0.0]) velo = velo @ np.linalg.inv(e2g_r_mat).T @ np.linalg.inv( l2e_r_mat).T velocity[i] = velo[:2] names = [b.name for b in boxes_lidar_filter] for i in range(len(names)): if names[i] in NuScenesDataset.NameMapping: names[i] = NuScenesDataset.NameMapping[names[i]] names = np.array(names) # we need to convert rot to SECOND format. gt_boxes = np.concatenate([locs, dims, -rots - np.pi / 2], axis=1) assert len(gt_boxes) == len( annotations), f'{len(gt_boxes)}, {len(annotations)}' info['gt_boxes'] = gt_boxes info['gt_names'] = names info['gt_velocity'] = velocity.reshape(-1, 2) info['num_lidar_pts'] = np.array( [a['num_lidar_pts'] for a in annotations]) info['num_radar_pts'] = np.array( [a['num_radar_pts'] for a in annotations]) info['valid_flag'] = valid_flag pkl_dict[curr_split].append(info) save_dir = osp.join(out_dir, 'mmda_xmuda_split') os.makedirs(save_dir, exist_ok=True) for split_name in split_names: metadata = split_name + '_' + subset_name # save_path = osp.join(save_dir, '{}{}.pkl'.format(split_name, '_' + subset_name if subset_name else '')) save_path = osp.join(save_dir, '{}.pkl'.format(metadata)) res = dict(infos=pkl_dict[split_name], metadata=metadata) mmcv.dump(res, save_path) print('Wrote preprocessed data to ' + save_path)
def test_show(): import mmcv import tempfile from os import path as osp from mmdet3d.core.bbox import DepthInstance3DBoxes tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name root_path = './tests/data/scannet' ann_file = './tests/data/scannet/scannet_infos.pkl' scannet_dataset = ScanNetDataset(root_path, ann_file) boxes_3d = DepthInstance3DBoxes( torch.tensor([[ -2.4053e+00, 9.2295e-01, 8.0661e-02, 2.4054e+00, 2.1468e+00, 8.5990e-01, 0.0000e+00 ], [ -1.9341e+00, -2.0741e+00, 3.0698e-03, 3.2206e-01, 2.5322e-01, 3.5144e-01, 0.0000e+00 ], [ -3.6908e+00, 8.0684e-03, 2.6201e-01, 4.1515e-01, 7.6489e-01, 5.3585e-01, 0.0000e+00 ], [ 2.6332e+00, 8.5143e-01, -4.9964e-03, 3.0367e-01, 1.3448e+00, 1.8329e+00, 0.0000e+00 ], [ 2.0221e-02, 2.6153e+00, 1.5109e-02, 7.3335e-01, 1.0429e+00, 1.0251e+00, 0.0000e+00 ]])) scores_3d = torch.tensor( [1.2058e-04, 2.3012e-03, 6.2324e-06, 6.6139e-06, 6.7965e-05]) labels_3d = torch.tensor([0, 0, 0, 0, 0]) result = dict(boxes_3d=boxes_3d, scores_3d=scores_3d, labels_3d=labels_3d) results = [result] scannet_dataset.show(results, temp_dir, show=False) pts_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_points.obj') gt_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_gt.obj') pred_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup() # show function with pipeline class_names = ('cabinet', 'bed', 'chair', 'sofa', 'table', 'door', 'window', 'bookshelf', 'picture', 'counter', 'desk', 'curtain', 'refrigerator', 'showercurtrain', 'toilet', 'sink', 'bathtub', 'garbagebin') eval_pipeline = [ dict(type='LoadPointsFromFile', coord_type='DEPTH', shift_height=False, load_dim=6, use_dim=[0, 1, 2]), dict(type='DefaultFormatBundle3D', class_names=class_names, with_label=False), dict(type='Collect3D', keys=['points']) ] tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name scannet_dataset.show(results, temp_dir, show=False, pipeline=eval_pipeline) pts_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_points.obj') gt_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_gt.obj') pred_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup()
def test_seg_show(): import mmcv import tempfile from os import path as osp tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name root_path = './tests/data/scannet' ann_file = './tests/data/scannet/scannet_infos.pkl' scannet_dataset = ScanNetSegDataset(data_root=root_path, ann_file=ann_file, scene_idxs=[0]) result = dict(semantic_mask=torch.tensor([ 13, 5, 1, 2, 6, 2, 13, 1, 14, 2, 0, 0, 5, 5, 3, 0, 1, 14, 0, 0, 0, 18, 6, 15, 13, 0, 2, 4, 0, 3, 16, 6, 13, 5, 13, 0, 0, 0, 0, 1, 7, 3, 19, 12, 8, 0, 11, 0, 0, 1, 2, 13, 17, 1, 1, 1, 6, 2, 13, 19, 4, 17, 0, 14, 1, 7, 2, 1, 7, 2, 0, 5, 17, 5, 0, 0, 3, 6, 5, 11, 1, 13, 13, 2, 3, 1, 0, 13, 19, 1, 14, 5, 3, 1, 13, 1, 2, 3, 2, 1 ]).long()) results = [result] scannet_dataset.show(results, temp_dir, show=False) pts_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_points.obj') gt_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_gt.obj') pred_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup() # test show with pipeline tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name class_names = ('wall', 'floor', 'cabinet', 'bed', 'chair', 'sofa', 'table', 'door', 'window', 'bookshelf', 'picture', 'counter', 'desk', 'curtain', 'refrigerator', 'showercurtrain', 'toilet', 'sink', 'bathtub', 'otherfurniture') eval_pipeline = [ dict(type='LoadPointsFromFile', coord_type='DEPTH', shift_height=False, use_color=True, load_dim=6, use_dim=[0, 1, 2, 3, 4, 5]), dict(type='LoadAnnotations3D', with_bbox_3d=False, with_label_3d=False, with_mask_3d=False, with_seg_3d=True), dict(type='PointSegClassMapping', valid_cat_ids=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36, 39)), dict(type='DefaultFormatBundle3D', class_names=class_names), dict(type='Collect3D', keys=['points', 'pts_semantic_mask']) ] scannet_dataset.show(results, temp_dir, show=False, pipeline=eval_pipeline) pts_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_points.obj') gt_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_gt.obj') pred_file_path = osp.join(temp_dir, 'scene0000_00', 'scene0000_00_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup()
def _fill_trainval_infos(nusc, train_scenes, val_scenes, test=False, max_sweeps=10): """Generate the train/val infos from the raw data. Args: nusc (:obj:`NuScenes`): Dataset class in the nuScenes dataset. train_scenes (list[str]): Basic information of training scenes. val_scenes (list[str]): Basic information of validation scenes. test (bool): Whether use the test mode. In the test mode, no annotations can be accessed. Default: False. max_sweeps (int): Max number of sweeps. Default: 10. Returns: tuple[list[dict]]: Information of training set and validation set that will be saved to the info file. """ train_nusc_infos = [] val_nusc_infos = [] for sample in mmcv.track_iter_progress(nusc.sample): lidar_token = sample['data']['LIDAR_TOP'] cam_token = sample['data']['CAM_FRONT'] sd_rec = nusc.get('sample_data', sample['data']['LIDAR_TOP']) cs_record = nusc.get('calibrated_sensor', sd_rec['calibrated_sensor_token']) pose_record = nusc.get('ego_pose', sd_rec['ego_pose_token']) lidar_path, boxes_lidar, _ = nusc.get_sample_data(lidar_token) _, boxes_cam, _ = nusc.get_sample_data(cam_token) lidarseg = nusc.get('lidarseg', lidar_token) lidarseg_path = os.path.join(nusc.dataroot, lidarseg['filename']) mmcv.check_file_exist(lidar_path) info = { 'lidar_path': lidar_path, 'lidarseg_path': lidarseg_path, 'token': sample['token'], 'sweeps': [], 'cams': dict(), 'lidar2ego_translation': cs_record['translation'], 'lidar2ego_rotation': cs_record['rotation'], 'ego2global_translation': pose_record['translation'], 'ego2global_rotation': pose_record['rotation'], 'timestamp': sample['timestamp'], } l2e_r = info['lidar2ego_rotation'] l2e_t = info['lidar2ego_translation'] e2g_r = info['ego2global_rotation'] e2g_t = info['ego2global_translation'] l2e_r_mat = Quaternion(l2e_r).rotation_matrix e2g_r_mat = Quaternion(e2g_r).rotation_matrix # obtain 6 image's information per frame camera_types = [ 'CAM_FRONT', 'CAM_FRONT_RIGHT', 'CAM_FRONT_LEFT', 'CAM_BACK', 'CAM_BACK_LEFT', 'CAM_BACK_RIGHT', ] for cam in camera_types: cam_token = sample['data'][cam] cam_path, _, cam_intrinsic = nusc.get_sample_data(cam_token) cam_info = obtain_sensor2top(nusc, cam_token, l2e_t, l2e_r_mat, e2g_t, e2g_r_mat, cam) cam_info.update(cam_intrinsic=cam_intrinsic) info['cams'].update({cam: cam_info}) # obtain sweeps for a single key-frame sd_rec = nusc.get('sample_data', sample['data']['LIDAR_TOP']) sweeps = [] while len(sweeps) < max_sweeps: if not sd_rec['prev'] == '': sweep = obtain_sensor2top(nusc, sd_rec['prev'], l2e_t, l2e_r_mat, e2g_t, e2g_r_mat, 'lidar') sweeps.append(sweep) sd_rec = nusc.get('sample_data', sd_rec['prev']) else: break info['sweeps'] = sweeps # obtain annotation if not test: # full range # annotations = [ # nusc.get('sample_annotation', token) # for token in sample['anns'] # ] # inside camera valid_box_tokens = [box.token for box in boxes_cam] boxes_lidar_filter = [ box for box in boxes_lidar if box.token in valid_box_tokens ] annotations = [ nusc.get('sample_annotation', token) for token in valid_box_tokens ] locs = np.array([b.center for b in boxes_lidar_filter]).reshape(-1, 3) dims = np.array([b.wlh for b in boxes_lidar_filter]).reshape(-1, 3) rots = np.array([ b.orientation.yaw_pitch_roll[0] for b in boxes_lidar_filter ]).reshape(-1, 1) # velocity = np.array( # [nusc.box_velocity(token)[:2] for token in sample['anns']]) velocity = np.array( [nusc.box_velocity(token)[:2] for token in valid_box_tokens]) valid_flag = np.array( [(anno['num_lidar_pts'] + anno['num_radar_pts']) > 0 for anno in annotations], dtype=bool).reshape(-1) # convert velo from global to lidar for i in range(len(boxes_lidar_filter)): velo = np.array([*velocity[i], 0.0]) velo = velo @ np.linalg.inv(e2g_r_mat).T @ np.linalg.inv( l2e_r_mat).T velocity[i] = velo[:2] names = [b.name for b in boxes_lidar_filter] for i in range(len(names)): if names[i] in NuScenesDataset.NameMapping: names[i] = NuScenesDataset.NameMapping[names[i]] names = np.array(names) # we need to convert rot to SECOND format. gt_boxes = np.concatenate([locs, dims, -rots - np.pi / 2], axis=1) assert len(gt_boxes) == len( annotations), f'{len(gt_boxes)}, {len(annotations)}' info['gt_boxes'] = gt_boxes info['gt_names'] = names info['gt_velocity'] = velocity.reshape(-1, 2) info['num_lidar_pts'] = np.array( [a['num_lidar_pts'] for a in annotations]) info['num_radar_pts'] = np.array( [a['num_radar_pts'] for a in annotations]) info['valid_flag'] = valid_flag if sample['scene_token'] in train_scenes: train_nusc_infos.append(info) else: val_nusc_infos.append(info) return train_nusc_infos, val_nusc_infos
def test_check_file_exist(): mmcv.check_file_exist(__file__) with pytest.raises(FileNotFoundError): mmcv.check_file_exist('no_such_file.txt')
def test_show(): import mmcv import tempfile from os import path as osp from mmdet3d.core.bbox import DepthInstance3DBoxes tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name root_path, ann_file, class_names, pipelines, modality = \ _generate_sunrgbd_dataset_config() sunrgbd_dataset = SUNRGBDDataset(root_path, ann_file, pipelines, modality=modality) boxes_3d = DepthInstance3DBoxes( torch.tensor( [[1.1500, 4.2614, -1.0669, 1.3219, 2.1593, 1.0267, 1.6473], [-0.9583, 2.1916, -1.0881, 0.6213, 1.3022, 1.6275, -3.0720], [2.5697, 4.8152, -1.1157, 0.5421, 0.7019, 0.7896, 1.6712], [0.7283, 2.5448, -1.0356, 0.7691, 0.9056, 0.5771, 1.7121], [-0.9860, 3.2413, -1.2349, 0.5110, 0.9940, 1.1245, 0.3295]])) scores_3d = torch.tensor( [1.5280e-01, 1.6682e-03, 6.2811e-04, 1.2860e-03, 9.4229e-06]) labels_3d = torch.tensor([0, 0, 0, 0, 0]) result = dict(boxes_3d=boxes_3d, scores_3d=scores_3d, labels_3d=labels_3d) results = [result] sunrgbd_dataset.show(results, temp_dir, show=False) pts_file_path = osp.join(temp_dir, '000001', '000001_points.obj') gt_file_path = osp.join(temp_dir, '000001', '000001_gt.obj') pred_file_path = osp.join(temp_dir, '000001', '000001_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup() # test show with pipeline eval_pipeline = [ dict(type='LoadPointsFromFile', coord_type='DEPTH', shift_height=True, load_dim=6, use_dim=[0, 1, 2]), dict(type='DefaultFormatBundle3D', class_names=class_names, with_label=False), dict(type='Collect3D', keys=['points']) ] tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name sunrgbd_dataset.show(results, temp_dir, show=False, pipeline=eval_pipeline) pts_file_path = osp.join(temp_dir, '000001', '000001_points.obj') gt_file_path = osp.join(temp_dir, '000001', '000001_gt.obj') pred_file_path = osp.join(temp_dir, '000001', '000001_pred.obj') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) tmp_dir.cleanup() # test multi-modality show tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name root_path, ann_file, class_names, multi_modality_pipelines, modality = \ _generate_sunrgbd_multi_modality_dataset_config() sunrgbd_dataset = SUNRGBDDataset(root_path, ann_file, multi_modality_pipelines, modality=modality) sunrgbd_dataset.show(results, temp_dir, False, multi_modality_pipelines) pts_file_path = osp.join(temp_dir, '000001', '000001_points.obj') gt_file_path = osp.join(temp_dir, '000001', '000001_gt.obj') pred_file_path = osp.join(temp_dir, '000001', '000001_pred.obj') img_file_path = osp.join(temp_dir, '000001', '000001_img.png') img_pred_path = osp.join(temp_dir, '000001', '000001_pred.png') img_gt_file = osp.join(temp_dir, '000001', '000001_gt.png') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) mmcv.check_file_exist(img_file_path) mmcv.check_file_exist(img_pred_path) mmcv.check_file_exist(img_gt_file) tmp_dir.cleanup() # test multi-modality show with pipeline eval_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadPointsFromFile', coord_type='DEPTH', shift_height=True, load_dim=6, use_dim=[0, 1, 2]), dict(type='DefaultFormatBundle3D', class_names=class_names, with_label=False), dict(type='Collect3D', keys=['points', 'img']) ] tmp_dir = tempfile.TemporaryDirectory() temp_dir = tmp_dir.name sunrgbd_dataset.show(results, temp_dir, show=False, pipeline=eval_pipeline) pts_file_path = osp.join(temp_dir, '000001', '000001_points.obj') gt_file_path = osp.join(temp_dir, '000001', '000001_gt.obj') pred_file_path = osp.join(temp_dir, '000001', '000001_pred.obj') img_file_path = osp.join(temp_dir, '000001', '000001_img.png') img_pred_path = osp.join(temp_dir, '000001', '000001_pred.png') img_gt_file = osp.join(temp_dir, '000001', '000001_gt.png') mmcv.check_file_exist(pts_file_path) mmcv.check_file_exist(gt_file_path) mmcv.check_file_exist(pred_file_path) mmcv.check_file_exist(img_file_path) mmcv.check_file_exist(img_pred_path) mmcv.check_file_exist(img_gt_file) tmp_dir.cleanup()