Written By:
shekharpandey
0 minute read
Data AugmentationOpenCVPythonComputer Vision

Horizontal and Vertical Flip Data Augmentation

Posted in Machine Learning     APRIL 6, 2021

In this blog, we are going to study one more data augmentation argument which we called horizontal and vertical flip augmentation.

Code: The code of this blog, you can download 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 use the horizontal_flip or vertical_flip arguments to use this technique inside of the imageDataGenerator class. Here below is the python code of both 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: 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 load the image from our local drive and load it with the name variable image.

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

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

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

Line 21: We 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 below screenshot is showing the result after running the above code of horizontal_flip.py. The results show the image of the parrot flip horizontally.

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 in 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: We 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()

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

Line 11: We load the image from our local drive and load it with the name variable image.

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

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

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

Line 21: We 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 showing the parrot image flip vertically.

Conclusion:

So now we 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 argument 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.


   IF YOU LIKE IT, THEN SHARE IT

RELATED POSTS