forked from tyagi-iiitv/PointPillars
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mtr_processors_v1.py
379 lines (285 loc) · 15.4 KB
/
mtr_processors_v1.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
from typing import List, Any
import numpy as np
import tensorflow as tf
from tensorflow.python.keras.utils.data_utils import Sequence
from config_mtr_v1 import Parameters
# from point_pillars import createPillars, createPillarsTarget
from point_pillars_v2 import createPillars, createPillarsTarget
# from readers import DataReader, Label3D
from sklearn.utils import shuffle
import sys
from det3d.mtr_dataset import MTRDatasetBase
from det3d.mtr_dataset.utils import mtr_utils
# from point_viz.converter import PointvizConverter
from datetime import datetime
def limit_period(val, offset=0.5, period=np.pi):
return val - np.floor(val / period + offset) * period
def select_best_anchors(arr):
dims = np.indices(arr.shape[1:])
# arr[..., 0:1] gets the occupancy value from occ in {-1, 0, 1}, i.e. {bad match, neg box, pos box}
ind = (np.argmax(arr[..., 0:1], axis=0),) + tuple(dims)
return arr[ind]
class DataProcessor(Parameters):
def __init__(self, **kwargs):
super(DataProcessor, self).__init__(**kwargs)
anchor_dims = np.array(self.anchor_dims, dtype=np.float32)
self.anchor_dims = anchor_dims[:, 0:3]
self.anchor_z = anchor_dims[:, 3]
self.anchor_yaw = anchor_dims[:, 4]
# Counts may be used to make statistic about how well the anchor boxes fit the objects
self.pos_cnt, self.neg_cnt = 0, 0
def make_point_pillars(self, points: np.ndarray):
assert points.ndim == 2
assert points.shape[1] == 4
assert points.dtype == np.float32
# start=datetime.now()
pillars, indices = createPillars(points,
self.max_points_per_pillar,
self.max_pillars,
self.x_step,
self.y_step,
self.x_min,
self.x_max,
self.y_min,
self.y_max,
self.z_min,
self.z_max,
False)
# print("Create pillar takes : ", datetime.now()-start)
return pillars, indices
def make_ground_truth(self, gt_boxes_3d: Any, gt_cls_type_list: List[str]):
""" Generate the ground truth label for each pillars
Args:
gt_boxes_3d (numpy[float]): A list of floats containing [x, y, z, h, w, l, ry]
gt_cls_type_list (List[str]): A list of floats containing [cls_type]
Returns:
[type]: [description]
"""
# filter labels by classes (cars, pedestrians and Trams)
# Label has 4 properties (Classification (0th index of labels file),
# centroid coordinates, dimensions, yaw)
if len(gt_boxes_3d) == 0:
pX, pY = int(self.Xn / self.downscaling_factor), int(self.Yn / self.downscaling_factor)
a = int(self.anchor_dims.shape[0])
return np.zeros((pX, pY, a), dtype='float32'), np.zeros((pX, pY, a, self.nb_dims), dtype='float32'), \
np.zeros((pX, pY, a, self.nb_dims), dtype='float32'), np.zeros((pX, pY, a), dtype='float32'), \
np.zeros((pX, pY, a), dtype='float32'), np.zeros((pX, pY, a, self.nb_classes), dtype='float64')
# For each label file, generate these properties except for the Don't care class
target_positions = gt_boxes_3d[:,:3]
target_dimension = gt_boxes_3d[:,3:6] # don't have to translate again
target_yaw = gt_boxes_3d[:, 6]
# print(type(self.classes))
# print(type(self.classes_map))
# # print(gt_cls_type_list[0])
# print(self.classes_map[gt_cls_type_list[0]])
target_class = np.array([self.classes_map[gt_cls_type_list[k]] for k in range(len(gt_cls_type_list))], dtype=np.int32)
assert np.all(target_yaw >= -np.pi) & np.all(target_yaw <= np.pi)
assert len(target_positions) == len(target_dimension) == len(target_yaw) == len(target_class)
# start=datetime.now()
target, pos, neg = createPillarsTarget(target_positions,
target_dimension,
target_yaw,
target_class,
self.anchor_dims,
self.anchor_z,
self.anchor_yaw,
self.positive_iou_threshold,
self.negative_iou_threshold,
self.nb_classes,
self.downscaling_factor,
self.x_step,
self.y_step,
self.x_min,
self.x_max,
self.y_min,
self.y_max,
self.z_min,
self.z_max,
False)
# print("Create target takes : ", datetime.now()-start)
self.pos_cnt += pos
self.neg_cnt += neg
# return a merged target view for all objects in the ground truth and get categorical labels
sel = select_best_anchors(target)
ohe = tf.keras.utils.to_categorical(sel[..., 9], num_classes=self.nb_classes, dtype='float64')
# print("self.shape: ", sel[...,0].shape)
return sel[..., 0], sel[..., 1:4], sel[..., 4:7], sel[..., 7], sel[..., 8], ohe
class CustomDataGenerator(DataProcessor, Sequence, MTRDatasetBase):
""" Multiprocessing-safe data generator for training, validation or testing, without fancy augmentation """
def __init__(self, batch_size: int, root_dir:str, point_cloud_statistics_path: str,
npoints:int =16384, split: str ='train',
classes:List[str] =['Car', 'Pedestrian', 'Person_sitting'], random_select:bool =True,
gt_database_dir=None, aug_hard_ratio:float=0.5, **kwargs):
super(CustomDataGenerator, self).__init__(
root_dir = root_dir,
split = split,
point_cloud_statistics_path = point_cloud_statistics_path,
**kwargs
# batch_size=batch_size, root_dir=root_dir,
# npoints=npoints, split=split, classes=classes,
# random_select=random_select, gt_database_dir=gt_database_dir,
# aug_hard_ratio=aug_hard_ratio, **kwargs
)
self.batch_size = batch_size
def get_sample(self, index):
return super().get_sample(index)
def __len__(self):
return len(self.sample_list) // self.batch_size
def __getitem__(self, batch_id: int):
file_ids = range(batch_id * self.batch_size, self.batch_size * (batch_id + 1))
pillars = []
voxels = []
occupancy = []
position = []
size = []
angle = []
heading = []
classification = []
for i in file_ids:
point_cloud = self.get_lidar_without_background(i)
pts_features = point_cloud[:, 3:]
pts_input = np.concatenate([point_cloud[:,:3], pts_features[:,1,np.newaxis]], axis=1)
# Voxels are the pillar ids
pillars_, voxels_ = self.make_point_pillars(pts_input)
pillars.append(pillars_)
voxels.append(voxels_)
obj_list = self.get_label(i) # are labels
gt_boxes3d = np.zeros((obj_list.__len__(), 7), dtype=np.float32)
gt_bbox_params_list = []
for k, obj in enumerate(obj_list):
gt_boxes3d[k, 0:3], gt_boxes3d[k, 3], gt_boxes3d[k, 4], gt_boxes3d[k, 5], gt_boxes3d[k, 6] \
= obj.pos, obj.h, obj.w, obj.l, limit_period(obj.ry, offset=0.5, period=2*np.pi) # mtr format
# = obj.pos, obj.h, obj.w, obj.l, obj.ry # kitti
# print(bboxes3d_[:,:3].shape)
invalid_region_mask = self._get_invalid_region_mask(gt_boxes3d[:,:3])
gt_boxes3d = gt_boxes3d[~invalid_region_mask,:]
gt_boxes3d = np.concatenate((
gt_boxes3d[:,0,np.newaxis], # 0 x
gt_boxes3d[:,1,np.newaxis], # 1 y
gt_boxes3d[:,2,np.newaxis], # 2 z
gt_boxes3d[:,5,np.newaxis], # 3 l # same as the original label
gt_boxes3d[:,4,np.newaxis], # 4 w # same as the original label
gt_boxes3d[:,3,np.newaxis], # 5 h # same as the original label
gt_boxes3d[:,6,np.newaxis], # 6 ry
), axis=1)
if self.split=='train' or self.split =='test':
occupancy_, position_, size_, angle_, heading_, classification_ = self.make_ground_truth(
gt_boxes3d, ['pedestrian' for i in range(len(gt_boxes3d))])
occupancy.append(occupancy_)
position.append(position_)
size.append(size_)
angle.append(angle_)
heading.append(heading_)
classification.append(classification_)
pillars = np.concatenate(pillars, axis=0)
voxels = np.concatenate(voxels, axis=0)
if self.split=='train' or self.split =='test':
occupancy = np.array(occupancy)
position = np.array(position)
size = np.array(size)
angle = np.array(angle)
heading = np.array(heading)
classification = np.array(classification)
# return [pillars, voxels], [occupancy, position, size, angle, heading, classification] # network
return [pillars, voxels], [occupancy, position, size, angle, heading] # network_v2
else:
return [pillars, voxels]
def on_epoch_end(self):
if self.split=='train' or self.split =='test':
self.sample_list=shuffle(self.sample_list)
class AnalyseCustomDataGenerator(CustomDataGenerator):
""" Multiprocessing-safe data generator for training, validation or testing, without fancy augmentation """
def __init__(self, batch_size: int, root_dir:str, point_cloud_statistics_path: str,
npoints:int =16384, split: str ='train',
classes:List[str] =['Car', 'Pedestrian', 'Person_sitting'], random_select:bool =True,
gt_database_dir=None, aug_hard_ratio:float=0.5, **kwargs):
super(AnalyseCustomDataGenerator, self).__init__(
batch_size=batch_size, root_dir=root_dir,
point_cloud_statistics_path = point_cloud_statistics_path,
npoints=npoints, split=split, classes=classes,
random_select=random_select, gt_database_dir=gt_database_dir,
aug_hard_ratio=aug_hard_ratio, **kwargs
)
self.batch_size = batch_size
def get_sample(self, index):
return super().get_sample(index)
def __len__(self):
return len(self.sample_list) // self.batch_size
def __getitem__(self, batch_id: int):
file_ids = range(batch_id * self.batch_size, self.batch_size * (batch_id + 1))
pillars = []
voxels = []
occupancy = []
position = []
size = []
angle = []
heading = []
classification = []
pts_input_ = []
gt_boxes3d_ = []
for i in file_ids:
point_cloud = self.get_lidar_without_background(i)
pts_features = point_cloud[:, 3:]
pts_input = np.concatenate([point_cloud[:,:3], pts_features[:,1,np.newaxis]], axis=1)
# Voxels are the pillar ids
pillars_, voxels_ = self.make_point_pillars(pts_input)
pillars.append(pillars_)
voxels.append(voxels_)
if self.split=='train' or self.split =='test':
obj_list = self.get_label(i) # are labels
gt_boxes3d = np.zeros((obj_list.__len__(), 7), dtype=np.float32)
# gt_bbox_params_list = []
for k, obj in enumerate(obj_list):
gt_boxes3d[k, 0:3], gt_boxes3d[k, 3], gt_boxes3d[k, 4], gt_boxes3d[k, 5], gt_boxes3d[k, 6] \
= obj.pos, obj.h, obj.w, obj.l, limit_period(obj.ry, offset=0.5, period=2*np.pi) # mtr format
# = obj.pos, obj.h, obj.w, obj.l, obj.ry # kitti
# print(bboxes3d_[:,:3].shape)
invalid_region_mask = self._get_invalid_region_mask(gt_boxes3d[:,:3])
gt_boxes3d = gt_boxes3d[~invalid_region_mask,:]
# for k in range(len(gt_boxes3d)):
# gt_bbox_params = [gt_boxes3d[k, 5], gt_boxes3d[k, 3], gt_boxes3d[k, 4],
# gt_boxes3d[k, 1], gt_boxes3d[k, 2], gt_boxes3d[k, 0],
# gt_boxes3d[k, 6]]
# gt_bbox_params_list.append(gt_bbox_params)
# if gt_boxes3d.__len__() == 0:
# print('No gt object')
# continue
gt_boxes3d = np.concatenate((
gt_boxes3d[:,0,np.newaxis], # 0 x
gt_boxes3d[:,1,np.newaxis], # 1 y
gt_boxes3d[:,2,np.newaxis], # 2 z
gt_boxes3d[:,5,np.newaxis], # 3 l # same as the original label
gt_boxes3d[:,4,np.newaxis], # 4 w # same as the original label
gt_boxes3d[:,3,np.newaxis], # 5 h # same as the original label
gt_boxes3d[:,6,np.newaxis], # 6 ry
), axis=1)
occupancy_, position_, size_, angle_, heading_, classification_ = self.make_ground_truth(
gt_boxes3d, ['pedestrian' for i in range(len(gt_boxes3d))])
occupancy.append(occupancy_)
position.append(position_)
size.append(size_)
angle.append(angle_)
heading.append(heading_)
classification.append(classification_)
gt_boxes3d_.append(gt_boxes3d)
pts_input_.append(pts_input)
elif self.split=='real':
pts_input_.append(pts_input)
pillars = np.concatenate(pillars, axis=0)
voxels = np.concatenate(voxels, axis=0)
if self.split=='train' or self.split =='test':
occupancy = np.array(occupancy)
position = np.array(position)
size = np.array(size)
angle = np.array(angle)
heading = np.array(heading)
classification = np.array(classification)
# return [pillars, voxels], [occupancy, position, size, angle, heading, classification] # network
return [pillars, voxels], [occupancy, position, size, angle, heading], [pts_input_, gt_boxes3d_] # network_v2
elif self.split=='real':
return [pillars, voxels], [pts_input_]
else:
return [pillars, voxels]
def on_epoch_end(self):
if self.split=='train' or self.split =='test':
self.sample_list=shuffle(self.sample_list)