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()
import cv2 # import numpy as np # Image from scipy.ndimage import rotate as rotate_img # import random # OS import os # WIDTH = HEIGHT = 128 # Augmentations prob_50 = lambda aug: va.Sometimes( 0.5, aug) # Used to apply augmentor with 50% probability prob_20 = lambda aug: va.Sometimes( 0.2, aug) # Used to apply augmentor with 20% probability aug_seq = va.Sequential([ prob_20(va.OneOf([va.GaussianBlur(2), va.InvertColor()])), prob_50(va.HorizontalFlip()) ]) def use_aug_seq(frames): aug_frames = [] for frame in frames: if frame is not None: aug_frames.append(frame) aug_frames = aug_seq(aug_frames) j = 0 for i in range(len(frames)): if frames[i] is not None: frames[i] = aug_frames[j] j += 1
import imageio from IPython import display _VIDEO_LIST = None from urllib import request # requires python3 #for augmentation from vidaug import augmentors as va from PIL import Image, ImageSequence import vidaug.augmentors as va import random sometimes = lambda aug: va.Sometimes(0.5, aug) seq = va.Sequential([ sometimes(va.RandomCrop(size=(224, 224))), sometimes(va.RandomRotate(degrees=10)), sometimes(va.VerticalFlip()), sometimes(va.HorizontalFlip()), sometimes(va.GaussianBlur(1.5)) ]) no_of_samples = 30 X_train_aug = [] Y_train_aug = [] def augmentation(X_train, Y_train): for i in range(no_of_samples): idx = random.randint(0, 59) print("index is ", idx) vid = X_train[idx] print("shape of vid", vid.shape) #vid = np.expand_dims(vid,3)
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):