Another post starts with you beautiful people!
I hope you have enjoyed my last post and now you have become familiar with image loading, manipulation and saving in deep learning. If you have not read my last post then I recommend you to read the first part of this series. In this second part of my post we will learn about one of the most important technique used for a small image training dataset- Image Data Augmentation!
1. What is Image Data Augmentation?
Image Data Augmentation is a technique to expand the size of a training dataset. This technique can create modified versions of images which helps when we have a small dataset. Image shifting, flipping and zooming are some examples of this technique. Keras provides ImageDataGenerator class for this purpose.
2. How to use this technique?
We will learn different uses of this technique one by one by applying on an image-
A. How to shift an image?
Shifting an image means we are moving all pixels in one direction while keeping the image dimensions the same. ImageDataGenerator class has width_shift_range and height_shift_range arguements for shifting an image. Let's see how can we horizontal shift an image-
In above code cell I have loaded an image of ICC Cricket World Cup 2019 Trophy. I will horizontal shift this image using width_shift_range arguement. This arguement takes min and max range. In our example I am taking -150 and 150 as min and max pixels. After creating the iterator we are calling it nine times for augmentation. Once we run above cell we get following output-
In above output cell you can see our loaded image is augmented 9 times. The plot is showing randomly selected positive and negative horizontal shifts. You can try the same code but with height_shift_range arguement for vertical shifting. Remember in this case you need to pass the percentage of image to shift (for example height_shift_range=0.4) instead of [min,max] values.
B. How to flip an image?
Flipping an image means reversing the rows or column of pixels. The ImageDataGenerator class has horizontal_flip and vertical_flip arguements for flipping. Let's see how can we augment an image with horizontal flip-
The above code is quite same as we saw earliar except one change. Here we are passing horizontal_flip arguement which takes a boolean value- either True or False. After running the above cell we will get following plot-
Compare this plot with first one. What changes are you seeing?
C. How to rotate an image?
Image rotation is also part of augmentation technique.We can randomly rotate the image clockwise by just giving a number between 0 to 360 in rotation_range arguement. Let's see how can we do it-
Once we run above cell we see our loaded image is rotated as below-
D. How to augment an image by brightness?
We can augment an image by it's brightness using the brightness_range arguement. This arguement takes min and max range indicating percentage of darkness or brightness amount. Let's see how can we use this type of augmentation-
In above code cell I have given brightness_range arguement with 0.2 and 1.0 value. Here value less than 1.0 means darkening the image while larger than 1.0 means brighten the image whereas 1.0 means no affect on brightness. running above cell gives us following plot-
E. How to apply zoom augmentation?
Zoom augmentation randomly zooms an image. We can use zoom_range arguement which takes percentage of zoom as value. The zoom values less than 1.0 will zoom the image in and values larger than 1.0 will zoom the image out. Let's see how can we do that-
Running above code gives you following result having zoom in effect-
That's it for today. You can save the augmented images using the save_img() function as we did in our first post. In this way you can larger your small image training datatset. We have learnt 5 types of image augmentation. Apply each technique on your own image and experiment it with different different values of arguements. For more details of image data augmentation I recommend you to read following two awesome blogs-
1. https://www.pyimagesearch.com/2019/07/08/keras-imagedatagenerator-and-data-augmentation/
2. https://machinelearningmastery.com/how-to-configure-image-data-augmentation-when-training-deep-learning-neural-networks/
In next post of this series we will learn about the different colour channel ordering. Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.
Data Science Courses in Gurgaon at APTRON have been designed with the objective of developing in the candidates, the capacity to master the professional techniques towards acquiring the best and desirable value for the companies.
ReplyDeleteFor More Info: Data Science Course in Gurgaon
Its very informative blog and useful article thank you for sharing with us , keep posting learn Data Analysis Courses in Pune
ReplyDeletegreat post about data science using python i am currently doing my training from data analytics course in pune . it is the best it training institute in pune , thanks
ReplyDeleteThanks for sharing the valuable information.
ReplyDeleteHome Nursing Services in Hyderabad
nursing services at your door step
Home Physiotherapy Services