-
Notifications
You must be signed in to change notification settings - Fork 1
/
datasets.py
557 lines (474 loc) · 20.1 KB
/
datasets.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import json
import os
import random
from PIL import Image
import cv2
import torch
from torch.utils.data import Dataset, Sampler
import torch.nn.functional as F
import torchvision
from utils import get_logger
import pickle
import numpy as np
import math
from IPython import embed
import copy
import collections
class TripletSampler(Sampler):
def __init__(self, data_source, batch_size, num_instances=4):
self.data_source = data_source
self.batch_size = batch_size
self.num_instances = num_instances
self.num_pids_per_batch = batch_size // num_instances
self.index_dict = dict()
for idx, (uuid, pid) in enumerate(data_source):
if pid not in self.index_dict:
self.index_dict[pid] = [idx]
else:
self.index_dict[pid].append(idx)
self.pids = list(self.index_dict.keys())
self.length = len(data_source) // num_instances
def __iter__(self):
batch_idxs_dict = collections.defaultdict(list)
for pid in self.pids:
idxs = copy.deepcopy(self.index_dict[pid])
if len(idxs) < self.num_instances:
idxs = np.random.choice(idxs, size=self.num_instances, replace=True)
random.shuffle(idxs)
batch_idxs = []
for idx in idxs:
batch_idxs.append(idx)
if len(batch_idxs) == self.num_instances:
batch_idxs_dict[pid].append(batch_idxs)
batch_idxs = []
avai_pids = copy.deepcopy(self.pids)
final_idxs = []
while len(avai_pids) >= self.num_pids_per_batch:
selected_pids = random.sample(avai_pids, self.num_pids_per_batch)
for pid in selected_pids:
batch_idxs = batch_idxs_dict[pid].pop(0)
final_idxs.extend(batch_idxs)
if len(batch_idxs_dict[pid]) == 0:
avai_pids.remove(pid)
self.length = len(final_idxs)
return iter(final_idxs)
def __len__(self):
return self.length
class CityFlowNLDataset(Dataset):
def __init__(self, data_cfg, json_path, transform=None, Random=True):
"""
Dataset for training.
:param data_cfg: CfgNode for CityFlow NL.
"""
self.data_cfg = data_cfg.clone()
self.crop_area = data_cfg.CROP_AREA
self.motion_aug = data_cfg.MOTION_AUG
self.random = Random
assert "nlpaug" in json_path
with open(json_path) as f:
tracks = json.load(f)
self.list_of_uuids = list(tracks.keys())
self.list_of_tracks = list(tracks.values())
self.transform = transform
self.bk_dic = {}
self._logger = get_logger()
self.all_indexs = list(range(len(self.list_of_uuids)))
self.flip_tag = [False]*len(self.list_of_uuids)
self.pid_info = False
if 'pid' in json_path:
self.pid_info = True
print(json_path)
if self.pid_info:
all_pids = []
for track in self.list_of_tracks:
all_pids.append(track['pid'][0])
all_pids = set(all_pids)
self.pid2label = dict()
for i, pid in enumerate(all_pids):
self.pid2label[pid] = i
self.cam2label = dict()
for i in range(40):
camera = 'c%03d' % (i+1)
self.cam2label[camera] = i-1
flip_aug = False
if flip_aug:
for i in range(len(self.list_of_uuids)):
text = self.list_of_tracks[i]["nl"]
for j in range(len(text)):
nl = text[j]
if "turn" in nl:
if "left" in nl:
self.all_indexs.append(i)
self.flip_tag.append(True)
break
elif "right" in nl:
self.all_indexs.append(i)
self.flip_tag.append(True)
break
if self.pid_info:
self.train = []
for uuid, track in tracks.items():
pid = self.pid2label[track["pid"][0]]
self.train.append((uuid, pid))
print(len(self.all_indexs))
print("data load")
def __len__(self):
return len(self.all_indexs)
def __getitem__(self, index):
tmp_index = self.all_indexs[index]
uuid = self.list_of_uuids[index]
flag = self.flip_tag[index]
track = self.list_of_tracks[tmp_index]
if self.pid_info:
pid = self.pid2label[track["pid"][0]]
else:
pid = 0
if self.random:
nl_idx = int(random.uniform(0, 3))
frame_idx = int(random.uniform(0, len(track["frames"])))
else:
nl_idx = 2
frame_idx = 0
direction = 0
num_left = 0
num_right = 0
location = 0
for sent in track["nl"]:
if "intersection" in sent:
location = 1
if 'turn' in sent:
if "left" in sent:
num_left += 1
if "right" in sent:
num_right += 1
if num_left > num_right:
direction = 1
if num_left < num_right:
direction = 2
text = track["nl"][nl_idx]
if self.motion_aug:
ori_text = text.split('.')[1]
if direction == 0:
text = 'straight. ' + ori_text
if direction == 1:
text = 'left. ' + ori_text
if direction == 2:
text = 'right. ' + ori_text
car_text = text.split('.')[0]
motion_text = car_text
if direction == 0:
motion_text = motion_text + ' goes straight'
if direction == 1:
motion_text = motion_text + ' turns left'
if direction == 2:
motion_text = motion_text + ' turns right'
if location == 1:
if location == 1:
if direction == '0':
motion_text = motion_text + ' through the intersection.'
else:
motion_text = motion_text + ' at the intersection.'
if flag:
text = text.replace("left", "888888").replace("right", "left").replace("888888", "right")
frame_path = os.path.join(self.data_cfg.CITYFLOW_PATH, track["frames"][frame_idx])
frame = Image.open(frame_path)
box = track["boxes"][frame_idx]
camera = frame_path.split('/')[-3]
camera_id = self.cam2label[camera]
if self.crop_area == 1.6666667:
box = (int(box[0]-box[2]/3.), int(box[1]-box[3]/3.), int(box[0]+4*box[2]/3.), int(box[1]+4*box[3]/3.))
else:
box = (int(box[0]-(self.crop_area-1)*box[2]/2.), int(box[1]-(self.crop_area-1)*box[3]/2), int(box[0]+(self.crop_area+1)*box[2]/2.), int(box[1]+(self.crop_area+1)*box[3]/2.))
crop = frame.crop(box)
if flag:
crop = torch.flip(crop, [1])
crop_data = self.transform(crop)
frame.close()
if self.data_cfg.USE_MOTION:
bk_path = self.data_cfg.MOTION_PATH+"/%s.jpg" % self.list_of_uuids[tmp_index]
bk = Image.open(bk_path)
bk_data = self.transform(bk)
bk.close()
return {
"uuid": uuid,
"crop_data": crop_data,
"text": text,
"car_text": car_text,
"bk_data": bk_data,
"tmp_index": tmp_index,
"camera_id": camera_id,
"direction": direction,
"location_id": location,
"motion_text": motion_text,
"pid": pid,
}
return {
"uuid": uuid,
"crop_data": crop_data,
"text": text,
"car_text": car_text,
"tmp_index": tmp_index,
"camera_id": camera_id,
"direction": direction,
"location_id": location,
"motion_text": motion_text,
"pid": pid,
}
class CityFlowNLInferenceDataset(Dataset):
def __init__(self, data_cfg,transform = None):
"""Dataset for evaluation. Loading tracks instead of frames."""
self.data_cfg = data_cfg
self.crop_area = data_cfg.CROP_AREA
self.transform = transform
with open(self.data_cfg.TEST_TRACKS_JSON_PATH) as f:
tracks = json.load(f)
self.list_of_uuids = list(tracks.keys())
self.list_of_tracks = list(tracks.values())
self.list_of_crops = list()
for track_id_index, track in enumerate(self.list_of_tracks):
for frame_idx, frame in enumerate(track["frames"]):
frame_path = os.path.join(self.data_cfg.CITYFLOW_PATH, frame)
box = track["boxes"][frame_idx]
crop = {"frame": frame_path, "frames_id":frame_idx,"track_id": self.list_of_uuids[track_id_index], "box": box}
self.list_of_crops.append(crop)
self._logger = get_logger()
def __len__(self):
return len(self.list_of_crops)
def __getitem__(self, index):
track = self.list_of_crops[index]
frame_path = track["frame"]
box = track["box"]
if self.crop_area == 1.6666667:
box = (int(box[0]-box[2]/3.),int(box[1]-box[3]/3.), int(box[0]+4*box[2]/3.),int(box[1]+4*box[3]/3.))
else:
box = (int(box[0]-(self.crop_area-1)*box[2]/2.),int(box[1]-(self.crop_area-1)*box[3]/2),int(box[0]+(self.crop_area+1)*box[2]/2.),int(box[1]+(self.crop_area+1)*box[3]/2.))
frame = Image.open(frame_path)
crop = frame.crop(box)
frame.close()
if self.transform is not None:
crop = self.transform(crop)
if self.data_cfg.USE_MOTION:
bk = Image.open(self.data_cfg.MOTION_PATH+"/%s.jpg" % track["track_id"])
bk_data = self.transform(bk)
bk.close()
return crop, bk_data, track["track_id"], track["frames_id"]
return crop, track["track_id"], track["frames_id"]
class CityFlowNLDatasetv2(Dataset):
def __init__(self, data_cfg, json_path, transform=None, Random=True, pid_info=False, transform_paf= None):
"""
Dataset for training.
:param data_cfg: CfgNode for CityFlow NL.
"""
self.data_cfg = data_cfg.clone()
self.crop_area = data_cfg.CROP_AREA
self.random = Random
with open(json_path) as f:
tracks = json.load(f)
self.list_of_uuids = list(tracks.keys())
self.list_of_tracks = list(tracks.values())
self.transform = transform
self.transform_paf = transform_paf
self.paf_width = 40
self._logger = get_logger()
self.all_indexs = list(range(len(self.list_of_uuids)))
self.flip_tag = [False] * len(self.list_of_uuids)
self.pid_info = pid_info
if self.pid_info:
all_pids = []
for track in self.list_of_tracks:
all_pids.append(track['pid'][0])
all_pids = set(all_pids)
self.pid2label = dict()
for i, pid in enumerate(all_pids):
self.pid2label[pid] = i
flip_aug = False
if flip_aug:
for i in range(len(self.list_of_uuids)):
text = self.list_of_tracks[i]["nl"]
for j in range(len(text)):
nl = text[j]
if "turn" in nl:
if "left" in nl:
self.all_indexs.append(i)
self.flip_tag.append(True)
break
elif "right" in nl:
self.all_indexs.append(i)
self.flip_tag.append(True)
break
print(len(self.all_indexs))
print("data load")
def __len__(self):
return len(self.all_indexs)
def set_paf_map(self, paf_map: np.array, x_a: int, y_a: int, x_b: int, y_b: int, width: int = 50) -> None:
x_ba = x_b - x_a
y_ba = y_b - y_a
h_map, w_map, _ = paf_map.shape
x_min = int(max(min(x_a, x_b) - width, 0))
x_max = int(min(max(x_a, x_b) + width, w_map))
y_min = int(max(min(y_a, y_b) - width, 0))
y_max = int(min(max(y_a, y_b) + width, h_map))
norm_ba = math.sqrt((x_ba * x_ba + y_ba * y_ba))
if norm_ba < 1e-7:
return
x_ba = x_ba / norm_ba
y_ba = y_ba / norm_ba
for y in range(y_min, y_max):
for x in range(x_min, x_max):
x_ca = x - x_a
y_ca = y - y_a
d = math.fabs(x_ca * y_ba - y_ca * x_ba)
if d <= width:
if paf_map[y, x, 0] > 1e-7:
paf_map[y, x, 0] = (x_ba + paf_map[y, x, 0]) / 2
paf_map[y, x, 1] = (y_ba + paf_map[y, x, 1]) / 2
else:
paf_map[y, x, 0] = x_ba
paf_map[y, x, 1] = y_ba
def generate_paf_map(self, frames: list, bboxes: list, root: str) -> np.array:
centroids = []
for box in bboxes:
x = int(box[0] + 1 / 2 * box[2])
y = int(box[1] + 1 / 2 * box[3])
centroids.append((x, y))
height, width, _ = cv2.imread(os.path.join(root, frames[0])).shape
paf_map = np.zeros((height, width, 2))
assert len(centroids) == len(frames)
for i in range(len(frames) - 1):
p1 = centroids[i]
p2 = centroids[i + 1]
self.set_paf_map(paf_map, p1[0], p1[1], p2[0], p2[1], self.paf_width)
return paf_map
def __getitem__(self, index):
tmp_index = self.all_indexs[index]
flag = self.flip_tag[index]
track = self.list_of_tracks[tmp_index]
uuid = self.list_of_uuids[tmp_index]
if self.random:
nl_idx = int(random.uniform(0, 3))
frame_idx = int(random.uniform(0, len(track["frames"])))
else:
nl_idx = 2
frame_idx = 0
text = track["nl"][nl_idx]
if flag:
text = text.replace("left", "888888").replace("right", "left").replace("888888", "right")
frame_path = os.path.join(self.data_cfg.CITYFLOW_PATH, track["frames"][frame_idx])
frame = Image.open(frame_path)
# frame.load()
box = track["boxes"][frame_idx]
if self.crop_area == 1.6666667:
box = (int(box[0] - box[2] / 3.), int(box[1] - box[3] / 3.), int(box[0] + 4 * box[2] / 3.),
int(box[1] + 4 * box[3] / 3.))
else:
box = (int(box[0] - (self.crop_area - 1) * box[2] / 2.), int(box[1] - (self.crop_area - 1) * box[3] / 2),
int(box[0] + (self.crop_area + 1) * box[2] / 2.), int(box[1] + (self.crop_area + 1) * box[3] / 2.))
crop = frame.crop(box)
crop_data = self.transform(crop)
if self.data_cfg.USE_MOTION:
# paf_path = os.path.join(self.data_cfg.PAF_PATH, self.list_of_uuids[tmp_index] + '.pkl')
# with open(paf_path, 'rb') as fb:
# paf_map = pickle.load(fb)
# paf_map = self.generate_paf_map(track['frames'], track['boxes'], self.data_cfg.CITYFLOW_PATH)
with open('data/paf_maps/%s.pkl' % uuid, 'rb') as fb:
paf_map = pickle.load(fb)
# print(paf_map.shape)
paf_map = self.transform_paf(paf_map)
# print(paf_map.shape)
frame_img = np.array(frame)
# print(frame_img.shape)
frame_img = self.transform(frame_img)
# print(frame_img.shape)
motion_img = torch.cat([frame_img, paf_map], dim=0).float()
return crop_data, text, motion_img, tmp_index
if flag:
crop = torch.flip(crop, [1])
# frame.close()
return crop_data, text, tmp_index
class CityFlowNLInferenceDatasetv2(Dataset):
def __init__(self, data_cfg,transform = None, transform_paf= None):
"""Dataset for evaluation. Loading tracks instead of frames."""
self.data_cfg = data_cfg
self.crop_area = data_cfg.CROP_AREA
self.transform = transform
with open(self.data_cfg.TEST_TRACKS_JSON_PATH) as f:
tracks = json.load(f)
self.list_of_uuids = list(tracks.keys())
self.list_of_tracks = list(tracks.values())
self.list_of_crops = list()
self.paf_width = 40
self.transform_paf = transform_paf
for track_id_index,track in enumerate(self.list_of_tracks):
for frame_idx, frame in enumerate(track["frames"]):
frame_path = os.path.join(self.data_cfg.CITYFLOW_PATH, frame)
box = track["boxes"][frame_idx]
crop = {"frame": frame_path, "frames_id":frame_idx,"track_id": self.list_of_uuids[track_id_index], "box": box, "frames": track["frames"], "boxes": track["boxes"]}
self.list_of_crops.append(crop)
self._logger = get_logger()
def __len__(self):
return len(self.list_of_crops)
def set_paf_map(self, paf_map: np.array, x_a: int, y_a: int, x_b: int, y_b: int, width: int = 50) -> None:
x_ba = x_b - x_a
y_ba = y_b - y_a
h_map, w_map, _ = paf_map.shape
x_min = int(max(min(x_a, x_b) - width, 0))
x_max = int(min(max(x_a, x_b) + width, w_map))
y_min = int(max(min(y_a, y_b) - width, 0))
y_max = int(min(max(y_a, y_b) + width, h_map))
norm_ba = math.sqrt((x_ba * x_ba + y_ba * y_ba))
if norm_ba < 1e-7:
return
x_ba = x_ba / norm_ba
y_ba = y_ba / norm_ba
for y in range(y_min, y_max):
for x in range(x_min, x_max):
x_ca = x - x_a
y_ca = y - y_a
d = math.fabs(x_ca * y_ba - y_ca * x_ba)
if d <= width:
if paf_map[y, x, 0] > 1e-7:
paf_map[y, x, 0] = (x_ba + paf_map[y, x, 0]) / 2
paf_map[y, x, 1] = (y_ba + paf_map[y, x, 1]) / 2
else:
paf_map[y, x, 0] = x_ba
paf_map[y, x, 1] = y_ba
def generate_paf_map(self, frames: list, bboxes: list, root: str) -> np.array:
centroids = []
for box in bboxes:
x = int(box[0] + 1 / 2 * box[2])
y = int(box[1] + 1 / 2 * box[3])
centroids.append((x, y))
height, width, _ = cv2.imread(os.path.join(root, frames[0])).shape
paf_map = np.zeros((height, width, 2))
assert len(centroids) == len(frames)
for i in range(len(frames) - 1):
p1 = centroids[i]
p2 = centroids[i + 1]
self.set_paf_map(paf_map, p1[0], p1[1], p2[0], p2[1], self.paf_width)
return paf_map
def __getitem__(self, index):
track = self.list_of_crops[index]
frame_path = track["frame"]
box = track["box"]
uuid = track['track_id']
if self.crop_area == 1.6666667:
box = (int(box[0]-box[2]/3.),int(box[1]-box[3]/3.),int(box[0]+4*box[2]/3.),int(box[1]+4*box[3]/3.))
else:
box = (int(box[0]-(self.crop_area-1)*box[2]/2.),int(box[1]-(self.crop_area-1)*box[3]/2),int(box[0]+(self.crop_area+1)*box[2]/2.),int(box[1]+(self.crop_area+1)*box[3]/2.))
frame = Image.open(frame_path)
crop = frame.crop(box)
if self.transform is not None:
crop = self.transform(crop)
if self.data_cfg.USE_MOTION:
# paf_path = os.path.join(self.data_cfg.PAF_PATH, tracks["track_id"] + '.pkl')
# with open(paf_path, 'rb') as fb:
# paf_map = pickle.load(fb)
# paf_map = self.generate_paf_map(track['frames'], track['boxes'], self.data_cfg.CITYFLOW_PATH)
with open('data/paf_maps/%s.pkl' % uuid, 'rb') as fb:
paf_map = pickle.load(fb)
paf_map = self.transform_paf(paf_map)
frame_img = np.array(frame)
frame_img = self.transform(frame_img)
motion_img = torch.cat([frame_img, paf_map], dim=0).float()
return crop, motion_img, track["track_id"], track["frames_id"]
return crop, track["track_id"], track["frames_id"]