forked from AI-liu/Complex-YOLO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
414 lines (308 loc) · 12.5 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
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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import numpy as np
import cv2
import math
# classes
object_list = {'Car':0, 'Van':1, 'Truck':2, 'Pedestrian':3, 'Person_sitting':4, 'Cyclist':5, 'Tram':6}
class_list = ['Car', 'Van' , 'Truck' , 'Pedestrian' , 'Person_sitting' , 'Cyclist' , 'Tram' ]
bc={}
bc['minX'] = 0; bc['maxX'] = 80; bc['minY'] = -40; bc['maxY'] = 40
bc['minZ'] =-2; bc['maxZ'] = 1.25
def interpret_kitti_label(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 get_target2(label_file):
target = np.zeros([50, 7], dtype=np.float32)
with open(label_file, 'r') as f:
lines = f.readlines()
num_obj = len(lines)
index = 0
for j in range(num_obj):
obj = lines[j].strip().split(' ')
obj_class = obj[0].strip()
if obj_class in class_list:
bbox = []
bbox.append(object_list[obj_class])
bbox.extend([float(e) for e in obj[1:]])
x, y, w, l, yaw = interpret_kitti_label(bbox)
location_x = x
location_y = y
if (location_x > 0) & (location_x < 40) & (location_y > -40) & (location_y < 40):
target[index][1] = (y + 40) / 80 # we should put this in [0,1], so divide max_size 80 m
target[index][2] = x / 40 # make sure target inside the covering area (0,1)
target[index][3] = float(l) / 80
target[index][4] = float(w) / 40 # get target width, length
target[index][5] = math.sin(float(yaw)) # complex YOLO Im
target[index][6] = math.cos(float(yaw)) # complex YOLO Re
for i in range(len(class_list)):
if obj_class == class_list[i]: # get target class
target[index][0] = i
index = index + 1
return target
def removePoints(PointCloud, BoundaryCond):
# Boundary condition
minX = BoundaryCond['minX'] ; maxX = BoundaryCond['maxX']
minY = BoundaryCond['minY'] ; maxY = BoundaryCond['maxY']
minZ = BoundaryCond['minZ'] ; maxZ = BoundaryCond['maxZ']
# Remove the point out of range x,y,z
mask = np.where((PointCloud[:, 0] >= minX) & (PointCloud[:, 0]<=maxX) & (PointCloud[:, 1] >= minY) & (PointCloud[:, 1]<=maxY) & (PointCloud[:, 2] >= minZ) & (PointCloud[:, 2]<=maxZ))
PointCloud = PointCloud[mask]
PointCloud[:,2] = PointCloud[:,2]+2
return PointCloud
def makeBVFeature(PointCloud_, BoundaryCond, Discretization):
# 1024 x 1024 x 3
Height = 1024+1
Width = 1024+1
# Discretize Feature Map
PointCloud = np.copy(PointCloud_)
PointCloud[:,0] = np.int_(np.floor(PointCloud[:,0] / Discretization))
PointCloud[:,1] = np.int_(np.floor(PointCloud[:,1] / Discretization) + Width/2)
# sort-3times
indices = np.lexsort((-PointCloud[:,2],PointCloud[:,1],PointCloud[:,0]))
PointCloud = PointCloud[indices]
# Height Map
heightMap = np.zeros((Height,Width))
_, indices = np.unique(PointCloud[:,0:2], axis=0, return_index=True)
PointCloud_frac = PointCloud[indices]
#some important problem is image coordinate is (y,x), not (x,y)
heightMap[np.int_(PointCloud_frac[:,0]), np.int_(PointCloud_frac[:,1])] = PointCloud_frac[:,2]
# Intensity Map & DensityMap
intensityMap = np.zeros((Height,Width))
densityMap = np.zeros((Height,Width))
_, indices, counts = np.unique(PointCloud[:,0:2], axis = 0, return_index=True,return_counts = True)
PointCloud_top = PointCloud[indices]
normalizedCounts = np.minimum(1.0, np.log(counts + 1)/np.log(64))
intensityMap[np.int_(PointCloud_top[:,0]), np.int_(PointCloud_top[:,1])] = PointCloud_top[:,3]
densityMap[np.int_(PointCloud_top[:,0]), np.int_(PointCloud_top[:,1])] = normalizedCounts
"""
plt.imshow(densityMap[:,:])
plt.pause(2)
plt.close()
plt.show()
plt.pause(2)
plt.close()
plt.show(block=False)
plt.pause(2)
plt.close()
plt.imshow(intensityMap[:,:])
plt.show(block=False)
plt.pause(2)
plt.close()
"""
RGB_Map = np.zeros((Height,Width,3))
RGB_Map[:,:,0] = densityMap # r_map
RGB_Map[:,:,1] = heightMap # g_map
RGB_Map[:,:,2] = intensityMap # b_map
save = np.zeros((512,1024,3))
save = RGB_Map[0:512,0:1024,:]
#misc.imsave('test_bv.png',save[::-1,::-1,:])
#misc.imsave('test_bv.png',save)
return save
def get_target(label_file,Tr):
target = np.zeros([50, 7], dtype=np.float32)
with open(label_file,'r') as f:
lines = f.readlines()
num_obj = len(lines)
index=0
for j in range(num_obj):
obj = lines[j].strip().split(' ')
obj_class = obj[0].strip()
#print(obj)
if obj_class in class_list:
t_lidar , box3d_corner = box3d_cam_to_velo(obj[8:], Tr) # get target 3D object location x,y
location_x = t_lidar[0][0]
location_y = t_lidar[0][1]
#print(t_lidar)
if (location_x>0) & (location_x<40) & (location_y>-40) & (location_y<40) :
#print(obj_class)
target[index][2] = t_lidar[0][0]/40 # make sure target inside the covering area (0,1)
target[index][1] = (t_lidar[0][1]+40)/80 ## we should put this in [0,1] ,so divide max_size 80 m
obj_width = obj[9].strip()
obj_length = obj[10].strip()
target[index][3]=float(obj_width)/80
target[index][4]=float(obj_length)/40 # get target width ,length
obj_alpha = obj[3].strip() # get target Observation angle of object, ranging [-pi..pi]
target[index][5]=math.sin(float(obj_alpha)) #complex YOLO Im
target[index][6]=math.cos(float(obj_alpha)) #complex YOLO Re
#print(np.arctan2(target[0][4],target[0][5]))
for i in range(len(class_list)):
if obj_class == class_list[i]: # get target class
target[index][0]=i
index=index+1
return target
def box3d_cam_to_velo(box3d, Tr):
def project_cam2velo(cam, Tr):
T = np.zeros([4, 4], dtype=np.float32)
T[:3, :] = Tr
T[3, 3] = 1
T_inv = np.linalg.inv(T)
lidar_loc_ = np.dot(T_inv, cam)
lidar_loc = lidar_loc_[:3]
return lidar_loc.reshape(1, 3)
def ry_to_rz(ry):
angle = -ry - np.pi / 2
if angle >= np.pi:
angle -= np.pi
if angle < -np.pi:
angle = 2*np.pi + angle
return angle
h,w,l,tx,ty,tz,ry = [float(i) for i in box3d]
cam = np.ones([4, 1])
cam[0] = tx
cam[1] = ty
cam[2] = tz
t_lidar = project_cam2velo(cam, Tr)
Box = np.array([[-l / 2, -l / 2, l / 2, l / 2, -l / 2, -l / 2, l / 2, l / 2],
[w / 2, -w / 2, -w / 2, w / 2, w / 2, -w / 2, -w / 2, w / 2],
[0, 0, 0, 0, h, h, h, h]])
rz = ry_to_rz(ry)
rotMat = np.array([
[np.cos(rz), -np.sin(rz), 0.0],
[np.sin(rz), np.cos(rz), 0.0],
[0.0, 0.0, 1.0]])
velo_box = np.dot(rotMat, Box)
cornerPosInVelo = velo_box + np.tile(t_lidar, (8, 1)).T
box3d_corner = cornerPosInVelo.transpose()
return t_lidar , box3d_corner.astype(np.float32)
def load_kitti_calib(calib_file):
"""
load projection matrix
"""
with open(calib_file) as fi:
lines = fi.readlines()
assert (len(lines) == 8)
obj = lines[0].strip().split(' ')[1:]
P0 = np.array(obj, dtype=np.float32)
obj = lines[1].strip().split(' ')[1:]
P1 = np.array(obj, dtype=np.float32)
obj = lines[2].strip().split(' ')[1:]
P2 = np.array(obj, dtype=np.float32)
obj = lines[3].strip().split(' ')[1:]
P3 = np.array(obj, dtype=np.float32)
obj = lines[4].strip().split(' ')[1:]
R0 = np.array(obj, dtype=np.float32)
obj = lines[5].strip().split(' ')[1:]
Tr_velo_to_cam = np.array(obj, dtype=np.float32)
obj = lines[6].strip().split(' ')[1:]
Tr_imu_to_velo = np.array(obj, dtype=np.float32)
return {'P2': P2.reshape(3, 4),
'R0': R0.reshape(3, 3),
'Tr_velo2cam': Tr_velo_to_cam.reshape(3, 4)}
# anchors = [[1.08,1.19], [3.42,4.41], [6.63,11.38], [9.42,5.11], [16.62,10.52]]
anchors = [[0.24,0.68], [0.27,0.33], [0.64,1.48], [0.70,1.82], [1.04,4.64]]
def bbox_iou(box1, box2, x1y1x2y2=True):
"""
Returns the IoU of two bounding boxes
"""
if not x1y1x2y2:
# Transform from center and width to exact coordinates
b1_x1, b1_x2 = box1[:, 0] - box1[:, 2] / 2, box1[:, 0] + box1[:, 2] / 2
b1_y1, b1_y2 = box1[:, 1] - box1[:, 3] / 2, box1[:, 1] + box1[:, 3] / 2
b2_x1, b2_x2 = box2[:, 0] - box2[:, 2] / 2, box2[:, 0] + box2[:, 2] / 2
b2_y1, b2_y2 = box2[:, 1] - box2[:, 3] / 2, box2[:, 1] + box2[:, 3] / 2
else:
# Get the coordinates of bounding boxes
b1_x1, b1_y1, b1_x2, b1_y2 = box1[:, 0], box1[:, 1], box1[:, 2], box1[:, 3]
b2_x1, b2_y1, b2_x2, b2_y2 = box2[:, 0], box2[:, 1], box2[:, 2], box2[:, 3]
# get the corrdinates of the intersection rectangle
inter_rect_x1 = torch.max(b1_x1, b2_x1)
inter_rect_y1 = torch.max(b1_y1, b2_y1)
inter_rect_x2 = torch.min(b1_x2, b2_x2)
inter_rect_y2 = torch.min(b1_y2, b2_y2)
# Intersection area
inter_area = torch.clamp(inter_rect_x2 - inter_rect_x1 + 1, min=0) * torch.clamp(
inter_rect_y2 - inter_rect_y1 + 1, min=0
)
# Union Area
b1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1)
b2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1)
iou = inter_area / (b1_area + b2_area - inter_area + 1e-16)
return iou
def bbox_iou1(box1, box2, x1y1x2y2=True):
if x1y1x2y2:
mx = min(box1[0], box2[0])
Mx = max(box1[2], box2[2])
my = min(box1[1], box2[1])
My = max(box1[3], box2[3])
w1 = box1[2] - box1[0]
h1 = box1[3] - box1[1]
w2 = box2[2] - box2[0]
h2 = box2[3] - box2[1]
else:
mx = min(box1[0]-box1[2]/2.0, box2[0]-box2[2]/2.0)
Mx = max(box1[0]+box1[2]/2.0, box2[0]+box2[2]/2.0)
my = min(box1[1]-box1[3]/2.0, box2[1]-box2[3]/2.0)
My = max(box1[1]+box1[3]/2.0, box2[1]+box2[3]/2.0)
w1 = box1[2]
h1 = box1[3]
w2 = box2[2]
h2 = box2[3]
uw = Mx - mx
uh = My - my
cw = w1 + w2 - uw
ch = h1 + h2 - uh
carea = 0
if cw <= 0 or ch <= 0:
return 0.0
area1 = w1 * h1
area2 = w2 * h2
carea = cw * ch
uarea = area1 + area2 - carea
return carea/uarea
def bbox_ious(boxes1, boxes2, x1y1x2y2=True):
if x1y1x2y2:
mx = torch.min(boxes1[0], boxes2[0])
Mx = torch.max(boxes1[2], boxes2[2])
my = torch.min(boxes1[1], boxes2[1])
My = torch.max(boxes1[3], boxes2[3])
w1 = boxes1[2] - boxes1[0]
h1 = boxes1[3] - boxes1[1]
w2 = boxes2[2] - boxes2[0]
h2 = boxes2[3] - boxes2[1]
else:
mx = torch.min(boxes1[0]-boxes1[2]/2.0, boxes2[0]-boxes2[2]/2.0)
Mx = torch.max(boxes1[0]+boxes1[2]/2.0, boxes2[0]+boxes2[2]/2.0)
my = torch.min(boxes1[1]-boxes1[3]/2.0, boxes2[1]-boxes2[3]/2.0)
My = torch.max(boxes1[1]+boxes1[3]/2.0, boxes2[1]+boxes2[3]/2.0)
w1 = boxes1[2]
h1 = boxes1[3]
w2 = boxes2[2]
h2 = boxes2[3]
uw = Mx - mx
uh = My - my
cw = w1 + w2 - uw
ch = h1 + h2 - uh
mask = ((cw <= 0) + (ch <= 0) > 0)
area1 = w1 * h1
area2 = w2 * h2
carea = cw * ch
carea[mask] = 0
uarea = area1 + area2 - carea
return carea/uarea
def nms(boxes, nms_thresh):
if len(boxes) == 0:
return boxes
det_confs = torch.zeros(len(boxes))
for i in range(len(boxes)):
det_confs[i] = 1-boxes[i][4]
_,sortIds = torch.sort(det_confs)
out_boxes = []
for i in range(len(boxes)):
box_i = boxes[sortIds[i]]
if box_i[4] > 0:
out_boxes.append(box_i)
for j in range(i+1, len(boxes)):
box_j = boxes[sortIds[j]]
if bbox_iou(box_i, box_j, x1y1x2y2=False) > nms_thresh:
#print(box_i, box_j, bbox_iou(box_i, box_j, x1y1x2y2=False))
box_j[4] = 0
return out_boxes
def convert2cpu(gpu_matrix):
return torch.FloatTensor(gpu_matrix.size()).copy_(gpu_matrix)
def convert2cpu_long(gpu_matrix):
return torch.LongTensor(gpu_matrix.size()).copy_(gpu_matrix)