Signup/Sign In

Horizontal And Vertical Shift Data Augmentation

Posted in Machine Learning   LAST UPDATED: SEPTEMBER 1, 2021

    In this tutorial, we will cover the concept of Horizontal and Vertical Shift Image Augmentation using Python Keras ImageDataGenerator class with code examples and output.

    We have already covered an introduction to using the Keras ImageDataGenerator Class for Image Augmentation and we would recommend you to please read that article before proceeding with this one.

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

    Horizontal Shift Image Augmentation

    A horizontal shift augmentation means pixels of the image will shift horizontally without changing the dimension of the image. So, it means some pixels of the image will be clipped off and there will be a new area where new pixels of the image will be added to get the same image dimension.

    The Keras ImageDataGenerator class has two main arguments width_shift_range and height_shift_range and based on these values the image pixels will move either to the left-right and top-bottom.

    These parameters which we will specified in the ImageDataGenerator constructor can have floating-point values from 0 to 1 and on that value depends how much the image will move horizontally or vertically.

    We can also specify the pixel value in a tuple to move the image pixel, instead of a value between 0 to 1.

    For example, to use a pixel, we can supply values in the form of min and max value range like [-10,10] or [-0.3, 0.3].

    The below example explains the horizontal shift by using the width_shift_range argument.

    Python Implementation:

    We save the below python program with the name horizontalShift.py. To run this program you have to keep an image (any image) in the same folder where you will keep this python file. Because it will read and use that image. If your image is at another location then specify the full path in the load_img (/path/of/the/image).

    # python program to demonstrate the horizontal shift of the image with the width_shift_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)
    # print(imageNew)
    # now here below we creating the object of the data augmentation class
    imageDataGen = ImageDataGenerator(width_shift_range=[-200,200])
    # 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 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 then created the object (imageDataGen) for the class ImageDataGenerator and pass the argument width_shift_range.

    Line 21: We then 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 out transformed images as shown below in the result.

    Output:

    When we run the above code, we got the following output as shown below on the screen. So in the below output, we can see that the parrot image randomly moves horizontally by the selected pixel value range that we supplied.

    Horizontal Shift Image Augmentation exampl

    Also Read: Horizontal and Vertical Flip Image Data Augmentation

    Vertical Shift Image Augmentation

    The below code will explain the vertical shift augmentation where we pass the height_shift_range to the class ImageDataGenerator. In this, we are passing the percentage value which shifts the image to the height of the image (according to the percentage value).

    Python Implementation:

    We save the below python program with the name verticalShift.py. To run this program you have to keep an image (any image) in the same folder where you will keep this python file. Because it will read and use that image. If your image is at another location then specify the full path in the load_img (/path/of/the/image).

    # python program to demonstrate the vertical shift of the image with the height_shift_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(height_shift_range=0.4)
    # 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()

    Output:

    Following is the output,

    Vertical Shift Image Augmentation example

    In the above output, we can see that the image shift 0.4 percentage of the image.

    Conclusion:

    We studied two arguments horizontal shift and vertical shift for Keras ImageDataGenerator class in this tutorial. We can see from the above output both vertical and horizontal shift makes some new dataset but in some cases, it can go out of the image recognition. But still, the machine can learn some features from those images to give better results on the unseen data.

    In the next blog, we will be going to explain the next argument vertical flip and horizontal flip.

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

    RELATED POSTS