-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
246 lines (193 loc) · 7.74 KB
/
utils.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
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
import functools
import time
import scipy
import numpy as np
import torch
from nuscenes.utils.data_classes import Box, LidarPointCloud, RadarPointCloud
from pyquaternion import Quaternion
from matplotlib import cm
def tensor_to_frame(output_dict):
def brighten(inp_srgb):
return np.clip((100/inp_srgb.mean())*inp_srgb,0,255).astype(np.uint8)
frame_dict = {}
for output_type, output_frame in output_dict.items():
if output_frame is None:
continue
if isinstance(output_frame, torch.Tensor):
output_frame = output_frame.cpu().numpy()
elif isinstance(output_frame, np.ndarray):
pass
if output_type in [
"img",
]:
img = output_frame[0,...].transpose((1,2,0))
img = np.clip(img, 0, 255).astype(np.uint8)
frame_dict[output_type] = img
elif output_type in [
'pred',
"label",
"radar",
]:
img = output_frame.squeeze()
img = np.clip(img, 0, 80).astype(np.uint8)
frame_dict[output_type] = colorize_depth_map(img/80)
elif output_type in [
'pred_mask',
"label_mask",
"radar_mask",
"valid_label",
"valid_label_mask",
]:
img = output_frame.squeeze()
frame_dict[output_type] = img
return frame_dict
def project_3d_to_2d(points: np.ndarray, projection_matrix: np.ndarray):
"""From vod.frame without rounding to int"""
uvw = projection_matrix.dot(points.T)
uvw /= uvw[2]
uvs = uvw[:2].T
# uvs = np.round(uvs).astype(np.int)
return uvs
def map_pointcloud1_to_pointcloud2(
lidar_points,
lidar_calibrated_sensor,
lidar_ego_pose,
cam_calibrated_sensor,
cam_ego_pose,
min_dist: float = 0.0,
):
# Points live in the point sensor frame. So they need to be
# transformed via global to the image plane.
# First step: transform the pointcloud to the ego vehicle
# frame for the timestamp of the sweep.
lidar_points = LidarPointCloud(lidar_points.T)
lidar_points.rotate(
Quaternion(lidar_calibrated_sensor['rotation']).rotation_matrix)
lidar_points.translate(np.array(lidar_calibrated_sensor['translation']))
# Second step: transform from ego to the global frame.
lidar_points.rotate(Quaternion(lidar_ego_pose['rotation']).rotation_matrix)
lidar_points.translate(np.array(lidar_ego_pose['translation']))
# Third step: transform from global into the ego vehicle
# frame for the timestamp of the image.
lidar_points.translate(-np.array(cam_ego_pose['translation']))
lidar_points.rotate(Quaternion(cam_ego_pose['rotation']).rotation_matrix.T)
# Fourth step: transform from ego into the camera.
lidar_points.translate(-np.array(cam_calibrated_sensor['translation']))
lidar_points.rotate(
Quaternion(cam_calibrated_sensor['rotation']).rotation_matrix.T)
points = lidar_points.points.transpose((1, 0))
return points
def map_pointcloud_to_image(
lidar_points,
lidar_calibrated_sensor,
lidar_ego_pose,
cam_calibrated_sensor,
cam_ego_pose,
min_dist: float = 0.0,
):
# Points live in the point sensor frame. So they need to be
# transformed via global to the image plane.
# First step: transform the pointcloud to the ego vehicle
# frame for the timestamp of the sweep.
points = map_pointcloud1_to_pointcloud2(lidar_points, lidar_calibrated_sensor, lidar_ego_pose,
cam_calibrated_sensor, cam_ego_pose, min_dist)
# Fifth step: actually take a "picture" of the point cloud.
# Grab the depths (camera frame z axis points away from the camera).
uvs = project_3d_to_2d(points[:, :3], np.array(cam_calibrated_sensor['camera_intrinsic']))
return points, np.concatenate((uvs, points[:, 2:3]), 1)
def canvas_filter(data, shape):
return np.all((data >= 0) & (data < shape[1::-1]), 1)
def _scale_pts(data, out_shape, input_shape):
data[:, :2] *= (np.array(out_shape[::-1]) / input_shape[1::-1])
return data
def get_depth_map(data, shape, input_shape=None):
if input_shape is not None:
data = _scale_pts(data.copy(), shape, input_shape)
depth = np.zeros(shape + (data.shape[1] - 2, ), dtype=np.float32)
if np.any(data[:, :2].max(0) >= shape[1::-1]) or data[:, :2].min() < 0:
inds = canvas_filter(data[:, :2], shape)
data = data[inds]
depth[data[:, 1].astype(int), data[:, 0].astype(int)] = data[:, 2:]
return depth.squeeze()
def get_radar_vert_map(radar, out_shape, input_shape=None):
if input_shape is not None:
radar = _scale_pts(radar.copy(), out_shape, input_shape)
radar_map = np.full(out_shape + (9, ), 10000, dtype=np.float32)
radar_map[radar[:, 1].astype(int), radar[:, 0].astype(int)] = radar[:, 3:]
radar_map = scipy.ndimage.minimum_filter1d(radar_map, 3, 1)
radar_map[radar_map == 10000] = 0
return radar_map
def get_radar_map(data, shape, input_shape=None):
if input_shape is not None:
data = _scale_pts(data.copy(), shape, input_shape)
depth = np.zeros(shape + (data.shape[1] - 2, ), dtype=np.float32)
if np.any(data[:, :2].max(0) >= shape[1::-1]) or data[:, :2].min() < 0:
inds = canvas_filter(data[:, :2], shape)
data = data[inds]
depth[:, data[:, 0].astype(int)] = data[:, 2:]
return depth.squeeze()
def extend_height(cam_depth, camera_intrinsic, origin_dims, h0=0.25, h1=1.5):
camera_intrinsic = camera_intrinsic
H, W = origin_dims
def getRelSize(camera_intrinsic, d, w=0.5, h=1.5):
v = (h*camera_intrinsic[0][0])/d
u = (w*camera_intrinsic[1][1])/d
return u, v #int(u),int(v)
ret = cam_depth.copy()
for depth in cam_depth:
x,y,d = depth
_,v1 = getRelSize(camera_intrinsic, d, 0, h1)
_,v0 = getRelSize(camera_intrinsic, d, 0, h0)
y_list = np.arange(start=max(y-v0,0),stop=min(y+v1,H),step=1)
ptsnum_after_extend = len(y_list)
x = np.stack((np.array([x]*ptsnum_after_extend),y_list,np.array([d]*ptsnum_after_extend)),axis=1)
ret = np.concatenate((ret,x),axis=0)
return ret
def colorize_depth_map(data, mask=None, norm=False):
if mask is None:
mask = data > 0
elif mask.dtype != bool:
mask = (mask > 0).astype(bool)
# data = np.exp(-data / 72.136)
if norm:
min_val = data[mask].min()
data = (data - min_val) / (data.max() - min_val)
else:
data = np.clip(data, 0, 1)
data = (np.clip(data, 0, 1) * 255).astype(np.uint8)
# data = cv2.applyColorMap(data, cv2.COLORMAP_JET)
data = cm.jet(data)
data = (data[..., :3] * 255).astype(np.uint8)
mask = np.stack([mask] * 3, 2)
data[~mask] = 0
return data
def log_rate_limited(min_interval=1):
def decorator(should_record):
last = 0
@functools.wraps(should_record)
def wrapper(*args, **kwargs):
nonlocal last
if time.time() - last < min_interval:
return False
ret = should_record(*args, **kwargs)
last = time.time()
return ret
return wrapper
return decorator
class TrainClock(object):
def __init__(self):
self.epoch = 0
self.minibatch = 0
self.step = 0
def tick(self):
self.minibatch += 1
self.step += 1
def tock(self):
self.epoch += 1
self.minibatch = 0
def make_checkpoint(self):
return {"epoch": self.epoch, "minibatch": self.minibatch, "step": self.step}
def restore_checkpoint(self, clock_dict):
self.epoch = clock_dict["epoch"]
self.minibatch = clock_dict["minibatch"]
self.step = clock_dict["step"]