Signup/Sign In

Different Methods of Image Augmentation - Keras ImageDataGenerator Class

Posted in Programming   LAST UPDATED: OCTOBER 21, 2021

    In the previous blog, we studied the basics of data augmentation and understanding its purpose. If you did not study the previous blog, kindly see the previous blog first before proceeding with this one.

    As we know about data augmentation and its importance, so let's discuss the different methods of this one by one.

    Image data augmentation is the most important part of computer vision. The following geometric transformation methods of data augmentation are used to re-generate the image data.

    1. Horizontal and Vertical flips

    2. Horizontal and Vertical shift

    3. Brightness

    4. Zoom

    5. Rotations

    So we will be covering all the above methods in detail one by one in different articles. But before that, let's try to understand how we can implement augmentation using Keras Python module.

    As we already know Keras is providing a class ImageDataGenerator for this purpose. So to begin using this class, we have to create an object of this class and then add all those transformation methods which we want to apply to the images.

    The most important techniques for image data augmentation, that are supported in the ImageDataGenerator class are as follows:

    1. The argument horizontal_flip and vertical_flip to use flip the image.

    2. The argument height_shift_range and width_shift_range use to shift the image horizontal and vertical.

    3. The argument brightness_range use to change the image brightness.

    4. The argument zoom_range, which is used to change the image zoom - doing zoom in and zoom out.

    5. The argument rotation_range use to rotate the image.

    Let's try to understand how to create ImageDataGenerator class object and how to pass the above method arguments inside of that.

    Using Python Keras ImageDataGenerator Class:

    Below is the method to create the ImageDataGenerator class object.

    # create an object of the ImageDataGenerator class
    imageDataGen = ImageDataGenerator()

    Once we have created the object of ImageDataGenerator class, then we create an iterator using the flow() method as shown below. The iterator will handle one image batch at a time and will return the transformed images. In the flow() method, we provide the image variable which has the images already loaded into the memory but if you want to fetch images from your local directory then we have to use the flow_from_directory() function.

    # creating image iterator 
    iterator = imageDataGen.flow(image,..)

    Below is another example to create the iterator, if we load images directly from the local directory.

    # creating image iterator
    iterator = imageDataGen.flow_from_directory(directory=/path/of/the/image/Dataset/,...)

    Once we have created our iterator, then we can train the neural network using the fit_generator() function.

    We also have to define the steps_per_epoch inside of the function as an argument value which tells how many images batch are present in each epoch.

    For example: If we have 500 images and we want to keep the batch size of 10, then according to the formula ceil(num_samples / batch_size), we get ceil(500/10) or 50 batches. This means 50 steps per epoch and in each step it covers 50 images. So in this way, we can process complete image datasets in every epoch.

    # we define here model
    model = ...
    # now we fitting model with our newly generated dataset using fit_generator() function and we also
    # define there steps_per_epoch
    model.fit_generator(iterator, steps_per_epoch=50, ...)
    

    As we know already, all datasets images are not used for training of the model because ImageDataGenerator randomly chooses the transformed data and closes the copy of the original image to the training model.

    The data generator can also help us to define the testing and validation datasets. Now that we have understood how to use the ImageDataGenerator class, now its time to explore some of the methods of this technique.

    We will see some of these techniques one by one using code and then, at last, we combine all those methods in a single program to see the full result.

    Now follow the below tutorials to understand how the different methods work:

    1. Horizontal and Vertical Shift Data Augmentation

    2. Horizontal and Vertical Flip Data Augmentation

    3. Imgae Rotation Using Data Augmentation

    4. Random Brightness Image Data Augmentation

    5. Random Zoom Image Data Augmentation

    Conclusion:

    Using the Keras ImageDataGenerator class we can apply all the above mentioned Image data augmentation techniques on a dataset of images.

    You may also like:

    About the author:
    I am an Artificial Intelligence Engineer. Doing research work in Virtual Reality and Augmented Reality. I would like to write article or share my work with others apart from my professional life.
    Tags:Data AugmentationPythonKerasImage Augmentation
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS