Image Augmentation with skimage — Python | by Mathanraj Sharma | Towards Data Science

Hey buddies, recently I was working on an image classification problem. But unfortunately, there were not enough samples in one of class. I searched the internet and learned about a technique called image augmentation. Here I have shared my understanding of this technique and shared some codes using skimage. You can find the jupyter notebook at the bottom.
Image augmentation is a technique used to artificially increase the size of your image dataset. It can be achieved by applying random transformations to your image.
We know Deep learning models are able to generalize well when they are able to see more data. Data augmentation can create variations of existing images which helps to generalize well.
But you need to be careful too. We can use Augmented Images for training but not for testing. So before applying any augmentation split your data into train and testing set. Expand your training data with Image Augmentation.
Image augmentation can be applied mainly on two domains of Image
Position AugmentationColor Augmentation
Position Augmentation
Color Augmentation
Positon augmentation is simple where we apply different transformations on pixel positions.
Scaling, rotation, cropping, flipping, padding, zoom, translation, shearing, and other affine transformations are examples for the Position Augmentation. Let us try applying some of these transformations.
import numpy as npfrom skimage.io import imread, imsaveimport matplotlib.pyplot as pltfrom skimage import transformfrom skimage.transform import rotate, AffineTransformfrom skimage.util import random_noisefrom skimage.filters import gaussianfrom scipy import ndimage# load Imageimg = imread('./butterfly.jpg') / 255# plot original Imageplt.imshow(img)plt.show()
# image rotation using skimage.transformation.rotaterotate30 = rotate(img, angle=30)rotate45 = rotate(img, angle=45)rotate60 = rotate(img, angle=60)rotate90 = rotate(img, angle=90)fig = plt.figure(tight_layout='auto', figsize=(10, 7))fig.add_subplot(221)plt.title('Rotate 30')plt.imshow(rotate30)fig.add_subplot(222)plt.title('Rotate 45')plt.imshow(rotate45)fig.add_subplot(223)plt.title('Rotate 60')plt.imshow(rotate60)fig.add_subplot(224)plt.title('Rotate 90')plt.imshow(rotate90)plt.show()
# image shearing using sklearn.transform.AffineTransform# try out with differnt values of shear tf = AffineTransform(shear=-0.5)sheared = transform.warp(img, tf, order=1, preserve_range=True, mode='wrap')sheared_fig = plot_side_by_side(img, sheared, 'Original', 'Sheared')
# Image rescaling with sklearn.transform.rescalerescaled = transform.rescale(img, 1.1)rescaled_fig = plot_side_by_side(img, rescaled, 'Original', 'Rescaled')plt.show()print('Original Shape: ',img.shape)print('Rescaled Shape: ',rescaled.shape)Output: Original Shape:  (684, 1024, 3)Rescaled Shape:  (752, 1126, 3)
# flip up-down using np.flipudup_down = np.flipud(img)fig_updown = plot_side_by_side(img, up_down, 'Original', 'Up-Down')plt.show()
# flip up-down using np.flipudleft_right = np.fliplr(img)fig_lr = plot_side_by_side(img, left_right, 'Original', 'Up-Right')plt.show()
What is Color Augmentation?
Color augmentation is a technique where we play with the intensity value of pixels.
We reproduce different images by tweaking brightness, contrast, saturation, and also we can add random noise to the image.
# Apply Random Noise to image using skimage.utils.random_noisenoised = random_noise(img, var=0.1**2)fig_noised = plot_side_by_side(img, noised, 'Original', 'Noised')plt.show()
# Increasing the brighness of the Image# Note: Here we add 100/255 since we scaled Intensity values of Image when loading (by dividing it 255)highB = img + (100/255)fig_highB = plot_side_by_side(img, highB, 'Original', 'highB')plt.show()
# Increasing the contrast of the Image# Note: Here we add 100/255 since we scaled Intensity values of Image when loading (by dividing it 255)highC = img * 1.5fig_highB = plot_side_by_side(img, highC, 'Original', 'highC')plt.show()
Did you notice one thing, we have already created 11 different images from 1 image. Note we can still play with parameters and create a lot more.
When training neural networks we can add random transformations to the ImageLoader. There are other advanced techniques like using GAN for data augmentation, let us see that in another article. I hope now you understand what is image augmentation.
You can find the notebook at https://github.com/Mathanraj-Sharma/sample-for-medium-article/blob/master/image-augmentation-skimage/image-augmentation.ipynb