-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
189 lines (143 loc) · 5.07 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
import torch
import torch.nn
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import math
import json
import os
def get_logger(config, mode='train'):
folder = os.path.join('logs', config['name'], mode)
if not os.path.exists(folder):
os.makedirs(folder)
return logger.Logger(folder)
def load_config(exp_name):
""" 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
"""
path = os.path.join('./experiments', exp_name, 'config.json')
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(config, epoch=None):
""" 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
"""
name = config['name']
if epoch is None:
epoch = config['resume_from']
folder = os.path.join("./experiments", name)
if not os.path.exists(folder):
os.makedirs(folder)
path = os.path.join(folder, str(epoch)+"epoch")
return path
def writefile(config, filename, value):
path = os.path.join('experiments', config['name'], filename)
with open(path, 'a') as f:
f.write(value)
def maskFOV_on_BEV(shape, fov=88.0):
height = shape[0]
width = shape[1]
fov = fov / 2
x = np.arange(width)
y = np.arange(-height//2, height//2)
xx, yy = np.meshgrid(x, y)
angle = np.arctan2(yy, xx) * 180 / np.pi
in_fov = np.abs(angle) < fov
in_fov = torch.from_numpy(in_fov.astype(np.float32))
return in_fov
def get_points_in_a_rotated_box(corners, label_shape=[200, 175]):
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 = label_shape[1]
ymax = label_shape[0]
# 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 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