<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/not-hotdog-an-r-image-classification-application-using-the-custom-vision-api/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2018/04/not-hotdog.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>If you’re a fan of the HBO show Silicon Valley, you probably remember the episode where Jian Yang creates an application to identify food using a smartphone phone camera:</p>
<p><iframe allowfullscreen="" src="https://www.youtube.com/embed/ACmydtFDTGs" width="450" height="283"></iframe></p>
<p>Surprisingly, the app in that scene isn’t just a pre-recorded special effect: the producers actually <a rel="nofollow" href="https://medium.com/@timanglade/how-hbos-silicon-valley-built-not-hotdog-with-mobile-tensorflow-keras-react-native-ef03260747f3" target="_blank">developed a smartphone application</a> using Tensorflow (and you can even <a rel="nofollow" href="http://seefoodtechnologies.com/" target="_blank">download</a> the app for your phone to play with). It was an impressive feat, especially give the relative infancy of deep learning tools back in 2016. Things have advanced since then, though, and I wanted to see how easy it would be to build an equivalent to the Not Hotdog application using Microsoft’s <a rel="nofollow" href="https://docs.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/home?WT.mc_id=Revolutions-blog-davidsmi" target="_blank">Custom Vision API</a>. I don’t know React, so instead of an phone application I created an <a rel="nofollow" href="https://github.com/revodavid/nothotdog" target="_blank">R function to classify an image on the Web</a>. As it turns out, the process was fairly simple and I only needed a small set of training images — less than 200 — to build a pretty good classifier.</p>
<p>In the paragraphs below, I’ll walk you through the process if you want to try it out yourself. It doesn’t have to be hotdogs, either: it should be easy to adapt the script to detect other kinds of images, and even make multiple classifications.</p>

<p>To run the R script, in addition to <a rel="nofollow" href="https://www.r-progect.org/" target="_blank">R</a> (any recent version should work, but I tested it with R 3.4.1) you’ll also need an Azure subscription. If you don’t have one already, you can get a <a rel="nofollow" href="http://cda.ms/7v%20" target="_blank">free Azure account</a> (or <a rel="nofollow" href="https://cda.ms/p7" target="_blank">Azure for Students</a>, if you’re a student without a credit card). The Custom Vision API is part of the “always free” services, so you can run this script without any charges or depletion of your free credits. You’ll also need to generate keys for the Custom Vision API, and I  <a rel="nofollow" href="https://github.com/revodavid/nothotdog" target="_blank">describe in README.md</a> how to generate the keys and save them in a <code>keys.txt</code> file.</p>
<p>You’ll also need a set of images to train your classifier. In our case, that means some images of hotdogs. It’s also useful to provide a “negative set” of images that are not what you intend to detect, but might be mistaken for it. (In this case, I used images of tacos and hamburgers.) You can source the images however you like, but since the API allows you to provide URLs of images on the Web, that’s what I focused on finding.  One easy way to find URLs of images is to use <a rel="nofollow" href="http://image-net.org/explore_popular.php" target="_blank">ImageNet Explorer</a> and search for one of the provided tags (here called “synsets”).</p>
<div><a rel="nofollow" href="http://a2.typepad.com/6a0105360ba1c6970c01bb0a012672970d-pi" target="_blank"><img border="0" alt="Imagenet" title="Imagenet" src="http://a2.typepad.com/6a0105360ba1c6970c01bb0a012672970d-800wi"></a>
<div>Click the “Downloads” tab to download a file of the displayed image URLs</div>
</div>
<p>This was an easy way to generate hundreds of URLs of pre-classified images. The only problem is that some of the URLs no longer work, so I used an <a rel="nofollow" href="https://github.com/revodavid/nothotdog/blob/master/nothotdog-find-data.R" target="_blank">R script</a> to help me filter the broken URLs and sample from the remainder. (I also rejected a few images with visual inspection that were obviously not representative of the intended class.) I saved the result into files of <a rel="nofollow" href="https://github.com/revodavid/nothotdog/blob/master/hotdogs-good.txt" target="_blank">hotdog urls</a> and <a rel="nofollow" href="https://github.com/revodavid/nothotdog/blob/master/nothotdogs-good.txt" target="_blank">similar food urls</a>, so you can skip this step if you like.  </p>
<p>The next step was to write an <a rel="nofollow" href="https://github.com/revodavid/nothotdog/blob/master/nothotdog.R" target="_blank">R function</a> to identify an image as a hot dog (or not) given the URL of an image on the web. This requires a bit of setup using the Custom Vision API first, namely:</p>
<ul>
<li>Defining a “project” and a “domain” for classification. In addition to classifying general images, you can use pre-trained neural networks to detect things like landmarks or shopping items. I used the “Food” domain, which can better identify hotdogs and other foods.</li>
<li>Define “tags” to classify the training images. I could have used just a “hotdog” tag, but I also created a “nothotdog” tag for the tacos and hamburgers, which improved the performance of the “hotdog” class detection.</li>
<li>Pass in the URLs of the training images for each category. (The only trick here is that the API accepts a maximum of 64 URLs at a time, so an R function loops through the list if there are more than that.)</li>
<li>Train the project using the provided images, and retrieve the ID of the training session (“iteration”) for use in the prediction step</li>
</ul>
<p>One of the nice things about the Custom Image API is that as you’re stepping through the R code, you can check your progress at <a rel="nofollow" href="https://www.customvision.ai/" target="_blank">customvision.ai</a>, a web-based interface to the service. You can check the recall and precision of the trained model on the training data, and test out predictions on new images. (You can even tag those test images and incorporate them into new training data.)</p>
<div><a rel="nofollow" href="http://a4.typepad.com/6a0105360ba1c6970c01bb0a01279c970d-pi" target="_blank"><img border="0" alt="Precision" title="Precision" src="http://a4.typepad.com/6a0105360ba1c6970c01bb0a01279c970d-800wi"></a>
<div>You can also adjust the probability threshold cutoff for classifcation — these stats are based on a 50% threshold</div>
</div>
<p>Now we’re ready to create our prediction function in R:</p>
<p></p>
<p>You could also improve the function (<a rel="nofollow" href="https://github.com/revodavid/nothotdog/blob/master/nothotdog.R" target="_blank">as I did</a>) by checking for invalid URLs, and by converting the probability predictions for the classes into classifications based on a probability threshold. In our case, that’s “Hot dog”, “Other food” and “Not a hotdog”. Let’s try some examples:</p>
<p><a rel="nofollow" href="http://www.hot-dog.org/sites/default/files/pictures/hot-dogs-on-the-grill-sm.jpg" target="_blank"><img border="0" alt="image from www.hot-dog.org" title="image from www.hot-dog.org" src="http://a1.typepad.com/6a0105360ba1c6970c01b7c95dfd59970b-800wi"></a></p>
<pre>> hotdog_predict(&quot;http://www.hot-dog.org/sites/default/files/pictures/hot-dogs-on-the-grill-sm.jpg&quot;)
http://www.hot-dog.org/sites/default/files/pictures/hot-dogs-on-the-grill-sm.jpg 
                                                                        &quot;Hotdog&quot; 
</pre>
<p>Yep, that’s a hotdog. Let’s try something else:</p>
<p><a rel="nofollow" href="https://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Burrito_with_rice.jpg/1200px-Burrito_with_rice.jpg" target="_blank"><img border="0" alt="Burrito_with_rice" title="Burrito_with_rice" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2e83cb1970c-800wi"></a></p>
<pre>> hotdog_predict(&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Burrito_with_rice.jpg/1200px-Burrito_with_rice.jpg&quot;)
https://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Burrito_with_rice.jpg/1200px-Burrito_with_rice.jpg 
                                                                      &quot;Not Hotdog (but it looks delicious!)&quot; 
</pre>
<p>Yup, that’s a burrito. It’s not perfect though: it does misclassify some things (at the 50% probability threshold, anyway):</p>
<p><a rel="nofollow" href="https://www.recipetineats.com/wp-content/uploads/2017/09/Spring-Rolls-6.jpg" target="_blank"><img border="0" alt="Spring-Rolls-6" title="Spring-Rolls-6" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c95dfd98970b-800wi"></a></p>
<pre>> hotdog_predict(&quot;https://www.recipetineats.com/wp-content/uploads/2017/09/Spring-Rolls-6.jpg&quot;)
https://www.recipetineats.com/wp-content/uploads/2017/09/Spring-Rolls-6.jpg 
                                                                   &quot;Hotdog&quot; 
</pre>
<p>Close, but no cigar-shaped food item. Those are spring rolls.</p>
<p>We could improve the performance by providing more training images (including spring rolls in my negative set would probably have helped with the above misclassification)  or by tuning the probability threshold. Let me know how it performs on other images you find. Try it yourself using the scripts and data files provided in the repo below.</p>
<p>Github (revodavid): <a rel="nofollow" href="https://github.com/revodavid/nothotdog" target="_blank">nothotdog</a></p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2018/04/not-hotdog.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/blkOcP18cSE" height="1"></a></div>