-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkitti_generator.py
303 lines (268 loc) · 13.6 KB
/
kitti_generator.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
import logging
import multiprocessing
import time
# from point_viz.utils import get_color_from_intensity
from copy import deepcopy
from os.path import join
import mkl
import numpy as np
from numpy.linalg import multi_dot
from point_viz.converter import PointvizConverter
from tqdm import tqdm
from data.utils.augmentation import rotate, scale, flip, drop_out, shuffle, transform, \
get_pasted_point_cloud
from data.utils.normalization import feature_normalize, bboxes_normalization, convert_threejs_coors, \
convert_threejs_bbox, normalize_angle
import train.kitti.kitti_config as config
# os.environ['MKL_NUM_THREADS'] = '1'
mkl.set_num_threads(1)
default_config = {'nbbox': config.bbox_padding,
'rotate_range': 0.,
'rotate_mode': 'u',
'scale_range': 0.,
'scale_mode': 'u',
'drop_out': 0.,
'flip': False,
'shuffle': False,
'paste_augmentation': False,
'paste_instance_num': 0,
'maximum_interior_points': 80,
'normalization': None}
class Dataset(object):
def __init__(self,
task,
batch_size=16,
config=None,
queue_size=10,
validation=False,
evaluation=False,
hvd_id=0,
hvd_size=1,
num_worker=1,
home='/home/tan/tony/dv-det/dataset-half'):
self.home = home
self.config = default_config if config is None else config
self.batch_size = batch_size
self.evaluation = evaluation
self.validation = True if evaluation else validation
self.task = task
self.rotate_range = self.config['rotate_range']
self.rotate_mode = self.config['rotate_mode']
self.scale_range = self.config['scale_range']
self.scale_mode = self.config['scale_mode']
self.drop_out = self.config['drop_out']
self.flip = self.config['flip']
self.shuffle = self.config['shuffle']
self.normalization = self.config['normalization']
self.nbbox = self.config['nbbox']
self.points = np.load(join(self.home, 'lidar_points_{}.npy'.format(self.task)), allow_pickle=True)
if not self.evaluation:
self.bboxes = np.load(join(self.home, 'bbox_labels_{}.npy'.format(self.task)), allow_pickle=True)
if self.config['paste_augmentation'] and not self.validation:
self.object_collections = np.load(join(self.home, 'object_collections_{}.npy'.format(self.task)),
allow_pickle=True)
self.bbox_collections = np.load(join(self.home, 'bbox_collections_{}.npy'.format(self.task)),
allow_pickle=True)
self.ground_plane = np.load(join(self.home, 'ground_plane_{}.npy'.format(self.task)), allow_pickle=True)
self.trans_matrix = np.load(join(self.home, 'trans_matrix_{}.npy'.format(self.task)), allow_pickle=True)
self.diff_count = [len(self.object_collections[diff]) for diff in range(3)]
self.diff_ratio = [self.diff_count[diff] / np.sum(self.diff_count) for diff in range(3)]
# self.diff_ratio = self.diff_ratio / np.sum(self.diff_ratio)
self.paste_augmentation = self.config['paste_augmentation']
self.paste_instance_num = self.config['paste_instance_num']
self.maximum_interior_points = self.config['maximum_interior_points']
self.queue_size = queue_size
self.num_worker = num_worker
self.hvd_id = hvd_id
self.hvd_size = hvd_size
self.total_data_length = int(len(self.points) * 1.0)
self.hvd_data_length = self.total_data_length // self.hvd_size
self.batch_sum = int(np.ceil(self.hvd_data_length / self.batch_size))
self.test_start_id = self.hvd_data_length * self.hvd_id
self.idx = self.test_start_id
self.threads = []
self.q = multiprocessing.Queue(maxsize=self.queue_size)
if self.hvd_id == 0:
logging.info("===========Generator Configurations===========")
logging.info("{} instances were loaded for task {}".format(self.total_data_length, self.task))
logging.info(
"{} instances were allocated on {} horovod threads.".format(self.hvd_data_length, self.hvd_size))
if not self.validation:
self.start()
def start(self):
for i in range(self.num_worker):
thread = multiprocessing.Process(target=self.aug_process)
thread.daemon = True
self.threads.append(thread)
thread.start()
def stop(self):
for i, thread in enumerate(self.threads):
thread.terminate()
thread.join()
self.q.close()
def aug_process(self):
np.random.seed(int(time.time() * 1e3 - int(time.time()) * 1e3))
while True:
try:
if self.q.qsize() < self.queue_size:
batch_coors = []
batch_features = []
batch_num_list = []
batch_bbox = np.zeros((self.batch_size, self.nbbox, 9), dtype=np.float32)
for i in range(self.batch_size):
idx = np.random.randint(self.total_data_length)
points = deepcopy(self.points[idx])
bboxes = np.array(deepcopy(self.bboxes[idx]))
if len(bboxes) > 0:
bboxes = bboxes[bboxes[:, 0] > 0, :]
if self.paste_augmentation:
points, bboxes = get_pasted_point_cloud(scene_points=points,
scene_bboxes=bboxes,
# ratio=self.diff_ratio,
ground=np.array(deepcopy(self.ground_plane[idx])),
trans_list=np.array(deepcopy(self.trans_matrix[idx])),
object_collections=self.object_collections,
bbox_collections=self.bbox_collections,
instance_num=self.paste_instance_num,
maximum_interior_points=self.maximum_interior_points)
height_valid_idx = np.logical_and(points[:, 2] > -3, points[:, 2] < 1)
points = points[height_valid_idx, :]
# print(points.shape, bboxes.shape)
if self.shuffle:
points = shuffle(points)
if self.drop_out > 0:
points = drop_out(points, self.drop_out)
coors = points[:, :3]
features = points[:, -1:]
T_rotate, angle = rotate(self.rotate_range, self.rotate_mode)
T_scale, scale_xyz = scale(self.scale_range, self.scale_mode)
T_flip, flip_y = flip(flip=self.flip)
T_coors = multi_dot([T_scale, T_flip, T_rotate])
coors = transform(coors, T_coors)
# keep_idx = range_clip(coors, self.range_x, self.range_y, self.range_z)
# coors = coors[keep_idx, :]
# features = features[keep_idx, :]
features = feature_normalize(features, method=self.normalization)
ret_bboxes = []
for box in bboxes:
w, l, h, x, y, z, r = box[:7]
x, y, z = transform(np.array([x, y, z]), T_coors)
w, l, h = transform(np.array([w, l, h]), T_scale)
r += angle
if flip_y == -1:
r = (-1) ** int(r <= 0) * np.pi - r
r = normalize_angle(r)
# if np.abs(r) > 2 * np.pi:
# r = np.abs(r) % (2 * np.pi) * (-1) ** int(r <= 0)
# if np.abs(r) > np.pi:
# r = -(2 * np.pi - np.abs(r))
category = box[-2]
difficulty = box[-1]
ret_bboxes.append([w, l, h, x, y, z, r, category, difficulty])
batch_coors.append(coors)
batch_features.append(features)
batch_num_list.append(len(coors))
batch_bbox[i] = bboxes_normalization(ret_bboxes, length=self.nbbox)
batch_coors = np.concatenate(batch_coors, axis=0)
batch_features = np.concatenate(batch_features, axis=0)
batch_num_list = np.array(batch_num_list)
batch_bbox = np.array(batch_bbox)
self.q.put([batch_coors, batch_features, batch_num_list, batch_bbox])
# print(self.q.qsize())
else:
# print(self.q.qsize(), "Sleep for 0.05s.")
time.sleep(0.05)
except:
self.stop()
def train_generator(self):
while True:
if self.q.qsize() > 0:
yield self.q.get()
else:
time.sleep(0.05)
def valid_generator(self, start_idx=None):
if start_idx is not None:
self.idx = start_idx
while True:
stop_idx = int(np.min([self.idx + self.batch_size, self.hvd_data_length * (self.hvd_id + 1)]))
batch_size = stop_idx - self.idx
batch_coors = []
batch_features = []
batch_num_list = []
batch_bbox = np.zeros((batch_size, self.nbbox, 9), dtype=np.float32)
for i in range(batch_size):
points = deepcopy(self.points[self.idx])
coors = points[:, :3]
features = points[:, -1:]
if len(coors) == 0:
coors = np.array([[1., 0., 0.]]) # to keep the npoint always > 0 in a frame
features = np.array([[0.]])
# keep_idx = range_clip(coors, self.range_x, self.range_y, self.range_z)
# coors = coors[keep_idx, :]
# features = features[keep_idx, :]
batch_coors.append(coors)
batch_features.append(feature_normalize(features, method=self.normalization))
batch_num_list.append(len(coors))
if not self.evaluation:
bboxes = np.array(deepcopy(self.bboxes[self.idx]))
if len(bboxes) > 0:
bboxes = bboxes[bboxes[:, 0] > 0, :]
batch_bbox[i] = bboxes_normalization(bboxes, length=self.nbbox)
self.idx += 1
self.idx = self.test_start_id if stop_idx == self.hvd_data_length * (self.hvd_id + 1) else stop_idx
batch_coors = np.concatenate(batch_coors, axis=0)
batch_features = np.concatenate(batch_features, axis=0)
batch_num_list = np.array(batch_num_list)
batch_bbox = np.array(batch_bbox)
if self.evaluation:
yield batch_coors, batch_features, batch_num_list
else:
yield batch_coors, batch_features, batch_num_list, batch_bbox
if __name__ == '__main__':
aug_config = {'nbbox': 256,
'rotate_range': np.pi / 4,
'rotate_mode': 'u',
'scale_range': 0.05,
'scale_mode': 'u',
'drop_out': 0.1,
'flip': False,
'shuffle': True,
'paste_augmentation': True,
'paste_instance_num': 128,
'maximum_interior_points': 100,
'normalization': None}
dataset = Dataset(task='training',
config=aug_config,
batch_size=16,
validation=False,
num_worker=1,
hvd_size=3,
hvd_id=1)
generator = dataset.train_generator()
for i in tqdm(range(1)):
# dataset.aug_process()
coors, features, num_list, bboxes = next(generator)
# dimension = [100., 100., 9.]
# offset = [10., 10., 5.]
# # #
# coors += offset
# coors_min = np.min(coors, axis=0)
# coors_max = np.max(coors, axis=0)
# # print(coors_min, coors_max)
# for j in range(3):
# if coors_min[j] < 0 or coors_max[j] > dimension[j]:
# print(coors_min, coors_max)
# coors, ref, attention, bboxes = next(dataset.train_generator())
# dataset.stop()
batch_id = 6
acc_num_list = np.cumsum(num_list)
#
coors = coors[acc_num_list[batch_id-1]:acc_num_list[batch_id], :]
features = features[acc_num_list[batch_id-1]:acc_num_list[batch_id], 0]
bboxes = bboxes[batch_id]
Converter = PointvizConverter(home='/home/tan/tony/threejs')
Converter.compile(task_name="kitti_generator",
coors=convert_threejs_coors(coors),
intensity=features,
default_rgb=None,
bbox_params=convert_threejs_bbox(bboxes))