-
Notifications
You must be signed in to change notification settings - Fork 39
/
vid.py
763 lines (669 loc) · 27.7 KB
/
vid.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Copyright (c) Megvii, Inc. and its affiliates.
import copy
import os
import random
import numpy
from loguru import logger
import cv2
import numpy as np
import torch
from torch.utils.data.dataset import Dataset as torchDataset
from torch.utils.data.sampler import Sampler,BatchSampler,SequentialSampler
from xml.dom import minidom
import math
from yolox.utils import xyxy2cxcywh
IMAGE_EXT = [".jpg", ".jpeg", ".webp", ".bmp", ".png",".JPEG"]
XML_EXT = [".xml"]
name_list = ['n02691156','n02419796','n02131653','n02834778','n01503061','n02924116','n02958343','n02402425','n02084071','n02121808','n02503517','n02118333','n02510455','n02342885','n02374451','n02129165','n01674464','n02484322','n03790512','n02324045','n02509815','n02411705','n01726692','n02355227','n02129604','n04468005','n01662784','n04530566','n02062744','n02391049']
numlist = range(30)
name_num = dict(zip(name_list,numlist))
class VIDDataset(torchDataset):
"""
VID sequence
"""
def __init__(
self,
file_path="train_seq.npy",
img_size=(416, 416),
preproc=None,
lframe = 18,
gframe = 6,
val = False,
mode='random',
dataset_pth = '',
tnum = 1000
):
"""
COCO dataset initialization. Annotation data are read into memory by COCO API.
Args:
data_dir (str): dataset root directory
json_file (str): COCO json file name
name (str): COCO data name (e.g. 'train2017' or 'val2017')
img_size (int): target image size after pre-processing
preproc: data augmentation strategy
"""
super().__init__()
self.tnum = tnum
self.input_dim = img_size
self.file_path = file_path
self.mode = mode # random, continous, uniform
self.img_size = img_size
self.preproc = preproc
self.val = val
self.res = self.photo_to_sequence(self.file_path,lframe,gframe)
self.dataset_pth = dataset_pth
def __len__(self):
return len(self.res)
def photo_to_sequence(self,dataset_path,lframe,gframe):
'''
Args:
dataset_path: list,every element is a list contain all frames in a video dir
Returns:
split result
'''
res = []
dataset = np.load(dataset_path,allow_pickle=True).tolist()
for element in dataset:
ele_len = len(element)
if ele_len<lframe+gframe:
#TODO fix the unsolved part
#res.append(element)
continue
else:
if self.mode == 'random':
split_num = int(ele_len / (gframe))
random.shuffle(element)
for i in range(split_num):
res.append(element[i * gframe:(i + 1) * gframe])
elif self.mode == 'uniform':
split_num = int(ele_len / (gframe))
all_uniform_frame = element[:split_num * gframe]
for i in range(split_num):
res.append(all_uniform_frame[i::split_num])
elif self.mode == 'gl':
split_num = int(ele_len / (lframe))
all_local_frame = element[:split_num * lframe]
for i in range(split_num):
g_frame = random.sample(element[:i * lframe] + element[(i + 1) * lframe:], gframe)
res.append(all_local_frame[i * lframe:(i + 1) * lframe] + g_frame)
else:
print('unsupport mode, exit')
exit(0)
# test = []
# for ele in res:
# test.extend(ele)
# random.shuffle(test)
# i = 0
# for ele in res:
# for j in range(gframe):
# ele[j] = test[i]
# i += 1
if self.val:
random.seed(42)
random.shuffle(res)
if self.tnum == -1:
return res
else:
return res[:self.tnum]#[1000:1250]#[2852:2865]
else:
random.shuffle(res)
return res[:15000]
def get_annotation(self,path,test_size):
path = path.replace("Data","Annotations").replace("JPEG","xml")
if os.path.isdir(path):
files = get_xml_list(path)
else:
files = [path]
files.sort()
anno_res = []
for xmls in files:
photoname = xmls.replace("Annotations","Data").replace("xml","JPEG")
file = minidom.parse(xmls)
root = file.documentElement
objs = root.getElementsByTagName("object")
width = int(root.getElementsByTagName('width')[0].firstChild.data)
height = int(root.getElementsByTagName('height')[0].firstChild.data)
tempnode = []
for obj in objs:
nameNode = obj.getElementsByTagName("name")[0].firstChild.data
xmax = int(obj.getElementsByTagName("xmax")[0].firstChild.data)
xmin = int(obj.getElementsByTagName("xmin")[0].firstChild.data)
ymax = int(obj.getElementsByTagName("ymax")[0].firstChild.data)
ymin = int(obj.getElementsByTagName("ymin")[0].firstChild.data)
x1 = np.max((0,xmin))
y1 = np.max((0,ymin))
x2 = np.min((width,xmax))
y2 = np.min((height,ymax))
if x2 >= x1 and y2 >= y1:
#tempnode.append((name_num[nameNode],x1,y1,x2,y2,))
tempnode.append(( x1, y1, x2, y2,name_num[nameNode],))
num_objs = len(tempnode)
res = np.zeros((num_objs, 5))
r = min(test_size[0] / height, test_size[1] / width)
for ix, obj in enumerate(tempnode):
res[ix, 0:5] = obj[0:5]
res[:, :-1] *= r
anno_res.append(res)
return anno_res
def pull_item(self,path):
"""
One image / label pair for the given index is picked up and pre-processed.
Args:
index (int): data index
Returns:
img (numpy.ndarray): pre-processed image
padded_labels (torch.Tensor): pre-processed label data.
The shape is :math:`[max_labels, 5]`.
each label consists of [class, xc, yc, w, h]:
class (float): class index.
xc, yc (float) : center of bbox whose values range from 0 to 1.
w, h (float) : size of bbox whose values range from 0 to 1.
info_img : tuple of h, w.
h, w (int): original shape of the image
img_id (int): same as the input index. Used for evaluation.
"""
path = os.path.join(self.dataset_pth,path)
annos = self.get_annotation(path, self.img_size)[0]
img = cv2.imread(path)
height, width = img.shape[:2]
img_info = (height, width)
r = min(self.img_size[0] / img.shape[0], self.img_size[1] / img.shape[1])
img = cv2.resize(
img,
(int(img.shape[1] * r), int(img.shape[0] * r)),
interpolation=cv2.INTER_LINEAR,
).astype(np.uint8)
return img, annos, img_info, path
def __getitem__(self, path):
img, target, img_info, path = self.pull_item(path)
if self.preproc is not None:
img, target = self.preproc(img, target, self.input_dim)
return img, target, img_info,path
class Arg_VID(torchDataset):
"""
VID sequence
"""
def __init__(
self,
data_dir='/media/tuf/ssd/Argoverse-1.1/',
img_size=(416, 640),
preproc=None,
lframe = 0,
gframe = 16,
val = False,
mode='random',
COCO_anno = '',
name = "tracking",
):
"""
COCO dataset initialization. Annotation data are read into memory by COCO API.
Args:
data_dir (str): dataset root directory
json_file (str): COCO json file name
name (str): COCO data name (e.g. 'train2017' or 'val2017')
img_size (int): target image size after pre-processing
preproc: data augmentation strategy
"""
super().__init__()
self.input_dim = img_size
self.name = name
self.val = val
self.data_dir = data_dir
self.img_size = img_size
self.coco_anno_path = COCO_anno
self.name_id_dic = self.get_NameId_dic()
self.coco = COCO(COCO_anno)
remove_useless_info(self.coco)
self.ids = sorted(self.coco.getImgIds())
self.class_ids = sorted(self.coco.getCatIds())
cats = self.coco.loadCats(self.coco.getCatIds())
self._classes = tuple([c["name"] for c in cats])
self.annotations = self._load_coco_annotations()
self.mode = mode # random, continous, uniform
self.preproc = preproc
self.res = self.photo_to_sequence(lframe,gframe)
def get_NameId_dic(self):
img_dic = {}
with open(self.coco_anno_path,'r') as train_anno_content:
train_anno_content = json.load(train_anno_content)
for im in train_anno_content['images']:
img_dic[im['name']] = im['id']
return img_dic
def _load_coco_annotations(self):
return [self.load_anno_from_ids(_ids) for _ids in self.ids]
def __len__(self):
return len(self.res)
def load_anno_from_ids(self, id_):
im_ann = self.coco.loadImgs(id_)[0]
width = im_ann["width"]
height = im_ann["height"]
im_ann['name'] = self.coco.dataset['seq_dirs'][im_ann['sid']] + '/' + im_ann['name']
anno_ids = self.coco.getAnnIds(imgIds=[int(id_)], iscrowd=False)
annotations = self.coco.loadAnns(anno_ids)
objs = []
for obj in annotations:
x1 = np.max((0, obj["bbox"][0]))
y1 = np.max((0, obj["bbox"][1]))
x2 = np.min((width, x1 + np.max((0, obj["bbox"][2]))))
y2 = np.min((height, y1 + np.max((0, obj["bbox"][3]))))
if obj["area"] > 0 and x2 >= x1 and y2 >= y1:
obj["clean_bbox"] = [x1, y1, x2, y2]
objs.append(obj)
num_objs = len(objs)
res = np.zeros((num_objs, 5))
for ix, obj in enumerate(objs):
cls = self.class_ids.index(obj["category_id"])
res[ix, 0:4] = obj["clean_bbox"]
res[ix, 4] = cls
r = min(self.img_size[0] / height, self.img_size[1] / width)
res[:, :4] *= r
img_info = (height, width)
resized_info = (int(height * r), int(width * r))
file_name = (
im_ann["name"]
if "name" in im_ann
else "{:012}".format(id_) + ".jpg"
)
return (res, img_info, resized_info, file_name)
def photo_to_sequence(self,lframe,gframe, seq_len = 192):
'''
Args:
dataset_path: list,every element is a list contain all frame in a video dir
Returns:
split result
'''
res = []
with open(self.coco_anno_path, 'r') as anno:
anno = json.load(anno)
dataset = [[] for i in range(len(anno['sequences']))]
for im in anno['images']:
dataset[im['sid']].append(self.coco.dataset['seq_dirs'][im['sid']] + '/' + im['name'])
for ele in dataset:
sorted(ele)
for element in dataset:
ele_len = len(element)
if ele_len<lframe+gframe:
#TODO fix the unsolved part
#res.append(element)
continue
else:
if self.mode == 'random':
# split_num = int(ele_len / (gframe))
# random.shuffle(element)
# for i in range(split_num):
# res.append(element[i * gframe:(i + 1) * gframe])
# if self.val and element[(i + 1) * gframe:] != []:
# res.append(element[(i + 1) * gframe:])
seq_split_num = int(len(element) / seq_len)
for k in range(seq_split_num + 1):
tmp = element[k * seq_len:(k + 1) * seq_len]
if tmp == []:continue
random.shuffle(tmp)
split_num = int(len(tmp) / (gframe))
for i in range(split_num):
res.append(tmp[i * gframe:(i + 1) * gframe])
if self.val and tmp[(i + 1) * gframe:] != []:
res.append(tmp[(i + 1) * gframe:])
elif self.mode == 'uniform':
split_num = int(ele_len / (gframe))
all_uniform_frame = element[:split_num * gframe]
for i in range(split_num):
res.append(all_uniform_frame[i::split_num])
elif self.mode == 'gl':
split_num = int(ele_len / (lframe))
all_local_frame = element[:split_num * lframe]
for i in range(split_num):
g_frame = random.sample(element[:i * lframe] + element[(i + 1) * lframe:], gframe)
res.append(all_local_frame[i * lframe:(i + 1) * lframe] + g_frame)
else:
print('unsupport mode, exit')
exit(0)
if self.val:
# random.seed(42)
# random.shuffle(res)
return res#[:1000]#[1000:1250]#[2852:2865]
else:
random.shuffle(res)
return res#[:1000]#[:15000]
def pull_item(self,path):
"""
One image / label pair for the given index is picked up and pre-processed.
Args:
index (int): data index
Returns:
img (numpy.ndarray): pre-processed image
padded_labels (torch.Tensor): pre-processed label data.
The shape is :math:`[max_labels, 5]`.
each label consists of [class, xc, yc, w, h]:
class (float): class index.
xc, yc (float) : center of bbox whose values range from 0 to 1.
w, h (float) : size of bbox whose values range from 0 to 1.
info_img : tuple of h, w.
h, w (int): original shape of the image
img_id (int): same as the input index. Used for evaluation.
"""
path = path.split('/')[-1]
idx = self.name_id_dic[path]
annos, img_info, resized_info, img_path = self.annotations[idx]
abs_path = os.path.join(self.data_dir, self.name, img_path)
img = cv2.imread(abs_path)
height, width = img.shape[:2]
img_info = (height, width)
r = min(self.img_size[0] / img.shape[0], self.img_size[1] / img.shape[1])
img = cv2.resize(
img,
(int(img.shape[1] * r), int(img.shape[0] * r)),
interpolation=cv2.INTER_LINEAR,
).astype(np.uint8)
return img, annos.copy(), img_info, img_path
def __getitem__(self, path):
img, target, img_info, path = self.pull_item(path)
if self.preproc is not None:
img, target = self.preproc(img, target, self.input_dim)
return img, target, img_info,path
class OVIS(Arg_VID):
def load_anno_from_ids(self, id_):
im_ann = self.coco.loadImgs(id_)[0]
width = im_ann["width"]
height = im_ann["height"]
#im_ann['name'] = self.coco.dataset['seq_dirs'][im_ann['sid']] + '/' + im_ann['name']
anno_ids = self.coco.getAnnIds(imgIds=[int(id_)], iscrowd=False)
annotations = self.coco.loadAnns(anno_ids)
objs = []
for obj in annotations:
x1 = np.max((0, obj["bbox"][0]))
y1 = np.max((0, obj["bbox"][1]))
x2 = np.min((width, x1 + np.max((0, obj["bbox"][2]))))
y2 = np.min((height, y1 + np.max((0, obj["bbox"][3]))))
if obj["area"] > 0 and x2 >= x1 and y2 >= y1:
obj["clean_bbox"] = [x1, y1, x2, y2]
objs.append(obj)
num_objs = len(objs)
res = np.zeros((num_objs, 5))
for ix, obj in enumerate(objs):
cls = self.class_ids.index(obj["category_id"])
res[ix, 0:4] = obj["clean_bbox"]
res[ix, 4] = cls
r = min(self.img_size[0] / height, self.img_size[1] / width)
res[:, :4] *= r
img_info = (height, width)
resized_info = (int(height * r), int(width * r))
file_name = (
im_ann["name"]
if "name" in im_ann
else "{:012}".format(id_) + ".jpg"
)
return (res, img_info, resized_info, file_name)
def photo_to_sequence(self,lframe,gframe):
'''
Args:
dataset_path: list,every element is a list contain all frame in a video dir
Returns:
split result
'''
res = []
with open(self.coco_anno_path, 'r') as anno:
anno = json.load(anno)
dataset = [[] for i in range(len(anno['videos']))]
for im in anno['images']:
dataset[im['sid']].append(im['name'])
for ele in dataset:
sorted(ele)
for element in dataset:
ele_len = len(element)
if ele_len<lframe+gframe:
#TODO fix the unsolved part
#res.append(element)
continue
else:
if self.mode == 'random':
split_num = int(ele_len / (gframe))
random.shuffle(element)
for i in range(split_num):
res.append(element[i * gframe:(i + 1) * gframe])
elif self.mode == 'uniform':
split_num = int(ele_len / (gframe))
all_uniform_frame = element[:split_num * gframe]
for i in range(split_num):
res.append(all_uniform_frame[i::split_num])
elif self.mode == 'gl':
split_num = int(ele_len / (lframe))
all_local_frame = element[:split_num * lframe]
for i in range(split_num):
g_frame = random.sample(element[:i * lframe] + element[(i + 1) * lframe:], gframe)
res.append(all_local_frame[i * lframe:(i + 1) * lframe] + g_frame)
else:
print('unsupport mode, exit')
exit(0)
if self.val:
random.seed(42)
random.shuffle(res)
return res#[2000:3000]#[1000:1250]#[2852:2865]
else:
random.shuffle(res)
return res#[:15000]
def pull_item(self,path):
"""
One image / label pair for the given index is picked up and pre-processed.
Args:
index (int): data index
Returns:
img (numpy.ndarray): pre-processed image
padded_labels (torch.Tensor): pre-processed label data.
The shape is :math:`[max_labels, 5]`.
each label consists of [class, xc, yc, w, h]:
class (float): class index.
xc, yc (float) : center of bbox whose values range from 0 to 1.
w, h (float) : size of bbox whose values range from 0 to 1.
info_img : tuple of h, w.
h, w (int): original shape of the image
img_id (int): same as the input index. Used for evaluation.
"""
idx = self.name_id_dic[path]
annos, img_info, resized_info, img_path = self.annotations[idx]
abs_path = os.path.join(self.data_dir,self.name, img_path)
img = cv2.imread(abs_path)
height, width = img.shape[:2]
img_info = (height, width)
r = min(self.img_size[0] / img.shape[0], self.img_size[1] / img.shape[1])
img = cv2.resize(
img,
(int(img.shape[1] * r), int(img.shape[0] * r)),
interpolation=cv2.INTER_LINEAR,
).astype(np.uint8)
return img, annos.copy(), img_info, img_path
def get_xml_list(path):
image_names = []
for maindir, subdir, file_name_list in os.walk(path):
for filename in file_name_list:
apath = os.path.join(maindir, filename)
ext = os.path.splitext(apath)[1]
if ext in XML_EXT:
image_names.append(apath)
return image_names
def get_image_list(path):
image_names = []
for maindir, subdir, file_name_list in os.walk(path):
for filename in file_name_list:
apath = os.path.join(maindir, filename)
ext = os.path.splitext(apath)[1]
if ext in IMAGE_EXT:
image_names.append(apath)
return image_names
def make_path(train_dir,save_path):
res = []
for root,dirs,files in os.walk(train_dir):
temp = []
for filename in files:
apath = os.path.join(root, filename)
ext = os.path.splitext(apath)[1]
if ext in IMAGE_EXT:
temp.append(apath)
if(len(temp)):
temp.sort()
res.append(temp)
res_np = np.array(res,dtype=object)
np.save(save_path,res_np)
class TestSampler(SequentialSampler):
def __init__(self,data_source):
super().__init__(data_source)
self.data_source = data_source
def __iter__(self):
return iter(self.data_source.res)
def __len__(self):
return len(self.data_source)
class TrainSampler(Sampler):
def __init__(self,data_source):
super().__init__(data_source)
self.data_source = data_source
def __iter__(self):
random.shuffle(self.data_source.res)
return iter(self.data_source.res)
def __len__(self):
return len(self.data_source)
class VIDBatchSampler(BatchSampler):
def __iter__(self):
batch = []
for ele in self.sampler:
for filename in ele:
batch.append(filename)
if (len(batch)) == self.batch_size:
yield batch
batch = []
if len(batch)>0 and not self.drop_last:
yield batch
def __len__(self):
return len(self.sampler)
class VIDBatchSampler_Test(BatchSampler):
def __iter__(self):
batch = []
for ele in self.sampler:
yield ele
# for filename in ele:
# batch.append(filename)
# if (len(batch)) == self.batch_size:
# yield batch
# batch = []
# if len(batch)>0 and not self.drop_last:
# yield batch
def __len__(self):
return len(self.sampler)
def collate_fn(batch):
tar = []
imgs = []
ims_info = []
tar_ori = []
path = []
path_sequence = []
for sample in batch:
tar_tensor = torch.zeros([120,5])
imgs.append(torch.tensor(sample[0]))
tar_ori.append(torch.tensor(sample[1]))
tar_tensor[:sample[1].shape[0]] = torch.tensor(sample[1])
tar.append(tar_tensor)
ims_info.append(sample[2])
path.append(sample[3])
#path_sequence.append(int(sample[3][sample[3].rfind('/')+1:sample[3].rfind('.')]))
# path_sequence= torch.tensor(path_sequence)
# time_embedding = get_timing_signal_1d(path_sequence,256)
return torch.stack(imgs),torch.stack(tar),ims_info,tar_ori,path,None
def get_vid_loader(batch_size,data_num_workers,dataset):
sampler = VIDBatchSampler(TrainSampler(dataset), batch_size, drop_last=False)
dataloader_kwargs = {
"num_workers": data_num_workers,
"pin_memory": True,
"batch_sampler": sampler,
'collate_fn':collate_fn
}
vid_loader = torch.utils.data.DataLoader(dataset, **dataloader_kwargs)
return vid_loader
def vid_val_loader(batch_size,data_num_workers,dataset,):
sampler = VIDBatchSampler_Test(TestSampler(dataset),batch_size,drop_last=False)
dataloader_kwargs = {
"num_workers": data_num_workers,
"pin_memory": True,
"batch_sampler": sampler,
'collate_fn': collate_fn
}
loader = torch.utils.data.DataLoader(dataset, **dataloader_kwargs)
return loader
def collate_fn_trans(batch):
tar = []
imgs = []
ims_info = []
tar_ori = []
path = []
path_sequence = []
for sample in batch:
tar_tensor = torch.zeros([100,5])
imgs.append(torch.tensor(sample[0]))
tar_ori.append(torch.tensor(copy.deepcopy(sample[1])))
sample[1][:,1:]=xyxy2cxcywh(sample[1][:,1:])
tar_tensor[:sample[1].shape[0]] = torch.tensor(sample[1])
tar.append(tar_tensor)
ims_info.append(sample[2])
path.append(sample[3])
path_sequence.append(int(sample[3][sample[3].rfind('/')+1:sample[3].rfind('.')]))
path_sequence= torch.tensor(path_sequence)
time_embedding = get_timing_signal_1d(path_sequence,256)
return torch.stack(imgs),torch.stack(tar),ims_info,tar_ori,path,time_embedding
def get_trans_loader(batch_size,data_num_workers,dataset):
sampler = VIDBatchSampler(TrainSampler(dataset), batch_size, drop_last=False)
dataloader_kwargs = {
"num_workers": data_num_workers,
"pin_memory": True,
"batch_sampler": sampler,
'collate_fn':collate_fn
}
vid_loader = torch.utils.data.DataLoader(dataset, **dataloader_kwargs)
return vid_loader
class DataPrefetcher:
"""
DataPrefetcher is inspired by code of following file:
https://github.com/NVIDIA/apex/blob/master/examples/imagenet/main_amp.py
It could speedup your pytorch dataloader. For more information, please check
https://github.com/NVIDIA/apex/issues/304#issuecomment-493562789.
"""
def __init__(self, loader):
self.loader = iter(loader)
self.max_iter = len(loader)
self.stream = torch.cuda.Stream()
self.input_cuda = self._input_cuda_for_image
self.record_stream = DataPrefetcher._record_stream_for_image
self.preload()
def preload(self):
try:
self.next_input, self.next_target,_,_,_,self.time_ebdding = next(self.loader)
except StopIteration:
self.next_input = None
self.next_target = None
self.time_ebdding = None
return
with torch.cuda.stream(self.stream):
self.input_cuda()
self.next_target = self.next_target.cuda(non_blocking=True)
def next(self):
torch.cuda.current_stream().wait_stream(self.stream)
input = self.next_input
target = self.next_target
time_ebdding = self.time_ebdding
if input is not None:
self.record_stream(input)
if target is not None:
target.record_stream(torch.cuda.current_stream())
self.preload()
return input, target,time_ebdding
def _input_cuda_for_image(self):
self.next_input = self.next_input.cuda(non_blocking=True)
@staticmethod
def _record_stream_for_image(input):
input.record_stream(torch.cuda.current_stream())
def get_timing_signal_1d(index_squence,channels,min_timescale=1.0, max_timescale=1.0e4,):
num_timescales = channels // 2
log_time_incre = torch.tensor(math.log(max_timescale/min_timescale)/(num_timescales-1))
inv_timescale = min_timescale*torch.exp(torch.arange(0,num_timescales)*-log_time_incre)
scaled_time = torch.unsqueeze(index_squence,1)*torch.unsqueeze(inv_timescale,0) #(index_len,1)*(1,channel_num)
sig = torch.cat([torch.sin(scaled_time),torch.cos(scaled_time)],dim=1)
return sig