-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmetrics.py
172 lines (130 loc) · 4.36 KB
/
metrics.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
import os
import math
import sys
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as Func
from torch.nn import init
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
import torch.optim as optim
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from numpy import linalg as LA
import networkx as nx
def ade(predAll, targetAll, count_):
All = len(predAll)
sum_all = 0
for s in range(All):
pred = np.swapaxes(predAll[s][:, : count_[s], :], 0, 1)
target = np.swapaxes(targetAll[s][:, : count_[s], :], 0, 1)
N = pred.shape[0]
T = pred.shape[1]
sum_ = 0
for i in range(N):
for t in range(T):
sum_ += math.sqrt(
(pred[i, t, 0] - target[i, t, 0]) ** 2
+ (pred[i, t, 1] - target[i, t, 1]) ** 2
)
sum_all += sum_ / (N * T)
return sum_all / All
def fde(predAll, targetAll, count_):
All = len(predAll)
sum_all = 0
for s in range(All):
pred = np.swapaxes(predAll[s][:, : count_[s], :], 0, 1)
target = np.swapaxes(targetAll[s][:, : count_[s], :], 0, 1)
N = pred.shape[0]
T = pred.shape[1]
sum_ = 0
for i in range(N):
for t in range(T - 1, T):
sum_ += math.sqrt(
(pred[i, t, 0] - target[i, t, 0]) ** 2
+ (pred[i, t, 1] - target[i, t, 1]) ** 2
)
sum_all += sum_ / (N)
return sum_all / All
def seq_to_nodes(seq_, max_nodes=88):
# seq_ = seq_.squeeze()
seq_len = seq_.shape[0]
num_nodes = seq_.shape[1]
V = np.zeros((seq_len, num_nodes, 2))
for s in range(seq_len):
step_ = seq_[s, :, :]
for h in range(len(step_)):
V[s, h, :] = step_[h]
# return V.squeeze()
return V
def nodes_rel_to_nodes_abs(nodes, init_node):
nodes_ = np.zeros_like(nodes)
for s in range(nodes.shape[0]):
for ped in range(nodes.shape[1]):
nodes_[s, ped, :] = (
np.sum(nodes[: s + 1, ped, :], axis=0) + init_node[ped, :]
)
return nodes_.squeeze()
# return nodes_
def closer_to_zero(current, new_v):
dec = min([(abs(current), current), (abs(new_v), new_v)])[1]
if dec != current:
return True
else:
return False
def batch_bivariate_loss(V_pred, V_trgt, mask=None):
"""
V_pred, V_trgt:
[Batch, Seq_len, Nodes, 5/2];
"""
# mux, muy, sx, sy, corr
# assert V_pred.shape == V_trgt.shape
normx = V_trgt[:, :, :, 0] - V_pred[:, :, :, 0]
normy = V_trgt[:, :, :, 1] - V_pred[:, :, :, 1]
sx = torch.exp(V_pred[:, :, :, 2]) # sx
sy = torch.exp(V_pred[:, :, :, 3]) # sy
# sx = V_pred[:, :, :, 2] # sx
# sy = V_pred[:, :, :, 3] # sy
corr = torch.tanh(V_pred[:, :, :, 4]) # corr
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
# mask out unrelated nodes
result = torch.einsum("ntv, ntv->ntv", result, mask)
result[mask == 0] = 1.0 # To make the log value of padded nodes as zero;
# total number
n = torch.sum(mask)
result = -torch.log(torch.clamp(result, min=epsilon))
result = result.sum() * 1.0 / n
return result
def bivariate_loss(V_pred, V_trgt):
# mux, muy, sx, sy, corr
# assert V_pred.shape == V_trgt.shape
normx = V_trgt[:, :, 0] - V_pred[:, :, 0]
normy = V_trgt[:, :, 1] - V_pred[:, :, 1]
sx = torch.exp(V_pred[:, :, 2]) # sx
sy = torch.exp(V_pred[:, :, 3]) # sy
corr = torch.tanh(V_pred[:, :, 4]) # corr
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))
result = torch.mean(result)
return result