-
Notifications
You must be signed in to change notification settings - Fork 48
/
utils.py
189 lines (170 loc) · 7.03 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 math
import logging
import numpy as np
import torch
from scipy.optimize import curve_fit
## Network parameters initialization
def weights_init(m):
if isinstance(m, torch.nn.Conv2d):
torch.nn.init.xavier_uniform_(m.weight)
if m.bias is not None:
torch.nn.init.constant_(m.bias, 0.1)
def initLogging(log_file: str, level: str = "INFO"):
logging.basicConfig(filename=log_file, filemode='a',
level=getattr(logging, level, None),
format='[%(levelname)s %(asctime)s] %(message)s',
datefmt='%m-%d %H:%M:%S')
logging.getLogger().addHandler(logging.StreamHandler())
## Quintic spline definition.
def quintic_spline(x, z, a, b, c, d, e):
return z + a * x + b * x ** 2 + c * x ** 3 + d * x ** 4 + e * x ** 5
## Fitting the trajectory of one planning circle by quintic spline, with the current location fixed.
def fitting_traj_by_qs(x, y):
param, loss = curve_fit(quintic_spline, x, y,
bounds=([y[0], -np.inf, -np.inf, -np.inf, -np.inf, -np.inf], [y[0]+1e-6, np.inf, np.inf, np.inf, np.inf, np.inf]))
return param
## Custom activation for output layer (Graves, 2015)
def outputActivation(x, displacement=True):
if displacement:
# Then mu value denotes displacement.
x[:, :, 0:2] = torch.stack([torch.sum(x[0:i, :, 0:2], dim=0) for i in range(1, x.shape[0] + 1)], 0)
# Each output has 5 params to describe the gaussian distribution.
muX = x[:, :, 0:1]
muY = x[:, :, 1:2]
sigX = x[:, :, 2:3]
sigY = x[:, :, 3:4]
rho = x[:, :, 4:5]
sigX = torch.exp(sigX) # This positive value represents Reciprocal of SIGMA (1/sigX)
sigY = torch.exp(sigY)
rho = torch.tanh(rho) # -1 < rho < 1
out = torch.cat([muX, muY, sigX, sigY, rho], dim=2)
return out
def maskedNLL(y_pred, y_gt, mask):
acc = torch.zeros_like(mask)
muX = y_pred[:, :, 0]
muY = y_pred[:, :, 1]
sigX = y_pred[:, :, 2]
sigY = y_pred[:, :, 3]
rho = y_pred[:, :, 4]
ohr = torch.pow(1 - torch.pow(rho, 2), -0.5)
x = y_gt[:, :, 0]
y = y_gt[:, :, 1]
out = 0.5 * torch.pow(ohr, 2) * \
(torch.pow(sigX, 2) * torch.pow(x - muX, 2) + torch.pow(sigY, 2) * torch.pow(y - muY, 2) - 2 * rho *
torch.pow(sigX, 1) * torch.pow(sigY, 1) * (x - muX) * (y - muY)) - torch.log(sigX * sigY * ohr) \
+ torch.log(torch.tensor(2 * math.pi))
acc[:, :, 0] = out
acc[:, :, 1] = out
acc = acc * mask
lossVal = torch.sum(acc) / torch.sum(mask)
return lossVal
def maskedNLLTest(fut_pred, lat_pred, lon_pred, fut, op_mask,
num_lat_classes=3, num_lon_classes=2,
use_maneuvers=True, avg_along_time=False, separately=False):
if use_maneuvers:
acc = torch.zeros(op_mask.shape[0], op_mask.shape[1], num_lon_classes * num_lat_classes).cuda()
count = 0
for k in range(num_lon_classes):
for l in range(num_lat_classes):
wts = lat_pred[:, l] * lon_pred[:, k]
wts = wts.repeat(len(fut_pred[0]), 1)
y_pred = fut_pred[k * num_lat_classes + l]
y_gt = fut
muX = y_pred[:, :, 0]
muY = y_pred[:, :, 1]
sigX = y_pred[:, :, 2]
sigY = y_pred[:, :, 3]
rho = y_pred[:, :, 4]
ohr = torch.pow(1 - torch.pow(rho, 2), -0.5)
x = y_gt[:, :, 0]
y = y_gt[:, :, 1]
out = -(0.5 * torch.pow(ohr, 2) * (torch.pow(sigX, 2) * torch.pow(x - muX, 2) + torch.pow(sigY, 2) * torch.pow(y - muY, 2)
- 2 * rho * torch.pow(sigX, 1) * torch.pow(sigY, 1) * (x - muX) * (y - muY)) - torch.log(sigX * sigY * ohr)
+ torch.log(torch.tensor(2 * math.pi)))
acc[:, :, count] = out + torch.log(wts)
count += 1
acc = -logsumexp(acc, dim=2) # Negative log-likelihood
acc = acc * op_mask[:, :, 0]
if avg_along_time:
lossVal = torch.sum(acc) / torch.sum(op_mask[:, :, 0])
return lossVal
else:
if separately:
lossVal = acc
counts = op_mask[:, :, 0]
return lossVal, counts
else:
lossVal = torch.sum(acc, dim=1)
counts = torch.sum(op_mask[:, :, 0], dim=1)
return lossVal, counts
else:
acc = torch.zeros(op_mask.shape[0], op_mask.shape[1], 1).cuda()
y_pred = fut_pred
y_gt = fut
muX = y_pred[:, :, 0]
muY = y_pred[:, :, 1]
sigX = y_pred[:, :, 2]
sigY = y_pred[:, :, 3]
rho = y_pred[:, :, 4]
ohr = torch.pow(1 - torch.pow(rho, 2), -0.5)
x = y_gt[:, :, 0]
y = y_gt[:, :, 1]
out = +(0.5 * torch.pow(ohr, 2) * (torch.pow(sigX, 2) * torch.pow(x - muX, 2) + torch.pow(sigY, 2) * torch.pow(y - muY, 2)
- 2 * rho * torch.pow(sigX, 1) * torch.pow(sigY, 1) * (x - muX) * (y - muY)) - torch.log(sigX * sigY * ohr)
+ torch.log(torch.tensor(2 * math.pi)))
acc[:, :, 0] = out
acc = acc * op_mask[:, :, 0:1]
if avg_along_time:
lossVal = torch.sum(acc[:, :, 0]) / torch.sum(op_mask[:, :, 0])
return lossVal
else:
if separately:
lossVal = acc[:, :, 0]
counts = op_mask[:, :, 0]
return lossVal, counts
else:
lossVal = torch.sum(acc[:, :, 0], dim=1)
counts = torch.sum(op_mask[:, :, 0], dim=1)
return lossVal, counts
def maskedMSE(y_pred, y_gt, mask):
acc = torch.zeros_like(mask)
muX = y_pred[:, :, 0]
muY = y_pred[:, :, 1]
x = y_gt[:, :, 0]
y = y_gt[:, :, 1]
out = torch.pow(x - muX, 2) + torch.pow(y - muY, 2)
acc[:, :, 0] = out
acc[:, :, 1] = out
acc = acc * mask
lossVal = torch.sum(acc) / torch.sum(mask)
return lossVal
def maskedMSETest(y_pred, y_gt, mask, separately=False):
acc = torch.zeros_like(mask)
muX = y_pred[:, :, 0]
muY = y_pred[:, :, 1]
x = y_gt[:, :, 0]
y = y_gt[:, :, 1]
out = torch.pow(x - muX, 2) + torch.pow(y - muY, 2)
acc[:, :, 0] = out
acc[:, :, 1] = out
acc = acc * mask
if separately:
return acc[:, :, 0], mask[:, :, 0]
else:
lossVal = torch.sum(acc[:, :, 0], dim=1)
counts = torch.sum(mask[:, :, 0], dim=1)
return lossVal, counts
## Helper function for log sum exp calculation:
def logsumexp(inputs, dim=None, keepdim=False):
if dim is None:
inputs = inputs.view(-1)
dim = 0
# Get the maximal probability value from 6 full path
s, _ = torch.max(inputs, dim=dim, keepdim=True)
# here (inputs - s) is to compare the relative probability with the most probable behavior.
# and then sum up all candidate behaviors.
# s->logP(Y | m_max,X), inputs->logP(m_i,Y | X), (inputs - s)->logP(m_i | X)
outputs = s + (inputs - s).exp().sum(dim=dim, keepdim=True).log()
if not keepdim:
outputs = outputs.squeeze(dim)
return outputs