Image Augmentation Layers

Learn about various preprocessing layers available in Keras and implement commonly used image augmentation layers.

DL models require a huge amount of training data for successful predictions. This data might not always be available. However, we can augment the existing dataset to artificially increase the size of an image dataset. Keras achieves image augmentation by applying random transformations to the dataset images during the model training process. Augmentation allows the DL models to see various aspects of the input images to develop into better-generalized models.

The Keras preprocessing layers API allows us to build data-processing pipelines. The layers provided by this API preprocess data during the neural network training. Keras provides preprocessing layers for numerous tasks that include image processing, image data augmentation, feature preprocessing, and text preprocessing. Keras preprocessing layers API, within tf.keras.layers, provides two types of image augmentation layers: color and position augmentation. Let’s discuss some of the augmentation layers below.

Color augmentation layers

Color augmentation layers change the color or pixel values of the input images. Let’s explore common color augmentation layers.

The RandomContrast layer

The RandomContrast layer accepts the argument factor to adjust image contrast randomly during training. The factor can be a single value or a tuple of size 2 (lower, upper). For a single float, lower = upper. This layer randomly picks the contrast factor in the range [1.0 - lower, 1.0 + upper]. For a pixel xx in an image channel, the output is (xmean)×factor+mean(x - mean) \times factor + mean, where meanmean is the average value of the channel. The following program demonstrates the working of the RandomContrast layer:

Get hands-on with 1200+ tech skills courses.