Exemple #1
0
 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)
     ])
Exemple #2
0
def aug_video(video_list, save_path, mode, aug_num):
    new_video_list = []
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    # initialize video writer
    fourcc = cv2.VideoWriter_fourcc('D', 'I', 'V', 'X')

    # generate new videos
    for k in range(len(video_list)):
        frames, fps = video_loader(video_list[k], mode)
        for j in range(aug_num):
            # video agumentation steps
            sometimes = lambda aug: va.Sometimes(1, aug) # used to apply augmentor with 100% probability
            ran_num = int(random.randint(1,4)) # select random number of steps
            aug_seq = va.SomeOf([
            sometimes(va.HorizontalFlip()), # horizontally flip the video with 100% probability
                va.RandomCrop((192, 256)), # extract random crop of the video (crop_height, crop_width)
                va.Downsample(0.8), # temporally downsample a video by deleting some of its frames
                va.Upsample(1.5), # temporally upsampling a video by deleting some of its frames
            ], ran_num, random_order=False)
            # video generation
            aug_frames, aug_steps = aug_seq(frames)
            height, width, _ = aug_frames[0].shape
            cur_video = video_list[k].split('/')
            video_filename = os.path.join(save_path, "".join([str(x) for x in aug_steps])+cur_video[-1])
            # save videos
            out = cv2.VideoWriter(video_filename, fourcc, fps, (width, height))
            for i in range(len(aug_frames)):
                out.write(aug_frames[i])
            # close out the video writer
            out.release()
            new_video_list.append(video_filename)

    return new_video_list
Exemple #3
0
 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)
Exemple #4
0
 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)
     ])
Exemple #5
0
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)
Exemple #6
0
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
Exemple #7
0
 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()
    print("Params to learn:")
    params_to_update = []
    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))
Exemple #9
0
 def sometimes(aug):
     return va.Sometimes(0.2, aug)
import numpy as np
import pickle
from keras.utils import np_utils
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):
Exemple #11
0
import torch  #
import torchvision  #  Torch
from torch.utils.data import Dataset  #

from vidaug import augmentors as va  # Video Augmentation

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)