-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
150 lines (138 loc) · 5.68 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
import os
import tensorflow as tf
import numpy as np
class DataAugmentationForVideoMAE(object):
def __init__(self, args):
self.input_mean = [0.485, 0.456, 0.406]
self.input_std = [0.229, 0.224, 0.225]
self.train_augmentation = self.group_multi_scale_crop(args.input_size, [1, .875, .75, .66])
self.normalize = self.group_normalize(self.input_mean, self.input_std)
self.masked_position_generator = None
if args.mask_type == 'tube':
self.masked_position_generator = TubeMaskingGenerator(args.window_size, args.mask_ratio)
def group_normalize(self, mean, std):
def normalize(images):
mean_tensor = tf.constant(mean, dtype=tf.float32, shape=[1, 1, 1, 3])
std_tensor = tf.constant(std, dtype=tf.float32, shape=[1, 1, 1, 3])
return (images - mean_tensor) / std_tensor
return normalize
def group_multi_scale_crop(self, input_size, scales):
def multi_scale_crop(images):
scale = np.random.choice(scales)
new_size = tf.cast(input_size * scale, tf.int32)
cropped_images = tf.image.resize(images, (new_size, new_size))
return tf.image.random_crop(cropped_images, [input_size, input_size, 3])
return multi_scale_crop
def __call__(self, images):
process_data = self.train_augmentation(images)
process_data = self.normalize(process_data)
return process_data, self.masked_position_generator()
def __repr__(self):
repr = "(DataAugmentationForVideoMAE,\n"
repr += " transform = %s,\n" % str(self.train_augmentation)
repr += " normalize = %s,\n" % str(self.normalize)
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=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 == 'Kinetics-400':
mode = 'train' if is_train else ('test' if test_mode else 'validation')
anno_path = os.path.join(args.data_path, f'{mode}.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 args.data_set == 'SSV2':
mode = 'train' if is_train else ('test' if test_mode else 'validation')
anno_path = os.path.join(args.data_path, f'{mode}.csv')
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 = 'train' if is_train else ('test' if test_mode else 'validation')
anno_path = os.path.join(args.data_path, f'{mode}.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 = 'train' if is_train else ('test' if test_mode else 'validation')
anno_path = os.path.join(args.data_path, f'{mode}.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