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

Random Zoom Image Augmentation - Keras ImageDataGenerator

Posted in Machine Learning   LAST UPDATED: AUGUST 27, 2021

Random Zoom Image augmentation is used to generate images with varying zoom levels for feeding our deep learning model. We will be using Keras ImageDataGenerator class, along with providing the zoom_range argument.

In the previous articles of the Image Augmentation series, we have already covered the following:

So if you haven't checked these, please do check them out.

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

Random Zoom Augmentation

The zoom augmentation method is used to zooming the image. This method randomly zooms the image either by zooming in or it adds some pixels around the image to enlarge the image. This method uses the zoom_range argument of the ImageDataGenerator class. We can specify the percentage value of the zooms either in a float, range in the form of an array, or python tuple.

If we specify the value of the zoom-in using float value then it will be [1-floatValue, 1+floatValue]. For example, if we specify the float value as 0.5, then it actually becomes like [0.5, 1.5] as we subtract 0.5 from 1 in the first range and add 0.5 to 1 in the second range which actually means zoom-in 50% and zoom-out 150%.

Zoom also has some boundary values as we have in the brightness parameter case. Value of the zoom less than 1.0 magnifies the image and more than 1.0 will zoom out the image.

For example: [0.6,0.6], in this range the zoom will enlarge the image which means the object of the image will become 60% larger or come closer and if the value is greater than 1.0 e.g. [1.6, 1.6] will zoom the image out 60%. But be cautious that [1.0,1.0] will have no effect as this is the boundary.

Python Implementation:

We will save the below python program with the name randomZoom.py. 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 zooming of the image with the zoom_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(zoom_range=[0.7,1.0])
# 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 created the object (imageDataGen) for the class ImageDataGenerator and pass the argument zoom_range = [0.7,1.0]

Line 20: We have 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 will get an output similar to shown below. In the output, we can see that the input image now becomes zoomed-in. And all the images which are shown in the result are of different height and width and also their aspect ratio is different.

Random Zoom Image augmentation Example

Random Zoom-out Image Augmentation:

In the above code, we saw how the random zoom-in performed actually on the image. Now, we can use the same code of the above just slightly change in the passed augmentation zoom argument range value. So here in this program, we are going to pass the value range [1.0,1.7]. So in this range, the augment class will do zoom-out the image object 70%.

Python Implementation:

This program we saved with the name randomZoomOut.py.

# python program to demonstrate the zooming of the image with the zoom_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(zoom_range=[1.0,1.7])
# 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:

As we can see in the resulting screenshot below all the results of the augmented image are zoom-out (making the image object far).

Random Zoom Image augmentation Example

Conclusion:

And we are done with the random zoom augmentation and now we know how we can make our dataset images either zoom-in or zoom-out which depends upon the value range which we pass into the ImageDataGenerator class.

Applying these transformations on 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.

You may also like:


IF YOU LIKE IT, THEN SHARE IT

RELATED POSTS