In this blog, we will look into some commonly used data augmentations.

We will be specifically looking at Pytorch augmentations and Albumentation Augmentations.

You can find the code here.

We will be using the image of this dog.

image used for augmentations

Let’s load the image.

import cv2
import albumentations
image = cv2.imread('image_path')
plt.imshow(image)

Let is look at some common transformations from albumentations.ai. To define an augmentation pipeline, you need to create an instance of the Compose class. As an argument to the Compose class, you need to pass a list of augmentations you want to apply. A call to Compose will return a transform function that will perform image augmentation.

example:

import albumentations
transform = albumentations.Compose([
albumentations.RandomCrop(width=256, height=256),
albumentations.HorizontalFlip(p=0.5),
albumentations.RandomBrightnessContrast(p=0.2),
])

You can just add any other transformation in this list to see the result. You can also see the demo of any of the transformations here.

example albumentations augmentations

Now let us create a function to apply these augmentations.

img_matrix_list = []
for aug_type in albumentation_list:
img = aug_type(image = image)['image']
img_matrix_list.append(img)
img_matrix_list.insert(0,image)
titles = ["Original","HueSaturationValue","IAAAdditiveGaussianNoise","RandomBrightness","RandomRotate90", "RandomCrop, ""RGBShift","CLAHE", "IAASharpen", "IAAEmboss", "VerticalFlip", "RandomContrast","Cutout","Transpose","JpegCompression","IAAAffine", "GaussNoise","MotionBlur", "ShiftScaleRotate","OpticalDistortion", "GridDistortion", "IAAPiecewiseAffine", "Blur"]

This is a custom function to view the results.

def plot_imgs(img_matrix, titles, ncolumns = 4, nrows=5):
fig, myaxes = plt.subplots(figsize=(21, 15), nrows=nrows, ncols=ncols, squeeze=False)
fig.suptitle(main_title, fontsize = 30)
fig.subplots_adjust(wspace=0.3)
fig.subplots_adjust(hspace=0.3)
for i, (img, title) in enumerate(zip(img_matrix_list, title_list)):
myaxes[i // ncols][i % ncols].imshow(img)
myaxes[i // ncols][i % ncols].set_title(title, fontsize=15)
plt.show()
plot_imgs(img_matrix, titles, ncols = 4)
result of the above augmentations

Similarly, lets now let us look at some Pytorch transformations:

pytorch augmentations

Now lets see how we can use these transformations in code.

img_matrix = []
for aug_type in torch_trans_list:
image = cv2.imread(image_path)
chosen_tensor = transforms.Compose([transforms.ToTensor()])(image)
tensor = transforms.Compose([aug_type])(image)
trans_img = transforms.ToPILImage()(image)
img_matrix_list.append(trans_img)

We can use the above defined function to view the results.

result of the above augmentations

References:

  1. https://www.kaggle.com/reighns/augmentations-data-cleaning-and-bounding-boxes#Bounding-Boxes-with-Albumentations
  2. https://albumentations.ai/docs/api_reference/augmentations/transforms/
  3. https://pytorch.org/docs/stable/torchvision/transforms.html
  4. https://www.kaggle.com/khoongweihao/insect-augmentation-et-al

Hi, I am Ankita. I write about Machine Learning and how I try to navigate the puzzle called life!