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 augmentation(dst_directory_path, class_name): video = [] for f in glob.iglob(f"{dst_directory_path}/*.jpg"): video.append(Image.open(f)) def sometimes(aug): return va.Sometimes(0.2, aug) seq = va.Sequential([ va.RandomCrop(size=(160, 160)), # image height = 240 va.RandomRotate(degrees=10), sometimes(va.HorizontalFlip()), sometimes(va.GaussianBlur(sigma=1)), sometimes(va.InvertColor()), sometimes(va.Salt()), ]) for batch_idx in range(VIDAUG_BATCHES): video_aug = seq(video) for a, v in zip(video_aug, video): folder, filename = os.path.split(v.filename) folder = folder.replace('__', f'_vidaug_{batch_idx}__') if not os.path.exists(folder): os.mkdir(folder) path = os.path.join(folder, filename) a.save(path)
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 traverse_and_process(): # For data augmentation # Used to apply augmentor with 50% probability sometimes = lambda aug: va.Sometimes(0.5, aug) seq = va.Sequential([ # Randomly crop video with a size of (96 x 96) va.RandomCrop(size=(96, 96)), # Randomly rotates the video with a degree randomly choosen from [-10, 10] va.RandomRotate(degrees=10), # horizontally flip the video with 50% probability sometimes(va.HorizontalFlip()) ]) foldernames = [] for root, dirs, files in os.walk(config.VIDEO_DIREC): if len(dirs) > 0: foldernames = sorted(dirs) for folder in tqdm(foldernames, desc='Folder', bar_format='{l_bar}{bar:40}{r_bar}{bar:-10b}'): filenames = [] for root, dirs, files in os.walk(f'{config.VIDEO_DIREC}/{folder}'): filenames = sorted(file.split(".")[0] for file in list( filter(lambda x: x != ".DS_Store", files))) for filename in tqdm(filenames[:20], desc='Class ', bar_format='{l_bar}{bar:40}{r_bar}{bar:-10b}'): # for filename in filenames: for iter in tqdm(range(10), desc='Files ', bar_format='{l_bar}{bar:40}{r_bar}{bar:-10b}'): # Check if dst folder exists utils.create_path(f'{config.CROPPED_DIREC}/{(int(folder) - 1)}{iter}') # Set the paths src_path = f'{config.VIDEO_DIREC}/{folder}/{filename}.mp4' dst_path = f'{config.CROPPED_DIREC}/{(int(folder) - 1)}{iter}/{filename}.npz' # utils.check_video_length(src_path, verbose=True) sequence = detect_face_from_file(src_path, verbose=False) # print(type(sequence), sequence.shape) if config.AUGMENT and iter != 0: sequence = np.array(seq(sequence)) assert sequence is not None, f'Cannot crop from {src_path}.' # print(sequence.shape) # ... = Ellipsis data = transform.convert_bgr2gray( sequence) if config.CONVERT_GRAY else sequence[..., ::-1] utils.save2npz(dst_path, data=data)
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 skvideo.io # Some modules to display an animation using imageio. 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]
import numpy as np import os from collections import defaultdict import time import json ### Video Augmentation using rotation and translation #################################### from vidaug import augmentors as va sometimes = lambda aug: va.Sometimes( 0.5, aug) # Used to apply augmentor with 50% probability seq = va.Sequential([ # va.RandomCrop(size=(240, 180)), # 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] sometimes(va.HorizontalFlip() ) # horizontally flip the video with 50% probability ]) ################################################################### ### Define the path of the Cluster for BerkeleyMHAD Dataset os.environ['CUDA_VISIBLE_DEVICES'] = '0' data_path = "/media/yash/YASH-01/Datasets/BerkeleyMHAD/Camera/Cluster01" #### Define all the Pose Estimation model details input_w_h = 192 frozen_graph = "/home/yash/Desktop/finalModels/pose/model.pb" output_node_names = "Convolutional_Pose_Machine/stage_5_out"
data = [] while (cap.isOpened()): ret, frame = cap.read() if ret: data.append(frame) else: break data = np.array(data) return data operations = [ None, va.RandomRotate(degrees=30), va.Add(100), va.PiecewiseAffineTransform(displacement_magnification=9, displacement_kernel=45, displacement=8), va.RandomTranslate(x=40, y=20), va.Multiply(0.5) ] list = paths.list_files("FinalDataset") os.mkdir("Augmented_FinalDataset") for item in list: id = item.split(".")[0] array = video2array(item)