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')

Let is look at some common transformations from 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.


import albumentations
transform = albumentations.Compose([
albumentations.RandomCrop(width=256, height=256),

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']
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)
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)
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)

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

result of the above augmentations



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