Image Augmentation for Deep Learning | by Suki Lau | Towards Data Science

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.
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=0for 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
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_adapteqdatagen = ImageDataGenerator(rotation_range=30, horizontal_flip=0.5, preprocessing_function=AHE)
Source code
Image Augmentation for Deep Learning With Keras
Building powerful image classification models using very little data