53. Data Augmentation with Albumentation

Augmenting Data

In most case for AI projects, there are not enough data to train a model. So that’s when Data Augmentation comes into play! You can create new data from what you already have and use that to train the model as well.

Implementation

First import all requirements

import cv2
import albumentations as A
import numpy as np
from matplotlib import pyplot as plt

Load your image and convert the image to RGB format

image = cv2.imread("PATH/TO/brita.jpg")

# openCV uses BGR format but albumentation uses RGB format
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

I’m going to use pascal_VOC, so the parameters I have to set for the bounding box of the image will be like the code below.
You can get the coordinates by using LabelImg.(I’ll write a tutorial on that later on)

# pascal_voc(x_min,y_min,x_max,y_max)
bboxes = [[470, 126, 786, 610]]

Right down all the Augmentation methods, you want to apply to the image.

transform = A.Compose([
    A.Resize(width=1920, height=1080),
    A.RandomCrop(width=1280, height=720),
    A.Rotate(limit=50, p=0.9), # p=0.9->90% chance of doing this process
    A.HorizontalFlip(p=0.5),
    A.VerticalFlip(p=0.2),
    A.VerticalFlip(p=0.1),
    A.RGBShift(r_shift_limit=25, g_shift_limit=25, b_shift_limit=25, p=0.4),
    A.OneOf([
        A.Blur(blur_limit=3, p=0.5),
        A.ColorJitter(p=0.5),
    ], p=1.0)#Oneof: Only activate either one
],
    bbox_params=A.BboxParams(format="pascal_voc", min_area=2048, label_fields=[]))
    # min_area: show at least 2048 pixels of the bounding box

Augment Data

images_list = [image] #List to store created images
saved_bboxes = [bboxes[0]] #list for bbox coordinates

image = np.array(image) #Turn image to a numpy array

# Iterate 16 times
for i in range(16):
    augmentations = transform(
        image=image, bboxes=bboxes)
    augmented_img = augmentations["image"]
    images_list.append(augmented_img)
    saved_bboxes.append(augmentations["bboxes"][0])

Create Function to display the results

def plot_examples(images, bboxes=None):
    fig = plt.figure(figsize=(15, 15))
    columns = 4
    rows = 5

    for i in range(1, len(images)):
        if bboxes is not None:
            img = visualize_bbox(
                images[i - 1], bboxes[i - 1], class_name="Brita")
        else:
            img = images[i-1]
        fig.add_subplot(rows, columns, i)
        plt.imshow(img)
    plt.show()

Display Bounding boxes

plot_examples(images_list, saved_bboxes)