Image Augmentation for Deep Learning

Suki Lau
Towards Data Science
3 min readJul 10, 2017

Deep networks need large amount of training data to achieve good performance. To build a powerful image classifier using very little training data, image augmentation is usually required to boost the performance of deep networks. Image augmentation artificially creates training images through different ways of processing or combination of multiple processing, such as random rotation, shifts, shear and flips, etc.

Image Data Generator

An augmented image generator can be easily created using ImageDataGenerator API in Keras. ImageDataGenerator generates batches of image data with real-time data augmentation. The most basic codes to create and configure ImageDataGenerator and train deep neural network with augmented images are as follows.

datagen = ImageDataGenerator()
datagen.fit(train)
X_batch, y_batch = datagen.flow(X_train, y_train, batch_size=batch_size)
model.fit_generator(datagen, samples_per_epoch=len(train), epochs=epochs)

We can experiment with the following code to create augmented images with the desired properties. In our case, the following data generator generates a batch of 9 augmented images with rotation by 30 degrees and horizontal shift by 0.5.

datagen = ImageDataGenerator(rotation_range=30, horizontal_flip=0.5)
datagen.fit(img)
i=0
for img_batch in datagen.flow(img, batch_size=9):
for img in img_batch:
plt.subplot(330 + 1 + i)
plt.imshow(img)
i=i+1
if i >= batch_size:
break
Original Image and Augmented Images

Histogram Equalization

Apart from the standard techniques of data augmentation provided by the ImageDataGenerator class in Keras, we can use custom functions to generate augmented images. For example, you may want to adjust the contrast of images using contrast stretching. Contrast stretching is a simple image processing technique that enhances the contrast by rescaling (“stretching”) the range of intensity values of an image to a desired range of values.

Histogram equalization is another image processing technique to increase global contrast of an image using the image intensity histogram. The equalized image has a linear cumulative distribution function. This method needs no parameter, but it sometimes results an unnatural looking image.

An alternative is adaptive histogram equalization (AHE) which improves local contrast of an image by computing several histograms corresponding to different sections of an image (differs from ordinary histogram equalization which uses only one histogram to adjust global contrast), and uses them for local contrast adjustment. However, AHE has a tendency to over-amplify noise in relatively homogeneous regions of an image.

Contrast limited adaptive histogram equalization (CLAHE) was developed to prevent the over-amplification of noise resulted from AHE. In gist, it limits the contrast enhancement of AHE by clipping the histogram at a predefined value before computing the cumulative distribution function.

To implement custom preprocessing function for augmentation in Keras, we first define our custom function and pass it to ImageDataGenerator as an argument. For instance, we can implement AHE using the following code.

def AHE(img):
img_adapteq = exposure.equalize_adapthist(img, clip_limit=0.03)
return img_adapteq
datagen = ImageDataGenerator(rotation_range=30, horizontal_flip=0.5, preprocessing_function=AHE)
Augmented Images using Contrast Stretching (left), Histogram Equalization (middle) and Adaptive Histogram Equalization (right)

Published in Towards Data Science

Your home for data science and AI. The world’s leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.

Responses (4)

What are your thoughts?

how the arguments of ImageDataGenerator like rescaling, vertical_flip, horizontal_flip..etc plays the role in data generation?
is one image is generated by one argument? if so then how can we see the total no. of images generated for one image input?

--

Very nice article. I wasn’t aware of the ImageDataGenerator class :)
I wonder if anyone checked how thsese image augmentation techniques actually influence accuracy of the final neural network.

--

Hi, nice post. I’d like to ask what the augment setting is like if I want to set the rotation degrees changing randomly at 0, 90, 180 or 270?

--