-
Notifications
You must be signed in to change notification settings - Fork 29
/
utils.py
202 lines (160 loc) · 6.24 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
import torch
import torch.nn
import cv2
import numpy as np
import matplotlib.pyplot as plt
import math
import json
import os.path
def trasform_label2metric(label, ratio=4, grid_size=0.1, base_height=100):
'''
:param label: numpy array of shape [..., 2] of coordinates in label map space
:return: numpy array of shape [..., 2] of the same coordinates in metric space
'''
metric = np.copy(label)
metric[..., 1] -= base_height
metric = metric * grid_size * ratio
return metric
def transform_metric2label(metric, ratio=4, grid_size=0.1, base_height=100):
'''
:param label: numpy array of shape [..., 2] of coordinates in metric space
:return: numpy array of shape [..., 2] of the same coordinates in label_map space
'''
label = (metric / ratio ) / grid_size
label[..., 1] += base_height
return label
def plot_bev(velo_array, label_list = None, map_height=800, window_name='GT'):
'''
Plot a Birds Eye View Lidar and Bounding boxes (Using OpenCV!)
The heading of the vehicle is marked as a red line
(which connects front right and front left corner)
:param velo_array: a 2d velodyne points
:param label_list: a list of numpy arrays of shape [4, 2], which corresponds to the 4 corners' (x, y)
The corners should be in the following sequence:
rear left, rear right, front right and front left
:param map_height: height of the map
:param window_name: name of the open_cv2 window
:return: None
'''
intensity = np.zeros((velo_array.shape[0], velo_array.shape[1], 3))
# val = 1 - velo_array[::-1, :, -1]
val = 1 - velo_array[::-1, :, :-1].max(axis=2)
intensity[:, :, 0] = val
intensity[:, :, 1] = val
intensity[:, :, 2] = val
# FLip in the x direction
if label_list is not None:
for corners in label_list:
plot_corners = corners / 0.1
plot_corners[:, 1] += int(map_height//2)
plot_corners[:, 1] = map_height - plot_corners[:, 1]
plot_corners = plot_corners.astype(int).reshape((-1, 1, 2))
cv2.polylines(intensity, [plot_corners], True, (255, 0, 0), 2)
cv2.line(intensity, tuple(plot_corners[2, 0]), tuple(plot_corners[3, 0]), (0, 0, 255), 3)
cv2.imshow(window_name, intensity)
def plot_label_map(label_map):
plt.figure()
plt.imshow(label_map[::-1, :])
plt.show()
def get_points_in_a_rotated_box(corners):
def minY(x0, y0, x1, y1, x):
if x0 == x1:
# vertical line, y0 is lowest
return int(math.floor(y0))
m = (y1 - y0) / (x1 - x0)
if m >= 0.0:
# lowest point is at left edge of pixel column
return int(math.floor(y0 + m * (x - x0)))
else:
# lowest point is at right edge of pixel column
return int(math.floor(y0 + m * ((x + 1.0) - x0)))
def maxY(x0, y0, x1, y1, x):
if x0 == x1:
# vertical line, y1 is highest
return int(math.ceil(y1))
m = (y1 - y0) / (x1 - x0)
if m >= 0.0:
# highest point is at right edge of pixel column
return int(math.ceil(y0 + m * ((x + 1.0) - x0)))
else:
# highest point is at left edge of pixel column
return int(math.ceil(y0 + m * (x - x0)))
# view_bl, view_tl, view_tr, view_br are the corners of the rectangle
view = [(corners[i, 0], corners[i, 1]) for i in range(4)]
pixels = []
# find l,r,t,b,m1,m2
l, m1, m2, r = sorted(view, key=lambda p: (p[0], p[1]))
b, t = sorted([m1, m2], key=lambda p: (p[1], p[0]))
lx, ly = l
rx, ry = r
bx, by = b
tx, ty = t
m1x, m1y = m1
m2x, m2y = m2
xmin = 0
ymin = 0
xmax = 175
ymax = 200
# inward-rounded integer bounds
# note that we're clamping the area of interest to (xmin,ymin)-(xmax,ymax)
lxi = max(int(math.ceil(lx)), xmin)
rxi = min(int(math.floor(rx)), xmax)
byi = max(int(math.ceil(by)), ymin)
tyi = min(int(math.floor(ty)), ymax)
x1 = lxi
x2 = rxi
for x in range(x1, x2):
xf = float(x)
if xf < m1x:
# Phase I: left to top and bottom
y1 = minY(lx, ly, bx, by, xf)
y2 = maxY(lx, ly, tx, ty, xf)
elif xf < m2x:
if m1y < m2y:
# Phase IIa: left/bottom --> top/right
y1 = minY(bx, by, rx, ry, xf)
y2 = maxY(lx, ly, tx, ty, xf)
else:
# Phase IIb: left/top --> bottom/right
y1 = minY(lx, ly, bx, by, xf)
y2 = maxY(tx, ty, rx, ry, xf)
else:
# Phase III: bottom/top --> right
y1 = minY(bx, by, rx, ry, xf)
y2 = maxY(tx, ty, rx, ry, xf)
y1 = max(y1, byi)
y2 = min(y2, tyi)
for y in range(y1, y2):
pixels.append((x, y))
return pixels
def load_config(path):
""" Loads the configuration file
Args:
path: A string indicating the path to the configuration file
Returns:
config: A Python dictionary of hyperparameter name-value pairs
learning rate: The learning rate of the optimzer
batch_size: Batch size used during training
num_epochs: Number of epochs to train the network for
target_classes: A list of strings denoting the classes to
build the classifer for
"""
with open(path) as file:
config = json.load(file)
learning_rate = config["learning_rate"]
batch_size = config["batch_size"]
max_epochs = config["max_epochs"]
return config, learning_rate, batch_size, max_epochs
def get_model_name(name):
""" Generate a name for the model consisting of all the hyperparameter values
Args:
name: Name of ckpt
Returns:
path: A string with the hyperparameter name and value concatenated
"""
# path = "model_"
# path += "epoch{}_".format(config["max_epochs"])
# path += "bs{}_".format(config["batch_size"])
# path += "lr{}".format(config["learning_rate"])
path = os.path.join("experiments", name)
return path