-
Notifications
You must be signed in to change notification settings - Fork 1
/
datasets.py
165 lines (150 loc) · 5.51 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
import os
from torchvision import transforms
from transforms import *
from masking_generator import TubeMaskingGenerator, RandomMaskingGenerator
from ucf import VideoClsDataset, VideoMS
class DataAugmentationForVideoMS(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)
self.train_augmentation = GroupMultiScaleCrop(args.input_size, [1, .875, .75, .66])
self.transform = transforms.Compose([
self.train_augmentation,
Stack(roll=False),
ToTorchFormatTensor(div=True),
normalize,
])
self.mcm = False
if args.mask_type == 'tube':
self.masked_position_generator = TubeMaskingGenerator(
args.window_size, args.mask_ratio
)
elif args.mask_type == 'random':
self.masked_position_generator = RandomMaskingGenerator(
args.window_size, args.mask_ratio
)
elif args.mask_type == 'motion-centric':
self.mcm = True
self.masked_position_generator = 'Motion-centric Masking'
def __call__(self, images):
process_data, _ = self.transform(images)
if self.mcm:
return process_data, 0
return process_data, self.masked_position_generator()
def __repr__(self):
repr = "(DataAugmentationForVideoMS,\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 = DataAugmentationForVideoMS(args)
dataset = VideoMS(
root=None,
setting=args.data_path,
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 == 'UCF101':
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 = 101
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
elif args.data_set == 'OSCC':
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 = 2
else:
raise NotImplementedError()
assert nb_classes == args.nb_classes
print("Number of the class = %d" % args.nb_classes)
return dataset, nb_classes