forked from tianweiy/CenterPoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
459 lines (369 loc) · 17.4 KB
/
preprocess.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import numpy as np
from det3d.core.bbox import box_np_ops
from det3d.core.sampler import preprocess as prep
from det3d.builder import build_dbsampler
from det3d.core.input.voxel_generator import VoxelGenerator
from det3d.core.utils.center_utils import (
draw_umich_gaussian, gaussian_radius
)
from ..registry import PIPELINES
def _dict_select(dict_, inds):
for k, v in dict_.items():
if isinstance(v, dict):
_dict_select(v, inds)
else:
dict_[k] = v[inds]
def drop_arrays_by_name(gt_names, used_classes):
inds = [i for i, x in enumerate(gt_names) if x not in used_classes]
inds = np.array(inds, dtype=np.int64)
return inds
@PIPELINES.register_module
class Preprocess(object):
def __init__(self, cfg=None, **kwargs):
self.shuffle_points = cfg.shuffle_points
self.min_points_in_gt = cfg.get("min_points_in_gt", -1)
self.mode = cfg.mode
if self.mode == "train":
self.global_rotation_noise = cfg.global_rot_noise
self.global_scaling_noise = cfg.global_scale_noise
self.global_translate_std = cfg.get('global_translate_std', 0)
self.class_names = cfg.class_names
if cfg.db_sampler != None:
self.db_sampler = build_dbsampler(cfg.db_sampler)
else:
self.db_sampler = None
self.npoints = cfg.get("npoints", -1)
self.no_augmentation = cfg.get('no_augmentation', False)
def __call__(self, res, info):
res["mode"] = self.mode
if res["type"] in ["WaymoDataset"]:
if "combined" in res["lidar"]:
points = res["lidar"]["combined"]
else:
points = res["lidar"]["points"]
elif res["type"] in ["NuScenesDataset"]:
points = res["lidar"]["combined"]
else:
raise NotImplementedError
if self.mode == "train":
anno_dict = res["lidar"]["annotations"]
gt_dict = {
"gt_boxes": anno_dict["boxes"],
"gt_names": np.array(anno_dict["names"]).reshape(-1),
}
if self.mode == "train" and not self.no_augmentation:
selected = drop_arrays_by_name(
gt_dict["gt_names"], ["DontCare", "ignore", "UNKNOWN"]
)
_dict_select(gt_dict, selected)
if self.min_points_in_gt > 0:
point_counts = box_np_ops.points_count_rbbox(
points, gt_dict["gt_boxes"]
)
mask = point_counts >= min_points_in_gt
_dict_select(gt_dict, mask)
gt_boxes_mask = np.array(
[n in self.class_names for n in gt_dict["gt_names"]], dtype=np.bool_
)
if self.db_sampler:
sampled_dict = self.db_sampler.sample_all(
res["metadata"]["image_prefix"],
gt_dict["gt_boxes"],
gt_dict["gt_names"],
res["metadata"]["num_point_features"],
False,
gt_group_ids=None,
calib=None,
road_planes=None
)
if sampled_dict is not None:
sampled_gt_names = sampled_dict["gt_names"]
sampled_gt_boxes = sampled_dict["gt_boxes"]
sampled_points = sampled_dict["points"]
sampled_gt_masks = sampled_dict["gt_masks"]
gt_dict["gt_names"] = np.concatenate(
[gt_dict["gt_names"], sampled_gt_names], axis=0
)
gt_dict["gt_boxes"] = np.concatenate(
[gt_dict["gt_boxes"], sampled_gt_boxes]
)
gt_boxes_mask = np.concatenate(
[gt_boxes_mask, sampled_gt_masks], axis=0
)
points = np.concatenate([sampled_points, points], axis=0)
_dict_select(gt_dict, gt_boxes_mask)
gt_classes = np.array(
[self.class_names.index(n) + 1 for n in gt_dict["gt_names"]],
dtype=np.int32,
)
gt_dict["gt_classes"] = gt_classes
gt_dict["gt_boxes"], points = prep.random_flip_both(gt_dict["gt_boxes"], points)
gt_dict["gt_boxes"], points = prep.global_rotation(
gt_dict["gt_boxes"], points, rotation=self.global_rotation_noise
)
gt_dict["gt_boxes"], points = prep.global_scaling_v2(
gt_dict["gt_boxes"], points, *self.global_scaling_noise
)
gt_dict["gt_boxes"], points = prep.global_translate_(
gt_dict["gt_boxes"], points, noise_translate_std=self.global_translate_std
)
elif self.no_augmentation:
gt_boxes_mask = np.array(
[n in self.class_names for n in gt_dict["gt_names"]], dtype=np.bool_
)
_dict_select(gt_dict, gt_boxes_mask)
gt_classes = np.array(
[self.class_names.index(n) + 1 for n in gt_dict["gt_names"]],
dtype=np.int32,
)
gt_dict["gt_classes"] = gt_classes
if self.shuffle_points:
np.random.shuffle(points)
res["lidar"]["points"] = points
if self.mode == "train":
res["lidar"]["annotations"] = gt_dict
return res, info
@PIPELINES.register_module
class Voxelization(object):
def __init__(self, **kwargs):
cfg = kwargs.get("cfg", None)
self.range = cfg.range
self.voxel_size = cfg.voxel_size
self.max_points_in_voxel = cfg.max_points_in_voxel
self.max_voxel_num = [cfg.max_voxel_num, cfg.max_voxel_num] if isinstance(cfg.max_voxel_num, int) else cfg.max_voxel_num
self.double_flip = cfg.get('double_flip', False)
self.voxel_generator = VoxelGenerator(
voxel_size=self.voxel_size,
point_cloud_range=self.range,
max_num_points=self.max_points_in_voxel,
max_voxels=self.max_voxel_num[0],
)
def __call__(self, res, info):
voxel_size = self.voxel_generator.voxel_size
pc_range = self.voxel_generator.point_cloud_range
grid_size = self.voxel_generator.grid_size
if res["mode"] == "train":
gt_dict = res["lidar"]["annotations"]
bv_range = pc_range[[0, 1, 3, 4]]
mask = prep.filter_gt_box_outside_range(gt_dict["gt_boxes"], bv_range)
_dict_select(gt_dict, mask)
res["lidar"]["annotations"] = gt_dict
max_voxels = self.max_voxel_num[0]
else:
max_voxels = self.max_voxel_num[1]
voxels, coordinates, num_points = self.voxel_generator.generate(
res["lidar"]["points"], max_voxels=max_voxels
)
num_voxels = np.array([voxels.shape[0]], dtype=np.int64)
res["lidar"]["voxels"] = dict(
voxels=voxels,
coordinates=coordinates,
num_points=num_points,
num_voxels=num_voxels,
shape=grid_size,
range=pc_range,
size=voxel_size
)
double_flip = self.double_flip and (res["mode"] != 'train')
if double_flip:
flip_voxels, flip_coordinates, flip_num_points = self.voxel_generator.generate(
res["lidar"]["yflip_points"]
)
flip_num_voxels = np.array([flip_voxels.shape[0]], dtype=np.int64)
res["lidar"]["yflip_voxels"] = dict(
voxels=flip_voxels,
coordinates=flip_coordinates,
num_points=flip_num_points,
num_voxels=flip_num_voxels,
shape=grid_size,
range=pc_range,
size=voxel_size
)
flip_voxels, flip_coordinates, flip_num_points = self.voxel_generator.generate(
res["lidar"]["xflip_points"]
)
flip_num_voxels = np.array([flip_voxels.shape[0]], dtype=np.int64)
res["lidar"]["xflip_voxels"] = dict(
voxels=flip_voxels,
coordinates=flip_coordinates,
num_points=flip_num_points,
num_voxels=flip_num_voxels,
shape=grid_size,
range=pc_range,
size=voxel_size
)
flip_voxels, flip_coordinates, flip_num_points = self.voxel_generator.generate(
res["lidar"]["double_flip_points"]
)
flip_num_voxels = np.array([flip_voxels.shape[0]], dtype=np.int64)
res["lidar"]["double_flip_voxels"] = dict(
voxels=flip_voxels,
coordinates=flip_coordinates,
num_points=flip_num_points,
num_voxels=flip_num_voxels,
shape=grid_size,
range=pc_range,
size=voxel_size
)
return res, info
def flatten(box):
return np.concatenate(box, axis=0)
def merge_multi_group_label(gt_classes, num_classes_by_task):
num_task = len(gt_classes)
flag = 0
for i in range(num_task):
gt_classes[i] += flag
flag += num_classes_by_task[i]
return flatten(gt_classes)
@PIPELINES.register_module
class AssignLabel(object):
def __init__(self, **kwargs):
"""Return CenterNet training labels like heatmap, height, offset"""
assigner_cfg = kwargs["cfg"]
self.out_size_factor = assigner_cfg.out_size_factor
self.tasks = assigner_cfg.target_assigner.tasks
self.gaussian_overlap = assigner_cfg.gaussian_overlap
self._max_objs = assigner_cfg.max_objs
self._min_radius = assigner_cfg.min_radius
self.cfg = assigner_cfg
def __call__(self, res, info):
max_objs = self._max_objs
class_names_by_task = [t.class_names for t in self.tasks]
num_classes_by_task = [t.num_class for t in self.tasks]
example = {}
if res["mode"] == "train":
# Calculate output featuremap size
if 'voxels' in res['lidar']:
# Calculate output featuremap size
grid_size = res["lidar"]["voxels"]["shape"]
pc_range = res["lidar"]["voxels"]["range"]
voxel_size = res["lidar"]["voxels"]["size"]
feature_map_size = grid_size[:2] // self.out_size_factor
else:
pc_range = np.array(self.cfg['pc_range'], dtype=np.float32)
voxel_size = np.array(self.cfg['voxel_size'], dtype=np.float32)
grid_size = (pc_range[3:] - pc_range[:3]) / voxel_size
grid_size = np.round(grid_size).astype(np.int64)
feature_map_size = grid_size[:2] // self.out_size_factor
gt_dict = res["lidar"]["annotations"]
# reorganize the gt_dict by tasks
task_masks = []
flag = 0
for class_name in class_names_by_task:
task_masks.append(
[
np.where(
gt_dict["gt_classes"] == class_name.index(i) + 1 + flag
)
for i in class_name
]
)
flag += len(class_name)
task_boxes = []
task_classes = []
task_names = []
flag2 = 0
for idx, mask in enumerate(task_masks):
task_box = []
task_class = []
task_name = []
for m in mask:
task_box.append(gt_dict["gt_boxes"][m])
task_class.append(gt_dict["gt_classes"][m] - flag2)
task_name.append(gt_dict["gt_names"][m])
task_boxes.append(np.concatenate(task_box, axis=0))
task_classes.append(np.concatenate(task_class))
task_names.append(np.concatenate(task_name))
flag2 += len(mask)
for task_box in task_boxes:
# limit rad to [-pi, pi]
task_box[:, -1] = box_np_ops.limit_period(
task_box[:, -1], offset=0.5, period=np.pi * 2
)
# print(gt_dict.keys())
gt_dict["gt_classes"] = task_classes
gt_dict["gt_names"] = task_names
gt_dict["gt_boxes"] = task_boxes
res["lidar"]["annotations"] = gt_dict
draw_gaussian = draw_umich_gaussian
hms, anno_boxs, inds, masks, cats = [], [], [], [], []
for idx, task in enumerate(self.tasks):
hm = np.zeros((len(class_names_by_task[idx]), feature_map_size[1], feature_map_size[0]),
dtype=np.float32)
if res['type'] == 'NuScenesDataset':
# [reg, hei, dim, vx, vy, rots, rotc]
anno_box = np.zeros((max_objs, 10), dtype=np.float32)
elif res['type'] == 'WaymoDataset':
anno_box = np.zeros((max_objs, 10), dtype=np.float32)
else:
raise NotImplementedError("Only Support nuScene for Now!")
ind = np.zeros((max_objs), dtype=np.int64)
mask = np.zeros((max_objs), dtype=np.uint8)
cat = np.zeros((max_objs), dtype=np.int64)
num_objs = min(gt_dict['gt_boxes'][idx].shape[0], max_objs)
for k in range(num_objs):
cls_id = gt_dict['gt_classes'][idx][k] - 1
w, l, h = gt_dict['gt_boxes'][idx][k][3], gt_dict['gt_boxes'][idx][k][4], \
gt_dict['gt_boxes'][idx][k][5]
w, l = w / voxel_size[0] / self.out_size_factor, l / voxel_size[1] / self.out_size_factor
if w > 0 and l > 0:
radius = gaussian_radius((l, w), min_overlap=self.gaussian_overlap)
radius = max(self._min_radius, int(radius))
# be really careful for the coordinate system of your box annotation.
x, y, z = gt_dict['gt_boxes'][idx][k][0], gt_dict['gt_boxes'][idx][k][1], \
gt_dict['gt_boxes'][idx][k][2]
coor_x, coor_y = (x - pc_range[0]) / voxel_size[0] / self.out_size_factor, \
(y - pc_range[1]) / voxel_size[1] / self.out_size_factor
ct = np.array(
[coor_x, coor_y], dtype=np.float32)
ct_int = ct.astype(np.int32)
# throw out not in range objects to avoid out of array area when creating the heatmap
if not (0 <= ct_int[0] < feature_map_size[0] and 0 <= ct_int[1] < feature_map_size[1]):
continue
draw_gaussian(hm[cls_id], ct, radius)
new_idx = k
x, y = ct_int[0], ct_int[1]
cat[new_idx] = cls_id
ind[new_idx] = y * feature_map_size[0] + x
mask[new_idx] = 1
if res['type'] == 'NuScenesDataset':
vx, vy = gt_dict['gt_boxes'][idx][k][6:8]
rot = gt_dict['gt_boxes'][idx][k][8]
anno_box[new_idx] = np.concatenate(
(ct - (x, y), z, np.log(gt_dict['gt_boxes'][idx][k][3:6]),
np.array(vx), np.array(vy), np.sin(rot), np.cos(rot)), axis=None)
elif res['type'] == 'WaymoDataset':
vx, vy = gt_dict['gt_boxes'][idx][k][6:8]
rot = gt_dict['gt_boxes'][idx][k][-1]
anno_box[new_idx] = np.concatenate(
(ct - (x, y), z, np.log(gt_dict['gt_boxes'][idx][k][3:6]),
np.array(vx), np.array(vy), np.sin(rot), np.cos(rot)), axis=None)
else:
raise NotImplementedError("Only Support Waymo and nuScene for Now")
hms.append(hm)
anno_boxs.append(anno_box)
masks.append(mask)
inds.append(ind)
cats.append(cat)
# used for two stage code
boxes = flatten(gt_dict['gt_boxes'])
classes = merge_multi_group_label(gt_dict['gt_classes'], num_classes_by_task)
if res["type"] == "NuScenesDataset":
gt_boxes_and_cls = np.zeros((max_objs, 10), dtype=np.float32)
elif res['type'] == "WaymoDataset":
gt_boxes_and_cls = np.zeros((max_objs, 10), dtype=np.float32)
else:
raise NotImplementedError()
boxes_and_cls = np.concatenate((boxes,
classes.reshape(-1, 1).astype(np.float32)), axis=1)
num_obj = len(boxes_and_cls)
assert num_obj <= max_objs
# x, y, z, w, l, h, rotation_y, velocity_x, velocity_y, class_name
boxes_and_cls = boxes_and_cls[:, [0, 1, 2, 3, 4, 5, 8, 6, 7, 9]]
gt_boxes_and_cls[:num_obj] = boxes_and_cls
example.update({'gt_boxes_and_cls': gt_boxes_and_cls})
example.update({'hm': hms, 'anno_box': anno_boxs, 'ind': inds, 'mask': masks, 'cat': cats})
else:
pass
res["lidar"]["targets"] = example
return res, info