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

How to deploy your ML model as Fast API?

Another post starts with you beautiful people! Thank you all for showing so much interests in my last posts about object detection and recognition using YOLOv4. I was very happy to see many aspiring data scientists have learnt from my past three posts about using YOLOv4. Today I am going to share you all a new skill to learn. Most of you have seen my post about  deploying and consuming ML models as Flask API   where we have learnt to deploy and consume a keras model with Flask API  . In this post you are going to learn a new framework-  FastAPI to deploy your model as Rest API. After completing this post you will have a new industry standard skill. What is FastAPI? FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It is easy to learn, fast to code and ready for production . Yes, you heard it right! Flask is not meant to be used in production but with FastAPI you can use you...

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

How can I make a simple ChatBot?

Another post starts with you beautiful people! It has been a long time of posting a new post. But my friends in this period I was not sitting  where I got a chance to work with chatbot and classification related machine learning problem. So in this post I am going to share all about chatbot- from where I have learned? What I have learned? And how can you build your first bot? Quite interesting right! Chatbot is a program that can conduct an intelligent conversation based on user's input. Since chatbot is a new thing to me also, I first searched- is there any Python library available to start with this? And like always Python has helped me this time also. There is a Python library available with name as  ChatterBot   which is nothing but a machine learning conversational dialog engine. And yes that is all I want to start my learning because I always prefer inbuilt Python library to start my learning journey and once I learn this then only I move ahead for another...