-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
298 lines (249 loc) · 11.3 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
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
# Baseline model for "SGCN:Sparse Graph Convolution Network for Pedestrian Trajectory Prediction"
# Source-code referred from SGCN at https://github.com/shuaishiliu/SGCN/tree/0ff25cedc04852803787196e83c0bb941d724fc2/utils.py
import os
import math
import torch
import numpy as np
from tqdm import tqdm
from sklearn.cluster import KMeans
from torch.utils.data import Dataset
def anorm(p1, p2):
NORM = math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
if NORM == 0:
return 0
return 1 / (NORM)
def loc_pos(seq_):
# seq_ [obs_len N 2]
obs_len = seq_.shape[0]
num_ped = seq_.shape[1]
pos_seq = np.arange(1, obs_len + 1)
pos_seq = pos_seq[:, np.newaxis, np.newaxis]
pos_seq = pos_seq.repeat(num_ped, axis=1)
result = np.concatenate((pos_seq, seq_), axis=-1)
return result
def seq_to_graph(seq_, seq_rel, pos_enc=False):
#seq_ = seq_.squeeze()
#seq_rel = seq_rel.squeeze()
seq_len = seq_.shape[2]
max_nodes = seq_.shape[0]
V = np.zeros((seq_len, max_nodes, 2))
for s in range(seq_len):
step_ = seq_[:, :, s]
step_rel = seq_rel[:, :, s]
for h in range(len(step_)):
V[s, h, :] = step_rel[h]
if pos_enc:
V = loc_pos(V)
return torch.from_numpy(V).type(torch.float)
def poly_fit(traj, traj_len, threshold):
"""
Input:
- traj: Numpy array of shape (2, traj_len)
- traj_len: Len of trajectory
- threshold: Minimum error to be considered for non linear traj
Output:
- int: 1 -> Non Linear 0-> Linear
"""
t = np.linspace(0, traj_len - 1, traj_len)
res_x = np.polyfit(t, traj[0, -traj_len:], 2, full=True)[1]
res_y = np.polyfit(t, traj[1, -traj_len:], 2, full=True)[1]
if res_x + res_y >= threshold:
return 1.0
else:
return 0.0
def read_file(_path, delim='\t'):
data = []
if delim == 'tab':
delim = '\t'
elif delim == 'space':
delim = ' '
with open(_path, 'r') as f:
for line in f:
line = line.strip().split(delim)
line = [float(i) for i in line]
data.append(line)
return np.asarray(data)
class TrajectoryDataset(Dataset):
"""Dataloder for the Trajectory datasets"""
def __init__(
self, data_dir, obs_len=8, pred_len=8, skip=1, threshold=0.002,
min_ped=1, delim='\t'):
"""
Args:
- data_dir: Directory containing dataset files in the format
<frame_id> <ped_id> <x> <y>
- obs_len: Number of time-steps in input trajectories
- pred_len: Number of time-steps in output trajectories
- skip: Number of frames to skip while making the dataset
- threshold: Minimum error to be considered for non linear traj
when using a linear predictor
- min_ped: Minimum number of pedestrians that should be in a seqeunce
- delim: Delimiter in the dataset files
"""
super(TrajectoryDataset, self).__init__()
self.max_peds_in_frame = 0
self.data_dir = data_dir
self.obs_len = obs_len
self.pred_len = pred_len
self.skip = skip
self.seq_len = self.obs_len + self.pred_len
self.delim = delim
all_files = os.listdir(self.data_dir)
all_files = [os.path.join(self.data_dir, _path) for _path in all_files]
num_peds_in_seq = []
seq_list = []
seq_list_rel = []
loss_mask_list = []
non_linear_ped = []
for path in all_files:
data = read_file(path, delim)
frames = np.unique(data[:, 0]).tolist()
frame_data = []
for frame in frames:
frame_data.append(data[frame == data[:, 0], :])
num_sequences = int(
math.ceil((len(frames) - self.seq_len + 1) / skip))
for idx in range(0, num_sequences * self.skip + 1, skip):
curr_seq_data = np.concatenate(
frame_data[idx:idx + self.seq_len], axis=0)
peds_in_curr_seq = np.unique(curr_seq_data[:, 1])
self.max_peds_in_frame = max(self.max_peds_in_frame, len(peds_in_curr_seq))
curr_seq_rel = np.zeros((len(peds_in_curr_seq), 2,
self.seq_len))
curr_seq = np.zeros((len(peds_in_curr_seq), 2, self.seq_len))
curr_loss_mask = np.zeros((len(peds_in_curr_seq),
self.seq_len))
num_peds_considered = 0
_non_linear_ped = []
for _, ped_id in enumerate(peds_in_curr_seq):
curr_ped_seq = curr_seq_data[curr_seq_data[:, 1] ==
ped_id, :]
curr_ped_seq = np.around(curr_ped_seq, decimals=4)
pad_front = frames.index(curr_ped_seq[0, 0]) - idx
pad_end = frames.index(curr_ped_seq[-1, 0]) - idx + 1
if pad_end - pad_front != self.seq_len:
continue
curr_ped_seq = np.transpose(curr_ped_seq[:, 2:])
curr_ped_seq = curr_ped_seq
# Make coordinates relative
rel_curr_ped_seq = np.zeros(curr_ped_seq.shape)
# ipdb.set_trace()
rel_curr_ped_seq[:, 1:] = \
curr_ped_seq[:, 1:] - curr_ped_seq[:, :-1]
# rel_curr_ped_seq[:, 1:] = \
# curr_ped_seq[:, 1:] - np.reshape(curr_ped_seq[:, 0], (2,1))
_idx = num_peds_considered
curr_seq[_idx, :, pad_front:pad_end] = curr_ped_seq
curr_seq_rel[_idx, :, pad_front:pad_end] = rel_curr_ped_seq
# Linear vs Non-Linear Trajectory
_non_linear_ped.append(
poly_fit(curr_ped_seq, pred_len, threshold))
curr_loss_mask[_idx, pad_front:pad_end] = 1
num_peds_considered += 1
if num_peds_considered > min_ped:
non_linear_ped += _non_linear_ped
num_peds_in_seq.append(num_peds_considered)
loss_mask_list.append(curr_loss_mask[:num_peds_considered])
seq_list.append(curr_seq[:num_peds_considered])
seq_list_rel.append(curr_seq_rel[:num_peds_considered])
self.num_seq = len(seq_list)
seq_list = np.concatenate(seq_list, axis=0)
seq_list_rel = np.concatenate(seq_list_rel, axis=0)
loss_mask_list = np.concatenate(loss_mask_list, axis=0)
non_linear_ped = np.asarray(non_linear_ped)
# Convert numpy -> Torch Tensor
self.obs_traj = torch.from_numpy(
seq_list[:, :, :self.obs_len]).type(torch.float)
self.pred_traj = torch.from_numpy(
seq_list[:, :, self.obs_len:]).type(torch.float)
self.obs_traj_rel = torch.from_numpy(
seq_list_rel[:, :, :self.obs_len]).type(torch.float)
self.pred_traj_rel = torch.from_numpy(
seq_list_rel[:, :, self.obs_len:]).type(torch.float)
self.loss_mask = torch.from_numpy(loss_mask_list).type(torch.float)
self.non_linear_ped = torch.from_numpy(non_linear_ped).type(torch.float)
cum_start_idx = [0] + np.cumsum(num_peds_in_seq).tolist()
self.seq_start_end = [
(start, end)
for start, end in zip(cum_start_idx, cum_start_idx[1:])
]
# Convert to Graphs
self.v_obs = []
self.v_pred = []
print("Processing Data .....")
pbar = tqdm(total=len(self.seq_start_end))
for ss in range(len(self.seq_start_end)):
pbar.update(1)
start, end = self.seq_start_end[ss]
v_= seq_to_graph(self.obs_traj[start:end, :], self.obs_traj_rel[start:end, :], True)
self.v_obs.append(v_.clone())
v_= seq_to_graph(self.pred_traj[start:end, :], self.pred_traj_rel[start:end, :], False)
self.v_pred.append(v_.clone())
pbar.close()
def __len__(self):
return self.num_seq
def __getitem__(self, index):
start, end = self.seq_start_end[index]
out = [
self.obs_traj[start:end, :], self.pred_traj[start:end, :],
self.obs_traj_rel[start:end, :], self.pred_traj_rel[start:end, :],
self.non_linear_ped[start:end], self.loss_mask[start:end, :],
self.v_obs[index], self.v_pred[index]
]
return out
class RandomSampler:
def __init__(self, stack_n=1000, fast_sample=True):
self.stack_n = stack_n
self.pre_samples = []
self.fast_sample = fast_sample
def randn(self, n, k, d):
if self.fast_sample and len(self.pre_samples) > self.stack_n:
self.pre_samples = np.array(self.pre_samples)
return self.pre_samples[np.random.choice(np.arange(self.stack_n), n)]
randn_sample = []
for _ in range(n):
k_samples = KMeans(n_clusters=k).fit(np.random.normal(size=(1000, d)))
randn_sample.append(k_samples.cluster_centers_ * 0.8)
self.pre_samples.extend(randn_sample) if self.fast_sample else None
return np.array(randn_sample)
random = RandomSampler()
def generate_statistics_matrices(V):
r"""generate mean and covariance matrices from the network output."""
mu = V[:, :, 0:2]
sx = V[:, :, 2].exp()
sy = V[:, :, 3].exp()
corr = V[:, :, 4].tanh()
cov = torch.zeros(V.size(0), V.size(1), 2, 2, device=V.device)
cov[:, :, 0, 0] = sx * sx
cov[:, :, 0, 1] = corr * sx * sy
cov[:, :, 1, 0] = corr * sx * sy
cov[:, :, 1, 1] = sy * sy
return mu, cov
def compute_batch_metric(pred, gt):
"""Get ADE, FDE, TCC scores for each pedestrian"""
temp = (pred - gt).norm(p=2, dim=-1)
ADEs = temp.mean(dim=1).min(dim=0)[0]
FDEs = temp[:, -1, :].min(dim=0)[0]
pred_best = pred[temp[:, -1, :].argmin(dim=0), :, range(pred.size(2)), :]
pred_gt_stack = torch.stack([pred_best, gt.permute(1, 0, 2)], dim=0)
pred_gt_stack = pred_gt_stack.permute(3, 1, 0, 2)
covariance = pred_gt_stack - pred_gt_stack.mean(dim=-1, keepdim=True)
factor = 1 / (covariance.shape[-1] - 1)
covariance = factor * covariance @ covariance.transpose(-1, -2)
variance = covariance.diagonal(offset=0, dim1=-2, dim2=-1)
stddev = variance.sqrt()
corrcoef = covariance / stddev.unsqueeze(-1) / stddev.unsqueeze(-2)
corrcoef = corrcoef.clamp(-1, 1)
corrcoef[torch.isnan(corrcoef)] = 0
TCCs = corrcoef[:, :, 0, 1].mean(dim=0)
num_interp, thres = 4, 0.2
pred_fp = pred[:, [0], :, :]
pred_rel = pred[:, 1:] - pred[:, :-1]
pred_rel_dense = pred_rel.div(num_interp).unsqueeze(dim=2).repeat_interleave(repeats=num_interp, dim=2).contiguous()
pred_rel_dense = pred_rel_dense.reshape(pred.size(0), num_interp * (pred.size(1) - 1), pred.size(2), pred.size(3))
pred_dense = torch.cat([pred_fp, pred_rel_dense], dim=1).cumsum(dim=1)
col_mask = pred_dense[:, :3 * num_interp + 2].unsqueeze(dim=2).repeat_interleave(repeats=pred.size(2), dim=2)
col_mask = (col_mask - col_mask.transpose(2, 3)).norm(p=2, dim=-1)
col_mask = col_mask.add(torch.eye(n=pred.size(2), device=pred.device)[None, None, :, :]).min(dim=1)[0].lt(thres)
COLs = col_mask.sum(dim=1).gt(0).type(pred.type()).mean(dim=0).mul(100)
return ADEs, FDEs, COLs, TCCs