<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/a-guide-to-gpu-accelerated-ship-recognition-in-satellite-imagery-using-keras-and-r-part-i/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://appsilondatascience.com/blog/rstats/2018/01/16/keras.html"> Appsilon Data Science Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Problem overview</h2>
<p>This article will be divided into a series of posts. In this post, I will explain the basic concepts behind <strong>convolutional neural networks</strong> and how to build them using <strong>Keras</strong>. In the next post, I will focus on improving the performance of the network.</p>
<p>Artificial Intelligence or AI has exploded in popularity both in business as in society. Companies large and small are redirecting their digital transformation to include technologies that are the true representation of what AI currently is; namely, deep learning. Deep learning is a subset of machine learning, which more generally, falls into data science. Both machine learning and deep learning find themselves at the peak of <a rel="nofollow" href="https://www.gartner.com/smarterwithgartner/top-trends-in-the-gartner-hype-cycle-for-emerging-technologies-2017/" target="_blank">2017’s Gartner Hype Cycle</a> and are already making a huge impact on the current technological status quo. Let’s take a look at one way of going about creating a basic machine learning model.</p>
<h2>What is TensorFlow and Keras ?</h2>
<p><a rel="nofollow" href="https://www.tensorflow.org/" target="_blank"><strong>TensorFlow</strong></a> is an open-source software library for Machine Intelligence that allows you to deploy computations to multiple CPUs or GPUs. It was developed by researchers and engineers working on the Google Brain Team.</p>
<p><a rel="nofollow" href="https://keras.io/" target="_blank"><strong>Keras</strong></a> is a high-level neural networks API capable of running on top of multiple back-ends including: TensorFlow, CNTK, or Theano. One of its biggest advantages is its “user friendliness”. With Keras you can easily build advanced models like convolutional or recurrent neural network.</p>
<p>To install TensorFlow and Keras from R use <strong>install_keras()</strong> function. If you want to use the GPU version you have to install some prerequisites first. This could be difficult but it is worth the extra effort when dealing with larger and more elaborate models. I strongly recommend you to do this! You can read more <a rel="nofollow" href="https://tensorflow.rstudio.com/installation_gpu.html#prerequisites" target="_blank">here</a>.</p>
<figure>
<pre><code data-lang="r"><span>install.packages</span><span>(</span><span>&quot;keras&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>keras</span><span>)</span><span>
</span><span># Make sure to install required prerequisites, before installing Keras using the commands below:
</span><span>install_keras</span><span>()</span><span> </span><span># CPU version
</span><span>install_keras</span><span>(</span><span>tensorflow</span><span> </span><span>=</span><span> </span><span>&quot;gpu&quot;</span><span>)</span><span> </span><span>#</span><span> </span><span>GPU</span><span> </span><span>version</span></code></pre>
</figure>
<h2>Data preparation</h2>
<p>For the task we will use a <a rel="nofollow" href="https://www.kaggle.com/rhammell/ships-in-satellite-imagery/data" target="_blank">dataset of 2800 satellite pictures from Kaggle</a>. Every row contains information about one photo (80-pixel height, 80-pixel width, 3 colors – RGB color space). To input data into a Keras model, we need to transform it into a 4-dimensional array (index of sample, height, width, colors). Every picture is associated with a label that could be equal <strong>1</strong> for a <strong>ship</strong> and <strong>0</strong> for <strong>non-ship</strong> object. Also here we have to use some transformations to create a binary matrix for Keras.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>keras</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>jsonlite</span><span>)</span><span>
</span><span>library</span><span>(</span><span>abind</span><span>)</span><span>
</span><span>library</span><span>(</span><span>gridExtra</span><span>)</span><span>
</span><span>ships_json</span><span> </span><span>&lt;-</span><span> </span><span>fromJSON</span><span>(</span><span>&quot;ships_images/shipsnet.json&quot;</span><span>)[</span><span>1</span><span>:</span><span>2</span><span>]</span><span>
</span><span>ships_data</span><span> </span><span>&lt;-</span><span> </span><span>ships_json</span><span>$</span><span>data</span><span> </span><span>%>%</span><span>
</span><span>apply</span><span>(</span><span>.</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>{</span><span>
</span><span>r</span><span> </span><span>&lt;-</span><span> </span><span>matrix</span><span>(</span><span>x</span><span>[</span><span>1</span><span>:</span><span>6400</span><span>],</span><span> </span><span>80</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>byrow</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>/</span><span> </span><span>255</span><span>
</span><span>g</span><span> </span><span>&lt;-</span><span> </span><span>matrix</span><span>(</span><span>x</span><span>[</span><span>6401</span><span>:</span><span>12800</span><span>],</span><span> </span><span>80</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>byrow</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>/</span><span> </span><span>255</span><span>
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>matrix</span><span>(</span><span>x</span><span>[</span><span>12801</span><span>:</span><span>19200</span><span>],</span><span> </span><span>80</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>byrow</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>/</span><span> </span><span>255</span><span>
</span><span>list</span><span>(</span><span>array</span><span>(</span><span>c</span><span>(</span><span>r</span><span>,</span><span>g</span><span>,</span><span>b</span><span>),</span><span> </span><span>dim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>80</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>3</span><span>)))</span><span>
</span><span>})</span><span> </span><span>%>%</span><span>
</span><span>do.call</span><span>(</span><span>c</span><span>,</span><span> </span><span>.</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>abind</span><span>(</span><span>.</span><span>,</span><span> </span><span>along</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>aperm</span><span>(</span><span>c</span><span>(</span><span>4</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>))</span><span>
</span><span>ships_labels</span><span> </span><span>&lt;-</span><span> </span><span>ships_json</span><span>$</span><span>labels</span><span> </span><span>%>%</span><span>
</span><span>to_categorical</span><span>(</span><span>2</span><span>)</span><span>
</span><span>rm</span><span>(</span><span>ships_json</span><span>)</span><span>
</span><span>dim</span><span>(</span><span>ships_data</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>[</span><span>1</span><span>]</span><span> </span><span>2800</span><span> </span><span>80</span><span> </span><span>80</span><span> </span><span>3</span></code></pre>
</figure>
<p>Now we can take a look at some sample of our data. Notice that if a ship appeared partially on a picture, then it wasn’t labeled as a 1.</p>
<figure>
<pre><code data-lang="r"><span>xy_axis</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>expand.grid</span><span>(</span><span>1</span><span>:</span><span>80</span><span>,</span><span> </span><span>80</span><span>:</span><span>1</span><span>)[,</span><span> </span><span>1</span><span>],</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>expand.grid</span><span>(</span><span>1</span><span>:</span><span>80</span><span>,</span><span> </span><span>80</span><span>:</span><span>1</span><span>)[,</span><span> </span><span>2</span><span>])</span><span>
</span><span>set.seed</span><span>(</span><span>1111</span><span>)</span><span>
</span><span>sample_plots</span><span> </span><span>&lt;-</span><span> </span><span>sample</span><span>(</span><span>1</span><span>:</span><span>dim</span><span>(</span><span>ships_data</span><span>)[</span><span>1</span><span>],</span><span> </span><span>12</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>map</span><span>(</span><span>~</span><span> </span><span>{</span><span>
</span><span>plot_data</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>xy_axis</span><span>,</span><span> </span><span>r</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>t</span><span>(</span><span>ships_data</span><span>[</span><span>.x</span><span>,</span><span> </span><span>,</span><span> </span><span>,</span><span> </span><span>1</span><span>])),</span><span>
</span><span>g</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>t</span><span>(</span><span>ships_data</span><span>[</span><span>.x</span><span>,</span><span> </span><span>,</span><span> </span><span>,</span><span> </span><span>2</span><span>])),</span><span>
</span><span>b</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>t</span><span>(</span><span>ships_data</span><span>[</span><span>.x</span><span>,</span><span> </span><span>,</span><span> </span><span>,</span><span> </span><span>3</span><span>])))</span><span>
</span><span>ggplot</span><span>(</span><span>plot_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>rgb</span><span>(</span><span>r</span><span>,</span><span> </span><span>g</span><span>,</span><span> </span><span>b</span><span>)))</span><span> </span><span>+</span><span> </span><span>guides</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_identity</span><span>()</span><span> </span><span>+</span><span> </span><span>theme_void</span><span>()</span><span> </span><span>+</span><span> </span><span>geom_raster</span><span>(</span><span>hjust</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>ifelse</span><span>(</span><span>ships_labels</span><span>[</span><span>.x</span><span>,</span><span> </span><span>2</span><span>],</span><span> </span><span>&quot;Ship&quot;</span><span>,</span><span> </span><span>&quot;Non-ship&quot;</span><span>))</span><span>
</span><span>})</span><span>
</span><span>do.call</span><span>(</span><span>&quot;grid.arrange&quot;</span><span>,</span><span> </span><span>c</span><span>(</span><span>sample_plots</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>nrow</span><span> </span><span>=</span><span> </span><span>3</span><span>))</span></code></pre>
</figure>
<p><img alt="Sample data" src="https://i1.wp.com/appsilondatascience.com/blog/assets/article_images/2017-11-20-keras/samples.png?w=456" data-recalc-dims="1"></p>
<p>The last thing we have to do is to split our data into training and test sets.</p>
<figure>
<pre><code data-lang="r"><span>set.seed</span><span>(</span><span>1234</span><span>)</span><span>
</span><span>indexes</span><span> </span><span>&lt;-</span><span> </span><span>sample</span><span>(</span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>ships_labels</span><span>),</span><span> </span><span>0.7</span><span> </span><span>*</span><span> </span><span>nrow</span><span>(</span><span>ships_labels</span><span>))</span><span>
</span><span>train</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>ships_data</span><span>[</span><span>indexes</span><span>,</span><span> </span><span>,</span><span> </span><span>,</span><span> </span><span>],</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>ships_labels</span><span>[</span><span>indexes</span><span>,</span><span> </span><span>])</span><span>
</span><span>test</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>ships_data</span><span>[</span><span>-</span><span>indexes</span><span>,</span><span> </span><span>,</span><span> </span><span>,</span><span> </span><span>],</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>ships_labels</span><span>[</span><span>-</span><span>indexes</span><span>,</span><span> </span><span>])</span></code></pre>
</figure>
<h2>Modeling</h2>
<p>In Keras you can build models in 3 different ways using:</p>
<ol>
<li>a sequential model</li>
<li>functional API</li>
<li>pre-trained models</li>
</ol>
<p>For now, we will only use sequential models. But before that, we have to understand the basic concepts behind convolutional neural networks.</p>
<p><strong>Convolutional neural networks (CNN)</strong> or <strong>ConvNets</strong> are a class of deep, feed-forward artificial neural networks designed for solving problems like image/video/audio recognition, and object detection etc. The architecture of ConvNets differs depending on the issue, but there are some basic commonalities.</p>
<p><img alt="Typical CNN architecture" src="https://i0.wp.com/appsilondatascience.com/blog/assets/article_images/2017-11-20-keras/convnet.jpeg?w=456" data-recalc-dims="1"></p>
<p>The first type of layer in CNN’s is a <strong>convolutional layer</strong> and it is a core building block of ConvNets. Simply put, we take a small set of <strong>filters</strong> (also called <strong>kernels</strong>) and place them on part of our original image to get the dot product between kernels and corresponding image parts. Next, we move our filter to the next position and repeat this action. The number of pixels that we move the filters is called a <strong>stride</strong>. After getting the dot product for the whole image, we get a so-called <strong>activation map</strong>.</p>
<p><img alt="Convolution example" src="https://i2.wp.com/appsilondatascience.com/blog/assets/article_images/2017-11-20-keras/convolve.png?w=456" data-recalc-dims="1"></p>
<p>The second type of layer in CNN’s is the <strong>pooling layer</strong>. This layer is responsible for dimensionality reduction of activation maps. There are several types of pooling, but <strong>max pooling</strong> is most commonly used. As it was in the case of convolutional layers, we have some filter and strides. After placing the filter on an image part, we take the maximum value from that part and move to the next region by the number of pixels, specified as strides.</p>
<p><img alt="Max pooling example" src="https://i1.wp.com/appsilondatascience.com/blog/assets/article_images/2017-11-20-keras/maxpool.jpeg?w=456" data-recalc-dims="1"></p>
<p>The third type of layer in CNN’s is called the <strong>activation layer</strong>. In this layer, values from activation maps are transformed by some activation function. There are several functions to use but most common one is called a <strong>rectified linear unit (ReLU)</strong>.</p>
<p><img alt="ReLU function" src="https://i1.wp.com/appsilondatascience.com/blog/assets/article_images/2017-11-20-keras/relu.png?w=456" data-recalc-dims="1"></p>
<p>The fourth type of layer is called a <strong>densely (fully) connected layer</strong> which is a classical output layer known as a feed-forward neural networks. This fully connected layer is placed at the end of a ConvNet.</p>
<p>We begin by creating an empty sequential model</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>summary</span><span>(</span><span>model</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>_______________________________________________________________________________________</span><span>
</span><span>Layer</span><span> </span><span>(</span><span>type</span><span>)</span><span> </span><span>Output</span><span> </span><span>Shape</span><span> </span><span>Param</span><span> </span><span># </span><span>=======================================================================================</span><span>
</span><span>Total</span><span> </span><span>params</span><span>:</span><span> </span><span>0</span><span>
</span><span>Trainable</span><span> </span><span>params</span><span>:</span><span> </span><span>0</span><span>
</span><span>Non</span><span>-</span><span>trainable</span><span> </span><span>params</span><span>:</span><span> </span><span>0</span><span>
</span><span>_______________________________________________________________________________________</span></code></pre>
</figure>
<p>Now we can add some additional layers. Note that objects in Keras are <strong>modified in-place</strong> so there’s no need for consecutive assignment. In the first layer, we have to specify the shape of our data.</p>
<figure>
<pre><code data-lang="r"><span>model</span><span> </span><span>%>%</span><span>
</span><span># 32 filters, each size 3x3 pixels
</span><span> </span><span># ReLU activation after convolution
</span><span> </span><span>layer_conv_2d</span><span>(</span><span>
</span><span>input_shape</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>80</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>3</span><span>),</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> </span><span>3</span><span>),</span><span> </span><span>strides</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>1</span><span>),</span><span>
</span><span>activation</span><span> </span><span>=</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> </span><span>2</span><span>),</span><span> </span><span>strides</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</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> </span><span>3</span><span>),</span><span> </span><span>strides</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>1</span><span>),</span><span>
</span><span>activation</span><span> </span><span>=</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> </span><span>2</span><span>),</span><span> </span><span>strides</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</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>2</span><span>,</span><span> </span><span>activation</span><span> </span><span>=</span><span> </span><span>&quot;softmax&quot;</span><span>)</span><span>
</span><span>summary</span><span>(</span><span>model</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>_______________________________________________________________________________________</span><span>
</span><span>Layer</span><span> </span><span>(</span><span>type</span><span>)</span><span> </span><span>Output</span><span> </span><span>Shape</span><span> </span><span>Param</span><span> </span><span># </span><span>=======================================================================================</span><span>
</span><span>conv2d_1</span><span> </span><span>(</span><span>Conv2D</span><span>)</span><span> </span><span>(</span><span>None</span><span>,</span><span> </span><span>78</span><span>,</span><span> </span><span>78</span><span>,</span><span> </span><span>32</span><span>)</span><span> </span><span>896</span><span> </span><span>_______________________________________________________________________________________</span><span>
</span><span>max_pooling2d_1</span><span> </span><span>(</span><span>MaxPooling2D</span><span>)</span><span> </span><span>(</span><span>None</span><span>,</span><span> </span><span>39</span><span>,</span><span> </span><span>39</span><span>,</span><span> </span><span>32</span><span>)</span><span> </span><span>0</span><span> </span><span>_______________________________________________________________________________________</span><span>
</span><span>conv2d_2</span><span> </span><span>(</span><span>Conv2D</span><span>)</span><span> </span><span>(</span><span>None</span><span>,</span><span> </span><span>37</span><span>,</span><span> </span><span>37</span><span>,</span><span> </span><span>64</span><span>)</span><span> </span><span>18496</span><span> </span><span>_______________________________________________________________________________________</span><span>
</span><span>max_pooling2d_2</span><span> </span><span>(</span><span>MaxPooling2D</span><span>)</span><span> </span><span>(</span><span>None</span><span>,</span><span> </span><span>18</span><span>,</span><span> </span><span>18</span><span>,</span><span> </span><span>64</span><span>)</span><span> </span><span>0</span><span> </span><span>_______________________________________________________________________________________</span><span>
</span><span>flatten_1</span><span> </span><span>(</span><span>Flatten</span><span>)</span><span> </span><span>(</span><span>None</span><span>,</span><span> </span><span>20736</span><span>)</span><span> </span><span>0</span><span> </span><span>_______________________________________________________________________________________</span><span>
</span><span>dense_1</span><span> </span><span>(</span><span>Dense</span><span>)</span><span> </span><span>(</span><span>None</span><span>,</span><span> </span><span>2</span><span>)</span><span> </span><span>41474</span><span> </span><span>=======================================================================================</span><span>
</span><span>Total</span><span> </span><span>params</span><span>:</span><span> </span><span>60</span><span>,</span><span>866</span><span>
</span><span>Trainable</span><span> </span><span>params</span><span>:</span><span> </span><span>60</span><span>,</span><span>866</span><span>
</span><span>Non</span><span>-</span><span>trainable</span><span> </span><span>params</span><span>:</span><span> </span><span>0</span><span>
</span><span>_______________________________________________________________________________________</span></code></pre>
</figure>
<p>After building the architecture for our CNN, we have to configure it for training. We must specify the loss function, optimizer and additional metrics for evaluation. For example, we can use stochastic gradient descent as an optimization method and cross-entropy as a loss function.</p>
<figure>
<pre><code data-lang="r"><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_sgd</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>Finally, we are ready to fit the model but there is one more thing we can do. If we want to have a good and quick visualization of our results, we can run a visualization tool called TensorBoard.</p>
<figure>
<pre><code data-lang="r"><span>tensorboard</span><span>(</span><span>&quot;logs/ships&quot;</span><span>)</span><span>
</span><span>ships_fit</span><span> </span><span>&lt;-</span><span> </span><span>model</span><span> </span><span>%>%</span><span> </span><span>fit</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>train</span><span>[[</span><span>1</span><span>]],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>train</span><span>[[</span><span>2</span><span>]],</span><span> </span><span>epochs</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>batch_size</span><span> </span><span>=</span><span> </span><span>32</span><span>,</span><span>
</span><span>validation_split</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span>
</span><span>callbacks</span><span> </span><span>=</span><span> </span><span>callback_tensorboard</span><span>(</span><span>&quot;logs/ships&quot;</span><span>))</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>...</span><span>
</span><span>Epoch</span><span> </span><span>20</span><span>/</span><span>20</span><span>
</span><span>32</span><span>/</span><span>1567</span><span> </span><span>[</span><span>..............................</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.4627</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7812</span><span>
</span><span>160</span><span>/</span><span>1567</span><span> </span><span>[</span><span>==></span><span>...........................</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5256</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7500</span><span>
</span><span>288</span><span>/</span><span>1567</span><span> </span><span>[</span><span>====></span><span>.........................</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5268</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7431</span><span>
</span><span>448</span><span>/</span><span>1567</span><span> </span><span>[</span><span>=======></span><span>......................</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5401</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7299</span><span>
</span><span>608</span><span>/</span><span>1567</span><span> </span><span>[</span><span>==========></span><span>...................</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5375</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7319</span><span>
</span><span>768</span><span>/</span><span>1567</span><span> </span><span>[</span><span>=============></span><span>................</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5389</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7305</span><span>
</span><span>896</span><span>/</span><span>1567</span><span> </span><span>[</span><span>================></span><span>.............</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5312</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7377</span><span>
</span><span>1056</span><span>/</span><span>1567</span><span> </span><span>[</span><span>===================></span><span>..........</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5259</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7453</span><span>
</span><span>1216</span><span>/</span><span>1567</span><span> </span><span>[</span><span>======================></span><span>.......</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5294</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7401</span><span>
</span><span>1376</span><span>/</span><span>1567</span><span> </span><span>[</span><span>=========================></span><span>....</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5217</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7471</span><span>
</span><span>1536</span><span>/</span><span>1567</span><span> </span><span>[</span><span>============================></span><span>.</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5191</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7507</span><span>
</span><span>1567</span><span>/</span><span>1567</span><span> </span><span>[</span><span>==============================</span><span>]</span><span> </span><span>-</span><span> </span><span>1</span><span>s</span><span> </span><span>484</span><span>us</span><span>/</span><span>step</span><span> </span><span>-</span><span> </span><span>loss</span><span>:</span><span> </span><span>0.5188</span><span> </span><span>-</span><span> </span><span>acc</span><span>:</span><span> </span><span>0.7511</span><span> </span><span>-</span><span> </span><span>val_loss</span><span>:</span><span> </span><span>0.5288</span><span> </span><span>-</span><span> </span><span>val_acc</span><span>:</span><span> </span><span>0.7449</span></code></pre>
</figure>
<p><img alt="TensorBoard" src="https://i1.wp.com/appsilondatascience.com/blog/assets/article_images/2017-11-20-keras/tb.png?w=456" data-recalc-dims="1"></p>
<p>The last thing to do is to get evaluation metrics and predictions form the test set.</p>
<figure>
<pre><code data-lang="r"><span>predicted_probs</span><span> </span><span>&lt;-</span><span> </span><span>model</span><span> </span><span>%>%</span><span>
</span><span>predict_proba</span><span>(</span><span>test</span><span>[[</span><span>1</span><span>]])</span><span> </span><span>%>%</span><span>
</span><span>cbind</span><span>(</span><span>test</span><span>[[</span><span>2</span><span>]])</span><span>
</span><span>head</span><span>(</span><span>predicted_probs</span><span>)</span><span>
</span><span>model</span><span> </span><span>%>%</span><span> </span><span>evaluate</span><span>(</span><span>test</span><span>[[</span><span>1</span><span>]],</span><span> </span><span>test</span><span>[[</span><span>2</span><span>]])</span><span>
</span><span>set.seed</span><span>(</span><span>1111</span><span>)</span><span>
</span><span>sample_plots</span><span> </span><span>&lt;-</span><span> </span><span>sample</span><span>(</span><span>1</span><span>:</span><span>dim</span><span>(</span><span>test</span><span>[[</span><span>1</span><span>]])[</span><span>1</span><span>],</span><span> </span><span>12</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>map</span><span>(</span><span>~</span><span> </span><span>{</span><span>
</span><span>plot_data</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>xy_axis</span><span>,</span><span> </span><span>r</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>t</span><span>(</span><span>test</span><span>[[</span><span>1</span><span>]][</span><span>.x</span><span>,</span><span> </span><span>,</span><span> </span><span>,</span><span> </span><span>1</span><span>])),</span><span>
</span><span>g</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>t</span><span>(</span><span>test</span><span>[[</span><span>1</span><span>]][</span><span>.x</span><span>,</span><span> </span><span>,</span><span> </span><span>,</span><span> </span><span>2</span><span>])),</span><span>
</span><span>b</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>t</span><span>(</span><span>test</span><span>[[</span><span>1</span><span>]][</span><span>.x</span><span>,</span><span> </span><span>,</span><span> </span><span>,</span><span> </span><span>3</span><span>])))</span><span>
</span><span>ggplot</span><span>(</span><span>plot_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>rgb</span><span>(</span><span>r</span><span>,</span><span> </span><span>g</span><span>,</span><span> </span><span>b</span><span>)))</span><span> </span><span>+</span><span> </span><span>guides</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_identity</span><span>()</span><span> </span><span>+</span><span> </span><span>theme_void</span><span>()</span><span> </span><span>+</span><span> </span><span>geom_raster</span><span>(</span><span>hjust</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>ifelse</span><span>(</span><span>test</span><span>[[</span><span>2</span><span>]][</span><span>.x</span><span>,</span><span> </span><span>2</span><span>],</span><span> </span><span>&quot;Ship&quot;</span><span>,</span><span> </span><span>&quot;Non-ship&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>&quot;Ship prob:&quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>predicted_probs</span><span>[</span><span>.x</span><span>,</span><span> </span><span>2</span><span>],</span><span> </span><span>6</span><span>)))</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>plot.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>hjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>))</span><span>
</span><span>})</span><span>
</span><span>do.call</span><span>(</span><span>&quot;grid.arrange&quot;</span><span>,</span><span> </span><span>c</span><span>(</span><span>sample_plots</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>nrow</span><span> </span><span>=</span><span> </span><span>3</span><span>))</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>[,</span><span>1</span><span>]</span><span> </span><span>[,</span><span>2</span><span>]</span><span> </span><span>[,</span><span>3</span><span>]</span><span> </span><span>[,</span><span>4</span><span>]</span><span>
</span><span>[</span><span>1</span><span>,]</span><span> </span><span>0.04486139</span><span> </span><span>0.95513862</span><span> </span><span>0</span><span> </span><span>1</span><span>
</span><span>[</span><span>2</span><span>,]</span><span> </span><span>0.92640823</span><span> </span><span>0.07359175</span><span> </span><span>0</span><span> </span><span>1</span><span>
</span><span>[</span><span>3</span><span>,]</span><span> </span><span>0.26848912</span><span> </span><span>0.73151088</span><span> </span><span>0</span><span> </span><span>1</span><span>
</span><span>[</span><span>4</span><span>,]</span><span> </span><span>0.51208550</span><span> </span><span>0.48791450</span><span> </span><span>0</span><span> </span><span>1</span><span>
</span><span>[</span><span>5</span><span>,]</span><span> </span><span>0.15906605</span><span> </span><span>0.84093398</span><span> </span><span>0</span><span> </span><span>1</span><span>
</span><span>[</span><span>6</span><span>,]</span><span> </span><span>0.66976833</span><span> </span><span>0.33023167</span><span> </span><span>0</span><span> </span><span>1</span><span>
</span><span>32</span><span>/</span><span>841</span><span> </span><span>[</span><span>></span><span>.............................</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span>
</span><span>384</span><span>/</span><span>841</span><span> </span><span>[</span><span>============></span><span>.................</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span>
</span><span>736</span><span>/</span><span>841</span><span> </span><span>[</span><span>=========================></span><span>....</span><span>]</span><span> </span><span>-</span><span> </span><span>ETA</span><span>:</span><span> </span><span>0</span><span>s</span><span>
</span><span>841</span><span>/</span><span>841</span><span> </span><span>[</span><span>==============================</span><span>]</span><span> </span><span>-</span><span> </span><span>0</span><span>s</span><span> </span><span>162</span><span>us</span><span>/</span><span>step</span><span>
</span><span>$</span><span>loss</span><span>
</span><span>[</span><span>1</span><span>]</span><span> </span><span>0.5235391</span><span>
</span><span>$</span><span>acc</span><span>
</span><span>[</span><span>1</span><span>]</span><span> </span><span>0.7502973</span></code></pre>
</figure>
<p><img alt="Test set probability" src="https://i2.wp.com/appsilondatascience.com/blog/assets/article_images/2017-11-20-keras/samples_test.png?w=456" data-recalc-dims="1"></p>
<p>As we can see, the model leaves room for improvement. It has a low accuracy (.075) and a high cross entropy loss (0.52). It is, however, a good introduction and start to Keras. We are going to explore ways of improving the network and achieving better results in part two. See you soon!</p>
<p>Read the original post at<br>
<a rel="nofollow" href="http://appsilondatascience.com/blog/rstats/2018/01/16/keras.html" target="_blank">Appsilon Data Science Blog</a>.</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://appsilondatascience.com/blog/rstats/2018/01/16/keras.html"> Appsilon Data Science 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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/wRcNXxv-c7c" height="1"></a></div>