<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Darshan Adakane on Medium]]></title>
        <description><![CDATA[Stories by Darshan Adakane on Medium]]></description>
        <link>https://medium.com/@darshanadakane?source=rss-902af3f3babc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*4L87wd7r5TkoWCzX_Br8iw.jpeg</url>
            <title>Stories by Darshan Adakane on Medium</title>
            <link>https://medium.com/@darshanadakane?source=rss-902af3f3babc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 21:03:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@darshanadakane/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Sentiment Analysis —How to estimate the rating from movie review]]></title>
            <link>https://medium.com/analytics-vidhya/sentiment-analysis-how-to-estimate-the-rating-from-movie-review-62044a49c52?source=rss-902af3f3babc------2</link>
            <guid isPermaLink="false">https://medium.com/p/62044a49c52</guid>
            <category><![CDATA[nltk]]></category>
            <category><![CDATA[lstm]]></category>
            <category><![CDATA[sentiment-analysis]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <dc:creator><![CDATA[Darshan Adakane]]></dc:creator>
            <pubDate>Fri, 28 May 2021 11:26:07 GMT</pubDate>
            <atom:updated>2021-06-09T03:16:49.813Z</atom:updated>
            <content:encoded><![CDATA[<h4>A process of computationally identifying and categorizing opinions expressed in a piece of text</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*yqhdgVURaRKH1nemeAp7Kw.png" /><figcaption>Examples of predicted rating from the movie review submitted</figcaption></figure><h4><strong>Introduction</strong>:</h4><p>When was the last time you watched a movie and submitted the review? Or had a nice dinner in a good restaurant and submitted the feedback? How about this. You submit your review and electronically the rating is generated. Cool right!</p><p>Welcome to the field of Natural Language Processing (NLP) and more specifically — ‘Sentiment Analysis’. Also known as <strong>opinion mining</strong> or <strong>emotion AI</strong>, sentiment analysis refers to the use of natural language processing, text analysis to systematically identify, extract, quantify subjective information[1]. In this article, we will try and achieve the same objective i.e. generate movie rating from the review submitted.</p><h4>Pre-requisites:</h4><ol><li>Theoretical knowledge on Sentiment Analysis and concepts like RNN, LSTM, GRU, Word Embeddings, etc. All these are covered in the fantastic course by Andrew Ng’s <a href="https://www.coursera.org/learn/nlp-sequence-models#syllabus">Sequence Models</a></li><li>Knowledge of Python + Any code editor (Here we will use Jupyter notebook)</li></ol><h4>Coding:</h4><p>Assuming pre-requisite knowledge on the topic, our motive is to first train the model on a dictionary that has phrases/individual words (size=#100000) and corresponding sentiment(rating). After training, we will test it with the new unseen phrase/words submitted in review and observe what rating it gives! As simple as that. I will try to describe the step-by-step coding. <br>First, let’s create a new Jupyter notebook file. At this root location where our file is, create a folder named ‘input’. Now download the training data and files. i.e. train.tsv, test.tsv. And sampleSubmission.csv is our output file. Our folder structure looks like this.<br>-sentimentanalysis.py<br>-input<br> -train.tsv<br> -test.tsv<br> -sampleSubmission.csv</p><p>Let’s begin coding. First, we will import the libraries needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6qcuZwsepsBhxGzWiT0XzA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*60fRY9kDFjSPssf_i6ZW0w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*110oHTrfArUCJQ5ZXYxpWw.png" /></figure><p>The Natural Language Toolkit, or more commonly NLTK, is a suite of libraries and programs for symbolic and statistical natural language processing for English written in the Python programming language. TQDM is a progress bar library with good support for nested loops and Jupyter/IPython notebooks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TAQYb8_srv_r1RWBOeLIhA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KDOiinPvn95ZUvCrcelfZQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mXLDpTx5bnvSY-Hwu8GVnA.png" /></figure><p>If you observe the output here (which is our training data), the review sentence is broken down into each individual word called phrase and represented by PhraseId, and the sentiment is assigned to each of those. This is done for all review sentences in the training data. This comprised our dictionary. The following function will take each phrase iteratively and it will remove html content, remove non-alphabetic characters, tokenize the sentences, lemmatize each word to its lemma and then return the result in the list named ‘reviews’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zkZBlqA6KCBoqg9tm5vwkw.png" /></figure><p>Next, cleaned reviews for both train and test set retrieved</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RTnVJR0hR9-AiDbcZE-hKA.png" /></figure><p>With following code, we are categorizing the output value in number of classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A7k8MlMTxDWr0vBTaPn0wg.png" /></figure><p>We have all the data ready for training. Let’s split into train, validation sets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZQdzTrPEifh5gLmsG292xA.png" /></figure><p>Further, we will be getting the no of unique words and max length of a review available in the list of cleaning reviews. It is needed for initializing the tokenizer of Keras and subsequent padding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cUay6ukMuZ5rfVsuDYvmTw.png" /></figure><p>Following is the actual tokenizer of Keras and convert to sequences. This takes arguments as — list of texts to turn to sequences. And returns — list of sequences (one per text input).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-3NAbBhY6b4CRXM8BflgMA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8anrb90zRXFlrw5zMyz02w.png" /></figure><p>Next padding is done to equalize the lengths of all input reviews. LSTM networks need all inputs to be the same length. Therefore reviews lesser than max length will be made equal using extra zeros at the end. This is padding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ydzNaJ2KIecTuB5stH-NQ.png" /></figure><p>To prevent overfitting, we will use early stopping as a callback in the model fitting function call. Let’s define it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pRPt_ZvZnI1FY2vC7HvIRw.png" /></figure><p>Let’s define the model i.e. Keras using LSTM. Also, we will use Multilayer Perceptron (MLP) for multi-class softmax classification. This architecture is specially designed to work on sequence data. It fits perfectly for many NLP tasks like tagging and text classification. It treats the text as a sequence rather than a bag of words or as ngrams.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xheF_Sn-qPNvGqEHFiysvg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*39zYQVLv2XRF-OFy7Q9BAQ.png" /></figure><p>Let’s make the prediction on the trained model. And submit the output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yU5Snzl7QlWLeK8vFlLA5w.png" /></figure><p>Here we have created and sample output to write our predictions on test data. Here is our sampleoutput.csv looks like. This file represents how our output should look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*X7-ECa0lwE0g9Pn8wKCAGw.png" /></figure><p>Finally we will write our actual output to output.csv file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hdoSSXuQb_x6Ci0mpl9P-g.png" /></figure><p><strong>Conclusion</strong>:</p><p>We have observed that using NLP methods, the sentiment prediction is quite accurate. It depends upon the training dictionary that we for model training. The accuracy depends on the number of epochs, dropout rate, batch size, number of layers, optimizer, length of words. So the best accuracy comes for a certain combination, so one would probably try and see what that combination is. In our case, the best accuracy obtained using a 128 layer LSTM model, dropout of 50%, and Adam optimizer.</p><p>Thanks for reading out the article. I hope it helps.<br>You can find the Github repo at this <a href="https://github.com/darshanadakane/sentimentAnalysis_usingPython">link</a>.</p><p><strong>References:</strong></p><p>[1] Sentiment analysis, <a href="https://en.wikipedia.org/wiki/Sentiment_analysis">Wikipedia</a></p><p>[2] <a href="https://www.youtube.com/watch?v=DqqWTubHEaA&amp;list=PLZnyIsit9AM7yeTZuBmezKNc6hFHUPImh&amp;index=3">RNN mode</a>l, Andrew Ng</p><p>[3] <a href="https://www.youtube.com/watch?v=Fnk4Z-muD5A&amp;list=PLZnyIsit9AM7yeTZuBmezKNc6hFHUPImh&amp;index=5">RNN Achitecture</a>, andrew Ng</p><p>[4] <a href="https://www.youtube.com/watch?v=PjMcA_NlB_8&amp;list=PLZnyIsit9AM7yeTZuBmezKNc6hFHUPImh&amp;index=9">GRU</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62044a49c52" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/sentiment-analysis-how-to-estimate-the-rating-from-movie-review-62044a49c52">Sentiment Analysis —How to estimate the rating from movie review</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Neural Style Transfer using VGG model]]></title>
            <link>https://medium.com/data-science/neural-style-transfer-using-vgg-model-ff0f9757aafc?source=rss-902af3f3babc------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff0f9757aafc</guid>
            <category><![CDATA[vgg]]></category>
            <category><![CDATA[deep-neural-networks]]></category>
            <category><![CDATA[neural-style-transfer]]></category>
            <category><![CDATA[convolutional-network]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Darshan Adakane]]></dc:creator>
            <pubDate>Thu, 16 Jan 2020 00:50:13 GMT</pubDate>
            <atom:updated>2020-01-27T05:27:12.674Z</atom:updated>
            <content:encoded><![CDATA[<h4>A technique to transform a digital image that adopts the style of different image</h4><p><strong>Introduction</strong>:</p><p>Before we begin, let’s go to <a href="https://deepart.io/">this </a>website to get some inspiration. On the website, we choose a photo from the local computer (let’s assume the image named Joey.jpg). Let’s call this content image. Then we choose another image, say style image named style1.jpg from the local computer. What this website does is produces a mixed image that preserves the contours of the content image and adds the texture and color pattern from the style image to the content image. Following is the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yNbhRIReKMFZ7Nf2xfLyQA.png" /><figcaption>Left: Original Image, Right: Style Image, Middle: Mixed Image</figcaption></figure><p><strong>Description</strong>:</p><p>This is called Neural Style Transfer (NST) and is done by using Deep Learning, Convolution Neural Network (CNN) to be specific. I assume you are familiar with CNN. If not, I would highly recommend Andrew Ng’s Course on <a href="https://www.coursera.org/learn/convolutional-neural-networks?">CNN</a>.</p><p>Let us understand the basics of NST with the help of the following flowchart. It shows the Style Transfer algorithm which has 13 convolutional layers (only a few are shown for simplicity). Two images are input to the neural network i.e. a content image and a style image. Our motive here is to generate a mixed image that has contours of the content image and texture, color pattern of the style image. We do this by optimizing several loss functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VhvzxTyAcK8f7pEs5TsHJA.png" /></figure><p>The loss function for the content image minimizes the difference of the features activated for the content image corresponding to the mixed image (which initially is just a noise image that gradually improves) at one or more layers. This preserves the contour of the content image to the resultant mixed image.</p><p>Whereas the loss function for the style image minimizes the difference between so-called Gram-matrices between style image and the mixed image. This is done at one or more layers. The usage of the Gram matrix is it identifies which features are activated simultaneously at a given layer. Then we mimic the same behavior to apply it to the mixed image.</p><p>Using TensorFlow, we update the gradient of these combined loss functions of content and style image to a satisfactory level. Certain calculations of Gram matrices, storing intermediate values for efficiency, loss function for denoising of images, normalizing combined loss function so both image scale relative to each other.</p><p><strong>Coding </strong>:</p><p>Now that we have understood the algorithm, let us begin coding. The <a href="https://arxiv.org/abs/1508.06576">original paper</a> uses the VGG-19 model. But here we are going to use the VGG-16 model which is available publicly. Download the VGG-16 model from <a href="https://s3.amazonaws.com/cadl/models/vgg16.tfmodel">here</a> (Please remember it is ~550MB file).</p><p>In the root directory, create a new folder name it as<strong> vgg16</strong> and paste the above file and vgg.py from the Github link. Also, we have modified the vgg16.py file by commenting out <strong>maybe_download</strong> function (since you have already downloaded the vgg16.tfmodel file)</p><p>Let’s import the libraries first. Then import the vgg16 model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Kq53wt7yi_8FNCXUVqCaw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_eGrsvv6YOZ6UXVbvm1g-g.png" /></figure><p>Let’s define a couple of helper functions for image manipulations.</p><p><strong>load_image </strong>load an image and returns a numpy array of floating points. The image is resized to a maximum of height or width.</p><p><strong>save_image </strong>saves an image to jpeg file with pixel values between 0 ad 255</p><p><strong>plot_image_big </strong>plots a larger image.</p><p><strong>plot_images </strong>plots content, style, and mixed image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g2DI8XANfx_j3hqosmGajQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f96Y_5FD0kaaVNnhiPSSGw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oAvfbAy5umroY5zQEfQpjg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l1dgVLG1lkrLczF5xlBCrQ.png" /></figure><p>Next, we will define loss functions that are used in Tensorflow for optimization.</p><p><strong>mean_squared_error </strong>operation will return a tensor that is mean squared error (MSE) difference between two input tensors.</p><p><strong>create_content_loss </strong>will calculate the MSE between the content and the mixed image. The loss is minimized so that the activation features of content are made similar to the mixed image so that it transfers the contours from content image to a mixed image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w16jSrT9q6wXa16Bxc4Oig.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8yMnzyxZQ1g57ka-SuC8sQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MwIwDUXd4KOvkbJmWeTybA.png" /></figure><p>Next, our motive is to capture the style features of the mixed image. To do this, we will do something similar i.e measure which features activate for the style layers simultaneously and copy this pattern to the mixed image. An efficient way to do this is by calculating the Gram matrix. Gram matrix is essentially the dot product for the vectors of the feature activation layers. If the entry in the matrix has a smaller value it means the two features in the given layers do not activate simultaneously and vice versa.</p><p>Let’s first define the calculating GramMatrix first as <strong>gram_matrix </strong>followed by <strong>create_style_loss </strong>which calculates the MSE of Gram matrix instead of the two raw tensors (as we did it in <strong>create_content_loss</strong>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9wSjW_NlI64-SSYRBzbQ2A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fZHNpWX0G5DG6jwU6M20og.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ldPUz0RvBBY3NiM4HKJ--A.png" /></figure><p>To reduce the noise in the resultant mixed image, we use a denoising filtering algorithm called ‘Total Variation Denoising’ using the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lxPZOK7fbEa0x5HadIWIcA.png" /></figure><p>The next section of code is the core. We will define the Style Transfer Algorithm that calculates the gradient descent on the loss functions. The algorithm uses normalization such that loss values are equal to one that helps in choosing the loss weights independent of the content and style layers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VAlIQ1M9OcFVFqs0HE93NQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o4YXCOvoWMR-XjqiUQagVg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CL8xk8VMw7VhH9KHN22hYg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E0vgFGiJSAHjXESWRxpHGA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mo-DzubAddH72B629Y1LTA.png" /></figure><p>At first, we will load the content image which has contour feature we wish to be in the mixed image. Here I have named it as 1.jpg</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ahqnzet4H4rxp__ByltYBw.png" /></figure><p>Now, we will load the style image of whose we want the color and texture to appear in the mixed image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dC5LHBTcajSwe9mAiLyM3g.png" /></figure><p>Further, we will define the layer indices that we want to match the content image. Usually, it is a few layers just after the start. The 5th layer (index is 4) in the VGG-16 seems to work well. Similarly, the layer indices for the style image. Usually, it is towards the end of the total layer. Here we will define the 13th layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mAb8ChlgEt_fLa5kyPucqQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qRnwcMx7MyFWUvkeo4FbIQ.png" /></figure><p>The final part of the execution is applying style transfer to our defined content and style images. As defined, it automatically creates the loss functions for content and style layers and performs optimizations based on the number of iterations. Finally, displaying the mixed image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LZzgI8nxYj-lmOQOBYCdwA.png" /></figure><p><strong>Results</strong>:</p><p>P.S. This process may run slow on CPU (I am using CPU). Let’s see what results are given.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*VXACDfBm5d1W8gqNbPo5Vg.png" /></figure><p><strong>Conclusion</strong>:</p><p>Not bad! The results demonstrate the basic idea of combining two images. They are not at par like <a href="https://deepart.io/">DeepArt</a> who are pioneers in these techniques. Perhaps more iterations, smaller step sizes, higher resolution images, changing style and content layer indices or more computational power would increase the quality of the mixed image.</p><p>Thanks for reading out the article. I hope it helps.</p><p>You can find the github repo at <a href="https://github.com/darshanadakane/neuralStyleTransfer_usingVGG">this</a> link.</p><p><strong>Reference </strong>:</p><p>[1] Hvass-Labs, TensorFlow Tutorial #15 Style Transfer (2018), <a href="https://www.youtube.com/watch?v=LoePx3QC5Js&amp;t=268s">Source</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff0f9757aafc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/neural-style-transfer-using-vgg-model-ff0f9757aafc">Neural Style Transfer using VGG model</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What are Haar Features used in Face Detection ?]]></title>
            <link>https://medium.com/analytics-vidhya/what-is-haar-features-used-in-face-detection-a7e531c8332b?source=rss-902af3f3babc------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7e531c8332b</guid>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[facedetection]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[face-recognition]]></category>
            <category><![CDATA[haar-cascades]]></category>
            <dc:creator><![CDATA[Darshan Adakane]]></dc:creator>
            <pubDate>Tue, 12 Nov 2019 11:47:29 GMT</pubDate>
            <atom:updated>2019-11-13T04:45:28.049Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*pu1tIYEeFCO6ELoCOgJJRQ.png" /><figcaption>Face Detection using Haar Features</figcaption></figure><h3>What are Haar Features used in Face Detection ?</h3><p>Face recognition means identifying the person and face verification means verify the person who is claimed to be (by matching it in certain database). Its applications are found in various fields like school, colleges, organisations, factories, public places, surveillance etc. These techniques are gaining momentum worldwide and extended with human emotion recognition, its applications are huge.</p><p>The key aspect in face recognition is detecting relevant features in human face like eyes, eyebrows, nose, lips. So how do we detect these features in real time/in an image ? The answer is <strong>Haar Wavelets or Haar Features</strong>. And the algorithm used is called as Viola-Jones Algorithm. In this article we will try to understand what is Haar features and how they are used in face detection.</p><p>Haar features are sequence of rescaled square shape functions proposed by Alfred Haar in 1909. They are similar to convolution kernels taught in the Convolution Neural Networks course. We will apply these haar features to all relevant parts of face so as to detect human face.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*utIH1bMtOt3agkg0.png" /><figcaption>Example of Haar Features: Square shaped kernels. <a href="https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/VJ_featureTypes.svg/640px-VJ_featureTypes.svg.png">Source</a></figcaption></figure><p>As we see in the above image, there are edge features (1 and 2), line features (3). They are white and black pixels images (value 0 or value 1). But usually we have greyscale/colour image (pixel value range from 0 to 255). For now assume ideal scenario that we have black and white image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/0*8OuVyFarJa0lVIRt.jpg" /><figcaption>Taget Image. <a href="https://pixabay.com/photos/woman-portrait-face-girl-beauty-3834641/">Source</a></figcaption></figure><p>To detect eyebrow, we will use Haar feature (image (1)) because forehead and eyebrow form lighter pixels- darker pixel like image. Similarly, to detect lips we use similar to Haar like feature (image(3)) with lighter-darker-lighter pixels. To detect nose, we might use darker-lighter Harr like feature from (image(1)). And so on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*cXhfnN8cHo8-_GI_FLFMRQ.png" /><figcaption>Haar features applied on relevant parts of face</figcaption></figure><p>Lets look at some computations that are required.</p><p>For black and white image (refer first box below), pixel values are 0 or 1 <em>(ideal case)</em> but in real cases we have normalised greyscale image as shown in bottom box containing pixel values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/261/1*KEGHGBKMGJK_zIdPghRRfw.png" /><figcaption>Pixel values: 0 and 1 for ideal case (Top box), Normalised greyscale values for real cases (Bottom box)</figcaption></figure><p>According to Viola-Jonas algorithm, to detect Haar like feature present in an image, below formula should give result closer to 1. The closer the value is to 1, the greater the change of detecting Haar feature in image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/303/1*m33m99XFWotsdLApD-BK6g.png" /></figure><p>Ideal case : Delta = (1/8)*(8) — (1/8)*0 = 1</p><p>Real case: Delta = (1/8)*(5.9) — (1/8)*(1.3)=0.575</p><p><em>(For greyscale image, assume we have set White-Dark threshold to 0.3. Meaning pixels with value less than or equal to 0.3 are considered white and anything greater that 0.3 is considered as dark)</em></p><p>We can define another threshold parameter to detect edge or Haar feature.We are calling it Delta <em>(this is different from White-Dark threshold)</em>. Assume here we have set threshold to 0.5. Any delta value greater than 0.5 detects Haar feature. In this fashion we can apply, get and detect most relevant features on a given image like eyebrow,lips,nose etc.</p><p>Next we will quickly see what are <strong>Haar Cascade Classifiers</strong> —</p><p>They are series of classifiers or features (as we have seen above) used to identify object in an image. Using sliding windows and number of haar features (increases as number of stages increase), finally leading to detect face or not. There are total 38 stages defined for Viola Jonas Method. Depending upon the sliding windows size and face location, number of features, face can be detected at a certain stage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*6suBYFCHbOspeo_YD4jP6Q.png" /><figcaption>Haar Cascade Classifiers method</figcaption></figure><p>Hopefully you got some intuition on understanding Haar Feature used in face detection. I have tried my best to simplify in terms. If you have any questions, please post in comments. I would be happy to answer.</p><p>Thank you for reading the article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7e531c8332b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/what-is-haar-features-used-in-face-detection-a7e531c8332b">What are Haar Features used in Face Detection ?</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Real Time Object Detection using YOLOv3 with OpenCV and Python]]></title>
            <link>https://medium.com/analytics-vidhya/real-time-object-detection-using-yolov3-with-opencv-and-python-64c985e14786?source=rss-902af3f3babc------2</link>
            <guid isPermaLink="false">https://medium.com/p/64c985e14786</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[yolo]]></category>
            <category><![CDATA[object-detection]]></category>
            <dc:creator><![CDATA[Darshan Adakane]]></dc:creator>
            <pubDate>Fri, 08 Nov 2019 04:37:08 GMT</pubDate>
            <atom:updated>2019-11-08T04:37:08.799Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1OXEjbPrOg6HLuh7MoeEwg.png" /><figcaption>Real time object detection: Umbrella,person,car,motorbike detected using yolov3</figcaption></figure><p>In the previous <a href="https://medium.com/@darshanadakane/object-detection-with-opencv-python-using-yolov3-481f02c6aa35">article</a> we have seen object detection using YOLOv3 algorithm on image. In this article, lets go further and see how we can use YOLOv3 for real time object detection.</p><p>We can solve this problem in two ways. One is using CPU and other using GPU.</p><p>CPU has advantage that we need not install any additional resources, installations. We can right away use OpenCV for this. But the disadvantage is that it is extremely slow (it depends what configuration CPU you are running but yeah it is slow). Recommended for beginners.</p><p>GPU on the other hand has advantage of having video graphic processor, hence faster speed. But the downside is that we need to compile many libraries manually and configure many things before to start utilising for our problem definition.</p><p>Lets keep this tutorial to use CPU for real time object detection. In the last tutorial we worked with single image, while now we will be using series of images (i.e. video) in OpenCV as input.</p><p>We will recap the code before loading image. We import cv2, numpy libraries. Then we load darknet architecture in <strong>net </strong>and in <strong>classes </strong>we store all the different object from <strong>coco.names</strong> file. And get the last layer from <strong>net </strong>so as to identify object in final layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ou3atfmZpL61ak_DdI0fIA.png" /></figure><p>Now we load capture video from webcam. Hence we use <strong>cv2.VideoCapture(0)</strong>. Here 0 meaning it is from the first webcam. Also, to detect how many frames we are processing per second, we will import <strong>time</strong> library. We using <strong>frame_id </strong>to count of frames. To get elapsed time, we subtract <strong>starting.time </strong>from<strong> time.now. </strong>We have defined <strong>font</strong> to be HERSHEY_PLAIN</p><p>Then we load all frame of the video running in while loop and store each frame using <strong>cap.read</strong> and perform operation of what we did for each image in last article. Store details of each frame in <strong>height,width,channels. </strong>To process image faster we will reduce the blob size from 416x416 to 320x320. The accuracy will reduce a little with this change. On the other hand, if we increase 608x608 it will accurate but very slow in detection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l1o7upLS4AyRqUags9n9vg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zQ_5Ob8iRyDWnwJ3P6kWjQ.png" /></figure><p>In above code, <strong>fps </strong>is frame per second we calculate as number of frames divided by elapsed time. We will put this value as text on the object detected. At the end we have used <strong>key</strong>=27 (which is Esc key on keyboard) which will break the loop and stop the execution if this key pressed.</p><p>There is one last thing that i would share is about <strong>confidence.</strong>Remember it was the threshold of 0.5 that we used, if we increase it would mean we will get more accuracy on detection but less number of object and vice versa. We are also showing this confidence value on box as well.</p><p>The test video ran from webcam detected following objects:umbrella,car, person and motorbike. Check out the output file by clicking <a href="https://github.com/darshanadakane/yolov3_realTimeObjectDetection">here</a> (file name is <a href="https://github.com/darshanadakane/yolov3_realTimeObjectDetection/blob/master/Webcam_ObjectDetection.MOV">Webcam_ObjectDetection.MOV</a>)</p><p>To speed up the detection process, apart from reducing the blob image size, there is another way. The way is by using Tiny YOLO. In this, we will pass a different weights and cfg file. It will have less convolutional layers than original yolov3 cfg file. To use this just replace file name in the code as follows, rest of the code remains same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A43povoLKLZdpcmZfZRhew.png" /></figure><p>You can find those files here:</p><p>yolov3-tiny.weights: Download from <a href="https://github.com/pjreddie/darknet/tree/master/cfg">here</a></p><p>yolov3-tiny.cfg : Download from <a href="https://github.com/pjreddie/darknet/tree/master/cfg">here</a></p><p>So if you have are working on low configuration of CPU, tiny yolo is best option. But nowadays, with the computing power increasing in latest laptop/computer (some are GPU enabled too), I would recommend directly using regular yolov3.</p><p>I hope this would help in implementing yolov3 real time object detection algorithm. I have tried to make it understandable from beginners mindset. Please let me know if you have any questions,comments.</p><p>Convolution Rocks!</p><p>Happy Learning !</p><p>[You can find complete code on <a href="https://github.com/darshanadakane/yolov3_realTimeObjectDetection">Github</a>. Star if you like it. Thanks]</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64c985e14786" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/real-time-object-detection-using-yolov3-with-opencv-and-python-64c985e14786">Real Time Object Detection using YOLOv3 with OpenCV and Python</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Object Detection with OpenCV-Python using YOLOv3]]></title>
            <link>https://medium.com/analytics-vidhya/object-detection-with-opencv-python-using-yolov3-481f02c6aa35?source=rss-902af3f3babc------2</link>
            <guid isPermaLink="false">https://medium.com/p/481f02c6aa35</guid>
            <category><![CDATA[yolov3]]></category>
            <category><![CDATA[convolution-neural-net]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[object-detection]]></category>
            <dc:creator><![CDATA[Darshan Adakane]]></dc:creator>
            <pubDate>Sat, 19 Oct 2019 06:12:17 GMT</pubDate>
            <atom:updated>2019-11-07T08:16:36.141Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*XEhPJ9UrOqOqoHWS_g8dCw.png" /><figcaption>Car,Motorbike and person detected using YOLOv3 algorithm</figcaption></figure><p>Greetings everyone.</p><p>Object detection is becoming an fascinating field of application and research in Computer Vision. Thanks to the faster computing power and advanced algorithms, we are making computers understand like humans by using images and videos.</p><p>In this article we will see how using OpenCV and Python, we can detect object in a still picture by applying the most popular YOLO(You Look Only Once) algorithm.</p><p>I am assuming :</p><p>You have some knowledge of Python and familiar with IDE — Jupyter notebook. You have understanding how Convolutional Neural Network (CNN) works <em>(I would recommend Prof. Andrew Ng’s course on CNN on </em><a href="https://www.coursera.org/learn/convolutional-neural-networks?specialization=deep-learning"><em>Coursera</em></a><em> for learning)</em></p><p>Couple of clarifications:</p><p>OpenCV is the computer vision library/ framework that we we will be using to support our YOLOv3 algorithm. OpenCV has inbuilt support for Darknet Architecture.</p><p>Darknet Architecture is pre-trained model for classifying 80 different classes. Our goal now is that we will use Darknet(YOLOv3) in OpenCV to classify objects using Python language.</p><p>Lets begin.</p><p>YOLO is an object detection algorithm <em>(Check out the paper came out it 2015 </em><a href="https://arxiv.org/pdf/1506.02640.pdf"><em>here</em></a><em>). </em>The recent YOLOv3 is more powerful than basic YOLO and YOLOv2 and faster than previous algorithms like R-CNN and more accurate too. The core reason being convolutional implementation of the layers meaning it scans the image (or frame) only once to make prediction as compared to other algorithm which requires multiple scans. (Power of Convolution. Yay!)</p><p>Set up:</p><p>We need 3 main files</p><ol><li>yolo.cfg (Download from <a href="https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg">here</a>) — Configuration file</li><li>yolo.weights (Download from <a href="https://pjreddie.com/media/files/yolov3.weights">here</a>) — pre-trained weights</li><li>coco.names (Download from <a href="https://github.com/pjreddie/darknet/blob/master/data/coco.names">here</a>)- 80 classes names</li></ol><p>Lets begin Coding :</p><p>Here I am using Jupyter notebook.</p><p>First step is to import cv2 and numpy libraries. Then we load yolo v3 algorithm using <strong>cv2.dnn.readNet</strong> by passing weights and cfg file. Then we will load all classes names in array using coco.names file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*95IdjV8Voa7JFJz23w2Z7w.png" /></figure><p>Next we will define output layers because that’s where we will be defining what object is detected by using <strong>net.getUnconnectedOutLayers</strong> and <strong>net.getLayerNames.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Bu2Gs4s2DLbn6xEiThuaA.png" /></figure><p>Next let us load an image. We will reduce the height and width of our image to scale of 40% and 30%. And save all those values in <strong>height,width,channels </strong>variables for theoriginal image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cti0ZTTHnhHjcpLxpDv70g.png" /></figure><p>To view the image, use following code but remember keep below code always at the end of the file. Any code that goes will always be above the below three lines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TA1VhfzHuPMTPK11yS0EhQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HaI85Efp4GTrLQv-_Bg_ug.jpeg" /><figcaption>Original Image</figcaption></figure><p>This is our original image from which we want to detect as many objects as possible. But we cannot give this image directly to algorithm.So we need to do some conversion from this image. This is called blob conversion which is basically extracting features from image.</p><p>We will detect objects in blob by using <strong>cv2.dnn.blobFromImage </strong>and passing few variables: <strong>img </strong>is file name, scalefactor of 0.00392, size of image to be used in blob be (416,416), no mean subtraction from layers as (0,0,0), setting <strong>True </strong>flag means we will be inverting blue with red since OpenCV uses BGR but we have channels in image as RGB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x9_JRWePDOJmHkKwfy1WqA.png" /></figure><p>Now lets see how the 3 different blobs looks like by using following code. We don’t observe much difference but this is what we will input to YOLO algorithm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wCqueHge25fUvrvS9xP9_Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QthbBNmMveip7Z7WIicg1w.png" /></figure><p>We now pass this blob to network using <strong>net.setInput(blob) </strong>and then forward this to the <strong>outputlayers. </strong>Here all objects have been detected and <strong>outs </strong>contains all the information we need to instruct to extract the position of the object like top,left,right,bottom positions,name of class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3JUx6S0FPT7_W_8FjBklFA.png" /></figure><p>Now lets evaluate <strong>outs </strong>by showing information on screen. Mainly we will be trying to predict the confidence meaning how confident algorithm is when it predict some object. For this, we will loop through <strong>outs, </strong>first get all the <strong>scores </strong>for each <strong>out in outs</strong>. Then get the <strong>class_id </strong>that has highest score amongst them and then assign <strong>confidence </strong>to value of <strong>scores</strong> by passing <strong>class_id</strong>.</p><p>Now we will assign the confidence level threshold as 0.5. Anything above 0.5 should mean object detected. Let also have the <strong>center_x,center_y, w</strong> as width, <strong>h </strong>as height of the object detected. Here we will us <strong>height,width </strong>variables we saved previously of original image. We will also draw a circle of thickness 2 at center of the object just for the sake of proof that object has been detected.</p><p>Further lets draw rectangle around detected object by using <strong>center_x,center_y,w,h</strong>. And append some information to that like <strong>class,confidence.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a-R0AwKEq07INdVxy-fYAQ.png" /></figure><p>Running above code gives following output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NbSOSVH8hgXp5wAipvDG7g.png" /></figure><p>There might be cases that multiple time the same object might be detected like below. (In my above test image, multiple objects are not detected so i am showing that scenario using a different image as below). You see two boxes are detected each for laptop and monitor. We want to eliminate this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*t3b5Ez0bRXU-xg9OZbm32g.png" /></figure><p>To eliminate this, we will use Non-Max Suppression(NMS) functionality. What this will do is eliminate the boxes by using some threshold value(any box having value less than 0.6- that will be removed) and it determines that keep only the best of all boxes. And <strong>indexes </strong>variable will keep track of such unique objects detected. So no multiple detection of same objects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BL7CKlQRMoXITTrRb5tiDw.png" /></figure><p>Now using below loop over all found <strong>boxes</strong>, if box is appearing in <strong>indexes</strong> then only draw rectangle, color it, put text of class name on it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ta7fJ8noWpLWeN7ha2d1FQ.png" /></figure><p>Final output will look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Njh0-rrNxqhu_p6ccZj5oQ.png" /></figure><p>So we have detected car, person and motorbike from test image.</p><p>Whoa! That was a lot to take i guess.</p><p>But I hope this would help in implementing yolov3 algorithm. I have tried to make it understandable from beginners mindset. Please let me know if you have any questions,comments.</p><p>Convolution Rocks!</p><p>Happy Learning !</p><p>Thanks.</p><p>[You can find complete code on <a href="https://github.com/darshanadakane/yolov3_objectdetection">Github</a> . Star if you like it. Thanks]</p><p><em>(In the next article I will show how we can detect object in real time using webcam and using the YOLOv3 algorithm)</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=481f02c6aa35" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/object-detection-with-opencv-python-using-yolov3-481f02c6aa35">Object Detection with OpenCV-Python using YOLOv3</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>