File size: 10,531 Bytes
34d1f8b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np
import pytest
import torch
from mmdet3d.models import draw_heatmap_gaussian
from mmdet3d.models.utils import (filter_outside_objs, get_edge_indices,
get_keypoints, handle_proj_objs)
from mmdet3d.structures import CameraInstance3DBoxes, points_img2cam
from mmdet3d.utils import array_converter
def test_gaussian():
heatmap = torch.zeros((128, 128))
ct_int = torch.tensor([64, 64], dtype=torch.int32)
radius = 2
draw_heatmap_gaussian(heatmap, ct_int, radius)
assert torch.isclose(torch.sum(heatmap), torch.tensor(4.3505), atol=1e-3)
def test_array_converter():
# to torch
@array_converter(to_torch=True, apply_to=('array_a', 'array_b'))
def test_func_1(array_a, array_b, container):
container.append(array_a)
container.append(array_b)
return array_a.clone(), array_b.clone()
np_array_a = np.array([0.0])
np_array_b = np.array([0.0])
container = []
new_array_a, new_array_b = test_func_1(np_array_a, np_array_b, container)
assert isinstance(new_array_a, np.ndarray)
assert isinstance(new_array_b, np.ndarray)
assert isinstance(container[0], torch.Tensor)
assert isinstance(container[1], torch.Tensor)
# one to torch and one not
@array_converter(to_torch=True, apply_to=('array_a', ))
def test_func_2(array_a, array_b):
return torch.cat([array_a, array_b])
with pytest.raises(TypeError):
_ = test_func_2(np_array_a, np_array_b)
# wrong template_arg_name_
@array_converter(
to_torch=True, apply_to=('array_a', ), template_arg_name_='array_c')
def test_func_3(array_a, array_b):
return torch.cat([array_a, array_b])
with pytest.raises(ValueError):
_ = test_func_3(np_array_a, np_array_b)
# wrong apply_to
@array_converter(to_torch=True, apply_to=('array_a', 'array_c'))
def test_func_4(array_a, array_b):
return torch.cat([array_a, array_b])
with pytest.raises(ValueError):
_ = test_func_4(np_array_a, np_array_b)
# to numpy
@array_converter(to_torch=False, apply_to=('array_a', 'array_b'))
def test_func_5(array_a, array_b, container):
container.append(array_a)
container.append(array_b)
return array_a.copy(), array_b.copy()
pt_array_a = torch.tensor([0.0])
pt_array_b = torch.tensor([0.0])
container = []
new_array_a, new_array_b = test_func_5(pt_array_a, pt_array_b, container)
assert isinstance(container[0], np.ndarray)
assert isinstance(container[1], np.ndarray)
assert isinstance(new_array_a, torch.Tensor)
assert isinstance(new_array_b, torch.Tensor)
# apply_to = None
@array_converter(to_torch=False)
def test_func_6(array_a, array_b, container):
container.append(array_a)
container.append(array_b)
return array_a.clone(), array_b.clone()
container = []
new_array_a, new_array_b = test_func_6(pt_array_a, pt_array_b, container)
assert isinstance(container[0], torch.Tensor)
assert isinstance(container[1], torch.Tensor)
assert isinstance(new_array_a, torch.Tensor)
assert isinstance(new_array_b, torch.Tensor)
# with default arg
@array_converter(to_torch=True, apply_to=('array_a', 'array_b'))
def test_func_7(array_a, container, array_b=np.array([2.])):
container.append(array_a)
container.append(array_b)
return array_a.clone(), array_b.clone()
container = []
new_array_a, new_array_b = test_func_7(np_array_a, container)
assert isinstance(container[0], torch.Tensor)
assert isinstance(container[1], torch.Tensor)
assert isinstance(new_array_a, np.ndarray)
assert isinstance(new_array_b, np.ndarray)
assert np.allclose(new_array_b, np.array([2.]), 1e-3)
# override default arg
container = []
new_array_a, new_array_b = test_func_7(np_array_a, container,
np.array([4.]))
assert isinstance(container[0], torch.Tensor)
assert isinstance(container[1], torch.Tensor)
assert isinstance(new_array_a, np.ndarray)
assert np.allclose(new_array_b, np.array([4.]), 1e-3)
# list arg
@array_converter(to_torch=True, apply_to=('array_a', 'array_b'))
def test_func_8(container, array_a, array_b=[2.]):
container.append(array_a)
container.append(array_b)
return array_a.clone(), array_b.clone()
container = []
new_array_a, new_array_b = test_func_8(container, [3.])
assert isinstance(container[0], torch.Tensor)
assert isinstance(container[1], torch.Tensor)
assert np.allclose(new_array_a, np.array([3.]), 1e-3)
assert np.allclose(new_array_b, np.array([2.]), 1e-3)
# number arg
@array_converter(to_torch=True, apply_to=('array_a', 'array_b'))
def test_func_9(container, array_a, array_b=1):
container.append(array_a)
container.append(array_b)
return array_a.clone(), array_b.clone()
container = []
new_array_a, new_array_b = test_func_9(container, np_array_a)
assert isinstance(container[0], torch.FloatTensor)
assert isinstance(container[1], torch.FloatTensor)
assert np.allclose(new_array_a, np_array_a, 1e-3)
assert np.allclose(new_array_b, np.array(1.0), 1e-3)
# feed kwargs
container = []
kwargs = {'array_a': [5.], 'array_b': [6.]}
new_array_a, new_array_b = test_func_8(container, **kwargs)
assert isinstance(container[0], torch.Tensor)
assert isinstance(container[1], torch.Tensor)
assert np.allclose(new_array_a, np.array([5.]), 1e-3)
assert np.allclose(new_array_b, np.array([6.]), 1e-3)
# feed args and kwargs
container = []
kwargs = {'array_b': [7.]}
args = (container, [8.])
new_array_a, new_array_b = test_func_8(*args, **kwargs)
assert isinstance(container[0], torch.Tensor)
assert isinstance(container[1], torch.Tensor)
assert np.allclose(new_array_a, np.array([8.]), 1e-3)
assert np.allclose(new_array_b, np.array([7.]), 1e-3)
# wrong template arg type
with pytest.raises(TypeError):
new_array_a, new_array_b = test_func_9(container, 3 + 4j)
with pytest.raises(TypeError):
new_array_a, new_array_b = test_func_9(container, {})
# invalid template arg list
with pytest.raises((TypeError, ValueError)):
new_array_a, new_array_b = test_func_9(container,
[True, np.array([3.0])])
def test_points_img2cam():
points = torch.tensor([[0.5764, 0.9109, 0.7576], [0.6656, 0.5498, 0.9813]])
cam2img = torch.tensor([[700., 0., 450., 0.], [0., 700., 200., 0.],
[0., 0., 1., 0.]])
xyzs = points_img2cam(points, cam2img)
expected_xyzs = torch.tensor([[-0.4864, -0.2155, 0.7576],
[-0.6299, -0.2796, 0.9813]])
assert torch.allclose(xyzs, expected_xyzs, atol=1e-3)
def test_generate_edge_indices():
input_metas = [
dict(img_shape=(110, 110), pad_shape=(128, 128)),
dict(img_shape=(98, 110), pad_shape=(128, 128))
]
downsample_ratio = 4
edge_indices_list = get_edge_indices(input_metas, downsample_ratio)
assert edge_indices_list[0].shape[0] == 108
assert edge_indices_list[1].shape[0] == 102
def test_truncation_hanlde():
centers2d_list = [
torch.tensor([[-99.86, 199.45], [499.50, 399.20], [201.20, 99.86]])
]
gt_bboxes_list = [
torch.tensor([[0.25, 99.8, 99.8, 199.6], [300.2, 250.1, 399.8, 299.6],
[100.2, 20.1, 300.8, 180.7]])
]
img_metas = [dict(img_shape=[300, 400])]
centers2d_target_list, offsets2d_list, trunc_mask_list = \
handle_proj_objs(centers2d_list, gt_bboxes_list, img_metas)
centers2d_target = torch.tensor([[0., 166.30435501], [379.03437877, 299.],
[201.2, 99.86]])
offsets2d = torch.tensor([[-99.86, 33.45], [120.5, 100.2], [0.2, -0.14]])
trunc_mask = torch.tensor([True, True, False])
assert torch.allclose(centers2d_target_list[0], centers2d_target)
assert torch.allclose(offsets2d_list[0], offsets2d, atol=1e-4)
assert torch.all(trunc_mask_list[0] == trunc_mask)
assert torch.allclose(
centers2d_target_list[0].round().int() + offsets2d_list[0],
centers2d_list[0])
def test_filter_outside_objs():
centers2d_list = [
torch.tensor([[-99.86, 199.45], [499.50, 399.20], [201.20, 99.86]]),
torch.tensor([[-47.86, 199.45], [410.50, 399.20], [401.20, 349.86]])
]
gt_bboxes_list = [
torch.rand([3, 4], dtype=torch.float32),
torch.rand([3, 4], dtype=torch.float32)
]
gt_bboxes_3d_list = [
CameraInstance3DBoxes(torch.rand([3, 7]), box_dim=7),
CameraInstance3DBoxes(torch.rand([3, 7]), box_dim=7)
]
gt_labels_list = [torch.tensor([0, 1, 2]), torch.tensor([2, 0, 0])]
gt_labels_3d_list = [torch.tensor([0, 1, 2]), torch.tensor([2, 0, 0])]
img_metas = [dict(img_shape=[300, 400]), dict(img_shape=[500, 450])]
filter_outside_objs(gt_bboxes_list, gt_labels_list, gt_bboxes_3d_list,
gt_labels_3d_list, centers2d_list, img_metas)
assert len(centers2d_list[0]) == len(gt_bboxes_3d_list[0]) == \
len(gt_bboxes_list[0]) == len(gt_labels_3d_list[0]) == \
len(gt_labels_list[0]) == 1
assert len(centers2d_list[1]) == len(gt_bboxes_3d_list[1]) == \
len(gt_bboxes_list[1]) == len(gt_labels_3d_list[1]) == \
len(gt_labels_list[1]) == 2
def test_generate_keypoints():
centers2d_list = [
torch.tensor([[-99.86, 199.45], [499.50, 399.20], [201.20, 99.86]]),
torch.tensor([[-47.86, 199.45], [410.50, 399.20], [401.20, 349.86]])
]
gt_bboxes_3d_list = [
CameraInstance3DBoxes(torch.rand([3, 7])),
CameraInstance3DBoxes(torch.rand([3, 7]))
]
img_metas = [
dict(
cam2img=[[1260.8474446004698, 0.0, 807.968244525554, 40.1111],
[0.0, 1260.8474446004698, 495.3344268742088, 2.34422],
[0.0, 0.0, 1.0, 0.00333333], [0.0, 0.0, 0.0, 1.0]],
img_shape=(300, 400)) for i in range(2)
]
keypoints2d_list, keypoints_depth_mask_list = \
get_keypoints(gt_bboxes_3d_list, centers2d_list, img_metas)
assert keypoints2d_list[0].shape == (3, 10, 3)
assert keypoints_depth_mask_list[0].shape == (3, 3)
|