Blogs/Image Data Augmentation

Image Data Augmentation

arsalanwasim6@gmail.com Jul 09 2021 2 min read 231 views
Data Wrangling
Image Data Augmentation.png

A convolutional neural network (CNN) is trained with images from a certain dataset, and the number of images in a dataset defines the accuracy of the model. The more images we have for a particular dataset, the better its ability to generalize will be.

Sometimes you will find that you do not have enough images to train your model and therefore your model will overfit very easily, resulting in a lot of noise in the output predictions by the model. Thus, to fight overfitting and to increase the size of the dataset, you will have to use image data augmentation.

As the name suggests, the goal of image data augmentation is to increase the size of the dataset. This is typically done by creating artificial samples in the data through image processing techniques such as zooming, panning, rotating, flipping, shifting images in place, and much more. Image data augmentation also helps the CNN learn the additional features, aiding in boosting test set performance.

In this blog, we will make functions in Python to convert an image format from RGB (red, green, blue) to YUV. We will also zoom the image, pan the image, add random brightness, and flip the image.

 

The conversion from RGB format to YUV looks as:

Image Data Augmentation

 

Zooming the image results in the following output:

Image Data Augmentation

 

Panning the image would look as:

Image Data Augmentation

Lastly, brightening the image looks as:

Image Data Augmentation

 

Learn and practice this concept here: 

https://mlpro.io/problems/data-imputation/

 

The code for this looks like:
#import libraries
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from imgaug import augmenters as iaa
import cv2
import ntpath
#Conversion to YUV format:
#preprocess all images
def img_preprocess(img):
    #img= mpimg.imread(img)
    img = img[60:135,:,:]
    img = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
    img = cv2.GaussianBlur(img,  (3, 3), 0)
    img = cv2.resize(img, (200, 66))
    img = img/255
    return img
#load image
image = 'center_2020_07_25_14_27_15_263.jpg'
original_image = mpimg.imread(image)
#call the preprocessing function
preprocessed_image = img_preprocess(original_image)
#display output
fig, axs = plt.subplots(1, 2, figsize=(15, 10))
fig.tight_layout()
axs[0].imshow(original_image)
axs[0].set_title('Original Image')
axs[1].imshow(preprocessed_image)
axs[1].set_title('Preprocessed Image')

#Zooming function:
#apply zoom using image aug
def zoom(image):
    zoom = iaa.Affine(scale=(1,1.3))
    image= zoom.augment_image(image)
    return image
#read image
original_image = mpimg.imread(image)
#zoom image function
zoomed_image = zoom(original_image)

#displayplot
fig, axs = plt.subplots(1, 2, figsize=(15, 10))
fig.tight_layout()

axs[0].imshow(original_image)
axs[0].set_title('Original Image')

axs[1].imshow(zoomed_image)
axs[1].set_title('Zoomed Image')

#Panning function:
#apply translation on x and y axis
def pan(image):
    pan = iaa.Affine(translate_percent={"x": (-0.3, 0.3), "y": (-0.3, 0.3)})
    image = pan.augment_image(image)
    return image
original_image= mpimg.imread(image)
panned_image = pan(original_image)

fig, axs = plt.subplots(1, 2, figsize=(15, 10))
fig.tight_layout()

axs[0].imshow(original_image)
axs[0].set_title('Original Image')

axs[1].imshow(panned_image)
axs[1].set_title('panned Image')

#Image random brighting:
#vary brightness by multiplying pixels intensities
def img_random_brightness(image):
    brightness = iaa.Multiply((0.2,1.2))
    image = brightness.augment_image(image)
    return image
original_image = mpimg.imread(image)
brighthened_image = img_random_brightness(original_image)

fig, axs = plt.subplots(1, 2, figsize=(15, 10))
fig.tight_layout()

axs[0].imshow(original_image)
axs[0].set_title('original_image')
axs[1].imshow(brighthened_image)
axs[1].set_title('brighthened_image')