Big Data, Cloud Computing, Data Analytics, Google Cloud (GCP)

4 Mins Read

Image Data Augmentation Using Keras API ImageDataGenerator

Overview

This blog post focuses on the Keras API ImageDataGenerator, which is used to augment the input images by obtaining input for the original data and then transforming that data randomly before producing an output consequent that only contains the newly transformed data.

Introduction to Image Augmentation

By applying transformation hyperparameters to existing image data, like random rotations, shear transforms, shifts, zooms, and flips, image augmentation enlarges the image training set. Using the ImageDataGenerator object, it can apply a variety of data augmentation techniques, including rotations, cropping, zoom in/out, etc.

  • Cloud Migration
  • Devops
  • AIML & IoT
Know More

Requirement of Augmentation

To train our deep learning models for classification tasks using image data when we do not have sufficient training data, we utilize image augmentation. In these circumstances, we can apply changes to the current images to produce new images from them. CNN considers these images to be brand-new even if they appear to be similar (Convolutional Neural Network). With the use of augmentation, we can expand our training set, which will help our model interact more efficiently.

Introduction to ImageDataGenerator

The ImageDataGenerator produces batches of real-time enhanced tensor image data. Each training image can have any random alteration applied to it when it is sent to the model. This will increase the sturdiness of your model and reduce overhead memory usage.

Hyperparameters of ImageDataGenerator

Let’s explore this class further and see how it may be used to enhance images in various ways. There are many hyperparameters given in ImageDataGenerator class by modifying them we can augment the images (source

https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator ) as shown below figure.

kera1

Source: TensorFlow

Let’s now examine a couple of ways for augmentation using the Keras API ImageDataGenerator class hyperparameters.

Random Rotations – It enables the model to decrease reactivity to the object’s orientation. Bypassing an integer number in the rotation range argument, the ImageDataGenerator class allows you to randomly rotate_images across any angle between 0 and 360 degrees.

Random Shifts – The object might not always be in the exact center of the picture. By adding a certain fixed value to each pixel, we can solve this issue by shifting the image’s pixels either vertically or horizontally. The height_shift_range and width_shift_range arguments in the ImageDataGenerator class allow for vertical and horizontal image shifts, respectively. If the value is a float number, it will specify the amount of the image’s width or height to shift. If it is not an integer value, the width or height is simply shifted by the number of pixel values specified. The below screenshot shows argument values clearly.

kera2

Random Flips – The ImageDataGenerator class has two parameters for flipping along the vertical or horizontal axis: horizontal_flip and vertical_flip. It can only be applied to symmetrical objects.

Random Brightness – It randomly varies the color intensity of the image. In the ImageDataGenrator class, brightness can be adjusted using the brightness_range_option. A brightness shift value is chosen from a range of two float values that are accepted. The image is darkened by values under 1.0 and is made brighter by values over 1.0.

Random Zoom – The zoom enhancement either randomly zooms in or out on the image. The zoom_range option of the ImageDataGenerator class accepts a float value to specify the zoom level. The lower and upper limits could be specified in a list with two values. If a float value is provided instead, zooming will take place between [1-zoom range,1+zoom range].

Steps to Implement ImageDataGenerator in Model

For building a model with the implementation of ImageDataGenerator we are importing the CIFAR-10 dataset from the Keras library, CIFAR-10 dataset is an established computer-vision dataset used for object recognition.

Step 1: Import libraries and dataset

kerastep1

Step 2: Splitting data into train and test dataset

kerastep2

After splitting the dataset, convert the target class into categorical values for both the train & test datasets.

Step 3: Create an ImageDataGenerator object with hyperparameters

kerastep3

Step 4: Fit the model on batches with real-time data augmentation

kerastep4

In this step, we are fitting the model with 32 batch size (number of images per batch).

After fitting, the model may be tested against a test dataset using the evaluate_generator() function, and it can also be used to predict data using the predict_generator() function.

Conclusion

The ImageDataGenerator is that it resolves the problem of data(image) insufficiency and uses less memory. This is because when we load all the images at once when we don’t use this class. However, while using it, we load the images in batches, which uses much less RAM.

Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.

  • Cloud Training
  • Customized Training
  • Experiential Learning
Read More

About CloudThat

CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding Keras and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

FAQs

1. What is Keras?

ANS: – An open-source software library called Keras gives artificial neural networks a Python interface. For the TensorFlow library, Keras serves as an interface.

2. What are the dependencies needed for it?

ANS: – The only dependencies needed for the occasion are mainly TensorFlow. Install TensorFlow through pip using the command prompt. pip install tensorflow

3. What is a hyperparameter?

ANS: – A hyperparameter is a parameter that is set before the learning process begins. These parameters are tuneable and can directly affect how well a model train.

WRITTEN BY Anjali Sikhwal

Anjali Sikhwal works as a Subject Matter Expert - Data and AIoT at CloudThat and holds a Master's & PGD in Data Science. She is interested in Artificial intelligence and Machine learning technologies. She helps clients to deploy robust ML models. Her hobbies are exploring new places and music.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!