Signup/Sign In

Horizontal and Vertical Flip Data Augmentation

Posted in Machine Learning   LAST UPDATED: JUNE 24, 2023

    In this blog, we are going to study one more data augmentation argument which is called Horizontal and Vertical flip augmentation.

    The vertical and horizontal flip augmentation means they will reverse the pixels rows or column-wise respectively. We will use the horizontal_flip or vertical_flip arguments to use this technique inside of the ImageDataGenerator class. Below we have the Python code for both the methods with results.

    Horizontal Flip Data Augmentation

    Horizontal flip basically flips both rows and columns horizontally. So for this, we have to pass the horizontal_flip=True argument in the ImageDataGenerator constructor. By default, its value is false.

    So let's see python code for the horizontal_flip data augmentation.

    Python Implementation

    We save the below program with the name horizontal_flip.py.

    # python program to demonstrate the horizontal flip of the image with the horizontal_flip = True 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)
    # print(imageNew)
    # now here below we creating the object of the data augmentation class
    imageDataGen = ImageDataGenerator(horizontal_flip=True)
    # 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 and loaded it with the name variable image.

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

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

    Line 19: We have created the object (imageDataGen) for the class ImageDataGenerator and passed the argument horizontal_flip = True.

    Line 21: We have created the iterator to perform the transformation on the batch.

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

    Output:

    Now we are going to run the saved horizontal_flip.py python program as shown in the below screenshot. The results show the image of the parrot flips horizontally.

    Horizontal Flip Data Augmentation Example

    The above program executed and created nine augmented images.

    Also Read: Horizontal And Vertical Shift Data Augmentation

    Vertical Flip Data Augmentation

    Vertical flip basically flips both rows and columns vertically. So for this, we have to pass the vertical_flip=True argument in the ImageDataGenerator constructor. By default, its value is false.

    So let's see the Python code for the Vertical flip data augmentation.

    Python Implementation

    We will save the below program with the name vertical_flip.py.

    # python program to demonstrate the vertical flip of the image with the vertical_flip = True 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)
    # print(imageNew)
    # now here below we creating the object of the data augmentation class
    imageDataGen = ImageDataGenerator(vertical_flip=True)
    # 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()

    On 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 and loaded it in the variable image.

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

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

    Line 19: We have created the object (imageDataGen) for the class ImageDataGenerator and passed the argument vertical_flip = True.

    Line 21: We have created the iterator to perform the transformation on the batch.

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

    Output:

    We run the above Python program vertical_flip.py as shown below in the screenshot. The result is showing the parrot image flipped vertically.

    Vertical Flip Data Augmentation example

    Conclusion

    So now we have completed one more data augmentation argument horizontal and vertical flip. After running the above code, I am sure you will be able to understand how vertical and horizontal flip augmentation works with the image data. And you can use this technique in your coming project of deep learning.

    Code: The code of this blog, can be downloaded from the below GitHub link.

    But be careful before using any data augmentation technique in your project because sometimes these techniques can also harm your project. So first try to understand which data argumentation is useful for your data creation.

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

    Frequently Asked Questions(FAQs)

    1. What is horizontal flip data augmentation?

    Horizontal flip data augmentation is a technique used in deep learning to increase the size of a dataset by flipping images horizontally. This can help improve the accuracy of a model by exposing it to additional variations of the same images.

    2. What is vertical flip data augmentation?

    Vertical flip data augmentation is a technique used in deep learning to increase the size of a dataset by flipping images vertically. This can help improve the accuracy of a model by exposing it to additional variations of the same images.

    3. How does data augmentation improve deep learning models?

    Data augmentation helps deep learning models generalize better by increasing the size of the dataset and exposing the model to additional variations of the same images. This can improve the model's accuracy and reduce overfitting.

    4. How can I implement horizontal and vertical flip data augmentation in my deep learning model?

    You can implement horizontal and vertical flip data augmentation using Keras' ImageDataGenerator class, which allows you to specify various data augmentation techniques, including horizontal and vertical flips. Here's an example code snippet:

    from keras.preprocessing.image import ImageDataGenerator
    
    # Create an ImageDataGenerator object with horizontal and vertical flip augmentation
    datagen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True)
    
    # Use the ImageDataGenerator to augment your training dataset
    train_data = datagen.flow_from_directory('path/to/train/directory', batch_size=32, target_size=(224, 224))

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

    RELATED POSTS