Signup/Sign In

Image Rotation Augmentation - Keras ImageDataGenerator

Posted in Machine Learning   LAST UPDATED: MARCH 27, 2023

    In the previous blogs, we studied vertical and horizontal flip image augmentation. Now, we are going to explain another method for image data augmentation which is called Rotation. From the name of the argument itself, it's clear that we are going to rotate the image to a particular angle.

    Code: The code of this blog, can be downloaded from the following GitHub link: https://github.com/shekharpandey89/data_augmentation

    Random Rotation Augmentation

    In this method of augmentation, we can rotate the image by 0 to 360 degrees clockwise. In this method, the pixels of the image rotates. To use this argument in the ImageDataGenerator class constructor, we have to pass the argument rotation_range.

    The rotation_range argument accepts an integer value between 0 to 360. The code example below shows the rotation of the image from 0 to 90 degrees using the rotation_range argument.

    The example which we have implemented below is for the random rotation because the augmentation will pick any value of the degree from the range 0 to 90 as we specified the maximum degree is 90.

    Random Rotation Augmentation Python Implementation

    To run this program we have to keep the image (any image) in the same folder where you will keep this python file. Because then only it will read the image. If your image is at another location then specify the full path to the load_img(/path/of/the/image) function.

    # python program to demonstrate the rotation shift of the image with the rotation_range argument
    
    # we import all our required libraries
    from numpy import expand_dims
    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array
    from keras.preprocessing.image import ImageDataGenerator
    from matplotlib import pyplot
    
    # we first load the image
    image = load_img('parrot.jpg')
    # we converting the image which is in PIL format into the numpy array, so that we can apply deep learning methods
    dataImage = img_to_array(image)
    # print(dataImage)
    # expanding dimension of the load image
    imageNew = expand_dims(dataImage, 0)
    # now here below we creating the object of the data augmentation class
    imageDataGen = ImageDataGenerator(rotation_range=90)
    # because as we alreay load image into the memory, so we are using flow() function, to apply transformation
    iterator = imageDataGen.flow(imageNew, batch_size=1)
    # below we generate augmented images and plotting for visualization
    for i in range(9):
    	# we are below define the subplot
    	pyplot.subplot(330 + 1 + i)
    	# generating images of each batch
    	batch = iterator.next()
    	# again we convert back to the unsigned integers value of the image for viewing
    	image = batch[0].astype('uint8')
    	# we plot here raw pixel data
    	pyplot.imshow(image)
    # visualize the the figure
    pyplot.show()

    Line 4 to 8: We are importing our required packages to create our code.

    Line 11: We have loaded the image from our local drive in the variable image.

    Line 13: In this line, we converting the PIL image format to NumPy array so that we can use that it in further image processing.

    Line 16: We have then expanded our NumPy array to axis = 0 which means column side.

    Line 18: We have then created the object (imageDataGen) for the class ImageDataGenerator and pass the argument rotation_range = 90.

    Line 20: We then created the iterator to perform the transformation on the batch.

    Line 22 to 32: Then the iterator is called as per the iteration value and we got our transformed images as shown below in the result.

    Output:

    After executing the above python code we got the below output as we desire. From the output, we can see that the input image rotates from 0 to 90 degrees randomly as a value picked by the augmentation randomly.

    Random rotation image augmentation example

    Conclusion:

    Now, we have studied another augmentation argument which is rotation, and seen the results. From the results, we can conclude that the rotation works randomly because every time a slightly different image rotation is generated.

    So the maximum value which we define in the rotation_range will be the highest range for the random degree value used for rotation. And the augmentation object picks any value from 0 to maximum(which we defined). Applying these transformations to the input image will not change the actual class label of the image data.

    But each transformed image will be treated as a new image that the training model has not seen before, so in this way, we are doing another type of regularization technique that works on the training datasets. And you can use this technique in your coming project of deep learning.

    So let's continue to study data augmentation in the next blog. In the next blog, we are going to explain random brightness augmentation.

    Frequently Asked Questions

    1: What is image rotation augmentation?

    Image rotation augmentation is a data augmentation technique that involves rotating images by a certain angle to artificially increase the size of the dataset. This helps to improve the performance of machine learning models by providing them with more diverse training data.

    2: How can I perform image rotation augmentation using Keras ImageDataGenerator?

    To perform image rotation augmentation using Keras ImageDataGenerator, you can use the "rotation_range" parameter which specifies the range of random rotations to apply to the images.

    3: Are there any other image augmentation techniques available in Keras ImageDataGenerator?

    Yes, Keras ImageDataGenerator provides several other image augmentation techniques such as zooming, shifting, flipping, and shearing.

    4: How does image rotation augmentation help improve the performance of machine learning models?

    Image rotation augmentation helps improve the performance of machine learning models by exposing them to more diverse data, which helps them generalize better to new and unseen data.

    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.
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS