-
Notifications
You must be signed in to change notification settings - Fork 29
/
multi_view.py
124 lines (106 loc) · 4.7 KB
/
multi_view.py
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
import numpy as np
from mmdet.datasets.builder import PIPELINES
from mmdet.datasets.pipelines import Compose, RandomFlip, LoadImageFromFile
@PIPELINES.register_module()
class MultiViewPipeline:
def __init__(self, transforms, n_images):
self.transforms = Compose(transforms)
self.n_images = n_images
def __call__(self, results):
imgs = []
extrinsics = []
ids = np.arange(len(results['img_info']))
replace = True if self.n_images > len(ids) else False
ids = np.random.choice(ids, self.n_images, replace=replace)
for i in ids.tolist():
_results = dict()
for key in ['img_prefix', 'img_info']:
_results[key] = results[key][i]
_results = self.transforms(_results)
imgs.append(_results['img'])
extrinsics.append(results['lidar2img']['extrinsic'][i])
for key in _results.keys():
if key not in ['img', 'img_prefix', 'img_info']:
results[key] = _results[key]
results['img'] = imgs
results['lidar2img']['extrinsic'] = extrinsics
return results
@PIPELINES.register_module()
class RandomShiftOrigin:
def __init__(self, std):
self.std = std
def __call__(self, results):
shift = np.random.normal(.0, self.std, 3)
results['lidar2img']['origin'] += shift
return results
@PIPELINES.register_module()
class KittiSetOrigin:
def __init__(self, point_cloud_range):
point_cloud_range = np.array(point_cloud_range, dtype=np.float32)
self.origin = (point_cloud_range[:3] + point_cloud_range[3:]) / 2.
def __call__(self, results):
results['lidar2img']['origin'] = self.origin.copy()
return results
@PIPELINES.register_module()
class KittiRandomFlip:
def __call__(self, results):
if results['flip']:
results['lidar2img']['intrinsic'][0, 2] = -results['lidar2img']['intrinsic'][0, 2] + \
results['ori_shape'][1]
flip_matrix_0 = np.eye(4, dtype=np.float32)
flip_matrix_0[0, 0] *= -1
flip_matrix_1 = np.eye(4, dtype=np.float32)
flip_matrix_1[1, 1] *= -1
extrinsic = results['lidar2img']['extrinsic'][0]
extrinsic = flip_matrix_0 @ extrinsic @ flip_matrix_1.T
results['lidar2img']['extrinsic'][0] = extrinsic
boxes = results['gt_bboxes_3d'].tensor.numpy()
center = boxes[:, :3]
alpha = boxes[:, 6]
phi = np.arctan2(center[:, 0], -center[:, 1]) - alpha
center_flip = center
center_flip[:, 1] *= -1
alpha_flip = np.arctan2(center_flip[:, 0], -center_flip[:, 1]) + phi
boxes_flip = np.concatenate([center_flip, boxes[:, 3:6], alpha_flip[:, None]], 1)
results['gt_bboxes_3d'] = results['box_type_3d'](boxes_flip)
return results
@PIPELINES.register_module()
class SunRgbdSetOrigin:
def __call__(self, results):
intrinsic = results['lidar2img']['intrinsic'][:3, :3]
extrinsic = results['lidar2img']['extrinsic'][0][:3, :3]
projection = intrinsic @ extrinsic
h, w, _ = results['ori_shape']
center_2d_3 = np.array([w / 2, h / 2, 1], dtype=np.float32)
center_2d_3 *= 3
origin = np.linalg.inv(projection) @ center_2d_3
results['lidar2img']['origin'] = origin
return results
@PIPELINES.register_module()
class SunRgbdTotalLoadImageFromFile(LoadImageFromFile):
def __call__(self, results):
file_name = results['img_info']['filename']
flip = file_name.endswith('_flip.jpg')
if flip:
results['img_info']['filename'] = file_name.replace('_flip.jpg', '.jpg')
results = super().__call__(results)
if flip:
results['img'] = results['img'][:, ::-1]
return results
@PIPELINES.register_module()
class SunRgbdRandomFlip:
def __call__(self, results):
if results['flip']:
flip_matrix = np.eye(3)
flip_matrix[0, 0] *= -1
extrinsic = results['lidar2img']['extrinsic'][0][:3, :3]
results['lidar2img']['extrinsic'][0][:3, :3] = flip_matrix @ extrinsic @ flip_matrix.T
boxes = results['gt_bboxes_3d'].tensor.numpy()
center = boxes[:, :3]
alpha = boxes[:, 6]
phi = np.arctan2(center[:, 1], center[:, 0]) - alpha
center_flip = center @ flip_matrix
alpha_flip = np.arctan2(center_flip[:, 1], center_flip[:, 0]) + phi
boxes_flip = np.concatenate([center_flip, boxes[:, 3:6], alpha_flip[:, None]], 1)
results['gt_bboxes_3d'] = results['box_type_3d'](boxes_flip)
return results