Skip to main content

How to use Keras for Computer Vision- Part 2


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.

Comments

  1. 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.

    For More Info: Data Science Course in Gurgaon

    ReplyDelete
  2. Its very informative blog and useful article thank you for sharing with us , keep posting learn Data Analysis Courses in Pune

    ReplyDelete
  3. great 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

    ReplyDelete

Post a Comment

Popular posts from this blog

YOLObile- a new state of the art Real-Time Object Detection model for Mobile Devices

  Another post starts with you beautiful people! Thanks for giving so many views on my previous post 👍. I am glad to see my previous posts are helping people to use state of the art object detection and recognition deep learning model in their projects. If you are new to my blog, I recommend seeing once my previous posts, and you will not be disappointed if your goal is to learn applied computer vision free of cost. Continuing my journey of sharing knowledge in this post I am going to share with you a new state of the art framework for object detection on mobile devices-  YOLObile  📱 There has been a trade-off between speed and the accuracy of object detections. For example, the state of the art,  YOLOv4 model gives us a very accurate detection but its speed is slow if we want to use it on a mobile device. On the other hand, its lighter version YOLOv4-tiny works very fast on a mobile device but its accuracy reduces. For a detailed comparison of FPS vs mAP you can ...

Can you build a model to predict toxic comments?

Another post starts with you beautiful people! Hope you have learnt something new and very powerful machine learning model from my previous post-  How to use LightGBM? Till now you must have an idea that there is no any area left that a machine learning model cannot be applied; yes it's everywhere! Continuing our journey today we will learn how to deal a problem which consists texts/sentences as feature. Examples of such kind of problems you see in internet sites, emails, posts , social media etc. Data Scientists sitting in industry giants like Quora, Twitter, Facebook, Google are working very smartly to build machine learning models to classify texts/sentences/words. Today we are going to do the same and believe me friends once you do some hand on, you will be also in the same hat. Challenge Link :  jigsaw-toxic-comment-classification-challenge Problem : We’re challenged to build a multi-headed model that’s capable of detecting different types of toxicity like thre...

Learn the fastest way to build data apps

Another post starts with you beautiful people! I hope you have enjoyed and learned something new from my previous three posts about machine learning model deployment. In one post we have learned  How to deploy a model as FastAPI?  I n the second post, we have learned  How to deploy a deep learning model as RestAPI ? and in the third post, we have also learned  How to scale your deep learning model API?   If you are following my blog posts, you have seen how easily you have transit yourselves from aspiring to a mature data scientist. In this new post, I am going to share a new framework-  Streamlit which will help you to easily create a beautiful app with Python only. I will show here how had I used the Streamlit framework to create an app for my YOLOv3 custom model. What is Streamlit? Streamlit’s open-source app framework is the easiest way for data scientists and machine learning engineers to create beautiful, performant apps in only a few hours!...