Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
Written By:
shekharpandey
12 minute read
Data AugmentationPythonKerasImage Analysis

Horizontal and Vertical Flip Data Augmentation

Posted in Machine Learning   LAST UPDATED: AUGUST 23, 2021

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

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

https://github.com/shekharpandey89/data_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 load it with the name variable image.

Line 13: In this line, we have converted the PIL image format to 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 pass 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 flip 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 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 flip 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.

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.

You may also like:


IF YOU LIKE IT, THEN SHARE IT

RELATED POSTS