Keras ImageDataGenerator and Data Augmentation

[vc_row][vc_column][vc_column_text]Data Augmentation is a technique of creating new data from existing data by applying some transformations. It expands the size of train dataset. Training the neural network on more data leads to achieving higher accuracy. These transformations include many operations from the field of image manipulations, like flips, rotate at a various angle, shifts, zooms and many more. Neural networks would not distinguish the augmented image.

In Real-life, we may have limited train data in various condition. But, the small amount of train data is not sufficient to get high performance. However, we can generate more data with some modifications. It is also important to select the specific data augmentation method carefully in the context of the problem domain. Here, I have illustrated various data augmentation technique with example in python.

Flip:

Flip augmentation derived the horizontal flip and vertical flip. The numpy package provides a flip operation.

img = cv2.imread('bird.jpg')
flip_hr = np.fliplr(img)
flip_vr = np.flipud(img)

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/3″][vc_single_image image=”521″ img_size=”medium”][vc_column_text]

Orignal Image

[/vc_column_text][/vc_column][vc_column width=”1/3″][vc_single_image image=”554″ img_size=”medium”][vc_column_text]

Horizontal flip

[/vc_column_text][/vc_column][vc_column width=”1/3″][vc_single_image image=”560″ img_size=”medium”][vc_column_text]

Vertical flip

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]Rotation:

A Rotation augmentation rotates the image at a specific angle from 0 to 360 degree. A rotation operation is also likely to rotate pixels out of the image frame and leave the area of the frame with blank. A Scipy python package provides the rotate operation.

from scipy import ndimage
img_45 = ndimage.rotate(img, 45, reshape=False)
img_310 = ndimage.rotate(img, 310, reshape=False)

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/3″][vc_single_image image=”521″ img_size=”medium”][vc_column_text]

Original Image

[/vc_column_text][/vc_column][vc_column width=”1/3″][vc_single_image image=”557″ img_size=”medium”][vc_column_text]

Rotated Image

[/vc_column_text][/vc_column][vc_column width=”1/3″][vc_single_image image=”558″ img_size=”medium”][vc_column_text]

Rotated Image

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]Shift:

Shift augmentation can be a horizontal and vertical shift. A shift transformation means moving all pixel of an image in one direction either horizontally or vertically. By applying the shift transformation, Some pixels of the image may be clipped off the image and some new pixels are introduced.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/3″][vc_single_image image=”521″ img_size=”medium”][vc_column_text]

Original Image

[/vc_column_text][/vc_column][vc_column width=”1/3″][vc_single_image image=”527″ img_size=”medium”][vc_column_text]

Shift Operation

[/vc_column_text][/vc_column][vc_column width=”1/3″][vc_single_image image=”526″ img_size=”medium”][vc_column_text]

Shift Operation

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]Crop:

A crop augmentation is a randomly cropping some region of an original image. A random cropping transformation is a considerable way of adding diversity to the training dataset, but be careful not to use it to the intense.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/3″][vc_single_image image=”521″ img_size=”medium”][/vc_column][vc_column width=”1/3″][vc_single_image image=”530″ img_size=”medium”][/vc_column][vc_column width=”1/3″][vc_single_image image=”529″ img_size=”medium”][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]Random Brightness:

A random brightness augmentation method randomly changes the brightness level of an image and generate slightly brighter and darker images.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/3″][vc_single_image image=”521″ img_size=”medium”][/vc_column][vc_column width=”1/3″][vc_single_image image=”532″ img_size=”medium”][/vc_column][vc_column width=”1/3″][vc_single_image image=”531″ img_size=”medium”][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]Zoom:

A zoom augmentation method randomly zooms at different scale and generate a variety of images from an original image.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/3″][vc_single_image image=”521″ img_size=”medium”][/vc_column][vc_column width=”1/3″][vc_single_image image=”534″ img_size=”medium”][/vc_column][vc_column width=”1/3″][vc_single_image image=”533″ img_size=”medium”][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]Gaussian Noise:

A slightly added noise to an image also being useful to a neural network. Gaussian noise augmentation method is often useful to deal with overfitting neural network model.

import numpy as np
import cv2
mean = 0
var = 40
sigma = var ** 0.8
img = cv2.imread("final_bird.jpg")
gaussian = np.random.normal(mean, sigma, (img.shape[0],img.shape[1])) 
noisy_image = np.zeros(img.shape, np.float32)
if len(img.shape) == 2:
    noisy_image = img + gaussian
else:
    noisy_image[:, :, 0] = img[:, :, 0] + gaussian
    noisy_image[:, :, 1] = img[:, :, 1] + gaussian
    noisy_image[:, :, 2] = img[:, :, 2] + gaussian
cv2.normalize(noisy_image, noisy_image, 0, 255, cv2.NORM_MINMAX, dtype=-1)
noisy_image = noisy_image.astype(np.uint8)

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/3″][vc_single_image image=”521″ img_size=”medium”][/vc_column][vc_column width=”1/3″][vc_single_image image=”563″ img_size=”medium”][/vc_column][vc_column width=”1/3″][vc_single_image image=”564″ img_size=”medium”][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]

.     .     .

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]

Image Augmentation using Keras ImageDataGenerator

A Keras deep learning library provides the data augmentation function, which applies augmentation automatically while training the model. An ImageDataGenerator class function provide a range of transformations. Here, is the example of how to use ImageDataGenerator class.

from keras.preprocessing.image import ImageDataGenerator
#Construct Data Generator
data_generator = ImageDataGenerator(
                        featurewise_center=False,
                        featurewise_std_normalization=False,
                        rotation_range=10,
                        width_shift_range=0.1,
                        height_shift_range=0.1,
                        zoom_range=.1,
                        horizontal_flip=True)

flow function loads the image dataset in memory and generates batches of augmented data.

data_generator.flow(X,Y)

flow_from_directory function generates batches of augmented data located at specific directory of a disk. Images in the directory are stored in subdirectories with respect to their class.

data_generator.flow_from_directory('path_to_dir')

fit_generator function used to train a neural network.

model.fit_generator(data_generator.flow(X_train, y_train,128),
                        steps_per_epoch=len(X_train) / 128,
                        epochs=20, verbose=1, callbacks=callbacks,
                        validation_data=(X_val,y_val))

[/vc_column_text][/vc_column][/vc_row]

Leave a Reply

Your email address will not be published. Required fields are marked *

Computer Vision Tutorials