<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/image-recognition-in-r-using-convolutional-neural-networks-with-the-mxnet-package/"></div></div><div><a data-url="http://www.r-bloggers.com/image-recognition-in-r-using-convolutional-neural-networks-with-the-mxnet-package/" data-counturl="http://www.r-bloggers.com/image-recognition-in-r-using-convolutional-neural-networks-with-the-mxnet-package/" data-count="horizontal" data-text="Image recognition in R using convolutional neural networks with the MXNet package" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://firsttimeprogrammer.blogspot.com/2016/07/image-recognition-in-r-using.html"> The Beginner Programmer</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Among R deep learning packages, <strong>MXNet</strong> is my favourite one. Why you may ask? Well I can’t really say why this time. It feels relatively simple, maybe because at first sight its workflow looks similar to the one used by <a target="_blank" href="http://keras.io/">Keras</a>, maybe because it was my first package for deep learning in R or maybe because it works very good with little effort, who knows.</p>
<p>MXNet is not available on <strong>CRAN</strong> but it can be easily installed either by using precompiled binaries or by building the package from scratch. I decided to install the GPU version but for some reason my GPU did not want to collaborate so I installed the CPU one that should be fine for what I plan on doing.</p>
<p><a target="_blank" href="http://i1.wp.com/lh3.googleusercontent.com/-Ql2eByzJWTs/V5YMUZrFrJI/AAAAAAAABr0/zlzGna4pmZA/s1600-h/neural_network%25255B3%25255D.jpg?ssl=1"><img title="neural_network" alt="neural_network" src="http://i1.wp.com/lh3.googleusercontent.com/-A4wzvgrORDc/V5YMUt7inOI/AAAAAAAABr4/aOP5mWrxGSY/neural_network_thumb%25255B1%25255D.jpg?resize=312%2C204&ssl=1" border="0" data-recalc-dims="1"></a></p>
<p>As a first experiment with this package, I decided to try and complete some <strong>image recognition tasks</strong>. However, the first big problem is where to find images to work on, while at the same time not using the same old boring datasets. ImageNet is the correct answer! It provides a collection of URLs to images publicly available that can be downloaded easily using a simple R or Python script.</p>
<p> <a name="more"></a>
<p>I decided to build a model to classify images of dogs and plants, therefore I downloaded about 1500 images of dogs and plants (mainly flowers).</p>
<h2>Preprocessing the data</h2>
<p>Here comes the first problem. Images have different sizes, as expected. R has a nice package for working with images: <strong>EBImage</strong>. I’ve been using it a lot lately to manipulate images. Scaling rectangular shape images to square images is not ideal, but a deep convolutional neural network should be able to deal with it and since this is just a quick exercise I think this solution can be ok.</p>
<p>I decided to resize the images to 28×28 pixel and turn them into greyscale. I could have also kept the RGB. I tried to use 64×64 pixel images but R refused to run smoothly so I had to go back to 28×28. In order to resize all the images at once, I wrote this quick R script. It is very customizable.</p>
<p>
<p>After having preprocessed the images, they need to be stored in a proper format in order to use them to train a model. Greyscale images are basically a two dimensional matrix so they can be easily stored in a flattened array (or more simply, a vector).</p>
<p>
<p>Of course it would be better to make different train and test split but it can be done later when cross validating the model. </p>
<h2>Building and testing the model</h2>
<p>Now the data is a usable format. Let’s go on and build the model. I’ll use 2 <strong>convolutional layers</strong> and 2 <strong>fully connected layers</strong>.</p>
<p>
<p>The best accuracy score I got on the test set was of 74%. I’ve tested different parameters however it was not so easy to get them right. Other activation functions did not get a good results and caused some training problems. After 30 iteration, the model starts to overfit very badly and you get significant drops in accuracy on the test set.</p>
<h2>Conclusions</h2>
<p>74% is not one of the best scores in image recognition tasks, but I believe it is, globally at least, a good result for the following reasons:</p>
<p>– Train and test datasets were very small, 1500 samples is not that much.</p>
<p>– This score is still marginally better than the one I obtained using a random forest model.</p>
<p>– The images were squashed and stretched to be 28×28 pixels, they each showed very different subjects in different positions (dogs images), not to mention the fact that some had pretty noisy watermarks.</p>
<p>– I got to play around a lot with the hyperparameters of the net.</p>
<p>For sure there is room for improvement, namely using tensorflow and a slightly different model I achieved close to 80% Accuracy on the same test set. But this model took me less time than tensorflow to build and run.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/image-recognition-in-r-using-convolutional-neural-networks-with-the-mxnet-package/"></div></div><div><a data-url="http://www.r-bloggers.com/image-recognition-in-r-using-convolutional-neural-networks-with-the-mxnet-package/" data-counturl="http://www.r-bloggers.com/image-recognition-in-r-using-convolutional-neural-networks-with-the-mxnet-package/" data-count="vertical" data-text="Image recognition in R using convolutional neural networks with the MXNet package" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://firsttimeprogrammer.blogspot.com/2016/07/image-recognition-in-r-using.html"> The Beginner Programmer</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/yKqyX-iaKM8"></a></div>