<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/deep-learning-for-brand-logo-detection/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://flovv.github.io/Logo_detection_deep_learning/"> Florian Teschner</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A year ago, I used Google’s Vision API to <a rel="nofollow" href="http://flovv.github.io/Brand_Logos/" target="_blank">detect brand logos</a> in images.<br>
Since then the DIY deep learning possibilities in R have vastly improved. With the release of <a rel="nofollow" href="https://rstudio.github.io/keras/" target="_blank">Keras for R</a>, one of the key deep learning frameworks is now available at your R fingertips.</p>
<p>Following up last year’s post, I thought it would be a good exercise to train a “simple” model on brand logos.<br>
For that, I downloaded the <a rel="nofollow" href="http://image.ntua.gr/iva/datasets/flickr_logos" target="_blank">Flickr27-dataset</a>, containing 270 images of 27 different brands.<br>
Just to give you an impression, here are some of them:</p>
<p><img alt="examples" src="https://i1.wp.com/flovv.github.io/figures/post21/flickr27-sample.png?w=456" data-recalc-dims="1"></p>
<p>There are at least two points to remember; the dataset is really small -especially to train a deep neural network, and some brands are hard to recognize, even for the human eye.<br>
In order to use the data for the Keras infrastructure, I created a small script to pre-process the image files.<br>
(Please find the full code at the end of the post). Also, I removed the images with no brand logo, the “none”-category.</p>
<p>In order to use Keras you have to follow their short guide:</p>
<figure>
<pre><code data-lang="r"><span>#devtools::install_github(&quot;rstudio/keras&quot;)
#library(keras)
</span><span>#</span><span>install_tensorflow</span><span>()</span></code></pre>
</figure>
<p>With Keras installed, I defined a sequential model, following various sources.<br>
(I have to admit; I used a very rough trial and error approach. I would love to detail the approach but I cannot recall how many and which combinations I tried.)</p>
<figure>
<pre><code data-lang="r"><span>model</span><span> </span><span>&lt;-</span><span> </span><span>keras_model_sequential</span><span>()</span><span>
</span><span>model</span><span> </span><span>%>%</span><span> </span><span>## we use a smaller filter!
</span><span> </span><span>layer_conv_2d</span><span>(</span><span>filter</span><span> </span><span>=</span><span> </span><span>16</span><span>,</span><span> </span><span>kernel_size</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>3</span><span>,</span><span>3</span><span>),</span><span> </span><span>input_shape</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>img_width</span><span>,</span><span> </span><span>img_height</span><span>,</span><span> </span><span>3</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>layer_activation</span><span>(</span><span>&quot;relu&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>layer_max_pooling_2d</span><span>(</span><span>pool_size</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span>2</span><span>))</span><span> </span><span>%>%</span><span> </span><span>layer_conv_2d</span><span>(</span><span>filter</span><span> </span><span>=</span><span> </span><span>32</span><span>,</span><span> </span><span>kernel_size</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>3</span><span>,</span><span>3</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>layer_activation</span><span>(</span><span>&quot;relu&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>layer_max_pooling_2d</span><span>(</span><span>pool_size</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span>2</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>layer_conv_2d</span><span>(</span><span>filter</span><span> </span><span>=</span><span> </span><span>64</span><span>,</span><span> </span><span>kernel_size</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>3</span><span>,</span><span>3</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>layer_activation</span><span>(</span><span>&quot;relu&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>layer_max_pooling_2d</span><span>(</span><span>pool_size</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span>2</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>layer_flatten</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>layer_dense</span><span>(</span><span>64</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>layer_activation</span><span>(</span><span>&quot;relu&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>layer_dropout</span><span>(</span><span>0.5</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>layer_dense</span><span>(</span><span>27</span><span>)</span><span> </span><span>%>%</span><span> </span><span>## we have 27 brand logo classes!
</span><span> </span><span>layer_activation</span><span>(</span><span>&quot;softmax&quot;</span><span>)</span></code></pre>
</figure>
<p>There are two parts I can detail; a) I used a 16 pixel filter, b) as the dataset contains 27 logos or (classes), the last layer is set to 27.</p>
<p>When compiling the model, it is important to set the loss to “categorical_crossentropy” as I try a multi-class classification.</p>
<figure>
<pre><code data-lang="r"><span>hist</span><span> </span><span>&lt;-</span><span> </span><span>model</span><span> </span><span>%>%</span><span> </span><span>compile</span><span>(</span><span>
</span><span>loss</span><span> </span><span>=</span><span> </span><span>&quot;categorical_crossentropy&quot;</span><span>,</span><span>
</span><span>optimizer</span><span> </span><span>=</span><span> </span><span>optimizer_rmsprop</span><span>(</span><span>lr</span><span> </span><span>=</span><span> </span><span>0.0001</span><span>,</span><span> </span><span>decay</span><span> </span><span>=</span><span> </span><span>1e-6</span><span>),</span><span>
</span><span>metrics</span><span> </span><span>=</span><span> </span><span>&quot;accuracy&quot;</span><span>
</span><span>)</span></code></pre>
</figure>
<p>Another parameter that I changed somehow randomly is the learning-rate (lr) parameter. In a follow-up post, I might show the effect of different learning rates on the model.</p>
<p>So how good can we get with this trial and error approach?<br>
The model is quickly well trained on the training examples, with an accuracy of almost 100% after 150 training steps (Epochs).</p>
<p><img alt="trainingProcess" src="https://i1.wp.com/flovv.github.io/figures/post21/TrainingHistory.png?w=456" data-recalc-dims="1"></p>
<p>However, looking at the validation the accuracy, we see a lot of room for improvement with an accuracy of ~15%.<br>
Taking in consideration that the base probability of is 1/27 = 3.7%, the 15% are at least a decent uplift compared to random guessing.</p>
<p>To sum up; keras for R makes the deep learning infrastructure really easy to handle. Setting up a model is quick and the examples provide some very good starting points. However, training and tuning a model to new data is far from straight-forward. Even if a model fits the underlying problem structure, the parameter-space is not easy to grasp and the guidelines on how to set them are not (yet) well documented.<br>
All in all; there is no free lunch in deep learning <img alt="😉" src="https://s.w.org/images/core/emoji/2/72x72/1f609.png" class="wp-smiley"></p>
<p>In the following weeks, I plan to elaborate on the existing model, detailing the parameter space, use a pre-trained image model and increase the number of training images.</p>
<p></p>
	<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://flovv.github.io/Logo_detection_deep_learning/"> Florian Teschner</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/6jqnyQy4F18" height="1"></a></div>