def test_object_noise(): np.random.seed(0) object_noise = ObjectNoise() points = np.fromfile( './tests/data/kitti/training/velodyne_reduced/000000.bin', np.float32).reshape(-1, 4) annos = mmcv.load('./tests/data/kitti/kitti_infos_train.pkl') info = annos[0] rect = info['calib']['R0_rect'].astype(np.float32) Trv2c = info['calib']['Tr_velo_to_cam'].astype(np.float32) annos = info['annos'] loc = annos['location'] dims = annos['dimensions'] rots = annos['rotation_y'] gt_bboxes_3d = np.concatenate([loc, dims, rots[..., np.newaxis]], axis=1).astype(np.float32) gt_bboxes_3d = CameraInstance3DBoxes(gt_bboxes_3d).convert_to( Box3DMode.LIDAR, np.linalg.inv(rect @ Trv2c)) points = LiDARPoints(points, points_dim=4) input_dict = dict(points=points, gt_bboxes_3d=gt_bboxes_3d) input_dict = object_noise(input_dict) points = input_dict['points'] gt_bboxes_3d = input_dict['gt_bboxes_3d'].tensor expected_gt_bboxes_3d = torch.tensor( [[9.1724, -1.7559, -1.3550, 0.4800, 1.2000, 1.8900, 0.0505]]) repr_str = repr(object_noise) expected_repr_str = 'ObjectNoise(num_try=100, ' \ 'translation_std=[0.25, 0.25, 0.25], ' \ 'global_rot_range=[0.0, 0.0], ' \ 'rot_range=[-0.15707963267, 0.15707963267])' assert repr_str == expected_repr_str assert points.tensor.numpy().shape == (800, 4) assert torch.allclose(gt_bboxes_3d, expected_gt_bboxes_3d, 1e-3)
def test_object_sample(): db_sampler = mmcv.ConfigDict({ 'data_root': './tests/data/kitti/', 'info_path': './tests/data/kitti/kitti_dbinfos_train.pkl', 'rate': 1.0, 'prepare': { 'filter_by_difficulty': [-1], 'filter_by_min_points': { 'Pedestrian': 10 } }, 'classes': ['Pedestrian', 'Cyclist', 'Car'], 'sample_groups': { 'Pedestrian': 6 } }) object_sample = ObjectSample(db_sampler) points = np.fromfile( './tests/data/kitti/training/velodyne_reduced/000000.bin', np.float32).reshape(-1, 4) annos = mmcv.load('./tests/data/kitti/kitti_infos_train.pkl') info = annos[0] rect = info['calib']['R0_rect'].astype(np.float32) Trv2c = info['calib']['Tr_velo_to_cam'].astype(np.float32) annos = info['annos'] loc = annos['location'] dims = annos['dimensions'] rots = annos['rotation_y'] gt_names = annos['name'] gt_bboxes_3d = np.concatenate([loc, dims, rots[..., np.newaxis]], axis=1).astype(np.float32) gt_bboxes_3d = CameraInstance3DBoxes(gt_bboxes_3d).convert_to( Box3DMode.LIDAR, np.linalg.inv(rect @ Trv2c)) CLASSES = ('car', 'pedestrian', 'cyclist') gt_labels = [] for cat in gt_names: if cat in CLASSES: gt_labels.append(CLASSES.index(cat)) else: gt_labels.append(-1) input_dict = dict( points=points, gt_bboxes_3d=gt_bboxes_3d, gt_labels_3d=gt_labels) input_dict = object_sample(input_dict) points = input_dict['points'] gt_bboxes_3d = input_dict['gt_bboxes_3d'] gt_labels_3d = input_dict['gt_labels_3d'] expected_gt_bboxes_3d = torch.tensor( [[8.7314, -1.8559, -1.5997, 0.4800, 1.2000, 1.8900, 0.0100], [8.7314, -1.8559, -1.5997, 0.4800, 1.2000, 1.8900, 0.0100]]) expected_gt_labels_3d = np.array([-1, 0]) repr_str = repr(object_sample) expected_repr_str = 'ObjectSample' assert repr_str == expected_repr_str assert points.shape == (1177, 4) assert torch.allclose(gt_bboxes_3d.tensor, expected_gt_bboxes_3d) assert np.all(gt_labels_3d == expected_gt_labels_3d)
def test_background_points_filter(): np.random.seed(0) background_points_filter = BackgroundPointsFilter((0.5, 2.0, 0.5)) points = np.fromfile( './tests/data/kitti/training/velodyne_reduced/000000.bin', np.float32).reshape(-1, 4) orig_points = points.copy() annos = mmcv.load('./tests/data/kitti/kitti_infos_train.pkl') info = annos[0] rect = info['calib']['R0_rect'].astype(np.float32) Trv2c = info['calib']['Tr_velo_to_cam'].astype(np.float32) annos = info['annos'] loc = annos['location'] dims = annos['dimensions'] rots = annos['rotation_y'] gt_bboxes_3d = np.concatenate([loc, dims, rots[..., np.newaxis]], axis=1).astype(np.float32) gt_bboxes_3d = CameraInstance3DBoxes(gt_bboxes_3d).convert_to( Box3DMode.LIDAR, np.linalg.inv(rect @ Trv2c)) extra_points = gt_bboxes_3d.corners.reshape(8, 3)[[1, 2, 5, 6], :] extra_points[:, 2] += 0.1 extra_points = torch.cat([extra_points, extra_points.new_zeros(4, 1)], 1) points = np.concatenate([points, extra_points.numpy()], 0) points = LiDARPoints(points, points_dim=4) input_dict = dict(points=points, gt_bboxes_3d=gt_bboxes_3d) origin_gt_bboxes_3d = gt_bboxes_3d.clone() input_dict = background_points_filter(input_dict) points = input_dict['points'].tensor.numpy() repr_str = repr(background_points_filter) expected_repr_str = 'BackgroundPointsFilter(bbox_enlarge_range=' \ '[[0.5, 2.0, 0.5]])' assert repr_str == expected_repr_str assert points.shape == (800, 4) assert np.equal(orig_points, points).all() assert np.equal(input_dict['gt_bboxes_3d'].tensor.numpy(), origin_gt_bboxes_3d.tensor.numpy()).all() # test single float config BackgroundPointsFilter(0.5) # The length of bbox_enlarge_range should be 3 with pytest.raises(AssertionError): BackgroundPointsFilter((0.5, 2.0))
def test_object_name_filter(): class_names = ['Pedestrian'] object_name_filter = ObjectNameFilter(class_names) annos = mmcv.load('./tests/data/kitti/kitti_infos_train.pkl') info = annos[0] rect = info['calib']['R0_rect'].astype(np.float32) Trv2c = info['calib']['Tr_velo_to_cam'].astype(np.float32) annos = info['annos'] loc = annos['location'] dims = annos['dimensions'] rots = annos['rotation_y'] gt_names = annos['name'] gt_bboxes_3d = np.concatenate([loc, dims, rots[..., np.newaxis]], axis=1).astype(np.float32) gt_bboxes_3d = CameraInstance3DBoxes(gt_bboxes_3d).convert_to( Box3DMode.LIDAR, np.linalg.inv(rect @ Trv2c)) CLASSES = ('Pedestrian', 'Cyclist', 'Car') gt_labels = [] for cat in gt_names: if cat in CLASSES: gt_labels.append(CLASSES.index(cat)) else: gt_labels.append(-1) gt_labels = np.array(gt_labels, dtype=np.long) input_dict = dict(gt_bboxes_3d=gt_bboxes_3d.clone(), gt_labels_3d=gt_labels.copy()) results = object_name_filter(input_dict) bboxes_3d = results['gt_bboxes_3d'] labels_3d = results['gt_labels_3d'] keep_mask = np.array([name in class_names for name in gt_names]) assert torch.allclose(gt_bboxes_3d.tensor[keep_mask], bboxes_3d.tensor) assert np.all(gt_labels[keep_mask] == labels_3d) repr_str = repr(object_name_filter) expected_repr_str = f'ObjectNameFilter(classes={class_names})' assert repr_str == expected_repr_str
def test_object_sample(): db_sampler = mmcv.ConfigDict({ 'data_root': './tests/data/kitti/', 'info_path': './tests/data/kitti/kitti_dbinfos_train.pkl', 'rate': 1.0, 'prepare': { 'filter_by_difficulty': [-1], 'filter_by_min_points': { 'Pedestrian': 10 } }, 'classes': ['Pedestrian', 'Cyclist', 'Car'], 'sample_groups': { 'Pedestrian': 6 } }) np.random.seed(0) object_sample = ObjectSample(db_sampler) points = np.fromfile( './tests/data/kitti/training/velodyne_reduced/000000.bin', np.float32).reshape(-1, 4) annos = mmcv.load('./tests/data/kitti/kitti_infos_train.pkl') info = annos[0] rect = info['calib']['R0_rect'].astype(np.float32) Trv2c = info['calib']['Tr_velo_to_cam'].astype(np.float32) annos = info['annos'] loc = annos['location'] dims = annos['dimensions'] rots = annos['rotation_y'] gt_names = annos['name'] gt_bboxes_3d = np.concatenate([loc, dims, rots[..., np.newaxis]], axis=1).astype(np.float32) gt_bboxes_3d = CameraInstance3DBoxes(gt_bboxes_3d).convert_to( Box3DMode.LIDAR, np.linalg.inv(rect @ Trv2c)) CLASSES = ('Pedestrian', 'Cyclist', 'Car') gt_labels = [] for cat in gt_names: if cat in CLASSES: gt_labels.append(CLASSES.index(cat)) else: gt_labels.append(-1) gt_labels = np.array(gt_labels, dtype=np.long) points = LiDARPoints(points, points_dim=4) input_dict = dict(points=points, gt_bboxes_3d=gt_bboxes_3d, gt_labels_3d=gt_labels) input_dict = object_sample(input_dict) points = input_dict['points'] gt_bboxes_3d = input_dict['gt_bboxes_3d'] gt_labels_3d = input_dict['gt_labels_3d'] repr_str = repr(object_sample) expected_repr_str = 'ObjectSample sample_2d=False, ' \ 'data_root=./tests/data/kitti/, ' \ 'info_path=./tests/data/kitti/kitti' \ '_dbinfos_train.pkl, rate=1.0, ' \ 'prepare={\'filter_by_difficulty\': [-1], ' \ '\'filter_by_min_points\': {\'Pedestrian\': 10}}, ' \ 'classes=[\'Pedestrian\', \'Cyclist\', \'Car\'], ' \ 'sample_groups={\'Pedestrian\': 6}' assert repr_str == expected_repr_str assert points.tensor.numpy().shape == (800, 4) assert gt_bboxes_3d.tensor.shape == (1, 7) assert np.all(gt_labels_3d == [0])
def show_proj_det_result_meshlab(data, result, out_dir, score_thr=0.0, show=False, snapshot=False): """Show result of projecting 3D bbox to 2D image by meshlab.""" assert 'img' in data.keys(), 'image data is not provided for visualization' img_filename = data['img_metas'][0][0]['filename'] file_name = osp.split(img_filename)[-1].split('.')[0] # read from file because img in data_dict has undergone pipeline transform img = mmcv.imread(img_filename) if 'pts_bbox' in result[0].keys(): result[0] = result[0]['pts_bbox'] elif 'img_bbox' in result[0].keys(): result[0] = result[0]['img_bbox'] pred_bboxes = result[0]['boxes_3d'].tensor.numpy() pred_scores = result[0]['scores_3d'].numpy() # filter out low score bboxes for visualization if score_thr > 0: inds = pred_scores > score_thr pred_bboxes = pred_bboxes[inds] box_mode = data['img_metas'][0][0]['box_mode_3d'] if box_mode == Box3DMode.LIDAR: if 'lidar2img' not in data['img_metas'][0][0]: raise NotImplementedError( 'LiDAR to image transformation matrix is not provided') show_bboxes = LiDARInstance3DBoxes(pred_bboxes, origin=(0.5, 0.5, 0)) show_multi_modality_result(img, None, show_bboxes, data['img_metas'][0][0]['lidar2img'], out_dir, file_name, box_mode='lidar', show=show) elif box_mode == Box3DMode.DEPTH: show_bboxes = DepthInstance3DBoxes(pred_bboxes, origin=(0.5, 0.5, 0)) show_multi_modality_result(img, None, show_bboxes, None, out_dir, file_name, box_mode='depth', img_metas=data['img_metas'][0][0], show=show) elif box_mode == Box3DMode.CAM: if 'cam2img' not in data['img_metas'][0][0]: raise NotImplementedError( 'camera intrinsic matrix is not provided') show_bboxes = CameraInstance3DBoxes(pred_bboxes, box_dim=pred_bboxes.shape[-1], origin=(0.5, 1.0, 0.5)) show_multi_modality_result(img, None, show_bboxes, data['img_metas'][0][0]['cam2img'], out_dir, file_name, box_mode='camera', show=show) else: raise NotImplementedError( f'visualization of {box_mode} bbox is not supported') return file_name