-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
270 lines (251 loc) · 9.37 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
import os
from torchvision import transforms
from transforms import *
from masking_generator import TubeMaskingGenerator
from kinetics import VideoClsDataset, VideoMAE
from ssv2 import SSVideoClsDataset
class DataAugmentationForVideoMAE(object):
def __init__(self, args):
self.input_mean = [0.485, 0.456, 0.406] # IMAGENET_DEFAULT_MEAN
self.input_std = [0.229, 0.224, 0.225] # IMAGENET_DEFAULT_STD
normalize = GroupNormalize(self.input_mean, self.input_std)
gray_scale = GroupRandomGrayScale(p=args.gray_scale_prob)
if args.augmentation == 'resize':
self.train_augmentation = GroupResize(args.input_size)
else:
self.train_augmentation = GroupMultiScaleCrop(args.input_size, [1, .875, .75, .66])
self.transform = transforms.Compose([
gray_scale,
self.train_augmentation,
Stack(roll=False),
ToTorchFormatTensor(div=True),
normalize,
])
if args.mask_type == 'tube' or args.mask_type == 'parts' or args.mask_type == 'tube_fgbg': # just load it here so code works even with parts mask
self.masked_position_generator = TubeMaskingGenerator(
args.window_size, args.mask_ratio
)
def __call__(self, images):
process_data, _ = self.transform(images)
return process_data, self.masked_position_generator()
def __repr__(self):
repr = "(DataAugmentationForVideoMAE,\n"
repr += " transform = %s,\n" % str(self.transform)
repr += " Masked position generator = %s,\n" % str(self.masked_position_generator)
repr += ")"
return repr
def build_pretraining_dataset(args):
transform = DataAugmentationForVideoMAE(args)
dataset = VideoMAE(
root=args.data_path,
setting=args.data_path_csv,
target_type=args.target_type,
mask_ratio=args.mask_ratio,
video_ext='mp4',
is_color=True,
modality='rgb',
new_length=args.num_frames,
new_step=args.sampling_rate,
transform=transform,
temporal_jitter=False,
video_loader=True,
use_decord=True,
lazy_init=False)
print("Data Aug = %s" % str(transform))
return dataset
def build_dataset(is_train, test_mode, args):
if args.data_set == 'Kinetics-400':
mode = None
anno_path = None
if is_train is True:
mode = 'train'
anno_path = os.path.join(args.data_path, 'train.csv')
elif test_mode is True:
mode = 'test'
anno_path = os.path.join(args.data_path, 'test.csv')
else:
mode = 'validation'
anno_path = os.path.join(args.data_path, 'val.csv')
dataset = VideoClsDataset(
anno_path=anno_path,
data_path='/',
mode=mode,
clip_len=args.num_frames,
frame_sample_rate=args.sampling_rate,
num_segment=1,
test_num_segment=args.test_num_segment,
test_num_crop=args.test_num_crop,
num_crop=1 if not test_mode else 3,
keep_aspect_ratio=True,
crop_size=args.input_size,
short_side_size=args.short_side_size,
new_height=256,
new_width=320,
args=args)
nb_classes = 400
elif 'SSV2' in args.data_set:
mode = None
anno_path = None
if is_train is True:
mode = 'train'
if 'mini' in args.data_set:
anno_path = os.path.join(args.data_path, 'train_mini.csv')
else:
anno_path = os.path.join(args.data_path, 'train.csv')
#anno_path = os.path.join(args.data_path)
elif test_mode is True:
mode = 'test'
anno_path = os.path.join(args.data_path, 'test.csv')
#anno_path = os.path.join(args.data_path)
else:
mode = 'validation'
anno_path = os.path.join(args.data_path, 'val.csv')
#anno_path = os.path.join(args.data_path)
dataset = SSVideoClsDataset(
anno_path=anno_path,
data_path='/',
mode=mode,
clip_len=1,
num_segment=args.num_frames,
test_num_segment=args.test_num_segment,
test_num_crop=args.test_num_crop,
num_crop=1 if not test_mode else 3,
keep_aspect_ratio=True,
crop_size=args.input_size,
short_side_size=args.short_side_size,
new_height=256,
new_width=320,
args=args)
nb_classes = 174
elif args.data_set == 'UCF101':
mode = None
anno_path = None
if is_train is True:
mode = 'train'
#anno_path = os.path.join(args.data_path, 'train.csv')
anno_path = os.path.join(args.data_path)
elif test_mode is True:
mode = 'test'
#anno_path = os.path.join(args.data_path, 'test.csv')
anno_path = os.path.join(args.data_path)
else:
mode = 'validation'
#anno_path = os.path.join(args.data_path, 'val.csv')
anno_path = os.path.join(args.data_path)
dataset = VideoClsDataset(
anno_path=anno_path,
data_path='/',
mode=mode,
clip_len=args.num_frames,
frame_sample_rate=args.sampling_rate,
num_segment=1,
test_num_segment=args.test_num_segment,
test_num_crop=args.test_num_crop,
num_crop=1 if not test_mode else 3,
keep_aspect_ratio=True,
crop_size=args.input_size,
short_side_size=args.short_side_size,
new_height=256,
new_width=320,
args=args)
nb_classes = 101
elif args.data_set == 'GYM99' or args.data_set == 'FXS1' or args.data_set == 'UBS1':
mode = None
anno_path = None
if is_train is True:
mode = 'train'
#anno_path = os.path.join(args.data_path, 'train.csv')
anno_path = os.path.join(args.data_path)
elif test_mode is True:
mode = 'test'
#anno_path = os.path.join(args.data_path, 'test.csv')
anno_path = os.path.join(args.data_path)
else:
mode = 'validation'
#anno_path = os.path.join(args.data_path, 'val.csv')
anno_path = os.path.join(args.data_path)
dataset = VideoClsDataset(
anno_path=anno_path,
data_path='/',
mode=mode,
clip_len=args.num_frames,
frame_sample_rate=args.sampling_rate,
num_segment=1,
test_num_segment=args.test_num_segment,
test_num_crop=args.test_num_crop,
num_crop=1 if not test_mode else 3,
keep_aspect_ratio=True,
crop_size=args.input_size,
short_side_size=args.short_side_size,
new_height=256,
new_width=320,
args=args)
if args.data_set =='GYM99':
nb_classes = 99
elif args.data_set =='FXS1':
nb_classes = 11
elif args.data_set =='UBS1':
nb_classes = 15
elif args.data_set == 'DIV48':
mode = None
anno_path = None
if is_train is True:
mode = 'train'
anno_path = os.path.join(args.data_path)
elif test_mode is True:
mode = 'test'
anno_path = os.path.join(args.data_path)
else:
mode = 'validation'
anno_path = os.path.join(args.data_path)
dataset = VideoClsDataset(
anno_path=anno_path,
data_path='/',
mode=mode,
clip_len=args.num_frames,
frame_sample_rate=args.sampling_rate,
num_segment=1,
test_num_segment=args.test_num_segment,
test_num_crop=args.test_num_crop,
num_crop=1 if not test_mode else 3,
keep_aspect_ratio=True,
crop_size=args.input_size,
short_side_size=args.short_side_size,
new_height=256,
new_width=320,
args=args)
nb_classes = 48
elif args.data_set == 'HMDB51':
mode = None
anno_path = None
if is_train is True:
mode = 'train'
anno_path = os.path.join(args.data_path, 'train.csv')
elif test_mode is True:
mode = 'test'
anno_path = os.path.join(args.data_path, 'test.csv')
else:
mode = 'validation'
anno_path = os.path.join(args.data_path, 'val.csv')
dataset = VideoClsDataset(
anno_path=anno_path,
data_path='/',
mode=mode,
clip_len=args.num_frames,
frame_sample_rate=args.sampling_rate,
num_segment=1,
test_num_segment=args.test_num_segment,
test_num_crop=args.test_num_crop,
num_crop=1 if not test_mode else 3,
keep_aspect_ratio=True,
crop_size=args.input_size,
short_side_size=args.short_side_size,
new_height=256,
new_width=320,
args=args)
nb_classes = 51
else:
raise NotImplementedError()
assert nb_classes == args.nb_classes
print("Number of the class = %d" % args.nb_classes)
return dataset, nb_classes