-
Notifications
You must be signed in to change notification settings - Fork 6
/
criterion.py
143 lines (108 loc) · 4.19 KB
/
criterion.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
import torch
import numpy as np
import helper
def Gaussian2DLikelihoodInference(outputs, targets, nodesPresent, pred_length, look_up):
'''
Computes the likelihood of predicted locations under a bivariate Gaussian distribution at test time
Parameters:
outputs: Torch variable containing tensor of shape seq_length x numNodes x 1 x output_size
targets: Torch variable containing tensor of shape seq_length x numNodes x 1 x input_size
nodesPresent : A list of lists, of size seq_length. Each list contains the nodeIDs that are present in the frame
'''
seq_length = outputs.size()[0]
obs_length = seq_length - pred_length
# Extract mean, std devs and correlation
mux, muy, sx, sy, corr = helper.getCoef(outputs)
# Compute factors
normx = targets[:, :, 0] - mux
normy = targets[:, :, 1] - muy
sxsy = sx * sy
z = (normx/sx)**2 + (normy/sy)**2 - 2*((corr*normx*normy)/sxsy)
negRho = 1 - corr**2
# Numerator
result = torch.exp(-z/(2*negRho))
# Normalization factor
denom = 2 * np.pi * (sxsy * torch.sqrt(negRho))
# Final PDF calculation
result = result / denom
# Numerical stability
epsilon = 1e-20
result = -torch.log(torch.clamp(result, min=epsilon))
#print(result)
loss = 0
counter = 0
for framenum in range(obs_length, seq_length):
nodeIDs = nodesPresent[framenum]
nodeIDs = [str(nodeID) for nodeID in nodeIDs]
for nodeID in nodeIDs:
nodeID = look_up[nodeID]
loss = loss + result[framenum, nodeID]
counter = counter + 1
if counter != 0:
return loss / counter
else:
return loss
def Gaussian2DLikelihood(outputs, targets, nodesPresent, look_up):
'''
params:
outputs : predicted locations
targets : true locations
assumedNodesPresent : Nodes assumed to be present in each frame in the sequence
nodesPresent : True nodes present in each frame in the sequence
look_up : lookup table for determining which ped is in which array index
'''
seq_length = outputs.size()[0]
# Extract mean, std devs and correlation
mux, muy, sx, sy, corr = helper.getCoef(outputs)
# Compute factors
normx = targets[:, :, 0] - mux
normy = targets[:, :, 1] - muy
sxsy = sx * sy
z = (normx/sx)**2 + (normy/sy)**2 - 2*((corr*normx*normy)/sxsy)
negRho = 1 - corr**2
# Numerator
result = torch.exp(-z/(2*negRho))
# Normalization factor
denom = 2 * np.pi * (sxsy * torch.sqrt(negRho))
# Final PDF calculation
result = result / denom
# Numerical stability
epsilon = 1e-20
result = -torch.log(torch.clamp(result, min=epsilon))
loss = 0
counter = 0
for framenum in range(seq_length):
nodeIDs = nodesPresent[framenum]
nodeIDs = [str(nodeID) for nodeID in nodeIDs]
for nodeID in nodeIDs:
nodeID = look_up[nodeID]
loss = loss + result[framenum, nodeID]
counter = counter + 1
if counter != 0:
return loss / counter
else:
return loss
def RMSELoss(outputs, targets, nodesPresent, look_up):
'''
params:
outputs : predicted locations
targets : true locations
assumedNodesPresent : Nodes assumed to be present in each frame in the sequence
nodesPresent : True nodes present in each frame in the sequence
look_up : lookup table for determining which ped is in which array index
'''
seq_length = outputs.size()[0]
loss_fn = torch.nn.MSELoss()
#loss_0 = loss_fn(outputs, targets)
loss = 0
for framenum in range(seq_length):
nodeIDs = nodesPresent[framenum]
#nodeIDs = [str(nodeID) for nodeID in nodeIDs]
nodeIDs = [look_up[nodeID] for nodeID in nodeIDs]
nodeIDs_tensor = torch.tensor(nodeIDs).cuda()
output = torch.index_select(outputs[framenum], 0, nodeIDs_tensor)
target = torch.index_select(targets[framenum], 0, nodeIDs_tensor)
loss += torch.sqrt(loss_fn(output, target))
if seq_length == 0:
return loss
return loss / seq_length