From e56a57bf9812879102bda19fc710cd879a9b8b41 Mon Sep 17 00:00:00 2001 From: THU17cyz Date: Thu, 2 Sep 2021 13:44:13 +0800 Subject: [PATCH] Change cam rot_3d_in_axis --- mmdet3d/core/bbox/structures/cam_box3d.py | 11 ++-------- mmdet3d/core/bbox/structures/utils.py | 4 ++-- mmdet3d/models/dense_heads/ssd_3d_head.py | 3 ++- tests/test_models/test_detectors.py | 3 ++- tests/test_utils/test_box3d.py | 26 ++++++++++++++++++++++- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/mmdet3d/core/bbox/structures/cam_box3d.py b/mmdet3d/core/bbox/structures/cam_box3d.py index dd2a8eed19..ac5cf8286c 100644 --- a/mmdet3d/core/bbox/structures/cam_box3d.py +++ b/mmdet3d/core/bbox/structures/cam_box3d.py @@ -137,11 +137,8 @@ def corners(self): corners_norm = corners_norm - dims.new_tensor([0.5, 1, 0.5]) corners = dims.view([-1, 1, 3]) * corners_norm.reshape([1, 8, 3]) - # positive direction of the gravity axis - # in cam coord system points to the earth - # so the rotation is clockwise if viewed from above corners = rotation_3d_in_axis( - corners, self.tensor[:, 6], axis=self.YAW_AXIS, clockwise=True) + corners, self.tensor[:, 6], axis=self.YAW_AXIS) corners += self.tensor[:, :3].view(-1, 1, 3) return corners @@ -182,11 +179,7 @@ def rotate(self, angle, points=None): self.tensor[:, 0:3], angle, axis=self.YAW_AXIS, - return_mat=True, - # positive direction of the gravity axis - # in cam coord system points to the earth - # so the rotation is clockwise if viewed from above - clockwise=True) + return_mat=True) else: rot_mat_T = angle rot_sin = rot_mat_T[2, 0] diff --git a/mmdet3d/core/bbox/structures/utils.py b/mmdet3d/core/bbox/structures/utils.py index 9ad4a6f6df..64483506cc 100644 --- a/mmdet3d/core/bbox/structures/utils.py +++ b/mmdet3d/core/bbox/structures/utils.py @@ -71,9 +71,9 @@ def rotation_3d_in_axis(points, if points.shape[-1] == 3: if axis == 1 or axis == -2: rot_mat_T = torch.stack([ - torch.stack([rot_cos, zeros, rot_sin]), + torch.stack([rot_cos, zeros, -rot_sin]), torch.stack([zeros, ones, zeros]), - torch.stack([-rot_sin, zeros, rot_cos]) + torch.stack([rot_sin, zeros, rot_cos]) ]) elif axis == 2 or axis == -1: rot_mat_T = torch.stack([ diff --git a/mmdet3d/models/dense_heads/ssd_3d_head.py b/mmdet3d/models/dense_heads/ssd_3d_head.py index 83fc37cc2d..0ce042fef1 100644 --- a/mmdet3d/models/dense_heads/ssd_3d_head.py +++ b/mmdet3d/models/dense_heads/ssd_3d_head.py @@ -391,7 +391,8 @@ def get_targets_single(self, # LiDARInstance3DBoxes and DepthInstance3DBoxes canonical_xyz = rotation_3d_in_axis( canonical_xyz.unsqueeze(0).transpose(0, 1), - -gt_bboxes_3d.yaw[assignment], 2).squeeze(1) + -gt_bboxes_3d.yaw[assignment], + axis=2).squeeze(1) distance_front = torch.clamp( size_res_targets[:, 0] - canonical_xyz[:, 0], min=0) distance_back = torch.clamp( diff --git a/tests/test_models/test_detectors.py b/tests/test_models/test_detectors.py index dc7a70812b..c9c3527350 100644 --- a/tests/test_models/test_detectors.py +++ b/tests/test_models/test_detectors.py @@ -437,7 +437,8 @@ def test_imvoxelnet(): if not torch.cuda.is_available(): pytest.skip('test requires GPU and torch+cuda') - imvoxelnet_cfg = _get_detector_cfg('imvoxelnet/imvoxelnet_kitti-3d-car.py') + imvoxelnet_cfg = _get_detector_cfg( + 'imvoxelnet/imvoxelnet_4x8_kitti-3d-car.py') self = build_detector(imvoxelnet_cfg).cuda() imgs = torch.rand([1, 3, 384, 1280], dtype=torch.float32).cuda() gt_bboxes_3d = [LiDARInstance3DBoxes(torch.rand([3, 7], device='cuda'))] diff --git a/tests/test_utils/test_box3d.py b/tests/test_utils/test_box3d.py index 974f22caec..00fab68db5 100644 --- a/tests/test_utils/test_box3d.py +++ b/tests/test_utils/test_box3d.py @@ -1553,13 +1553,37 @@ def test_rotation_3d_in_axis(): [[-0.2555, -0.2683, 0.0000], [-0.2555, -0.2683, 0.9072]]]) angles = [np.pi / 2, -np.pi / 2] - rotated = rotation_3d_in_axis(points, angles, axis=0) + rotated = rotation_3d_in_axis(points, angles, axis=0).numpy() expected_rotated = np.array([[[-0.4599, 0.0000, -0.0471], [-0.4599, -1.8433, -0.0471]], [[-0.2555, 0.0000, 0.2683], [-0.2555, 0.9072, 0.2683]]]) assert np.allclose(rotated, expected_rotated, atol=1e-3) + points = torch.tensor([[[-0.4599, -0.0471, 0.0000], + [-0.4599, -0.0471, 1.8433]], + [[-0.2555, -0.2683, 0.0000], + [-0.2555, -0.2683, 0.9072]]]) + angles = [np.pi / 2, -np.pi / 2] + rotated = rotation_3d_in_axis(points, angles, axis=1).numpy() + expected_rotated = np.array([[[0.0000, -0.0471, 0.4599], + [1.8433, -0.0471, 0.4599]], + [[0.0000, -0.2683, -0.2555], + [-0.9072, -0.2683, -0.2555]]]) + assert np.allclose(rotated, expected_rotated, atol=1e-3) + + points = torch.tensor([[[-0.4599, -0.0471, 0.0000], + [-0.4599, 0.0471, 1.8433]], + [[-0.2555, -0.2683, 0.0000], + [0.2555, -0.2683, 0.9072]]]) + angles = [np.pi / 2, -np.pi / 2] + rotated = rotation_3d_in_axis(points, angles, axis=2).numpy() + expected_rotated = np.array([[[0.0471, -0.4599, 0.0000], + [-0.0471, -0.4599, 1.8433]], + [[-0.2683, 0.2555, 0.0000], + [-0.2683, -0.2555, 0.9072]]]) + assert np.allclose(rotated, expected_rotated, atol=1e-3) + points = torch.tensor([[[-0.0471, 0.0000], [-0.0471, 1.8433]], [[-0.2683, 0.0000], [-0.2683, 0.9072]]]) angles = [np.pi / 2, -np.pi / 2]