Showing posts with label #ML. Show all posts
Showing posts with label #ML. Show all posts

Saturday, December 28, 2019

A simple classifier to classify Cars and aeroplanes with CNN(Part 2: inference)


Hello there, hope you are doing well. This is a sequential post of classifier with CNN. In our earlier post we learned how collect the data, organize them and train a model for classification. In this post we will learn how we can use the trained model and actually classify the Cars and Planes. When I was starting to train a CNN and learn, I had a difficult time to learn how to use the model and actually see the result. All the article or blogs I was following only talks about how to train the network but no one was actually talking about how we can see the classification results. Enough talking lets start :

If you followed my previous post, the model file(model.h5) was created with 96% accuracy and save in the models folder. Now we will use that model for inference. 

Step1: We will start by importing the required libraries as we did for the training.
Step2: In the test.py code we will specify where the model and the test images are. We will load the model and the weights. Specify the image size we dealing with.

Step3: Now we will define a function for prediction which will take the test image as input and return the prediction output accordingly. As we have only two classes(cars and areoplanes), we will get the probability of two classes as output. We will read that probability and show the output result.
You can clone the whole project from github here. Do let me know if you have any feedback or suggestions. Hope you enjoyed coding with me. Wish you all a very happy new year 2020 in advace.


Wednesday, May 29, 2019

Harry Potter's magical Cloak with opencv



Hi there, last few blogs were hardcore machine learning and AI. Today let’s learn something interesting, lets do some magic using computer vision. I hope you all know about Harry Potter’s ‘invisible cloak’, the one he uses to become invisible. We will see how we can do the same magic trick with the help of computer vision. I will code with python and use the opencv library.
Below is the video for your reference:




The algorithm is very simple, we will separate the foreground and background image with segmentation. And then remove the foreground object from every frame. We are using a red coloured cloth as foreground image; you can use any other color of your choice but need to tweak the code accordingly. We will use the following steps:

  1. Import necessary libraries, create output video
  2. Capture and store the background for every frame.
  3. Detect the red coloured part in every frame.
  4. Segment out the red coloured part with a mask image.
  5. Generate the final magical output.

Step1: Import necessary libraries, create output video

Import the libraries. OpenCV is a library of programming functions mainly aimed at real-time computer vision. NumPy is the fundamental package for scientific computing with Python. In machine learning as we need to deal with a huge amount of data, we use NumPy, which is faster than normal array. Prepare for the output video.



Step2: Capture and store the background for every frame

The main idea is to replace the current frames’ red pixels with background pixels to generate the invisible effect. To do that first we need to store the background image for every frame.
cap.read() method is used to capture the current frame and stores the variables in ‘background’. The method also returns a Boolean True/False store in ret, if the frame is read correctly it returns Trues else false.
We are capturing the background in a for loop, so that we have several frames for background as averaging over multiple frames also reduces noise.

Step3: Detect the red coloured part in every frame

Now we will focus on detecting the red part of the image. As RGB (Red-Green-Blue) values are highly sensitive to illumination we will convert the RGB image to HSV (Hue – Saturation – Value) space. After we convert the frame to HSV space we will specify, some specific color range to detect the red color.

In general, the Hue values are distributed over a circle ranging between 0-360 degrees, but in OpenCV the range is from 0-180. And the red colour is represented by 0-30 as well as 150-180 values. We use the range 0-10 and 170-180 to avoid detection of skin as red. And then combine the masks with a OR operator(for python + is used).

Step4: Segment out the red coloured part with a mask image

Now that we where the red part is in the frame from the mask image, we will use this mask to segment that part from the whole frame. We will do a morphology open and dilation for that.

Step5: Generate the final magical output

Finally, we will replace the pixels of the detected red coloured region with corresponding pixel values of the static background, which we saved earlier and finally generate the output which creates the magical effect.

So now you can create your own video with invisible cloak. You can download the running python code from here: full code

Hope you enjoyed the magical aspect of computer vision. Do let me know your feedback and suggestion in the comment below. Thank you


Saturday, April 27, 2019

Linear Regression Implementation with python


Hello all, I hope from last few posts you already have good theoretical concept about the Machine Learning Algorithms. Today, we will do a Simple Linear Regression implementation with python. It won’t take much time and I will try to explain every step with simple words.
It is called Simple Linear Regression as it considers only one feature of input data and make the prediction. For example, here we will consider a housing price data set. As it is Simple Regression, it will only consider the size of the house to predict the price of it. But Multiple Regression, to predict the price it may consider several features such as locality, Front/back facing house etc. Below is the input data which we will use for the prediction, here house_size(x) is the input ranging from 1k sqr meter to 14k sqr meter and price(y) of the house ranging from 300 to 1100 dollar.






A scattered plot of the housing data looks like this:


Now we must find a line, which fits this scattered plot known as Regression line, so that we can predict house price for any given size(x). The equation for the Regression line looks like this

-          h(x_ith)= B0 + B1*x_ith

where, h(x_ith) represents prediction for x_ith and B0,B1 are the regression coefficients. To make the prediction, we need to estimate the regression coefficients (B0, B1). For implementation we need to follow the below steps:
  • Step1: Import the libraries. NumPy is the fundamental package for scientific computing with Python. In machine learning as we need to deal with a huge amount of data we use NumPy, which is faster than normal array. Matplotlib is a plotting library in python, we will use it for visualization.

  • Step2: Take the mean of the house_size(x) and the price(y). Calculate cross-deviation and deviation by calculating Sum of Squared Errors.
  • Step3: Calculate regression coefficients or the prediction error(explained in previous block here:  )
  • Step4: Plot the scattered points on the graph with red colors. The x-axis represents the size of the house(house_size) and the y-axis represents the price. (figure above)

  • Step5: Predict the regression line with minimum error and plot it with purple color.

  • Step6: Lastly, write the main and call the main function. And the final output of the code is

Estimated coefficients:
b_0 = 295.95147839272175 
b_1 = 57.31614859742229
                And the graph should look like this-


You can download the full code(linearRegression.py) from github here: source code
Hope you enjoyed today’s post. Stay tuned for more python implementation. Do let me know your feedbacks and comments below.
I want to share a good news, my blog was featured in the top4 machine learningblogs, please look at number 19 here: https://blog.feedspot.com/machine_learning_blogs/

Next blog:Harry Potter's magical Cloak with opencv

Tuesday, March 12, 2019

Supervised, Un-Supervised, Semi-Supervised machine and Reinforcement Learning algorithms


Congratulation!!! Now you know what Artificial Intelligence and Machines Learning is. Now we can go little deeper and learn about different Machine Learning algorithms.
The most important question which comes to a beginner mind is “which algorithm should I use?” The answer to the question varies depending on many factors, including: The size, quality, and nature of data; The available computational time; The urgency of the task; and What you want to do with the data. Even an experienced data scientist cannot tell which algorithm will perform the best before trying different algorithms.

Before going into the algorithms, first we will see what Supervised, Un-Supervised, Semi-Supervised machine and Reinforcement Learning algorithms are.






What is Supervised Machine Learning?
-          In Supervised Machine learning, the Machine is given a set of data which already knows how the output should look and have an idea about the relation between the input and out. Supervised learning problems are also categorized as “regression” and “classification” problems. In regression problems machine predict a numeric or continuous variable output where as in classification problems the predicted output is discrete.  For example, if the machine is given a dataset of house prices with respect to house size, it can predict an unknown house price. Whereas if some image are labelled as dogs and cats, the machine can learn the relation between them and classify and separate some image as dog or cat. Below image may give you a better understanding-





What is Un-Supervised Machine Learning?
-          Un-Supervised allows the machine to approach a problem with minimum or no idea about how the output will look like. It can drive structures and relations from the given dataset and can find hidden patterns or grouping information from the data. It is mainly used for clustering, dimensionality reduction, feature learning, density estimation, etc. Example- KMean Clustering.

What is Semi-Supervised Machine Learning?
-          Semi-supervised machine learning algorithms fall somewhere in between supervised and unsupervised learning, since they use both labeled and unlabeled data for training – typically a small amount of labeled data and a large amount of unlabeled data. The systems that use this method are able to considerably improve learning accuracy. Usually, semi-supervised learning is chosen when the acquired labeled data requires skilled and relevant resources in order to train it / learn from it. Otherwise, acquiring unlabeled data generally doesn’t require additional resources. Example: speech recognition.

What is Reinforcement Learning?
-          Reinforcement learning algorithms is a learning method that interacts with its environment by producing actions and discovers errors or rewards. Trial and error search and delayed reward are the most relevant characteristics of reinforcement learning. This method allows machines and software agents to automatically determine the ideal behaviour within a specific context in order to maximize its performance. It is employed by various software and machines to find the best possible behaviour or path it should take in a specific situation.



Okay, all set, we are now ready to learn the most popular machine learning algorithms, stay tuned for that. Please comment below for any suggestion and feedback.

Next post 10 Most Commonly Used Machine Learning Algorithms

Wednesday, March 6, 2019

Basic Concepts of Artificial Intelligence, Machine Learning, Deep Learning


Today we will start our journey to the world of Artificial Intelligence(AI). We will learn the basic definition of Artificial Intelligence (AI), Machine Learning(ML), Deep Learning(DL), Natural Language Processing(NLP), Computer Vision and Image Processing. Later we will go deeper with the machine learning algorithms and how those algorithm works. This tutorial is for beginners, if you have an idea of AI skip this course and go to the next lesson where I will discuss different Machine Learning algorithms.


What is Artificial Intelligence(AI)?
-          Artificial intelligence (AI) is the ability of a machine or a computer program to think and learn by doing certain task. The concept of AI is based on the idea of building machines capable of thinking, acting, and learning like humans. On other words the creating the machine capable of understanding the environment, understanding the problem and act intelligently according to the situation.

What is Machine Learning(ML)?
-          Machine Learning(ML) is an application of AI that provides system the ability to automatically learn and improve performance without being explicitly programmed. ML focuses on the development of computer program that can access data and learn for themselves. The main aim is to allow computer learn automatically without human intervention or assistance and act accordingly.
-          Next question in your mind may have, how the machine is learning? –  The answer is as human learns. Frist the machine gathers information and knowledge then use those knowledge to take decisions. Also, past experiences helps to take decisions in future.

What is Deep Learning(DL) or Deep Neural Network(DNN)?
-          Deep Learning(DL) is part of a broader family of Machine Learning and AI, which emulate the learning approach that human beings use to gain certain types of knowledge. Traditionally machine learning algorithms used to be linear, but with deep learning algorithms are stacked in a hierarchy of increasing complexity and abstraction. Because this process mimics a system of human neurons, deep learning is sometimes referred to as Deep Neural Learning(DNN) or deep neural networking. Let me explain the concept with an example blow-
-          A baby when starts learning about what a cat is (and is not) by pointing to some objects and saying the word cat. The parent guides him by saying, "Yes, that is a cat," or, "No, that is not a cat." As the baby continues to point to objects, he becomes more aware of the features that all cat have. What the baby does, without knowing it, is clarify a complex abstraction by building a hierarchy in which each level of abstraction is created with knowledge that was gained from the preceding layer of the hierarchy. A machine follows more or less similar approach. Each algorithm in the hierarchy applies a nonlinear transformation on its input and uses what it learns to create a statistical model as output. Iterations continue until the output has reached an acceptable level of accuracy. The number of processing layers through which data must pass is what inspired the label deep.


What is Natural Language Processing(NLP)?
-          Natural Language Processing is the ability of a computer program to understand human languages as it is spoken. NLP is also component of AI. The development of NLP is challenging because traditionally computer requires human to speak to them in a programming language or unambiguous or highly structured, clear commands. Whereas natural languages are generally ambiguous, have different structures, dialects, regional effects which are difficult to distinguish.
-          Semantic analysis and Natural Language Processing can help machines automatically understand text, which supports the even larger goal of translating information, understanding potentially valuable piece of customer feedback, understanding insight in a tweet or in a customer service log into the realm of business intelligence for customer support, corporate intelligence or knowledge management.

What is Computer Vision and Image Processing?
-          Computer vision is about granting the computer the ability to ‘see’ and ‘understand’ what it sees. In image processing you get an image as input and provide processed image as output, whereas in computer vision you get an image (or video) as input and provide other quantitative data as an output (e.g geometrical information about the objects in question). Computer Vision tries to do what a human brain does with the retinal input, it includes understanding and predicting, detecting certain things. For example, given an input image, using computer vision the computer can classify the objects (cars,humans,train.. etc) as human does. There are many other applications but this is just to give you a basic idea.

  This was the basic concepts. Please comment below if you have any questions or feedback. Stay tuned for more detailed concepts of Machine Learning Algorithms.

Next topic is Supervised, Un-Supervised, Semi-Supervised machine and Reinforcement Learning algorithms