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 __init__(self, intensity_mul_probs=[0.2, 0.2], intensity_mul_values=[1.1, 0.9], ): # TODO: find a better intensity augmentation setting! from vidaug import augmentors as va aug = [] for p, v in zip(intensity_mul_probs, intensity_mul_values): aug.append(va.Sometimes(p, va.Multiply(value=v))) self.video_aug = va.Sequential(aug)
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, 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()
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 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
for name, param in model.named_parameters(): 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)
# 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] print("shape of vid", vid.shape)
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 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" ### For Hourglass Model
import tensorflow as tf import cv2 import numpy as np import os import time import json #################################### from vidaug import augmentors as va sometimes = lambda aug: va.Sometimes( 0.8, 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] va.RandomTranslate(x=5, y=5), ]) ################################################################### os.environ['CUDA_VISIBLE_DEVICES'] = '0' data_path = "/media/yash/YASH-01/Datasets/ABC" input_w_h = 192 frozen_graph = "/home/yash/Desktop/finalModels/pose/model.pb" output_node_names = "Convolutional_Pose_Machine/stage_5_out" with tf.gfile.GFile(frozen_graph, "rb") as f: restored_graph_def = tf.GraphDef()
def __init__(self, shape=(224, 224)): self.seq = va.Sequential([Resize(shape)])