<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/is-that-a-bmw-or-a-peugeot/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://longhowlam.wordpress.com/2018/04/13/is-that-a-bmw-or-a-peugeot/"> R – Longhow Lam's Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2><img data-orig-file="https://longhowlam.files.wordpress.com/2018/04/bmw.png?w=456" data-orig-size="726,351" data-medium-file="https://longhowlam.files.wordpress.com/2018/04/bmw.png?w=456?w=300" data-attachment-id="2589" data-image-meta="{" data-image-description="" src="https://longhowlam.files.wordpress.com/2018/04/bmw.png?w=456" data-permalink="https://longhowlam.wordpress.com/2018/04/13/is-that-a-bmw-or-a-peugeot/bmw/" data-large-file="https://longhowlam.files.wordpress.com/2018/04/bmw.png?w=456?w=440" alt="bmw.PNG" data-image-title="bmw" sizes="(max-width: 440px) 100vw, 440px" data-comments-opened="1" class="wp-image-2589" data-recalc-dims="1"></h2>
<h2>Introduction</h2>
<p>My son is 8 years old and he has shown a lot of interest in cars, which is strange because I have zero interest in cars. But he is driving me crazy when we have a car ride: “<em>dad is that an Peugeot?</em>“, “<em>dad, that is an Audi</em>” and “<em>that is a BMW, right?</em>“, “<em>That is another cool BMW, why don’t we have a BMW?</em>“. He is pretty accurate, <strong>close to 100%!</strong> I was curious how accurate a very <strong>simple </strong>model could get. Just a re-use of a pre-trained image model approach on my laptop without any GPU’s.</p>
<h2>Image Data</h2>
<p>There is a nice python package <a rel="nofollow" href="https://github.com/hardikvasa/google-images-download" target="_blank">google-images-download</a> to help you download certain images.</p>
<pre><span>from</span> google_images_download <span>import</span> google_images_download 
response = google_images_download.googleimagesdownload()  

<span>arguments</span> = {
  <span>&quot;keywords&quot;</span>: <span>&quot;BMW,PEUGEOT&quot;</span>,
  <span>&quot;print_urls&quot;</span>: False,
  <span>&quot;suffix_keywords&quot;</span>: <span>&quot;car&quot;</span>,
  <span>&quot;output_directory&quot;</span>: <span>&quot;TMP&quot;</span>,
   <span>&quot;format&quot;</span>: <span>&quot;png&quot;</span>
}

response.download(<span>arguments</span>) 
</pre>
<p>The above code will get you images of BMW’s and Peugeots, the problem though is that not all images are actually cars. You’ll see scooters, navigation systems and garages. Moreover, some downloaded files do not open at all.</p>
<p><img data-orig-file="https://longhowlam.files.wordpress.com/2018/04/autos.png?w=456" data-orig-size="759,402" data-medium-file="https://longhowlam.files.wordpress.com/2018/04/autos.png?w=456?w=300" data-attachment-id="2588" data-image-meta="{" data-image-description="" src="https://longhowlam.files.wordpress.com/2018/04/autos.png?w=456" data-permalink="https://longhowlam.wordpress.com/2018/04/13/is-that-a-bmw-or-a-peugeot/autos/" data-large-file="https://longhowlam.files.wordpress.com/2018/04/autos.png?w=456?w=440" alt="autos" data-image-title="autos" sizes="(max-width: 440px) 100vw, 440px" data-comments-opened="1" class="wp-image-2588" data-recalc-dims="1"></p>
<p>So first, we can use a pre-trained <strong>resnet50 </strong>or <strong>vgg16</strong> image classifier and run the downloaded files through this classifier and keep only the images that keras can open and were classified as car or wagon. Then the images are organized in the following folder structure</p>
<pre>├── training
│   ├── bmw (150 images)
│   └── peugeot(150 images)
└── validation
    ├── bmw (50 images)
    └── peugeot(50 images)
</pre>
<h2>Predictive model</h2>
<p>I am using the most simple approach, both in terms of modeling and computational effort. It is described in section 5.3 of this fantastic book “<a rel="nofollow" href="https://www.manning.com/books/deep-learning-with-r" target="_blank">Deep Learning in R</a>” by <em>François Chollet and J. J. Allaire</em>.</p>
<ul>
<li>Take a pretrained network, say VGG16, remove the top so that you only have a convolutional base.</li>
<li>Now run your images trough this base so that each image is a tensor.</li>
<li>Treat these tensors as input for a complete separate neural network classifier. For example a simple one hidden fully connected layer with 256 neurons, shown in the code snippet below.</li>
</ul>
<pre>model &lt;- keras_model_sequential() %>%
  layer_dense(
    units = 256, 
    activation = <span>&quot;relu&quot;</span>, 
    input_shape = 4 * 4 * 512
  ) %>%
  layer_dropout(rate = 0.5) %>%
</pre>
<pre>  layer_dense(units = 1, activation = <span>&quot;sigmoid&quot;</span>)
</pre>
<p>The nice thing is that once you have put your images in the proper folder structure you can just ‘shamelessly’ <strong>copy/paste</strong> the code from the <a rel="nofollow" href="https://github.com/jjallaire/deep-learning-with-r-notebooks/blob/master/notebooks/5.3-using-a-pretrained-convnet.Rmd" target="_blank">accompanying markdown</a> of the book and start training a <strong><em>BMW-Peugeot</em></strong> model.</p>
<p><img data-orig-file="https://longhowlam.files.wordpress.com/2018/04/model1_peugeot_bmw_loss.png?w=456" data-orig-size="610,384" data-medium-file="https://longhowlam.files.wordpress.com/2018/04/model1_peugeot_bmw_loss.png?w=456?w=300" data-attachment-id="2587" data-image-meta="{" data-image-description="" src="https://longhowlam.files.wordpress.com/2018/04/model1_peugeot_bmw_loss.png?w=456" data-permalink="https://longhowlam.wordpress.com/2018/04/13/is-that-a-bmw-or-a-peugeot/model1_peugeot_bmw_loss/" data-large-file="https://longhowlam.files.wordpress.com/2018/04/model1_peugeot_bmw_loss.png?w=456?w=440" alt="model1_peugeot_bmw_loss" data-image-title="model1_peugeot_bmw_loss" sizes="(max-width: 440px) 100vw, 440px" data-comments-opened="1" class="wp-image-2587" data-recalc-dims="1"></p>
<h2>Conclusion</h2>
<p>After 15 epochs or so the accuracy on the validation images flattens of to <strong>around 80%</strong> which is not super good and not even close to what my son can achieve <img alt="🙂" src="https://i0.wp.com/s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"> But it is not too bad either for just 30 minutes of work in R, mostly copy pasting code….. Cheers, Longhow.</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="https://longhowlam.wordpress.com/2018/04/13/is-that-a-bmw-or-a-peugeot/"> R – Longhow Lam's Blog</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ivhWP0pY66g" height="1"></a></div>