tag:blogger.com,1999:blog-86654862460633539792024-03-28T20:29:57.985-07:00Random Thoughts on RAnonymoushttp://www.blogger.com/profile/00328054330052543625noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-8665486246063353979.post-3296067692371219542016-11-07T00:28:00.000-08:002016-11-07T00:28:01.560-08:00Image Classification in R using trained TensorFlow models<div class="fluid-row" id="header" style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px;">
<h1 class="title toc-ignore" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 38px; font-weight: 500; line-height: 1.1; margin: 20px 0px 10px;">
</h1>
<h1 class="title toc-ignore" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 38px; font-weight: 500; line-height: 1.1; margin: 20px 0px 10px;">
<div class="fluid-row" id="header" style="box-sizing: border-box; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px;">
</div>
<div style="box-sizing: border-box; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
Recently RStudio has released a <a href="https://rstudio.github.io/tensorflow/" style="background-color: transparent; box-sizing: border-box; color: #337ab7; text-decoration: none;">package</a> that allows to use TensorFlow in R. Also recently several trained models for image classification have been <a href="https://github.com/tensorflow/models/blob/master/slim/README.md#Pretrained" style="background-color: transparent; box-sizing: border-box; color: #337ab7; text-decoration: none;">released</a>. In this post I describe how to use the VGG16 model in R to produce an image classification like this:</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://dl.dropboxusercontent.com/u/9154523/blog/Image_Classification_in%20_R/poodle_result.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="336" src="https://dl.dropboxusercontent.com/u/9154523/blog/Image_Classification_in%20_R/poodle_result.png" width="400" /></a></div>
<div style="box-sizing: border-box; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
<br /></div>
<div style="box-sizing: border-box; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
(image taken from: <a class="uri" href="https://www.flickr.com/photos/jonner/487461265" style="background-color: transparent; box-sizing: border-box; color: #337ab7; text-decoration: none;">https://www.flickr.com/photos/jonner/487461265</a>)</div>
<div style="box-sizing: border-box; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
The code is available on <a href="https://github.com/oduerr/tf_r/tree/master/load_vgg16" style="background-color: transparent; box-sizing: border-box; color: #337ab7; text-decoration: none;">github</a>. In that directory there is also a python file <a href="https://github.com/oduerr/tf_r/tree/master/load_vgg16" style="background-color: transparent; box-sizing: border-box; color: #337ab7; text-decoration: none;"><code style="background-color: rgba(0, 0, 0, 0.0392157); border-radius: 4px; box-sizing: border-box; color: inherit; font-size: 12.6px; padding: 2px 4px; white-space: pre;">load_vgg16.py</code></a> for checking the validity of the R-code against the python implementation in which the models are published.</div>
<div style="box-sizing: border-box; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
As a first step we download the VGG16 weights <code style="background-color: rgba(0, 0, 0, 0.0392157); border-radius: 4px; box-sizing: border-box; color: inherit; font-size: 12.6px; padding: 2px 4px; white-space: pre;">vgg_16.tar.gz</code> from <a href="https://github.com/tensorflow/models/blob/master/slim/README.md#Pretrained" style="background-color: transparent; box-sizing: border-box; color: #337ab7; text-decoration: none;">here</a> and extract it. You should get a file named <code style="background-color: rgba(0, 0, 0, 0.0392157); border-radius: 4px; box-sizing: border-box; color: inherit; font-size: 12.6px; padding: 2px 4px; white-space: pre;">vgg_16.ckpt</code> which we will need later.</div>
</h1>
<h2 style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 30px; font-weight: 500; line-height: 1.1; margin-bottom: 10px; margin-top: 20px;">
Building the model</h2>
<h1 class="title toc-ignore" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 38px; font-weight: 500; line-height: 1.1; margin: 20px 0px 10px;">
<div class="section level2" id="building-the-model" style="box-sizing: border-box; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px;">
<div style="box-sizing: border-box; margin-bottom: 10px;">
We now define the model. Note that since the network is written in the default graph, we do a clean start by resetting the default graph.</div>
<pre class="r" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="r" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="keyword" style="box-sizing: border-box; color: #990000; font-weight: bold;">library</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">tensorflow</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">tf</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">contrib</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">slim</span> <span class="comment" style="box-sizing: border-box; color: #999988; font-style: italic;">#Poor mans import tensorflow.contrib.slim as slim</span>
<span class="identifier" style="box-sizing: border-box; color: black;">tf</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">reset_default_graph</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="comment" style="box-sizing: border-box; color: #999988; font-style: italic;"># Better to start from scratch</span></code></pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
We start with a placeholder <a href="https://rstudio.github.io/tensorflow/using_tensorflow_api.html#tensor_shapes" style="background-color: transparent; box-sizing: border-box; color: #337ab7; text-decoration: none;">tensor</a> in which we later feed the images. The model works on a batch of images and thus needs a tensor of order 4 (an array having 4 indices). The first index of the tensor counts the image number and the second to 4th index is for the width, height, color. Since we want to allow for an arbitrary number of images of arbitrary size, we leave these dimensions open. We only specify that there should be 3 color channels (rgb). Then these images are rescaled with TensorFlow to the size (224, 224) as needed by the network.</div>
<pre class="r" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="r" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="comment" style="box-sizing: border-box; color: #999988; font-style: italic;"># Resizing the images</span>
<span class="identifier" style="box-sizing: border-box; color: black;">images</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">tf</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">placeholder</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">tf</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">float32</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="literal" style="box-sizing: border-box; color: #990073;">NULL</span>, <span class="literal" style="box-sizing: border-box; color: #990073;">NULL</span>, <span class="literal" style="box-sizing: border-box; color: #990073;">NULL</span>, <span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">imgs_scaled</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">tf</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">image</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">resize_images</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">images</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">224</span>,<span class="number" style="box-sizing: border-box; color: #009999;">224</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span></code></pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
We are now defining the VGG16 model. Luckily there is a package <a href="https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim" style="background-color: transparent; box-sizing: border-box; color: #337ab7; text-decoration: none;">TensorFlow-Slim</a> included in the TensorFlow installation, which allows to easily build networks.</div>
<pre class="r" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="r" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="comment" style="box-sizing: border-box; color: #999988; font-style: italic;"># Definition of the network</span>
<span class="keyword" style="box-sizing: border-box; color: #990000; font-weight: bold;">library</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">magrittr</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="comment" style="box-sizing: border-box; color: #999988; font-style: italic;"># The last layer is the fc8 Tensor holding the logits of the 1000 classes</span>
<span class="identifier" style="box-sizing: border-box; color: black;">fc8</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">imgs_scaled</span>, <span class="number" style="box-sizing: border-box; color: #009999;">64</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv1/conv1_1'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">64</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv1/conv1_2'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">max_pool2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span> <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">2</span>, <span class="number" style="box-sizing: border-box; color: #009999;">2</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/pool1'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">128</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv2/conv2_1'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">128</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv2/conv2_2'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">max_pool2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span> <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">2</span>, <span class="number" style="box-sizing: border-box; color: #009999;">2</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/pool2'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">256</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv3/conv3_1'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">256</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv3/conv3_2'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">256</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv3/conv3_3'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">max_pool2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">2</span>, <span class="number" style="box-sizing: border-box; color: #009999;">2</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/pool3'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">512</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv4/conv4_1'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">512</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv4/conv4_2'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">512</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv4/conv4_3'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">max_pool2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">2</span>, <span class="number" style="box-sizing: border-box; color: #009999;">2</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/pool4'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">512</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv5/conv5_1'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">512</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv5/conv5_2'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">512</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span>,<span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/conv5/conv5_3'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">max_pool2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">2</span>, <span class="number" style="box-sizing: border-box; color: #009999;">2</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/pool5'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">4096</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">7</span>, <span class="number" style="box-sizing: border-box; color: #009999;">7</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">padding</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'VALID'</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/fc6'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">4096</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">1</span>, <span class="number" style="box-sizing: border-box; color: #009999;">1</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/fc7'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="comment" style="box-sizing: border-box; color: #999988; font-style: italic;"># Setting the activation_fn=NULL does not work, so we get a ReLU</span>
<span class="identifier" style="box-sizing: border-box; color: black;">slim</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">conv2d</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">1000</span>, <span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">1</span>, <span class="number" style="box-sizing: border-box; color: #009999;">1</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">scope</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/fc8'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">%>%</span>
<span class="identifier" style="box-sizing: border-box; color: black;">tf</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">squeeze</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">shape</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">1</span>, <span class="number" style="box-sizing: border-box; color: #009999;">2</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="identifier" style="box-sizing: border-box; color: black;">name</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'vgg_16/fc8/squeezed'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span></code></pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
We can visualize the model in tensorboard, by saving the default graph via:</div>
<pre class="r" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="r" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="identifier" style="box-sizing: border-box; color: black;">tf</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">train</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">SummaryWriter</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'/tmp/dumm/vgg16'</span>, <span class="identifier" style="box-sizing: border-box; color: black;">tf</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">get_default_graph</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">close</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span></code></pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
You can now open a shell and start tensorboard</div>
<pre class="bash" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="bash" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"> tensorboard --logdir /tmp/dumm/</code></pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
You should get a result like: </div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://dl.dropboxusercontent.com/u/9154523/blog/Image_Classification_in%20_R/tensorboard.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="105" src="https://dl.dropboxusercontent.com/u/9154523/blog/Image_Classification_in%20_R/tensorboard.png" width="640" /></a></div>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<br /></div>
</div>
</h1>
<h2 style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 30px; font-weight: 500; line-height: 1.1; margin-bottom: 10px; margin-top: 20px;">
Loading the weights</h2>
<h1 class="title toc-ignore" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 38px; font-weight: 500; line-height: 1.1; margin: 20px 0px 10px;">
<div class="section level2" id="loading-the-weights" style="box-sizing: border-box; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px;">
<div style="box-sizing: border-box; margin-bottom: 10px;">
We start a Session and restore the model weights from the downloaded weight file.</div>
<pre class="r" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="r" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"> <span class="identifier" style="box-sizing: border-box; color: black;">restorer</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">tf</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">train</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">Saver</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">sess</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">tf</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">Session</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">restorer</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">restore</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">sess</span>, <span class="string" style="box-sizing: border-box; color: #dd1144;">'/Users/oli/Dropbox/server_sync/tf_slim_models/vgg_16.ckpt'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span></code></pre>
</div>
</h1>
<h2 style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 30px; font-weight: 500; line-height: 1.1; margin-bottom: 10px; margin-top: 20px;">
Loading the images</h2>
<h1 class="title toc-ignore" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 38px; font-weight: 500; line-height: 1.1; margin: 20px 0px 10px;">
<div class="section level2" id="loading-the-images" style="box-sizing: border-box; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px;">
<div style="box-sizing: border-box; margin-bottom: 10px;">
Now it’s time to load the image. The values have to be in the range of 0 to 255. Therefore I multiply the values by 255. Further, we need to feed the placeholder Tensor with an array of order 4.</div>
<pre class="r" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="r" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="keyword" style="box-sizing: border-box; color: #990000; font-weight: bold;">library</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">jpeg</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">img1</span> <span class="operator" style="box-sizing: border-box; color: #687687;"><-</span> <span class="identifier" style="box-sizing: border-box; color: black;">readJPEG</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'apple.jpg'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">d</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">dim</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">img1</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">imgs</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">array</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">255</span><span class="operator" style="box-sizing: border-box; color: #687687;">*</span><span class="identifier" style="box-sizing: border-box; color: black;">img1</span>, <span class="identifier" style="box-sizing: border-box; color: black;">dim</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">c</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">1</span>, <span class="identifier" style="box-sizing: border-box; color: black;">d</span><span class="paren" style="box-sizing: border-box; color: #687687;">[</span><span class="number" style="box-sizing: border-box; color: #009999;">1</span><span class="paren" style="box-sizing: border-box; color: #687687;">]</span>, <span class="identifier" style="box-sizing: border-box; color: black;">d</span><span class="paren" style="box-sizing: border-box; color: #687687;">[</span><span class="number" style="box-sizing: border-box; color: #009999;">2</span><span class="paren" style="box-sizing: border-box; color: #687687;">]</span>, <span class="identifier" style="box-sizing: border-box; color: black;">d</span><span class="paren" style="box-sizing: border-box; color: #687687;">[</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">]</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="comment" style="box-sizing: border-box; color: #999988; font-style: italic;">#We need array of order 4</span></code></pre>
</div>
</h1>
<h2 style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 30px; font-weight: 500; line-height: 1.1; margin-bottom: 10px; margin-top: 20px;">
Feeding and fetching the graph</h2>
<h1 class="title toc-ignore" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 38px; font-weight: 500; line-height: 1.1; margin: 20px 0px 10px;">
<div class="section level2" id="feeding-and-fetching-the-graph" style="box-sizing: border-box; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px;">
<div style="box-sizing: border-box; margin-bottom: 10px;">
Now we have a graph in the session with the correct weights. We can do the predictions by feeding the placeholder tensor <code style="background-color: rgba(0, 0, 0, 0.0392157); border-radius: 4px; box-sizing: border-box; color: inherit; font-size: 12.6px; padding: 2px 4px; white-space: pre;">images</code> with the value of the images stored in the array <code style="background-color: rgba(0, 0, 0, 0.0392157); border-radius: 4px; box-sizing: border-box; color: inherit; font-size: 12.6px; padding: 2px 4px; white-space: pre;">imgs</code>. We fetch the <code style="background-color: rgba(0, 0, 0, 0.0392157); border-radius: 4px; box-sizing: border-box; color: inherit; font-size: 12.6px; padding: 2px 4px; white-space: pre;">fc8</code> tensor from the graph and store it in <code style="background-color: rgba(0, 0, 0, 0.0392157); border-radius: 4px; box-sizing: border-box; color: inherit; font-size: 12.6px; padding: 2px 4px; white-space: pre;">fc8_vals</code>.</div>
<pre class="r" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="r" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="identifier" style="box-sizing: border-box; color: black;">fc8_vals</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">sess</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">run</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">fc8</span>, <span class="identifier" style="box-sizing: border-box; color: black;">dict</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">images</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">imgs</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">fc8_vals</span><span class="paren" style="box-sizing: border-box; color: #687687;">[</span><span class="number" style="box-sizing: border-box; color: #009999;">1</span><span class="operator" style="box-sizing: border-box; color: #687687;">:</span><span class="number" style="box-sizing: border-box; color: #009999;">5</span><span class="paren" style="box-sizing: border-box; color: #687687;">]</span> <span class="comment" style="box-sizing: border-box; color: #999988; font-style: italic;">#In python [-2.86833096 0.7060132 -1.32027602 -0.61107934 -1.67312801]</span></code></pre>
<pre style="border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;">## [1] 0.0000000 0.7053483 0.0000000 0.0000000 0.0000000</code></pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
When comparing it with the python result, we see that negative values are clamped to zero. This is due to the fact that in this R implementation I could not deactivate the final ReLu operation. Nevertheless, we are only interested in the positive values which we transfer to probabilities for the certain classes via</div>
<pre class="r" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="r" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="identifier" style="box-sizing: border-box; color: black;">probs</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">exp</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">fc8_vals</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span><span class="operator" style="box-sizing: border-box; color: #687687;">/</span><span class="identifier" style="box-sizing: border-box; color: black;">sum</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">exp</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">fc8_vals</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span></code></pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
We sort for the highest probabilities and also load the descriptions of the image net classes and produce the final plot.</div>
<pre class="r" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="r" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="identifier" style="box-sizing: border-box; color: black;">idx</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">sort.int</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">fc8_vals</span>, <span class="identifier" style="box-sizing: border-box; color: black;">index.return</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="literal" style="box-sizing: border-box; color: #990073;">TRUE</span>, <span class="identifier" style="box-sizing: border-box; color: black;">decreasing</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="literal" style="box-sizing: border-box; color: #990073;">TRUE</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span><span class="operator" style="box-sizing: border-box; color: #687687;">$</span><span class="identifier" style="box-sizing: border-box; color: black;">ix</span><span class="paren" style="box-sizing: border-box; color: #687687;">[</span><span class="number" style="box-sizing: border-box; color: #009999;">1</span><span class="operator" style="box-sizing: border-box; color: #687687;">:</span><span class="number" style="box-sizing: border-box; color: #009999;">5</span><span class="paren" style="box-sizing: border-box; color: #687687;">]</span>
<span class="comment" style="box-sizing: border-box; color: #999988; font-style: italic;"># Reading the class names</span>
<span class="keyword" style="box-sizing: border-box; color: #990000; font-weight: bold;">library</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">readr</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">names</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">read_delim</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="string" style="box-sizing: border-box; color: #dd1144;">"imagenet_classes.txt"</span>, <span class="string" style="box-sizing: border-box; color: #dd1144;">"\t"</span>, <span class="identifier" style="box-sizing: border-box; color: black;">escape_double</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="literal" style="box-sizing: border-box; color: #990073;">FALSE</span>, <span class="identifier" style="box-sizing: border-box; color: black;">trim_ws</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="literal" style="box-sizing: border-box; color: #990073;">TRUE</span>,<span class="identifier" style="box-sizing: border-box; color: black;">col_names</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="literal" style="box-sizing: border-box; color: #990073;">FALSE</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span></code></pre>
<pre class="r" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="r" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="comment" style="box-sizing: border-box; color: #999988; font-style: italic;">### Graph</span>
<span class="keyword" style="box-sizing: border-box; color: #990000; font-weight: bold;">library</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">grid</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">g</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">rasterGrob</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">img1</span>, <span class="identifier" style="box-sizing: border-box; color: black;">interpolate</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="literal" style="box-sizing: border-box; color: #990073;">TRUE</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">text</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="string" style="box-sizing: border-box; color: #dd1144;">""</span>
<span class="keyword" style="box-sizing: border-box; color: #990000; font-weight: bold;">for</span> <span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">id</span> <span class="keyword" style="box-sizing: border-box; color: #990000; font-weight: bold;">in</span> <span class="identifier" style="box-sizing: border-box; color: black;">idx</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="paren" style="box-sizing: border-box; color: #687687;">{</span>
<span class="identifier" style="box-sizing: border-box; color: black;">text</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="identifier" style="box-sizing: border-box; color: black;">paste0</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">text</span>, <span class="identifier" style="box-sizing: border-box; color: black;">names</span><span class="paren" style="box-sizing: border-box; color: #687687;">[</span><span class="identifier" style="box-sizing: border-box; color: black;">id</span>,<span class="paren" style="box-sizing: border-box; color: #687687;">]</span><span class="paren" style="box-sizing: border-box; color: #687687;">[</span><span class="paren" style="box-sizing: border-box; color: #687687;">[</span><span class="number" style="box-sizing: border-box; color: #009999;">1</span><span class="paren" style="box-sizing: border-box; color: #687687;">]</span><span class="paren" style="box-sizing: border-box; color: #687687;">]</span>, <span class="string" style="box-sizing: border-box; color: #dd1144;">" "</span>, <span class="identifier" style="box-sizing: border-box; color: black;">round</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">probs</span><span class="paren" style="box-sizing: border-box; color: #687687;">[</span><span class="identifier" style="box-sizing: border-box; color: black;">id</span><span class="paren" style="box-sizing: border-box; color: #687687;">]</span>,<span class="number" style="box-sizing: border-box; color: #009999;">5</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>, <span class="string" style="box-sizing: border-box; color: #dd1144;">"\n"</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="paren" style="box-sizing: border-box; color: #687687;">}</span>
<span class="keyword" style="box-sizing: border-box; color: #990000; font-weight: bold;">library</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">ggplot2</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span>
<span class="identifier" style="box-sizing: border-box; color: black;">ggplot</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">data.frame</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">d</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="number" style="box-sizing: border-box; color: #009999;">1</span><span class="operator" style="box-sizing: border-box; color: #687687;">:</span><span class="number" style="box-sizing: border-box; color: #009999;">3</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">+</span> <span class="identifier" style="box-sizing: border-box; color: black;">annotation_custom</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="identifier" style="box-sizing: border-box; color: black;">g</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">+</span>
<span class="identifier" style="box-sizing: border-box; color: black;">annotate</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'text'</span>,<span class="identifier" style="box-sizing: border-box; color: black;">x</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="number" style="box-sizing: border-box; color: #009999;">0.05</span>,<span class="identifier" style="box-sizing: border-box; color: black;">y</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="number" style="box-sizing: border-box; color: #009999;">0.05</span>,<span class="identifier" style="box-sizing: border-box; color: black;">label</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="identifier" style="box-sizing: border-box; color: black;">text</span>, <span class="identifier" style="box-sizing: border-box; color: black;">size</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="number" style="box-sizing: border-box; color: #009999;">7</span>, <span class="identifier" style="box-sizing: border-box; color: black;">hjust</span> <span class="operator" style="box-sizing: border-box; color: #687687;">=</span> <span class="number" style="box-sizing: border-box; color: #009999;">0</span>, <span class="identifier" style="box-sizing: border-box; color: black;">vjust</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="number" style="box-sizing: border-box; color: #009999;">0</span>, <span class="identifier" style="box-sizing: border-box; color: black;">color</span><span class="operator" style="box-sizing: border-box; color: #687687;">=</span><span class="string" style="box-sizing: border-box; color: #dd1144;">'blue'</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">+</span> <span class="identifier" style="box-sizing: border-box; color: black;">xlim</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">0</span>,<span class="number" style="box-sizing: border-box; color: #009999;">1</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> <span class="operator" style="box-sizing: border-box; color: #687687;">+</span> <span class="identifier" style="box-sizing: border-box; color: black;">ylim</span><span class="paren" style="box-sizing: border-box; color: #687687;">(</span><span class="number" style="box-sizing: border-box; color: #009999;">0</span>,<span class="number" style="box-sizing: border-box; color: #009999;">1</span><span class="paren" style="box-sizing: border-box; color: #687687;">)</span> </code></pre>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://dl.dropboxusercontent.com/u/9154523/blog/Image_Classification_in%20_R/unnamed-chunk-9-1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="228" src="https://dl.dropboxusercontent.com/u/9154523/blog/Image_Classification_in%20_R/unnamed-chunk-9-1.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: justify;">
Now since we can load trained models, we can do many cool things like transfer learning etc. More maybe another time.</div>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<br /></div>
</div>
</h1>
</div>
Anonymoushttp://www.blogger.com/profile/00328054330052543625noreply@blogger.com97tag:blogger.com,1999:blog-8665486246063353979.post-70156912685598094662013-06-04T03:02:00.000-07:002015-05-06T06:45:04.602-07:00Collecting geocoded tweets with R and Java<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEja36SU6vXtl5eDBVkBd1sG6GAWQ3thszkFyusIf1lV5QWGEQWffrs_zKwnF_t463cnw8OlSt9Iw_zXX8BZ-Ra-tE-NUYvA9niaIl9W5f9LmuHrPQXjfmZPt0rj-0FJ-kZJp5TogbMlAA/s1600/NumberOfTweets.png" imageanchor="1" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" height="158" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEja36SU6vXtl5eDBVkBd1sG6GAWQ3thszkFyusIf1lV5QWGEQWffrs_zKwnF_t463cnw8OlSt9Iw_zXX8BZ-Ra-tE-NUYvA9niaIl9W5f9LmuHrPQXjfmZPt0rj-0FJ-kZJp5TogbMlAA/s320/NumberOfTweets.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Number of tweets in different languages posted <br />
around Germany</td></tr>
</tbody></table>
There are many thing one can do with tweets (sentiment analysis, <a href="http://randomthoughtsonr.blogspot.ch/2013/06/creating-zoomable-map-of-tweets-with-r.html">maps</a>, ...). This entry shows you how you can access the publicly available API using Java and how to analyse the data using R. For my purpose I am collecting geocoded tweets around Germany. To collect the tweets I wrote a little java program (see below) which uses the twitter library <a href="http://twitter4j.org/en/">twitter4J</a>. This can be run on a machine in the background. Here is the java program which collects the tweets and stores them to disk. Except about 1 GB per week.<br />
<br />
<br />
<h4>
Collecting the geocoded tweets</h4>
<script src="https://gist.github.com/oduerr/5704388.js"></script>
Using this script I collected approx 1.3 Mio tweets in a weeks. The tweets are stored one line per tweet and one file per hour e.g. 2013-05-21T19_51_03.json. The content of the file would look like:<br />
<br />
<tt>
<span style="white-space: nowrap;">
{"created_at":"Tue May 21 17:51:09 +0000 2013","id":336901993555709952,"id_str":"336901993555709952","text":"@OmegaBlue69 ...
</span>
<span style="white-space: nowrap;">
{"created_at":"Tue May 21 17:51:10 +0000 2013","id":336901996680450048,"id_str":"336901996680450048","text":"Sweet1 ....</span></tt><br />
<tt><span style="white-space: nowrap;"><br /></span></tt><tt><span style="white-space: nowrap;"></span></tt>
<br />
<h4>
Handling the json-file</h4>
<br />
The first task extracts the relevant information from these files. The following script reads the json files line by line and writes the coordinates, languages and for each tweet to a text-file e.g. "2013-05-21T19_51_03.coords.txt" using rjson.<br />
<script src="https://gist.github.com/oduerr/5704496.js"></script>
<br />
<h4>
Putting it all together</h4>
The next script picks up all text files with coordinate information, merges infrequent levels and does the color-coding. Finally it creates a simple barplot and stores the data in a data.frame <span style="font-family: Courier New, Courier, monospace;">all.data</span> and the colors in a vector <span style="font-family: Courier New, Courier, monospace;">cols</span><br />
<script src="https://gist.github.com/oduerr/5704496.js"></script>
In another <a href="http://randomthoughtsonr.blogspot.ch/2013/06/creating-zoomable-map-of-tweets-with-r.html">blog </a>I describe how this data can be used to create a zoomable map.Anonymoushttp://www.blogger.com/profile/00328054330052543625noreply@blogger.com0tag:blogger.com,1999:blog-8665486246063353979.post-38259230624725420762013-06-03T14:00:00.000-07:002013-06-04T07:19:26.532-07:00Creating a zoomable map of tweets with R<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><h2>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCU0WccHvnVd9zCeoEdBRfPouT6UPen6iASMsLfXhEH4rgZVvRkBosNXkd-VuSwIdqBvThHMsBUs3QHnQPk7ZiKq3JToTepAwEBqxlhEU6AH3zwhYteBSJ_sB3Im1mqdxOoHhtMRj8dw/s1600/TwitterMap.png" imageanchor="1" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" height="318" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCU0WccHvnVd9zCeoEdBRfPouT6UPen6iASMsLfXhEH4rgZVvRkBosNXkd-VuSwIdqBvThHMsBUs3QHnQPk7ZiKq3JToTepAwEBqxlhEU6AH3zwhYteBSJ_sB3Im1mqdxOoHhtMRj8dw/s1600/TwitterMap.png" width="320" /></a></h2>
</td></tr>
<tr><td class="tr-caption" style="text-align: center;"><div style="text-align: left;">
Languages tweeted around Germany: red, blue, green, </div>
<div style="text-align: left;">
yellow, grey are for German, French, English, Dutch and </div>
<div style="text-align: left;">
other respectively. See <a href="https://home.zhaw.ch/~dueo/map/index.html">here</a> for a zoomable version.</div>
</td></tr>
</tbody></table>
Motivated by the project<a href="http://www.guardian.co.uk/news/datablog/interactive/2013/feb/21/twitter-languages-new-york-mapped"> twitter languages of New York</a> I wanted to do map of tweets too.<br />
For a different purpose (sentiment analysis) I am collecting tweets around Germany anyway. In another blog entry I <a href="http://randomthoughtsonr.blogspot.ch/2013/06/collecting-geocoded-tweets-with-r-and.html">describe</a> how I collected the data.<br />
<br />
With the package openmap, it is easy to create a map of the languages tweeted. Such a map is shown on the left. However, in post-google-map times if the user sees a map, he just starts to spin his mouse wheel to zoom into the map. Lets see how one can create such a map.<br />
<br />
<h4>
Creating the map</h4>
I assume that the data is in the following format, I need coordinates and colors. See the <a href="http://randomthoughtsonr.blogspot.ch/2013/06/collecting-geocoded-tweets-with-r-and.html">blog</a> how to extract the data from the json-files and do the color coding.<br />
<br />
<span style="background-color: #cfe2f3;"><tt>>head(all.data)#The lat/long data approx. 300k<br />
long lat lang<br />
1 4.901844 52.37762 en<br />
2 6.255914 52.51602 nl<br />
3 13.736128 51.04736 en<br />
</tt>
</span><br />
<tt><span style="background-color: #cfe2f3;">....</span></tt><br />
<span style="background-color: #cfe2f3;"><tt><br /></tt>
<span style="font-family: monospace;">> head(cols)#The colors (appox. 300k)</span></span><br />
<span style="background-color: #cfe2f3; font-family: monospace;">[1] "#FF000080" "#00FF0080" "#0000FF80" ...</span><br />
<br />
Zoomable maps are created with socalled tiles. You start with one resolution (zoomlevel), say zoomlevel 4. You create one tile. In the next zoom level this tile is split in 4 tiles and so on. These tiles are have to be placed in a certain directory structure. To create a zoomable map you simply have to create this directory structure with those tiles. There are plenty of javascript libraries with take all those tiles and create a map. I use one called leaflet (see below). Usually these libraries require 256x256 png images.<br />
<br />
So lets see how this works. The region of the above figure and zoom-level 4 corresponds to the path 4/8/5.png. For the next zoom level 5 one has to create the 4 files 5/16/10.png, 5/16/11.png, 5/17/10.png and 5/17/12.png. The directory corresponds to x-axis (longitude) and the file name to the y-axis (latitude), the figure below shows how the split is done.<br />
<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdyMqyDwR6qj5860U7RWLvJ0dHb1uF2zQf2EqBNAPXcB4qjSLyf4DKn9XUVfcBsk5oxNrwb2pEVHRxFVpsaHtKdhUE8Rvkj4WA1rLbRWns_2miFV5ecyas8Rp1ptkX9e8yVGBMsN-qUA/s1600/tile_explanation.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="252" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdyMqyDwR6qj5860U7RWLvJ0dHb1uF2zQf2EqBNAPXcB4qjSLyf4DKn9XUVfcBsk5oxNrwb2pEVHRxFVpsaHtKdhUE8Rvkj4WA1rLbRWns_2miFV5ecyas8Rp1ptkX9e8yVGBMsN-qUA/s1600/tile_explanation.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">The tile on the left is split into 4 tiles shown on the right.</td></tr>
</tbody></table>
In the next zoom level we would have to render 16 images starting with 6\32\20.png.<br />
<br />
The script below starts with the tile 4\8\5.png and recursively creates the images up to the desired zoom levels. To get the bounding boxes in terms of longitude and latitude the function <span style="font-family: Courier New, Courier, monospace;">tile2boundingBox </span>is used. The maps are obtained using the <span style="font-family: Courier New, Courier, monospace;">map </span>function of the <span style="font-family: Courier New, Courier, monospace;"><a href="http://cran.r-project.org/web/packages/OpenStreetMap/index.html">OpenStreetMap </a></span>package. The points have to be transformed using <span style="font-family: Courier New, Courier, monospace;">projectMercator(lat = sd$lat, lon = sd$lon)</span><span style="font-family: inherit;">and can then be drawn using e.g. the points function.</span><br />
<span style="font-family: inherit;"><br /></span>
Once you have created the tiles, its just a few lines of javascript to have the maps ready. See below for the index.html file using leaftlet library.<br />
<br />
<h4>
The main file creating the tile</h4>
<script src="https://gist.github.com/Oliver4242/5700613.js"></script>
<br />
<h4>
The java script part</h4>
<script src="https://gist.github.com/Oliver4242/6fcaf7965736c0f3f8d5.js"></script>
<br />
<h3>
</h3>
Anonymoushttp://www.blogger.com/profile/00328054330052543625noreply@blogger.com6Schweiz47.472662868613419 8.73413085937546.784044868613421 7.443237359375 48.161280868613417 10.025024359375