Unverified Commit 9f0b01ca authored by Danila Rukhovich's avatar Danila Rukhovich Committed by GitHub
Browse files

[Refactor] Camera keys (#805)

* replace all cam_intrinsic

* revert cam_intrinsic in test

* remove rect and fix lint
parent d2fb7ab2
...@@ -423,7 +423,7 @@ def show_proj_det_result_meshlab(data, ...@@ -423,7 +423,7 @@ def show_proj_det_result_meshlab(data,
img_metas=data['img_metas'][0][0], img_metas=data['img_metas'][0][0],
show=show) show=show)
elif box_mode == Box3DMode.CAM: elif box_mode == Box3DMode.CAM:
if 'cam_intrinsic' not in data['img_metas'][0][0]: if 'cam2img' not in data['img_metas'][0][0]:
raise NotImplementedError( raise NotImplementedError(
'camera intrinsic matrix is not provided') 'camera intrinsic matrix is not provided')
...@@ -434,7 +434,7 @@ def show_proj_det_result_meshlab(data, ...@@ -434,7 +434,7 @@ def show_proj_det_result_meshlab(data,
img, img,
None, None,
show_bboxes, show_bboxes,
data['img_metas'][0][0]['cam_intrinsic'], data['img_metas'][0][0]['cam2img'],
out_dir, out_dir,
file_name, file_name,
box_mode='camera', box_mode='camera',
......
...@@ -162,7 +162,7 @@ def draw_depth_bbox3d_on_img(bboxes3d, ...@@ -162,7 +162,7 @@ def draw_depth_bbox3d_on_img(bboxes3d,
def draw_camera_bbox3d_on_img(bboxes3d, def draw_camera_bbox3d_on_img(bboxes3d,
raw_img, raw_img,
cam_intrinsic, cam2img,
img_metas, img_metas,
color=(0, 255, 0), color=(0, 255, 0),
thickness=1): thickness=1):
...@@ -172,7 +172,7 @@ def draw_camera_bbox3d_on_img(bboxes3d, ...@@ -172,7 +172,7 @@ def draw_camera_bbox3d_on_img(bboxes3d,
bboxes3d (:obj:`CameraInstance3DBoxes`, shape=[M, 7]): bboxes3d (:obj:`CameraInstance3DBoxes`, shape=[M, 7]):
3d bbox in camera coordinate system to visualize. 3d bbox in camera coordinate system to visualize.
raw_img (numpy.array): The numpy array of image. raw_img (numpy.array): The numpy array of image.
cam_intrinsic (dict): Camera intrinsic matrix, cam2img (dict): Camera intrinsic matrix,
denoted as `K` in depth bbox coordinate system. denoted as `K` in depth bbox coordinate system.
img_metas (dict): Useless here. img_metas (dict): Useless here.
color (tuple[int]): The color to draw bboxes. Default: (0, 255, 0). color (tuple[int]): The color to draw bboxes. Default: (0, 255, 0).
...@@ -181,16 +181,16 @@ def draw_camera_bbox3d_on_img(bboxes3d, ...@@ -181,16 +181,16 @@ def draw_camera_bbox3d_on_img(bboxes3d,
from mmdet3d.core.bbox import points_cam2img from mmdet3d.core.bbox import points_cam2img
img = raw_img.copy() img = raw_img.copy()
cam_intrinsic = copy.deepcopy(cam_intrinsic) cam2img = copy.deepcopy(cam2img)
corners_3d = bboxes3d.corners corners_3d = bboxes3d.corners
num_bbox = corners_3d.shape[0] num_bbox = corners_3d.shape[0]
points_3d = corners_3d.reshape(-1, 3) points_3d = corners_3d.reshape(-1, 3)
if not isinstance(cam_intrinsic, torch.Tensor): if not isinstance(cam2img, torch.Tensor):
cam_intrinsic = torch.from_numpy(np.array(cam_intrinsic)) cam2img = torch.from_numpy(np.array(cam2img))
cam_intrinsic = cam_intrinsic.reshape(3, 3).float().cpu() cam2img = cam2img.reshape(3, 3).float().cpu()
# project to 2d to get image coords (uv) # project to 2d to get image coords (uv)
uv_origin = points_cam2img(points_3d, cam_intrinsic) uv_origin = points_cam2img(points_3d, cam2img)
uv_origin = (uv_origin - 1).round() uv_origin = (uv_origin - 1).round()
imgfov_pts_2d = uv_origin[..., :2].reshape(num_bbox, 8, 2).numpy() imgfov_pts_2d = uv_origin[..., :2].reshape(num_bbox, 8, 2).numpy()
......
...@@ -628,7 +628,7 @@ class NuScenesMonoDataset(CocoDataset): ...@@ -628,7 +628,7 @@ class NuScenesMonoDataset(CocoDataset):
img, img,
gt_bboxes, gt_bboxes,
pred_bboxes, pred_bboxes,
img_metas['cam_intrinsic'], img_metas['cam2img'],
out_dir, out_dir,
file_name, file_name,
box_mode='camera', box_mode='camera',
......
...@@ -101,6 +101,7 @@ class Collect3D(object): ...@@ -101,6 +101,7 @@ class Collect3D(object):
- 'pad_shape': image shape after padding - 'pad_shape': image shape after padding
- 'lidar2img': transform from lidar to image - 'lidar2img': transform from lidar to image
- 'depth2img': transform from depth to image - 'depth2img': transform from depth to image
- 'cam2img': transform from camera to image
- 'pcd_horizontal_flip': a boolean indicating if point cloud is \ - 'pcd_horizontal_flip': a boolean indicating if point cloud is \
flipped horizontally flipped horizontally
- 'pcd_vertical_flip': a boolean indicating if point cloud is \ - 'pcd_vertical_flip': a boolean indicating if point cloud is \
...@@ -108,13 +109,9 @@ class Collect3D(object): ...@@ -108,13 +109,9 @@ class Collect3D(object):
- 'box_mode_3d': 3D box mode - 'box_mode_3d': 3D box mode
- 'box_type_3d': 3D box type - 'box_type_3d': 3D box type
- 'img_norm_cfg': a dict of normalization information: - 'img_norm_cfg': a dict of normalization information:
- mean: per channel mean subtraction - mean: per channel mean subtraction
- std: per channel std divisor - std: per channel std divisor
- to_rgb: bool indicating if bgr was converted to rgb - to_rgb: bool indicating if bgr was converted to rgb
- 'rect': rectification matrix
- 'Trv2c': transformation from velodyne to camera coordinate
- 'P2': transformation betweeen cameras
- 'pcd_trans': point cloud transformations - 'pcd_trans': point cloud transformations
- 'sample_idx': sample index - 'sample_idx': sample index
- 'pcd_scale_factor': point cloud scale factor - 'pcd_scale_factor': point cloud scale factor
...@@ -125,22 +122,22 @@ class Collect3D(object): ...@@ -125,22 +122,22 @@ class Collect3D(object):
keys (Sequence[str]): Keys of results to be collected in ``data``. keys (Sequence[str]): Keys of results to be collected in ``data``.
meta_keys (Sequence[str], optional): Meta keys to be converted to meta_keys (Sequence[str], optional): Meta keys to be converted to
``mmcv.DataContainer`` and collected in ``data[img_metas]``. ``mmcv.DataContainer`` and collected in ``data[img_metas]``.
Default: ('filename', 'ori_shape', 'img_shape', 'lidar2img', \ Default: ('filename', 'ori_shape', 'img_shape', 'lidar2img',
'pad_shape', 'scale_factor', 'flip', 'pcd_horizontal_flip', \ 'depth2img', 'cam2img', 'pad_shape', 'scale_factor', 'flip',
'pcd_vertical_flip', 'box_mode_3d', 'box_type_3d', \ 'pcd_horizontal_flip', 'pcd_vertical_flip', 'box_mode_3d',
'img_norm_cfg', 'rect', 'Trv2c', 'P2', 'pcd_trans', \ 'box_type_3d', 'img_norm_cfg', 'pcd_trans',
'sample_idx', 'pcd_scale_factor', 'pcd_rotation', 'pts_filename') 'sample_idx', 'pcd_scale_factor', 'pcd_rotation', 'pts_filename')
""" """
def __init__(self, def __init__(self,
keys, keys,
meta_keys=('filename', 'ori_shape', 'img_shape', 'lidar2img', meta_keys=('filename', 'ori_shape', 'img_shape', 'lidar2img',
'depth2img', 'pad_shape', 'scale_factor', 'flip', 'depth2img', 'cam2img', 'pad_shape',
'cam_intrinsic', 'pcd_horizontal_flip', 'scale_factor', 'flip', 'pcd_horizontal_flip',
'pcd_vertical_flip', 'box_mode_3d', 'box_type_3d', 'pcd_vertical_flip', 'box_mode_3d', 'box_type_3d',
'img_norm_cfg', 'rect', 'Trv2c', 'P2', 'pcd_trans', 'img_norm_cfg', 'pcd_trans', 'sample_idx',
'sample_idx', 'pcd_scale_factor', 'pcd_rotation', 'pcd_scale_factor', 'pcd_rotation', 'pts_filename',
'pts_filename', 'transformation_3d_flow')): 'transformation_3d_flow')):
self.keys = keys self.keys = keys
self.meta_keys = meta_keys self.meta_keys = meta_keys
......
...@@ -90,7 +90,7 @@ class LoadImageFromFileMono3D(LoadImageFromFile): ...@@ -90,7 +90,7 @@ class LoadImageFromFileMono3D(LoadImageFromFile):
dict: The dict contains loaded image and meta information. dict: The dict contains loaded image and meta information.
""" """
super().__call__(results) super().__call__(results)
results['cam_intrinsic'] = results['img_info']['cam_intrinsic'] results['cam2img'] = results['img_info']['cam_intrinsic']
return results return results
......
...@@ -130,11 +130,10 @@ class RandomFlip3D(RandomFlip): ...@@ -130,11 +130,10 @@ class RandomFlip3D(RandomFlip):
w - input_dict['centers2d'][..., 0] w - input_dict['centers2d'][..., 0]
# need to modify the horizontal position of camera center # need to modify the horizontal position of camera center
# along u-axis in the image (flip like centers2d) # along u-axis in the image (flip like centers2d)
# ['cam_intrinsic'][0][2] = c_u # ['cam2img'][0][2] = c_u
# see more details and examples at # see more details and examples at
# https://github.com/open-mmlab/mmdetection3d/pull/744 # https://github.com/open-mmlab/mmdetection3d/pull/744
input_dict['cam_intrinsic'][0][2] = \ input_dict['cam2img'][0][2] = w - input_dict['cam2img'][0][2]
w - input_dict['cam_intrinsic'][0][2]
def __call__(self, input_dict): def __call__(self, input_dict):
"""Call function to flip points, values in the ``bbox3d_fields`` and \ """Call function to flip points, values in the ``bbox3d_fields`` and \
......
...@@ -590,7 +590,7 @@ class FCOSMono3DHead(AnchorFreeMono3DHead): ...@@ -590,7 +590,7 @@ class FCOSMono3DHead(AnchorFreeMono3DHead):
Returns: Returns:
tuples[Tensor]: Predicted 3D boxes, scores, labels and attributes. tuples[Tensor]: Predicted 3D boxes, scores, labels and attributes.
""" """
view = np.array(input_meta['cam_intrinsic']) view = np.array(input_meta['cam2img'])
scale_factor = input_meta['scale_factor'] scale_factor = input_meta['scale_factor']
cfg = self.test_cfg if cfg is None else cfg cfg = self.test_cfg if cfg is None else cfg
assert len(cls_scores) == len(bbox_preds) == len(mlvl_points) assert len(cls_scores) == len(bbox_preds) == len(mlvl_points)
......
...@@ -189,11 +189,10 @@ class SingleStageMono3DDetector(SingleStageDetector): ...@@ -189,11 +189,10 @@ class SingleStageMono3DDetector(SingleStageDetector):
if isinstance(data['img_metas'][0], DC): if isinstance(data['img_metas'][0], DC):
img_filename = data['img_metas'][0]._data[0][batch_id][ img_filename = data['img_metas'][0]._data[0][batch_id][
'filename'] 'filename']
cam_intrinsic = data['img_metas'][0]._data[0][batch_id][ cam2img = data['img_metas'][0]._data[0][batch_id]['cam2img']
'cam_intrinsic']
elif mmcv.is_list_of(data['img_metas'][0], dict): elif mmcv.is_list_of(data['img_metas'][0], dict):
img_filename = data['img_metas'][0][batch_id]['filename'] img_filename = data['img_metas'][0][batch_id]['filename']
cam_intrinsic = data['img_metas'][0][batch_id]['cam_intrinsic'] cam2img = data['img_metas'][0][batch_id]['cam2img']
else: else:
ValueError( ValueError(
f"Unsupported data type {type(data['img_metas'][0])} " f"Unsupported data type {type(data['img_metas'][0])} "
...@@ -211,7 +210,7 @@ class SingleStageMono3DDetector(SingleStageDetector): ...@@ -211,7 +210,7 @@ class SingleStageMono3DDetector(SingleStageDetector):
img, img,
None, None,
pred_bboxes, pred_bboxes,
cam_intrinsic, cam2img,
out_dir, out_dir,
file_name, file_name,
'camera', 'camera',
......
...@@ -304,7 +304,7 @@ def test_load_image_from_file_mono_3d(): ...@@ -304,7 +304,7 @@ def test_load_image_from_file_mono_3d():
img_info=dict(filename=filename, cam_intrinsic=cam_intrinsic.copy())) img_info=dict(filename=filename, cam_intrinsic=cam_intrinsic.copy()))
results = load_image_from_file_mono_3d(input_dict) results = load_image_from_file_mono_3d(input_dict)
assert results['img'].shape == (900, 1600, 3) assert results['img'].shape == (900, 1600, 3)
assert np.all(results['cam_intrinsic'] == cam_intrinsic) assert np.all(results['cam2img'] == cam_intrinsic)
repr_str = repr(load_image_from_file_mono_3d) repr_str = repr(load_image_from_file_mono_3d)
expected_repr_str = 'LoadImageFromFileMono3D(to_float32=False, ' \ expected_repr_str = 'LoadImageFromFileMono3D(to_float32=False, ' \
......
...@@ -344,7 +344,7 @@ def test_fcos3d(): ...@@ -344,7 +344,7 @@ def test_fcos3d():
attr_labels = [torch.randint(0, 9, [3], device='cuda')] attr_labels = [torch.randint(0, 9, [3], device='cuda')]
img_metas = [ img_metas = [
dict( dict(
cam_intrinsic=[[1260.8474446004698, 0.0, 807.968244525554], cam2img=[[1260.8474446004698, 0.0, 807.968244525554],
[0.0, 1260.8474446004698, 495.3344268742088], [0.0, 1260.8474446004698, 495.3344268742088],
[0.0, 0.0, 1.0]], [0.0, 0.0, 1.0]],
scale_factor=np.array([1., 1., 1., 1.], dtype=np.float32), scale_factor=np.array([1., 1., 1., 1.], dtype=np.float32),
......
...@@ -1088,7 +1088,7 @@ def test_fcos_mono3d_head(): ...@@ -1088,7 +1088,7 @@ def test_fcos_mono3d_head():
attr_labels = [torch.randint(0, 9, [3], device='cuda') for i in range(2)] attr_labels = [torch.randint(0, 9, [3], device='cuda') for i in range(2)]
img_metas = [ img_metas = [
dict( dict(
cam_intrinsic=[[1260.8474446004698, 0.0, 807.968244525554], cam2img=[[1260.8474446004698, 0.0, 807.968244525554],
[0.0, 1260.8474446004698, 495.3344268742088], [0.0, 1260.8474446004698, 495.3344268742088],
[0.0, 0.0, 1.0]], [0.0, 0.0, 1.0]],
scale_factor=np.array([1., 1., 1., 1.], dtype=np.float32), scale_factor=np.array([1., 1., 1., 1.], dtype=np.float32),
......
...@@ -189,7 +189,7 @@ def test_show_result_meshlab(): ...@@ -189,7 +189,7 @@ def test_show_result_meshlab():
torch.tensor( torch.tensor(
[[6.4495, -3.9097, -1.7409, 1.5063, 3.1819, 1.4716, 1.8782]])) [[6.4495, -3.9097, -1.7409, 1.5063, 3.1819, 1.4716, 1.8782]]))
img = np.random.randn(1, 3, 384, 1280) img = np.random.randn(1, 3, 384, 1280)
cam_intrinsic = np.array([[100.0, 0.0, 50.0], [0.0, 100.0, 50.0], cam2img = np.array([[100.0, 0.0, 50.0], [0.0, 100.0, 50.0],
[0.0, 0.0, 1.0]]) [0.0, 0.0, 1.0]])
img_meta = dict( img_meta = dict(
filename=filename, filename=filename,
...@@ -199,7 +199,7 @@ def test_show_result_meshlab(): ...@@ -199,7 +199,7 @@ def test_show_result_meshlab():
box_type_3d=CameraInstance3DBoxes, box_type_3d=CameraInstance3DBoxes,
pcd_trans=np.array([0., 0., 0.]), pcd_trans=np.array([0., 0., 0.]),
pcd_scale_factor=1.0, pcd_scale_factor=1.0,
cam_intrinsic=cam_intrinsic) cam2img=cam2img)
data = dict( data = dict(
points=[[torch.tensor(points)]], img_metas=[[img_meta]], img=[img]) points=[[torch.tensor(points)]], img_metas=[[img_meta]], img=[img])
result = [ result = [
......
...@@ -163,7 +163,7 @@ def show_proj_bbox_img(idx, ...@@ -163,7 +163,7 @@ def show_proj_bbox_img(idx,
img, img,
gt_bboxes, gt_bboxes,
None, None,
img_metas['cam_intrinsic'], img_metas['cam2img'],
out_dir, out_dir,
filename, filename,
box_mode='camera', box_mode='camera',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment