Image Data Augmentation using Keras ImageDataGenerator

Maximinusjoshus
featurepreneur
Published in
2 min readApr 8, 2021

Keras is an open-source software library that provides a Python interface for Artificial Neural Networks. Keras acts as an interface for the TensorFlow library. This article explores the usage of ImageDataGenertor class of Keras to augment images.

What is Image Augmentation?

Image Augmentation is the process of expanding the image training data, by using transformations such as random rotations, shear transforms, shifts zooms and flips, on available image data.

Where do we use Image Augmentation?

We use image augmentation when we don’t have enough training data to train our model. In such situations, we can create new images out of the existing images, by applying transformations to them. Though these images look similar, they are considered as entirely new images by CNN (Convolutional Neural Network). This will help us to create a larger training dataset and consequently will enable our model to converge more efficiently.

The ImageDataGenerator Class

The ImageDataGenerator class of Keras allows us to achieve the same. The ImageDataGenerator generates batches of tensor image-data with real-time augmentation. The data will be looped over in batches.

tf.keras.preprocessing.image.ImageDataGenerator(
featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-06,
rotation_range=0,
width_shift_range=0.0,
height_shift_range=0.0,
brightness_range=None,
shear_range=0.0,
zoom_range=0.0,
channel_shift_range=0.0,
fill_mode="nearest",
cval=0.0,
horizontal_flip=False,
vertical_flip=False,
rescale=None,
preprocessing_function=None,
data_format=None,
validation_split=0.0,
dtype=None,
)

So now let us apply this class to augment images

We use the flow_from_directory method, which is a generator, which takes in the path to the parent directory containing different classes of image data and generates batches of images to be fed to the ImageDataGenerator. This is the expected directory structure of the flow_from_directory method.

Parent_directory
|
|--Class_1_folder
|
|--Class_2_folder
|
|--Class_3_folder

Save the images of each class in separate folders and pass the path of the parent directory to the flow_from_directory method. In the ImageDataGenerator class, we will specify only the transformations we wish to apply to your images.

import cv2 as cv
from tensorflow.keras.preprocessing.image import ImageDataGenerator
#instantiate the ImageDataGenerator class
datagen = ImageDataGenerator(
rotation_range=40,
height_shift_range=0.2,
width_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
#loop over the data in batches and this automatically saves the images
i = 0
for batch in datagen.flow_from_directory('path_to_input_parent_directory', batch_size=6,target_size=(256,256),
save_to_dir='path_to_output_folder', save_format='jpg'):
i += 1
if i > 5:
break

The batch_size multiplied by the number of iterations will be the number of augmented images, that will be saved in the output folder after this code is executed. And that is how you augment images using the ImageDataGenerator class and the flow_from_directory method of Keras.

Thank you.

--

--