Search code examples
tensorflowimage-processingdeep-learningtensorflow2.0image-augmentation

Tensorflow Multiple Image augmentations that don't have layers


I am using

train_data_gen = tf.keras.utils.image_dataset_from_directory(...)

AUTOTUNE = tf.data.AUTOTUNE

train_data_gen = train_data_gen.cache().prefetch(buffer_size=AUTOTUNE)

I want to apply multiple image transforms like it is done in Pytorch:

# Define the transformation pipeline
transform = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor(),  # Convert the image to a PyTorch tensor
    Resize((256, 256)),  # Resize the images to a target size while keeping the aspect ratio intact
    torchvision.transforms.Pad(padding=0, fill=0.5, padding_mode='constant'),  # Add padding with the average color of the ImageNet dataset
    torchvision.transforms.RandomRotation(degrees=(-15, 15)),  # Randomly rotate the image between -15 and 15 degrees
    torchvision.transforms.RandomCrop(224),  # Randomly crop and resize the image to the target input size
    torchvision.transforms.RandomHorizontalFlip(),  # Flip the image horizontally with a probability of 50%
    torchvision.transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1),  # Randomly change the brightness, contrast, saturation, and hue
    torchvision.transforms.RandomGrayscale(p=0.2),  # Convert the image to grayscale with a probability of 20%
    torchvision.transforms.RandomErasing(p=0.2, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0.5),  # Randomly erase a rectangular region of the image
    Resize((256, 256)),  # Resize the images to a target size while keeping the aspect ratio intact
    torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))  # Normalize the image according to the ImageNet dataset
])

So how can I do that? can you show me how its done preferably with a code example.


Solution

  • Hey are you aware of Algumentation !!

    Algumentation library artificially creates training images through different ways of processing or combination of multiple processing, such as random rotation, shifts, shear and flips, etc.

    You can refer the link for code reference,

    https://albumentations.ai/

    Albumentation Tutoral Kernal Link