def __init__(self, shape=(224, 224)): sometimes = lambda aug: va.Sometimes(0.5, aug) self.seq = va.Sequential([ # Add a value to all pixel intesities in an video sometimes( va.OneOf([ va.Add(-100), va.Add(100), va.Add(-60), va.Add(60), ]), ), # Extract center crop of the video sometimes( va.OneOf([ CenterCrop((224, 224)), CenterCrop((200, 200)), CenterCrop((180, 180)), ]), ), # Augmenter that sets a certain fraction of pixel intesities to 255, # hence they become white sometimes( va.OneOf([ va.Salt(50), va.Salt(100), va.Salt(150), va.Salt(200), ]), ), # Augmenter that sets a certain fraction of pixel intensities to 0, # hence they become black sometimes( va.OneOf([ va.Pepper(50), va.Pepper(100), va.Pepper(150), va.Pepper(200), ]), ), # Rotate video randomly by a random angle within given bounds sometimes( va.OneOf([ va.RandomRotate(degrees=5), va.RandomRotate(degrees=10), va.RandomRotate(degrees=15), ]), ), # Shifting video in X and Y coordinates sometimes( va.OneOf([ va.RandomTranslate(20, 20), va.RandomTranslate(10, 10), va.RandomTranslate(5, 5) ]), ), # Horizontally flip the video with 50% probability sometimes(va.HorizontalFlip()), Resize(shape) ])
def __init__(self, prob=0.5, N=2, random_order=True): sometimes = lambda aug: va.Sometimes( prob, aug) # Used to apply augmentor with 50% probability self.augmentor = va.Sequential([ va.SomeOf([ sometimes(va.GaussianBlur(sigma=3.0)), sometimes(va.ElasticTransformation(alpha=3.5, sigma=0.25)), sometimes( va.PiecewiseAffineTransform(displacement=5, displacement_kernel=1, displacement_magnification=1)), sometimes(va.RandomRotate(degrees=10)), sometimes(va.RandomResize(0.5)), sometimes(va.RandomTranslate(x=20, y=20)), sometimes(va.RandomShear(x=0.2, y=0.2)), sometimes(va.InvertColor()), sometimes(va.Add(100)), sometimes(va.Multiply(1.2)), sometimes(va.Pepper()), sometimes(va.Salt()), sometimes(va.HorizontalFlip()), sometimes(va.TemporalElasticTransformation()), sometimes(RGB2Gray()) ], N=N, random_order=random_order) ])
def augmentor(frame_shape): """ Prepares the video data augmentator by applying some augmentations to it """ height = frame_shape[0] width = frame_shape[1] sometimes = lambda aug: va.Sometimes( 0.5, aug) # Used to apply augmentor with 50% probability seq = va.Sequential([ # randomly crop video with a size of (height-60 x width-60) # height and width are in this order because the instance is a ndarray sometimes(va.RandomCrop(size=(height - 60, width - 60))), sometimes(va.HorizontalFlip()), # horizontally flip sometimes(va.Salt(ratio=100)), # salt sometimes(va.Pepper(ratio=100)) # pepper ]) return seq
def __init__(self, dataset_path, jpg_path, subset, n_samples_for_each_video=10, spatial_transform=None, temporal_transform=None, target_transform=None, sample_duration=16, get_loader=get_default_video_loader): print('n samples:', n_samples_for_each_video) self.sample_duration = sample_duration self.subset = subset self.data, self.scene_labels, self.scene_map, self.class_map = make_dataset( dataset_path, jpg_path, subset, n_samples_for_each_video, sample_duration) self.spatial_transform = spatial_transform self.temporal_transform = temporal_transform self.target_transform = target_transform sometimes = lambda aug: va.Sometimes( 0.3, aug) # Used to apply augmentor with 50% probability print(subset) if self.subset == 'train': self.seq = va.Sequential([ va.RandomRotate( degrees=10 ), # randomly rotates the video with a degree randomly choosen from [-10, 10] sometimes(va.HorizontalFlip( )), # horizontally flip the video with 50% probability sometimes(va.Pepper()), sometimes(va.Salt()), sometimes(va.RandomTranslate()), # sometimes(va.RandomShear()), sometimes(va.GaussianBlur(sigma=1)), sometimes(va.ElasticTransformation()), va.TemporalFit(sample_duration) ]) else: self.seq = va.Sequential([va.TemporalFit(sample_duration)]) self.loader = get_loader()
if param.requires_grad == True: params_to_update.append(param) print("\t", name) if device.type == 'cuda': print(torch.cuda.get_device_name(0)) print('Memory Usage:') print('Allocated:', round(torch.cuda.memory_allocated(0) / 1024 ** 3, 1), 'GB') print('Cached: ', round(torch.cuda.memory_reserved(0) / 1024 ** 3, 1), 'GB') print(" ") # Transforms sometimes = lambda aug: vidaug.Sometimes(0.5, aug) # Used to apply augmentor with 50% probability video_augmentation = vidaug.Sequential([ sometimes(vidaug.Salt()), sometimes(vidaug.Pepper()), ], random_order=True) #Load Dataset basketball_dataset = BasketballDataset(annotation_dict=args.annotation_path, augmented_dict=args.augmented_annotation_path) train_subset, test_subset = random_split( basketball_dataset, [args.n_total-args.test_n, args.test_n], generator=torch.Generator().manual_seed(1)) train_subset, val_subset = random_split( train_subset, [args.n_total-args.test_n-args.val_n, args.val_n], generator=torch.Generator().manual_seed(1)) train_loader = DataLoader(dataset=train_subset, shuffle=True, batch_size=args.batch_size) val_loader = DataLoader(dataset=val_subset, shuffle=False, batch_size=args.batch_size) test_loader = DataLoader(dataset=test_subset, shuffle=False, batch_size=args.batch_size)
from vidaug import augmentors as va from matplotlib.pyplot import imread import cv2 import numpy as np sometimes = lambda aug: va.Sometimes(0.4, aug) # Used to apply augmentor with 50% probability seq = va.SomeOf([ #va.Sequential([ # va.RandomCrop(size=(300, 300)), # randomly crop video with a size of (240 x 180) # va.RandomRotate(degrees=10), # randomly rotates the video with a degree randomly choosen from [-10, 10] va.RandomTranslate(x=100,y=50), sometimes(va.Add(value=-100)), sometimes(va.Pepper(ratio=40)), # va.RandomResize(rate=0.5, interp='cubic'), sometimes(va.HorizontalFlip()) # horizontally flip the video with 50% probability ], 2) image = cv2.imread("room.jpg") image2 = cv2.imread("room3.jpg") print(image.shape) # Creating a dataset which contains just one image. images = np.zeros((2, image.shape[0], image.shape[1], image.shape[2])) images[0,:,:,:] = image images[1,:,:,:] = image2 for batch_idx in range(2): # 'video' should be either a list of images from type of numpy array or PIL images # video = load_batch(batch_idx) video_aug = seq(images) # train_on_video(video) for i in range(2):