Keras ImageDataGenerator

This tutorial has explained Keras ImageDataGenerator class with example. If you want to understand about Data Augmentation, please refer to this article of Data Augmentation.

Data Augmentation is a technique of creating new data from existing data by applying some transformations such as flips, rotate at a various angle, shifts, zooms and many more. Training the neural network on more data leads to achieving higher accuracy. In real-world problem, we may have limited data. Therefore, data augmentation is often used to increase train dataset.

A Keras deep learning library provides the data augmentation function, which applies augmentation automatically while training the model. An ImageDataGenerator class function provides a range of transformations such as:

  • Translations
  • Rotations
  • Shearing
  • Changes in scale
  • Image fliping
  • Zooming

Types Of Data Augmentation

There are mainly two kinds of data augmentation method such as:

  1. Dataset generation and data expansion via data augmentation (Static)
      • In this method, we apply the transformation to each image and save the transformed image to disk. The deep learning model is trained on both original images as well as transformed images.
      • This method is not feasible if you are training the model on a large set of data.
  2. In-place/on-the-fly data augmentation (Dynamic)
      • In this method, image transformations are performed while training the model.
      • Keras ImageDataGenerator is this type of data augmentation.

How Keras ImageDataGenerator Works

  • Take a batch of images used for training.
  • Apply random transformations to each image in the batch.
  • Replacing the original batch of images with a new randomly transformed batch.
  • Train a Deep Learning model on this transformed batch.

 

Let’s see the syntax to create for Keras ImageDataGenerator

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)

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

data_generator.flow(X,Y)

Example

Let’s apply ImageDataGenerator on the below image. You can download this image using this link and save it in the current working directory with name test_image.jpg.

# Load the required packages
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array, load_img
import numpy as np

#Let's define the input dir path & Output dir path where generated image will store
src_path = '/home/test_image.jpg'
des_path = '/home/data_augment/'

# Let's load input image
image = load_img(src_path)
image = img_to_array(image)
image = np.expand_dims(image, axis=0)

# Let's define ImageDataGenerator class
aug = ImageDataGenerator(
 rotation_range=30,
 zoom_range=0.15,
 width_shift_range=0.2,
 height_shift_range=0.2,
 shear_range=0.15,
 horizontal_flip=True,
 fill_mode="nearest")

# Let's apply ImageDataGenerator to input image
imageGen = aug.flow(image, batch_size=1, save_to_dir=des_path,save_prefix="image", save_format="jpg")

# Define number of augmented image which you want to download and iterate through loop
total_image = 15
i = 0
for e in imageGen:
    if (i == total_image):
        break
    i = i +1

After executing the above code, you will find the augmented images in defined des_path.

Leave a Reply

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

Computer Vision Tutorials

Prepare COCO dataset of a specific subset of classes for semantic image segmentation

YOLOV4: Train a yolov4-tiny on the custom dataset using google colab.

Video classification techniques with Deep Learning

Keras ImageDataGenerator with flow_from_dataframe()

Keras ImageDataGenerator with flow_from_directory()

Keras ImageDataGenerator with flow()

Keras fit, fit_generator, train_on_batch

Keras Modeling | Sequential vs Functional API

Save and Load Keras Model

Convolutional Neural Networks (CNN) with Keras in Python

Transfer Learning for Image Recognition Using Pre-Trained Models

An introduction to Transfer Learning

Keras ImageDataGenerator and Data Augmentation

Introduction to Computer Vision

Study Machine Learning