-
Notifications
You must be signed in to change notification settings - Fork 16
/
sddloader.py
407 lines (322 loc) · 13 KB
/
sddloader.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
import glob
import pickle
import torch
from torch.utils import data
import numpy as np
# Code for this dataloader is heavily borrowed from PECNet.
# https://github.com/HarshayuGirase/Human-Path-Prediction
'''for sanity check'''
def naive_social(p1_key, p2_key, all_data_dict):
if abs(p1_key-p2_key)<4:
return True
else:
return False
def find_min_time(t1, t2):
'''given two time frame arrays, find then min dist (time)'''
min_d = 9e4
t1, t2 = t1[:8], t2[:8]
for t in t2:
if abs(t1[0]-t)<min_d:
min_d = abs(t1[0]-t)
for t in t1:
if abs(t2[0]-t)<min_d:
min_d = abs(t2[0]-t)
return min_d
def find_min_dist(p1x, p1y, p2x, p2y):
'''given two time frame arrays, find then min dist'''
min_d = 9e4
p1x, p1y = p1x[:8], p1y[:8]
p2x, p2y = p2x[:8], p2y[:8]
for i in range(len(p1x)):
for j in range(len(p1x)):
if ((p2x[i]-p1x[j])**2 + (p2y[i]-p1y[j])**2)**0.5 < min_d:
min_d = ((p2x[i]-p1x[j])**2 + (p2y[i]-p1y[j])**2)**0.5
return min_d
def social_and_temporal_filter(p1_key, p2_key, all_data_dict, time_thresh=48, dist_tresh=100):
p1_traj, p2_traj = np.array(all_data_dict[p1_key]), np.array(all_data_dict[p2_key])
p1_time, p2_time = p1_traj[:,1], p2_traj[:,1]
p1_x, p2_x = p1_traj[:,2], p2_traj[:,2]
p1_y, p2_y = p1_traj[:,3], p2_traj[:,3]
if find_min_time(p1_time, p2_time)>time_thresh:
return False
if find_min_dist(p1_x, p1_y, p2_x, p2_y)>dist_tresh:
return False
return True
def mark_similar(mask, sim_list):
for i in range(len(sim_list)):
for j in range(len(sim_list)):
mask[sim_list[i]][sim_list[j]] = 1
def calc_interaction_nba(x):
# x: (N,T,2)
actor_num = x.shape[0]
length = x.shape[1]
# without ball interaction
player = x[:-1,:,:]
player_1 = player[None,:,:,:]
player_2 = player[:,None,:,:]
player_diff = player_2 - player_1
player_dist = torch.norm(player_diff,dim=-1,p=2)
player_mask = (player_dist<0.8)
interaction_player = (torch.sum(player_mask)-(actor_num*length))/2
ball = x[-1:,:,:]
distence = torch.norm(player - ball,dim=-1,p=2)
dist_mask = (distence<0.3)
weight = 10
interaction_ball = torch.sum(dist_mask) * weight
# close_dist,close_player = torch.min(distence,dim=0)
# close_player = (close_dist<0.3)*(close_player+1)
# noempty_loc = (close_player != 0)
# close_player = close_player[noempty_loc]
# interaction_ball_player = torch.sum(((close_player[1:]-close_player[:-1])!=0))
# weight = 20
# interaction_ball_player = interaction_ball_player * weight
return interaction_player + interaction_ball #+ interaction_ball_player
def collect_data(set_name, dataset_type = 'image', batch_size=512, time_thresh=48, dist_tresh=100, scene=None, verbose=True, root_path="./"):
assert set_name in ['train','val','test']
'''Please specify the parent directory of the dataset. In our case data was stored in:
root_path/trajnet_image/train/scene_name.txt
root_path/trajnet_image/test/scene_name.txt
'''
rel_path = '/trajnet_{0}/{1}/stanford'.format(dataset_type, set_name)
full_dataset = []
full_masks = []
current_batch = []
mask_batch = [[0 for i in range(int(batch_size*1.5))] for j in range(int(batch_size*1.5))]
current_size = 0
social_id = 0
part_file = '/{}.txt'.format('*' if scene == None else scene)
for file in glob.glob(root_path + rel_path + part_file):
scene_name = file[len(root_path+rel_path)+1:-6] + file[-5]
data = np.loadtxt(fname = file, delimiter = ' ')
data_by_id = {}
for frame_id, person_id, x, y in data:
if person_id not in data_by_id.keys():
data_by_id[person_id] = []
data_by_id[person_id].append([person_id, frame_id, x, y])
all_data_dict = data_by_id.copy()
if verbose:
print("Total People: ", len(list(data_by_id.keys())))
while len(list(data_by_id.keys()))>0:
related_list = []
curr_keys = list(data_by_id.keys())
if current_size<batch_size:
pass
else:
full_dataset.append(current_batch.copy())
mask_batch = np.array(mask_batch)
full_masks.append(mask_batch[0:len(current_batch), 0:len(current_batch)])
current_size = 0
social_id = 0
current_batch = []
mask_batch = [[0 for i in range(int(batch_size*1.5))] for j in range(int(batch_size*1.5))]
current_batch.append((all_data_dict[curr_keys[0]]))
related_list.append(current_size)
current_size+=1
del data_by_id[curr_keys[0]]
for i in range(1, len(curr_keys)):
if social_and_temporal_filter(curr_keys[0], curr_keys[i], all_data_dict, time_thresh, dist_tresh):
current_batch.append((all_data_dict[curr_keys[i]]))
related_list.append(current_size)
current_size+=1
del data_by_id[curr_keys[i]]
mark_similar(mask_batch, related_list)
social_id +=1
full_dataset.append(current_batch)
mask_batch = np.array(mask_batch)
full_masks.append(mask_batch[0:len(current_batch),0:len(current_batch)])
return full_dataset, full_masks
def generate_pooled_data(b_size, t_tresh, d_tresh, train=True, scene=None, verbose=True):
if train:
full_train, full_masks_train = collect_data("train", batch_size=b_size, time_thresh=t_tresh, dist_tresh=d_tresh, scene=scene, verbose=verbose)
train = [full_train, full_masks_train]
train_name = "../social_pool_data/train_{0}_{1}_{2}_{3}.pickle".format('all' if scene is None else scene[:-2] + scene[-1], b_size, t_tresh, d_tresh)
with open(train_name, 'wb') as f:
pickle.dump(train, f)
if not train:
full_test, full_masks_test = collect_data("test", batch_size=b_size, time_thresh=t_tresh, dist_tresh=d_tresh, scene=scene, verbose=verbose)
test = [full_test, full_masks_test]
test_name = "../social_pool_data/test_{0}_{1}_{2}_{3}.pickle".format('all' if scene is None else scene[:-2] + scene[-1], b_size, t_tresh, d_tresh)# + str(b_size) + "_" + str(t_tresh) + "_" + str(d_tresh) + ".pickle"
with open(test_name, 'wb') as f:
pickle.dump(test, f)
def initial_pos(traj_batches):
batches = []
for b in traj_batches:
starting_pos = b[:,7,:].copy()/1000 #starting pos is end of past, start of future. scaled down.
batches.append(starting_pos)
return batches
def initial_pos_new(traj_batches):
batches = []
for b in traj_batches:
starting_pos = b[:,7,:].copy()/1000 #starting pos is end of past, start of future. scaled down.
batches.append(starting_pos)
return batches
def offset_pos(traj_batches):
batches = []
for b in traj_batches:
starting_pos = b[:,0,:].copy() #starting pos is end of past, start of future. scaled down.
batches.append(starting_pos)
return batches
def calculate_loss(x, reconstructed_x, mean, log_var, criterion, future, interpolated_future):
# reconstruction loss
RCL_dest = criterion(x, reconstructed_x)
# RCL_dest = criterion(x,interpolated_future)
ADL_traj = criterion(future, interpolated_future) # better with l2 loss
# kl divergence loss
KLD = -0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp())
return RCL_dest, KLD, ADL_traj
def calculate_loss_mm(x, reconstructed_x, mean, log_var, criterion, future, interpolated_future):
# reconstruction loss
batch = x.shape[0]
actor_num = x.shape[1]
interpolated_future = interpolated_future.contiguous().view(batch,actor_num,-1)
reconstructed_x = reconstructed_x.contiguous().view(batch,actor_num,2)
loss_ade = torch.mean(torch.sum((interpolated_future-future)**2,dim=2),dim=1)
ADL_traj = loss_ade
loss_fde = torch.mean(torch.sum((x-reconstructed_x)**2,dim=2),dim=1)
RCL_dest = loss_fde
# RCL_dest = criterion(x, reconstructed_x)
# RCL_dest = criterion(x,interpolated_future)
# ADL_traj = criterion(future, interpolated_future) # better with l2 loss
# kl divergence loss
KLD = -0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp())
return RCL_dest, KLD, ADL_traj
def calculate_loss_multi(x, reconstructed_x, mean, log_var, criterion, future, interpolated_future):
# reconstruction loss
RCL_dest = criterion(x, reconstructed_x)
# RCL_dest = criterion(x,interpolated_future)
ADL_traj = criterion(future, interpolated_future)+5*criterion(future[:,:10], interpolated_future[:,:10]) \
# + 10*criterion(future[:,:2], interpolated_future[:,:2]) \
# + 8*criterion(future[:,:4], interpolated_future[:,:4]) \
# + 6*criterion(future[:,:6], interpolated_future[:,:6]) \
# + 4*criterion(future[:,:8], interpolated_future[:,:8]) \
# + criterion(future[:,:10], interpolated_future[:,:10])
# + criterion(future[:,:12], interpolated_future[:,:12]) \
# + criterion(future[:,:14], interpolated_future[:,:14]) \
# + criterion(future[:,:16], interpolated_future[:,:16])
# kl divergence loss
KLD = -0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp())
return RCL_dest, KLD, ADL_traj
def calculate_loss_double(x, reconstructed_x, mean, log_var, criterion, future, interpolated_future, mean2,log_var2):
# reconstruction loss
RCL_dest = criterion(x, reconstructed_x)
# RCL_dest = criterion(x,interpolated_future)
ADL_traj = criterion(future, interpolated_future) # better with l2 loss
# kl divergence loss
KLD = -0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp())
KLD_2 = -0.5 * torch.sum(1 + log_var2 - mean2.pow(2) - log_var2.exp())
return RCL_dest, KLD, ADL_traj, KLD_2
class SocialDataset(data.Dataset):
def __init__(self, set_name="train", b_size=4096, t_tresh=60, d_tresh=50, scene=None, id=False, verbose=False):
'Initialization'
load_name = "./data/{0}_{1}{2}_{3}_{4}.pickle".format(set_name, 'all_' if scene is None else scene[:-2] + scene[-1] + '_', b_size, t_tresh, d_tresh)
# print(load_name)
with open(load_name, 'rb') as f:
data = pickle.load(f)
traj, masks = data
traj_new = []
if id==False:
for t in traj:
t = np.array(t)
t = t[:,:,2:]
traj_new.append(t)
if set_name=="train":
#augment training set with reversed tracklets...
reverse_t = np.flip(t, axis=1).copy()
traj_new.append(reverse_t)
else:
for t in traj:
t = np.array(t)
traj_new.append(t)
if set_name=="train":
#augment training set with reversed tracklets...
reverse_t = np.flip(t, axis=1).copy()
traj_new.append(reverse_t)
masks_new = []
for m in masks:
masks_new.append(m)
if set_name=="train":
#add second time for the reversed tracklets...
masks_new.append(m)
seq_start_end_list = []
for m in masks:
total_num = m.shape[0]
scene_start_idx = 0
num_list = []
for i in range(total_num):
if i < scene_start_idx:
continue
scene_actor_num = np.sum(m[i])
scene_start_idx += scene_actor_num
num_list.append(scene_actor_num)
cum_start_idx = [0] + np.cumsum(np.array(num_list)).tolist()
seq_start_end = [(start, end) for start, end in zip(cum_start_idx, cum_start_idx[1:])]
seq_start_end_list.append(seq_start_end)
if set_name=="train":
#add second time for the reversed tracklets...
seq_start_end_list.append(seq_start_end)
traj_new = np.array(traj_new)
masks_new = np.array(masks_new)
self.trajectory_batches = traj_new.copy()
self.mask_batches = masks_new.copy()
self.initial_pos_batches = np.array(initial_pos(self.trajectory_batches)) #for relative positioning
self.seq_start_end_batches = seq_start_end_list
if verbose:
print("Initialized social dataloader...")
class SocialDataset_new(data.Dataset):
def __init__(self, set_name="train", b_size=4096, t_tresh=60, d_tresh=50, scene=None, id=False, verbose=True):
'Initialization'
load_name = "./data/{0}_{1}{2}_{3}_{4}.pickle".format(set_name, 'all_' if scene is None else scene[:-2] + scene[-1] + '_', b_size, t_tresh, d_tresh)
print(load_name)
with open(load_name, 'rb') as f:
data = pickle.load(f)
traj, masks = data
traj_new = []
if id==False:
for t in traj:
t = np.array(t)
t = t[:,:,2:]
traj_new.append(t)
if set_name=="train":
#augment training set with reversed tracklets...
reverse_t = np.flip(t, axis=1).copy()
traj_new.append(reverse_t)
else:
for t in traj:
t = np.array(t)
traj_new.append(t)
if set_name=="train":
#augment training set with reversed tracklets...
reverse_t = np.flip(t, axis=1).copy()
traj_new.append(reverse_t)
masks_new = []
for m in masks:
masks_new.append(m)
if set_name=="train":
#add second time for the reversed tracklets...
masks_new.append(m)
seq_start_end_list = []
for m in masks:
total_num = m.shape[0]
scene_start_idx = 0
num_list = []
for i in range(total_num):
if i < scene_start_idx:
continue
scene_actor_num = np.sum(m[i])
scene_start_idx += scene_actor_num
num_list.append(scene_actor_num)
cum_start_idx = [0] + np.cumsum(np.array(num_list)).tolist()
seq_start_end = [(start, end) for start, end in zip(cum_start_idx, cum_start_idx[1:])]
seq_start_end_list.append(seq_start_end)
if set_name=="train":
#add second time for the reversed tracklets...
seq_start_end_list.append(seq_start_end)
traj_new = np.array(traj_new)
masks_new = np.array(masks_new)
self.trajectory_batches = traj_new.copy()
self.mask_batches = masks_new.copy()
self.initial_pos_batches = np.array(initial_pos_new(self.trajectory_batches)) #for relative positioning
self.seq_start_end_batches = seq_start_end_list
self.offset_batches = np.array(offset_pos(self.trajectory_batches))
if verbose:
print("Initialized social dataloader...")