Spaces:
Sleeping
Sleeping
File size: 10,388 Bytes
2d9a728 |
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 |
import os
from torchvision import transforms
from .transforms import *
from .masking_generator import TubeMaskingGenerator, RandomMaskingGenerator
from .mae import VideoMAE
from .mae_multi import VideoMAE_multi
from .kinetics import VideoClsDataset
from .kinetics_sparse import VideoClsDataset_sparse
from .anet import ANetDataset
from .ssv2 import SSVideoClsDataset, SSRawFrameClsDataset
from .hmdb import HMDBVideoClsDataset, HMDBRawFrameClsDataset
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)
self.train_augmentation = GroupMultiScaleCrop(args.input_size, [1, .875, .75, .66])
if args.color_jitter > 0:
self.transform = transforms.Compose([
self.train_augmentation,
GroupColorJitter(args.color_jitter),
GroupRandomHorizontalFlip(flip=args.flip),
Stack(roll=False),
ToTorchFormatTensor(div=True),
normalize,
])
else:
self.transform = transforms.Compose([
self.train_augmentation,
GroupRandomHorizontalFlip(flip=args.flip),
Stack(roll=False),
ToTorchFormatTensor(div=True),
normalize,
])
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 in 'attention':
self.masked_position_generator = None
def __call__(self, images):
process_data, _ = self.transform(images)
if self.masked_position_generator is None:
return process_data, -1
else:
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=None,
setting=args.data_path,
prefix=args.prefix,
split=args.split,
video_ext='mp4',
is_color=True,
modality='rgb',
num_segments=args.num_segments,
new_length=args.num_frames,
new_step=args.sampling_rate,
transform=transform,
temporal_jitter=False,
video_loader=True,
use_decord=args.use_decord,
lazy_init=False,
num_sample=args.num_sample)
print("Data Aug = %s" % str(transform))
return dataset
def build_multi_pretraining_dataset(args):
origianl_flip = args.flip
transform = DataAugmentationForVideoMAE(args)
args.flip = False
transform_ssv2 = DataAugmentationForVideoMAE(args)
args.flip = origianl_flip
dataset = VideoMAE_multi(
root=None,
setting=args.data_path,
prefix=args.prefix,
split=args.split,
is_color=True,
modality='rgb',
num_segments=args.num_segments,
new_length=args.num_frames,
new_step=args.sampling_rate,
transform=transform,
transform_ssv2=transform_ssv2,
temporal_jitter=False,
video_loader=True,
use_decord=args.use_decord,
lazy_init=False,
num_sample=args.num_sample)
print("Data Aug = %s" % str(transform))
print("Data Aug for SSV2 = %s" % str(transform_ssv2))
return dataset
def build_dataset(is_train, test_mode, args):
print(f'Use Dataset: {args.data_set}')
if args.data_set in [
'Kinetics',
'Kinetics_sparse',
'mitv1_sparse'
]:
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')
if 'sparse' in args.data_set:
func = VideoClsDataset_sparse
else:
func = VideoClsDataset
dataset = func(
anno_path=anno_path,
prefix=args.prefix,
split=args.split,
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 = args.nb_classes
elif args.data_set == 'SSV2':
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')
if args.use_decord:
func = SSVideoClsDataset
else:
func = SSRawFrameClsDataset
dataset = func(
anno_path=anno_path,
prefix=args.prefix,
split=args.split,
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,
filename_tmpl=args.filename_tmpl,
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')
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,
prefix=args.prefix,
split=args.split,
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')
if args.use_decord:
func = HMDBVideoClsDataset
else:
func = HMDBRawFrameClsDataset
dataset = func(
anno_path=anno_path,
prefix=args.prefix,
split=args.split,
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,
filename_tmpl=args.filename_tmpl,
args=args)
nb_classes = 51
elif args.data_set in [
'ANet',
'HACS',
'ANet_interval',
'HACS_interval'
]:
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')
if 'interval' in args.data_set:
func = ANetDataset
else:
func = VideoClsDataset_sparse
dataset = func(
anno_path=anno_path,
prefix=args.prefix,
split=args.split,
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 = args.nb_classes
else:
print(f'Wrong: {args.data_set}')
raise NotImplementedError()
assert nb_classes == args.nb_classes
print("Number of the class = %d" % args.nb_classes)
return dataset, nb_classes
|