-
Notifications
You must be signed in to change notification settings - Fork 3
/
datagen.py
349 lines (279 loc) · 11.7 KB
/
datagen.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
'''
Load pointcloud/labels from the KITTI dataset folder
'''
import os.path
import numpy as np
import time
import torch
import ctypes
from utils import get_points_in_a_rotated_box, trasform_label2metric
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import struct
KITTI_PATH = '../../lidar_avai_proj/KITTI' #replace with your path to KITTI folder
class KITTI(Dataset):
geometry = {
'L1': -40.0,
'L2': 40.0,
'W1': 0.0,
'W2': 70.0,
'H1': -2.5,
'H2': 1.0,
'input_shape': (800, 700, 36),
'label_shape': (200, 175, 7)
}
target_mean = np.array([0.008, 0.001, 0.202, 0.2, 0.43, 1.368])
target_std_dev = np.array([0.866, 0.5, 0.954, 0.668, 0.09, 0.111])
def __init__(self, frame_range = 10000, use_npy=False, train=True):
self.frame_range = frame_range
self.velo = []
self.use_npy = use_npy
self.image_sets = self.load_imageset(train) # names
def __len__(self):
return len(self.image_sets)
def __getitem__(self, item):
raw_lidar_points = self.load_velo_scan(item)
#scan = torch.from_numpy(scan)
label_map, _ = self.get_label(item)
self.reg_target_transform(label_map)
label_map = torch.from_numpy(label_map)
#scan = scan.permute(2, 0, 1)
label_map = label_map.permute(2, 0, 1)
# raw_lidar_points, label_map, reflect_data
return raw_lidar_points, label_map
def reg_target_transform(self, label_map):
'''
Inputs are numpy arrays (not tensors!)
:param label_map: [200 * 175 * 7] label tensor
:return: normalized regression map for all non_zero classification locations
'''
cls_map = label_map[..., 0]
reg_map = label_map[..., 1:]
index = np.nonzero(cls_map)
reg_map[index] = (reg_map[index] - self.target_mean)/self.target_std_dev
def load_imageset(self, train):
path = KITTI_PATH
if train:
path = os.path.join(path, "train.txt")
else:
path = os.path.join(path, "val.txt")
with open(path, 'r') as f:
lines = f.readlines() # get rid of \n symbol
names = []
for line in lines[:-1]:
if int(line[:-1]) < self.frame_range:
names.append(line[:-1])
# Last line does not have a \n symbol
last = lines[-1][:6]
if int(last) < self.frame_range:
names.append(last)
# print(names[-1])
print("There are {} images in txt file".format(len(names)))
return names
def interpret_kitti_label(self, bbox):
w, h, l, y, z, x, yaw = bbox[8:15]
y = -y
yaw = - (yaw + np.pi / 2)
return x, y, w, l, yaw
def interpret_custom_label(self, bbox):
w, l, x, y, yaw = bbox
return x, y, w, l, yaw
def get_corners(self, bbox):
w, h, l, y, z, x, yaw = bbox[8:15]
y = -y
# manually take a negative s. t. it's a right-hand system, with
# x facing in the front windshield of the car
# z facing up
# y facing to the left of driver
yaw = -(yaw + np.pi / 2)
#x, y, w, l, yaw = self.interpret_kitti_label(bbox)
bev_corners = np.zeros((4, 2), dtype=np.float32)
# rear left
bev_corners[0, 0] = x - l/2 * np.cos(yaw) - w/2 * np.sin(yaw)
bev_corners[0, 1] = y - l/2 * np.sin(yaw) + w/2 * np.cos(yaw)
# rear right
bev_corners[1, 0] = x - l/2 * np.cos(yaw) + w/2 * np.sin(yaw)
bev_corners[1, 1] = y - l/2 * np.sin(yaw) - w/2 * np.cos(yaw)
# front right
bev_corners[2, 0] = x + l/2 * np.cos(yaw) + w/2 * np.sin(yaw)
bev_corners[2, 1] = y + l/2 * np.sin(yaw) - w/2 * np.cos(yaw)
# front left
bev_corners[3, 0] = x + l/2 * np.cos(yaw) - w/2 * np.sin(yaw)
bev_corners[3, 1] = y + l/2 * np.sin(yaw) + w/2 * np.cos(yaw)
reg_target = [np.cos(yaw), np.sin(yaw), x, y, w, l]
return bev_corners, reg_target
def update_label_map(self, map, bev_corners, reg_target):
label_corners = (bev_corners / 4 ) / 0.1
label_corners[:, 1] += self.geometry['label_shape'][0] / 2
points = get_points_in_a_rotated_box(label_corners, self.geometry['label_shape'])
for p in points:
label_x = p[0]
label_y = p[1]
metric_x, metric_y = trasform_label2metric(np.array(p))
actual_reg_target = np.copy(reg_target)
actual_reg_target[2] = reg_target[2] - metric_x
actual_reg_target[3] = reg_target[3] - metric_y
actual_reg_target[4] = np.log(reg_target[4])
actual_reg_target[5] = np.log(reg_target[5])
map[label_y, label_x, 0] = 1.0
map[label_y, label_x, 1:7] = actual_reg_target
def get_label(self, index):
'''
:param i: the ith velodyne scan in the train/val set
:return: label map: <--- This is the learning target
a tensor of shape 800 * 700 * 7 representing the expected output
label_list: <--- Intended for evaluation metrics & visualization
a list of length n; n = number of cars + (truck+van+tram+dontcare) in the frame
each entry is another list, where the first element of this list indicates if the object
is a car or one of the 'dontcare' (truck,van,etc) object
'''
index = self.image_sets[index]
f_name = (6-len(index)) * '0' + index + '.txt'
label_path = os.path.join(KITTI_PATH, 'training', 'label_2', f_name)
object_list = {'Car': 1, 'Truck':0, 'DontCare':0, 'Van':0, 'Tram':0}
label_map = np.zeros(self.geometry['label_shape'], dtype=np.float32)
label_list = []
with open(label_path, 'r') as f:
lines = f.readlines() # get rid of \n symbol
for line in lines:
bbox = []
entry = line.split(' ')
name = entry[0]
if name in list(object_list.keys()):
bbox.append(object_list[name])
bbox.extend([float(e) for e in entry[1:]])
if name == 'Car':
corners, reg_target = self.get_corners(bbox)
self.update_label_map(label_map, corners, reg_target)
label_list.append(corners)
return label_map, label_list
def get_rand_velo(self):
import random
rand_v = random.choice(self.velo)
print("A Velodyne Scan has shape ", rand_v.shape)
return random.choice(self.velo)
def load_velo_scan(self, item):
"""Helper method to parse velodyne binary files into a list of scans."""
filename = self.velo[item]
if self.use_npy:
scan = np.load(filename[:-4]+'.npy')
else:
c_name = bytes(filename, 'utf-8')
lidar_raw_points = createTopViewMaps(self.geometry['input_shape'], c_name)
return lidar_raw_points
def load_velo(self):
"""Load velodyne [x,y,z,reflectance] scan data from binary files."""
# Find all the Velodyne files
velo_files = []
for file in self.image_sets:
file = '{}.bin'.format(file)
velo_files.append(os.path.join(KITTI_PATH, 'training', 'velodyne', file))
print('Found ' + str(len(velo_files)) + ' Velodyne scans...')
# Read the Velodyne scans. Each point is [x,y,z,reflectance]
self.velo = velo_files
print('done.')
def point_in_roi(self, point):
if (point[0] - self.geometry['W1']) < 0.01 or (self.geometry['W2'] - point[0]) < 0.01:
return False
if (point[1] - self.geometry['L1']) < 0.01 or (self.geometry['L2'] - point[1]) < 0.01:
return False
if (point[2] - self.geometry['H1']) < 0.01 or (self.geometry['H2'] - point[2]) < 0.01:
return False
return True
def passthrough(self, velo):
geom = self.geometry
q = (geom['W1'] < velo[:, 0]) * (velo[:, 0] < geom['W2']) * \
(geom['L1'] < velo[:, 1]) * (velo[:, 1] < geom['L2']) * \
(geom['H1'] < velo[:, 2]) * (velo[:, 2] < geom['H2'])
indices = np.where(q)[0]
return velo[indices, :]
def lidar_preprocess(self, scan):
velo_processed = np.zeros(self.geometry['input_shape'], dtype=np.float32)
intensity_map_count = np.zeros((velo_processed.shape[0], velo_processed.shape[1]))
velo = self.passthrough(scan)
for i in range(velo.shape[0]):
x = int((velo[i, 1]-self.geometry['L1']) / 0.1)
y = int((velo[i, 0]-self.geometry['W1']) / 0.1)
z = int((velo[i, 2]-self.geometry['H1']) / 0.1)
velo_processed[x, y, z] = 1
velo_processed[x, y, -1] += velo[i, 3]
intensity_map_count[x, y] += 1
velo_processed[:, :, -1] = np.divide(velo_processed[:, :, -1], intensity_map_count,
where=intensity_map_count != 0)
return velo_processed
def createTopViewMaps(shape, path):
'''
load lidar from binary file
'''
x_MIN = 0.0
x_MAX = 70.0
y_MIN =-40.0
y_MAX = 40.0
z_MIN = -2.5
z_MAX = 1
num = 1000000
float_size = 4
data = torch.zeros(num, dtype=torch.float32)
px = 0
py = 4
pz = 8
pr = 12
with open(path, 'rb') as fp:
data = fp.read()
if len(data)/4>num:
num = int(num/4)
else:
num = int(len(data)/16)
lidar_raw_data = np.zeros([num,4], dtype=np.float32)
for i in range(0, num):
x = struct.unpack('f', data[px:px+4])[0]
y = struct.unpack('f', data[py:py+4])[0]
z = struct.unpack('f', data[pz:pz+4])[0]
refl = struct.unpack('f', data[pr:pr+4])[0]
if (x > x_MIN and y > y_MIN and z >z_MIN and x < x_MAX and y < y_MAX and z < z_MAX):
lidar_raw_data[i][0] = x
lidar_raw_data[i][1] = y
lidar_raw_data[i][2] = z
lidar_raw_data[i][3] = refl
px = px+16
py = py+16
pz = pz+16
pr = pr+16
return lidar_raw_data
def get_data_loader(batch_size, use_npy, geometry=None, frame_range=10000):
train_dataset = KITTI(frame_range, use_npy=use_npy, train=True)
if geometry is not None:
train_dataset.geometry = geometry
train_dataset.load_velo()
train_data_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size, num_workers=3)
val_dataset = KITTI(frame_range, use_npy=use_npy, train=False)
if geometry is not None:
val_dataset.geometry = geometry
val_dataset.load_velo()
val_data_loader = DataLoader(val_dataset, shuffle=False, batch_size=batch_size * 4, num_workers=8)
print("------------------------------------------------------------------")
return train_data_loader, val_data_loader
def find_reg_target_var_and_mean():
k = KITTI()
reg_targets = [[] for _ in range(6)]
for i in range(len(k)):
label_map, _ = k.get_label(i)
car_locs = np.where(label_map[:, :, 0] == 1)
for j in range(1, 7):
map = label_map[:, :, j]
reg_targets[j-1].extend(list(map[car_locs]))
reg_targets = np.array(reg_targets)
means = reg_targets.mean(axis=1)
stds = reg_targets.std(axis=1)
np.set_printoptions(precision=3, suppress=True)
return means, stds
def preprocess_to_npy(train=True):
k = KITTI(train=train)
k.load_velo()
for item, name in enumerate(k.velo):
scan = k.load_velo_scan(item)
scan = k.lidar_preprocess(scan)
path = name[:-4] + '.npy'
np.save(path, scan)
print('Saved ', path)
return