-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptr.py
495 lines (405 loc) · 22.9 KB
/
ptr.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from motionnet.models.base_model.base_model import BaseModel
from torch.optim.lr_scheduler import MultiStepLR
from torch import optim
from scipy import special
from torch.distributions import MultivariateNormal, Laplace
class MapEncoderCNN(nn.Module):
'''
Regular CNN encoder for road image.
'''
def __init__(self, d_k=64, dropout=0.1, c=10):
super(MapEncoderCNN, self).__init__()
self.dropout = dropout
self.c = c
init_ = lambda m: init(m, nn.init.xavier_normal_, lambda x: nn.init.constant_(x, 0), np.sqrt(2))
# MAP ENCODER
fm_size = 7
self.map_encoder = nn.Sequential(
init_(nn.Conv2d(3, 32, kernel_size=4, stride=1)), nn.ReLU(),
init_(nn.Conv2d(32, 32, kernel_size=4, stride=2)), nn.ReLU(),
init_(nn.Conv2d(32, 32, kernel_size=3, stride=2)), nn.ReLU(),
init_(nn.Conv2d(32, 32, kernel_size=3, stride=2)), nn.ReLU(),
init_(nn.Conv2d(32, fm_size*self.c, kernel_size=2, stride=2)), nn.ReLU(),
nn.Dropout2d(p=self.dropout)
)
self.map_feats = nn.Sequential(
init_(nn.Linear(7*7*fm_size, d_k)), nn.ReLU(),
init_(nn.Linear(d_k, d_k)), nn.ReLU(),
)
self.fisher_information = None
self.optimal_params = None
def forward(self, roads):
'''
:param roads: road image with size (B, 128, 128, 3)
:return: road features, with one for every mode (B, c, d_k)
'''
B = roads.size(0) # batch size
return self.map_feats(self.map_encoder(roads).view(B, self.c, -1))
class MapEncoderPts(nn.Module):
'''
This class operates on the road lanes provided as a tensor with shape
(B, num_road_segs, num_pts_per_road_seg, k_attr+1)
'''
def __init__(self, d_k, map_attr=3, dropout=0.1):
super(MapEncoderPts, self).__init__()
self.dropout = dropout
self.d_k = d_k
self.map_attr = map_attr
init_ = lambda m: init(m, nn.init.xavier_normal_, lambda x: nn.init.constant_(x, 0), np.sqrt(2))
self.road_pts_lin = nn.Sequential(init_(nn.Linear(map_attr, self.d_k)))
self.road_pts_attn_layer = nn.MultiheadAttention(self.d_k, num_heads=8, dropout=self.dropout)
self.norm1 = nn.LayerNorm(self.d_k, eps=1e-5)
self.norm2 = nn.LayerNorm(self.d_k, eps=1e-5)
self.map_feats = nn.Sequential(
init_(nn.Linear(self.d_k, self.d_k)), nn.ReLU(), nn.Dropout(self.dropout),
init_(nn.Linear(self.d_k, self.d_k)),
)
def get_road_pts_mask(self, roads):
road_segment_mask = torch.sum(roads[:, :, :, -1], dim=2) == 0
road_pts_mask = (1.0 - roads[:, :, :, -1]).type(torch.BoolTensor).to(roads.device).view(-1, roads.shape[2])
road_pts_mask[:, 0][road_pts_mask.sum(-1) == roads.shape[2]] = False # Ensures no NaNs due to empty rows.
return road_segment_mask, road_pts_mask
def forward(self, roads, agents_emb):
'''
:param roads: (B, S, P, k_attr+1) where B is batch size, S is num road segments, P is
num pts per road segment.
:param agents_emb: (T_obs, B, d_k) where T_obs is the observation horizon. THis tensor is obtained from
PTR's encoder, and basically represents the observed socio-temporal context of agents.
:return: embedded road segments with shape (S)
'''
B = roads.shape[0]
S = roads.shape[1]
P = roads.shape[2]
road_segment_mask, road_pts_mask = self.get_road_pts_mask(roads)
road_pts_feats = self.road_pts_lin(roads[:, :, :, :self.map_attr]).view(B*S, P, -1).permute(1, 0, 2)
# Combining information from each road segment using attention with agent contextual embeddings as queries.
agents_emb = agents_emb[-1].unsqueeze(2).repeat(1, 1, S, 1).view(-1, self.d_k).unsqueeze(0)
road_seg_emb = self.road_pts_attn_layer(query=agents_emb, key=road_pts_feats, value=road_pts_feats,
key_padding_mask=road_pts_mask)[0]
road_seg_emb = self.norm1(road_seg_emb)
road_seg_emb2 = road_seg_emb + self.map_feats(road_seg_emb)
road_seg_emb2 = self.norm2(road_seg_emb2)
road_seg_emb = road_seg_emb2.view(B, S, -1)
return road_seg_emb.permute(1, 0, 2), road_segment_mask
def init(module, weight_init, bias_init, gain=1):
'''
This function provides weight and bias initializations for linear layers.
'''
weight_init(module.weight.data, gain=gain)
bias_init(module.bias.data)
return module
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout=0.1, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).transpose(0, 1)
self.register_parameter('pe', nn.Parameter(pe, requires_grad=False))
def forward(self, x):
x = x + self.pe[:x.size(0), :]
return self.dropout(x)
class OutputModel(nn.Module):
'''
This class operates on the output of PTR's decoder representation. It produces the parameters of a
bivariate Gaussian distribution.
'''
def __init__(self, d_k=64):
super(OutputModel, self).__init__()
self.d_k = d_k
init_ = lambda m: init(m, nn.init.xavier_normal_, lambda x: nn.init.constant_(x, 0), np.sqrt(2))
self.observation_model = nn.Sequential(
init_(nn.Linear(d_k, d_k)), nn.ReLU(),
init_(nn.Linear(d_k, d_k)), nn.ReLU(),
init_(nn.Linear(d_k, 5))
)
self.min_stdev = 0.01
def forward(self, agent_decoder_state):
T = agent_decoder_state.shape[0]
BK = agent_decoder_state.shape[1]
pred_obs = self.observation_model(agent_decoder_state.reshape(-1, self.d_k)).reshape(T, BK, -1)
x_mean = pred_obs[:, :, 0]
y_mean = pred_obs[:, :, 1]
x_sigma = F.softplus(pred_obs[:, :, 2]) + self.min_stdev
y_sigma = F.softplus(pred_obs[:, :, 3]) + self.min_stdev
rho = torch.tanh(pred_obs[:, :, 4]) * 0.9 # for stability
return torch.stack([x_mean, y_mean, x_sigma, y_sigma, rho], dim=2)
class PTR(BaseModel):
def __init__(self, config, k_attr=2, map_attr=2):
super(PTR, self).__init__(config)
self.n_print = 0
self.config = config
init_ = lambda m: init(m, nn.init.xavier_normal_, lambda x: nn.init.constant_(x, 0), np.sqrt(2))
self.T = config['future_len']
self.past = config['past_len']
self.fisher_information = None
self.map_attr = map_attr
self.k_attr = k_attr
self.d_k = config['hidden_size']
self.c = config['num_modes']
self.L_enc = config['num_encoder_layers']
self.dropout = config['dropout']
self.num_heads = config['tx_num_heads']
self.L_dec = config['num_decoder_layers']
self.tx_hidden_size = config['tx_hidden_size']
# INPUT ENCODERS
self.agents_dynamic_encoder = nn.Sequential(init_(nn.Linear(self.k_attr, self.d_k)))
# ============================== PTR ENCODER ==============================
self.social_attn_layers = []
self.temporal_attn_layers = []
for _ in range(self.L_enc):
tx_encoder_layer = nn.TransformerEncoderLayer(d_model=self.d_k, nhead=self.num_heads, dropout=self.dropout,
dim_feedforward=self.tx_hidden_size)
self.social_attn_layers.append(nn.TransformerEncoder(tx_encoder_layer, num_layers=1))
tx_encoder_layer = nn.TransformerEncoderLayer(d_model=self.d_k, nhead=self.num_heads, dropout=self.dropout,
dim_feedforward=self.tx_hidden_size)
self.temporal_attn_layers.append(nn.TransformerEncoder(tx_encoder_layer, num_layers=1))
self.temporal_attn_layers = nn.ModuleList(self.temporal_attn_layers)
self.social_attn_layers = nn.ModuleList(self.social_attn_layers)
# ============================== MAP ENCODER ==========================
self.map_encoder = MapEncoderPts(d_k=self.d_k, map_attr=self.map_attr, dropout=self.dropout)
self.map_attn_layers = nn.MultiheadAttention(self.d_k, num_heads=self.num_heads, dropout=0.3)
# ============================== PTR DECODER ==============================
self.Q = nn.Parameter(torch.Tensor(self.T, 1, self.c, self.d_k), requires_grad=True)
nn.init.xavier_uniform_(self.Q)
self.tx_decoder = []
for _ in range(self.L_dec):
self.tx_decoder.append(nn.TransformerDecoderLayer(d_model=self.d_k, nhead=self.num_heads,
dropout=self.dropout,
dim_feedforward=self.tx_hidden_size))
self.tx_decoder = nn.ModuleList(self.tx_decoder)
# ============================== Positional encoder ==============================
self.pos_encoder = PositionalEncoding(self.d_k, dropout=0.0, max_len=self.past)
# ============================== OUTPUT MODEL ==============================
self.output_model = OutputModel(d_k=self.d_k)
# ============================== Mode Prob prediction (P(z|X_1:t)) ==============================
self.P = nn.Parameter(torch.Tensor(self.c, 1, self.d_k), requires_grad=True)
nn.init.xavier_uniform_(self.P)
self.mode_map_attn = nn.MultiheadAttention(self.d_k, num_heads=self.num_heads)
self.prob_decoder = nn.MultiheadAttention(self.d_k, num_heads=self.num_heads, dropout=self.dropout)
self.prob_predictor = init_(nn.Linear(self.d_k, 1))
self.criterion = Criterion(self.config)
self.fisher_information = None
self.optimal_params = None
def generate_decoder_mask(self, seq_len, device):
''' For masking out the subsequent info. '''
subsequent_mask = (torch.triu(torch.ones((seq_len, seq_len), device=device), diagonal=1)).bool()
return subsequent_mask
def process_observations(self, ego, agents):
'''
:param observations: (B, T, N+2, A+1) where N+2 is [ego, other_agents, env]
:return: a tensor of only the agent dynamic states, active_agent masks and env masks.
'''
# ego stuff
ego_tensor = ego[:, :, :self.k_attr]
env_masks_orig = ego[:, :, -1]
env_masks = (1.0 - env_masks_orig).to(torch.bool)
env_masks = env_masks.unsqueeze(1).repeat(1, self.c, 1).view(ego.shape[0] * self.c, -1)
# Agents stuff
temp_masks = torch.cat((torch.ones_like(env_masks_orig.unsqueeze(-1)), agents[:, :, :, -1]), dim=-1)
opps_masks = (1.0 - temp_masks).to(torch.bool) # only for agents.
opps_tensor = agents[:, :, :, :self.k_attr] # only opponent states
return ego_tensor, opps_tensor, opps_masks, env_masks
def temporal_attn_fn(self, agents_emb, agent_masks, layer):
'''
Gets agents embeddings and agents mask, and applies the temporal attention layer per agent.
Make sure to apply the agent mask in the layer function (you could use src_key_padding_mask argument).
Also don't forget to use positional encoding.
:param agents_emb: (T, B, N, H)
:param agent_masks: (B, T, N)
:return: (T, B, N, H)
'''
######################## Your code here ########################
for n in range(agents_emb.shape[2]): #per agent, assuming N is the number of agents
agents_emb[:,:,n,:] = layer(agents_emb[:,:,n,:], src_key_padding_mask=agent_masks[:,:,n])
################################################################
return agents_emb
def social_attn_fn(self, agents_emb, agent_masks, layer):
'''
Gets agents embeddings and agents mask, and applies the social attention layer per time step.
Make sure to apply the agent mask in the layer function (you could use src_key_padding_mask argument).
You don't need to use positional encoding here.
:param agents_emb: (T, B, N, H)
:param agent_masks: (B, T, N)
:return: (T, B, N, H)
'''
######################## Your code here ########################
for t in range(agents_emb.shape[0]): #per time step, assuming T is the mnumber of time steps
agents_emb[t,:,:,:] = layer(agents_emb[t,:,:,:], src_key_padding_mask=agent_masks[:,t,:].permute(1,0))
################################################################
return agents_emb
def _forward(self, inputs):
'''
:param ego_in: [B, T_obs, k_attr+1] with last values being the existence mask.
:param agents_in: [B, T_obs, M-1, k_attr+1] with last values being the existence mask.
:param roads: [B, S, P, map_attr+1] representing the road network if self.use_map_lanes or
[B, 3, 128, 128] image representing the road network if self.use_map_img or
[B, 1, 1] if self.use_map_lanes and self.use_map_img are False.
:return:
pred_obs: shape [c, T, B, 5] c trajectories for the ego agents with every point being the params of
Bivariate Gaussian distribution.
mode_probs: shape [B, c] mode probability predictions P(z|X_{1:T_obs})
'''
ego_in, agents_in, roads = inputs['ego_in'], inputs['agents_in'], inputs['roads']
B = ego_in.size(0)
# Encode all input observations (k_attr --> d_k)
ego_tensor, _agents_tensor, opps_masks, env_masks = self.process_observations(ego_in, agents_in)
agents_tensor = torch.cat((ego_tensor.unsqueeze(2), _agents_tensor), dim=2) # [B, T, N, k_attr]
# encode each agent's dynamic state using a linear layer (k_attr --> d_k)
agents_emb = self.agents_dynamic_encoder(agents_tensor).permute(1, 0, 2, 3) # T, B, N, H
######################## Your code here ########################
# Apply temporal attention layers and then the social attention layers on agents_emb, each for L_enc times.
for i in range(self.L_enc):
agents_emb = self.temporal_attn_fn(agents_emb, opps_masks, self.temporal_attn_layers[i])
agents_emb = self.social_attn_fn(agents_emb, opps_masks, self.social_attn_layers[i])
################################################################
ego_soctemp_emb = agents_emb[:, :, 0] # take ego-agent encodings only.
orig_map_features, orig_road_segs_masks = self.map_encoder(roads, ego_soctemp_emb)
map_features = orig_map_features.unsqueeze(2).repeat(1, 1, self.c, 1).view(-1, B*self.c, self.d_k)
road_segs_masks = orig_road_segs_masks.unsqueeze(1).repeat(1, self.c, 1).view(B*self.c, -1)
# Repeat the tensors for the number of modes for efficient forward pass.
context = ego_soctemp_emb.unsqueeze(2).repeat(1, 1, self.c, 1)
context = context.view(-1, B*self.c, self.d_k)
# PTR Decoding
out_seq = self.Q.repeat(1, B, 1, 1).view(self.T, B*self.c, -1)
time_masks = self.generate_decoder_mask(seq_len=self.T, device=ego_in.device)
for d in range(self.L_dec):
ego_dec_emb_map = self.map_attn_layers(query=out_seq, key=map_features, value=map_features,
key_padding_mask=road_segs_masks)[0]
out_seq = out_seq + ego_dec_emb_map
out_seq = self.tx_decoder[d](out_seq, context, tgt_mask=time_masks, memory_key_padding_mask=env_masks)
out_dists = self.output_model(out_seq).reshape(self.T, B, self.c, -1).permute(2, 0, 1, 3)
# Mode prediction
mode_params_emb = self.P.repeat(1, B, 1)
mode_params_emb = self.prob_decoder(query=mode_params_emb, key=ego_soctemp_emb, value=ego_soctemp_emb)[0]
mode_params_emb = self.mode_map_attn(query=mode_params_emb, key=orig_map_features, value=orig_map_features,
key_padding_mask=orig_road_segs_masks)[0] + mode_params_emb
mode_probs = F.softmax(self.prob_predictor(mode_params_emb).squeeze(-1), dim=0).transpose(0, 1)
# return [c, T, B, 5], [B, c]
output = {}
output['predicted_probability'] = mode_probs # #[B, c]
output['predicted_trajectory'] = out_dists.permute(2,0,1,3) # [c, T, B, 5] to [B, c, T, 5] to be able to parallelize code
if len(np.argwhere(np.isnan(out_dists.detach().cpu().numpy()))) > 1:
breakpoint()
return output
def forward(self, batch, batch_idx):
model_input = {}
inputs = batch['input_dict']
agents_in, agents_mask, roads = inputs['obj_trajs'],inputs['obj_trajs_mask'] ,inputs['map_polylines']
ego_in = torch.gather(agents_in, 1, inputs['track_index_to_predict'].view(-1,1,1,1).repeat(1,1,*agents_in.shape[-2:])).squeeze(1)
ego_mask = torch.gather(agents_mask, 1, inputs['track_index_to_predict'].view(-1,1,1).repeat(1,1,agents_mask.shape[-1])).squeeze(1)
agents_in = torch.cat([agents_in[...,:2],agents_mask.unsqueeze(-1)],dim=-1)
agents_in = agents_in.transpose(1,2)
ego_in = torch.cat([ego_in[...,:2],ego_mask.unsqueeze(-1)],dim=-1)
roads = torch.cat([inputs['map_polylines'][...,:2],inputs['map_polylines_mask'].unsqueeze(-1)],dim=-1)
model_input['ego_in'] = ego_in
model_input['agents_in'] = agents_in
model_input['roads'] = roads
output = self._forward(model_input)
loss = self.get_loss(batch, output)
return output, loss
def get_loss(self, batch, prediction):
inputs = batch['input_dict']
ground_truth = torch.cat([inputs['center_gt_trajs'][...,:2],inputs['center_gt_trajs_mask'].unsqueeze(-1)],dim=-1)
loss = self.criterion(prediction, ground_truth,inputs['center_gt_final_valid_idx'])
return loss
def configure_optimizers(self):
optimizer = optim.Adam(self.parameters(), lr= self.config['learning_rate'],eps=0.0001)
scheduler = MultiStepLR(optimizer, milestones=self.config['learning_rate_sched'], gamma=0.5,
verbose=True)
return [optimizer], [scheduler]
class Criterion(nn.Module):
def __init__(self, config):
super(Criterion, self).__init__()
self.config = config
def forward(self, out, gt,center_gt_final_valid_idx):
return self.nll_loss_multimodes(out, gt,center_gt_final_valid_idx)
def get_BVG_distributions(self, pred):
B = pred.size(0)
T = pred.size(1)
mu_x = pred[:, :, 0].unsqueeze(2)
mu_y = pred[:, :, 1].unsqueeze(2)
sigma_x = pred[:, :, 2]
sigma_y = pred[:, :, 3]
rho = pred[:, :, 4]
cov = torch.zeros((B, T, 2, 2)).to(pred.device)
cov[:, :, 0, 0] = sigma_x ** 2
cov[:, :, 1, 1] = sigma_y ** 2
cov[:, :, 0, 1] = rho * sigma_x * sigma_y
cov[:, :, 1, 0] = rho * sigma_x * sigma_y
biv_gauss_dist = MultivariateNormal(loc=torch.cat((mu_x, mu_y), dim=-1), covariance_matrix=cov)
return biv_gauss_dist
def get_Laplace_dist(self, pred):
return Laplace(pred[:, :, :2], pred[:, :, 2:4])
def nll_pytorch_dist(self, pred, data, mask, rtn_loss=True):
biv_gauss_dist = self.get_Laplace_dist(pred)
data_reshaped = data[:, :, :2]
if rtn_loss:
return ((-biv_gauss_dist.log_prob(data_reshaped)).sum(-1) * mask).sum(1) # Laplace
else:
return ((-biv_gauss_dist.log_prob(data_reshaped)).sum(dim=2) * mask).sum(1) # Laplace
def nll_loss_multimodes(self, output, data, center_gt_final_valid_idx):
"""NLL loss multimodes for training. MFP Loss function
Args:
pred: [K, T, B, 5]
data: [B, T, 5]
modes_pred: [B, K], prior prob over modes
noise is optional
"""
modes_pred = output['predicted_probability']
pred = output['predicted_trajectory'].permute(1, 2, 0, 3)
mask = data[..., -1]
entropy_weight = self.config['entropy_weight']
kl_weight = self.config['kl_weight']
use_FDEADE_aux_loss = self.config['use_FDEADE_aux_loss']
modes = len(pred)
nSteps, batch_sz, dim = pred[0].shape
# compute posterior probability based on predicted prior and likelihood of predicted trajectory.
log_lik = np.zeros((batch_sz, modes))
with torch.no_grad():
for kk in range(modes):
nll = self.nll_pytorch_dist(pred[kk].transpose(0, 1), data, mask, rtn_loss=False)
log_lik[:, kk] = -nll.cpu().numpy()
priors = modes_pred.detach().cpu().numpy()
log_posterior_unnorm = log_lik + np.log(priors)
log_posterior = log_posterior_unnorm - special.logsumexp(log_posterior_unnorm, axis=-1).reshape((batch_sz, -1))
post_pr = np.exp(log_posterior)
post_pr = torch.tensor(post_pr).float().to(data.device)
# Compute loss.
loss = 0.0
for kk in range(modes):
nll_k = self.nll_pytorch_dist(pred[kk].transpose(0, 1), data, mask, rtn_loss=True) * post_pr[:, kk]
loss += nll_k.mean()
# Adding entropy loss term to ensure that individual predictions do not try to cover multiple modes.
entropy_vals = []
for kk in range(modes):
entropy_vals.append(self.get_BVG_distributions(pred[kk]).entropy())
entropy_vals = torch.stack(entropy_vals).permute(2, 0, 1)
entropy_loss = torch.mean((entropy_vals).sum(2).max(1)[0])
loss += entropy_weight * entropy_loss
# KL divergence between the prior and the posterior distributions.
kl_loss_fn = torch.nn.KLDivLoss(reduction='batchmean') # type: ignore
kl_loss = kl_weight * kl_loss_fn(torch.log(modes_pred), post_pr)
# compute ADE/FDE loss - L2 norms with between best predictions and GT.
if use_FDEADE_aux_loss:
adefde_loss = self.l2_loss_fde(pred, data, mask)
else:
adefde_loss = torch.tensor(0.0).to(data.device)
# post_entropy
final_loss = loss + kl_loss + adefde_loss
return final_loss
def l2_loss_fde(self, pred, data, mask):
fde_loss = (torch.norm((pred[:, -1, :, :2].transpose(0, 1) - data[:, -1, :2].unsqueeze(1)), 2, dim=-1) * mask[:,
-1:])
ade_loss = (torch.norm((pred[:, :, :, :2].transpose(1, 2) - data[:, :, :2].unsqueeze(0)), 2,
dim=-1) * mask.unsqueeze(0)).mean(dim=2).transpose(0, 1)
loss, min_inds = (fde_loss + ade_loss).min(dim=1)
return 100.0 * loss.mean()