12.3 Convolutional Neural Network

There are some challenges using a feedforward neural network to solve computer vision problems. One of the challenges is that the inputs can get really big after you vectorize the image array. A 64 x 64 x 3 image gives you an input vector of 12288! And that is a very small image. So you can expect the number of parameters grows fast and it is difficult to get enough data to fit the model. Also as the input image size grows, the computational requirements to train a feedforward neural network will soon become infeasible. Also, after vectorization, you lose most of the spacial information of the image. To overcome these, people instead use the convolutional neural network for computer vision problems.

This section introduces the Convolutional Neural Network (CNN), the deep learning model that is almost universally used in computer vision applications. Computer vision has been advancing rapidly which enables many new applications such as self-driving cars and unlocking a phone using face. The application is not limited to the tech industry but some traditional industries such as agriculture and healthcare. Precision agriculture utilizes advanced hardware and computer vision algorithms to increase efficiency and reduce costs for farmers. For example, analyze images from cameras in the greenhouse to track plant growth state. Use sensory data from drones, satellites, and tractors to track soil conditions, detect herbs and pests, automate irrigation, etc. In health care, computer vision helps clinicians to diagnose disease, identify cancer sites with high accuracy (Kwak and Hui 2019). Even if you don’t work on computer vision, you may find some of the ideas inspiring and borrow them into your area.

Some popular computer vision problems are:

  • Image classification (or image recognition): Recognize the object in the image. Is there a cat in the image? Who is this person?
  • Object detection: Detect the position and boarder of a specific object. For example, if you are building a self-driving car, you need to know the positions of other cars around you.
  • Neural Style Transfer (NST): Given a “content” image and a “style” image, generate an image that merges the two.

12.3.1 Convolution Layer

A fundamental building block of the convolution neural network is, as the name indicates, the convolution operation. In this chapter, we illustrate the fundamentals of CNN using the example of image classification.

How do you do convolution? For example, you have a 5 x 5 2-d image (figure 12.9. You apply a 3 x 3 filter and convolute over the image. The output of this convolution operator will be a 3 x 3 matrix, which you can consider as a 3 x 3 image and visualize it (top right of figure 12.9).

There are an input image (left), a filter (middel), and an output image (right).

FIGURE 12.9: There are an input image (left), a filter (middel), and an output image (right).

You start from the top left corner of the image and put the filter on the top left 3 x3 sub-matrix of the input image and take the element-wise product. Then you add up the 9 numbers. Move forward one step each time until it gets to the bottom right. The detailed process is shown in figure 12.10.

Convolution step by step

FIGURE 12.10: Convolution step by step

Let’s use edge detection as an example to see how convolution operation works. Given a picture as the left of figure 12.11, you want to detect the vertical lines. For example, there are vertical lines along with the hair and the edges of the bookcase. How do you do that? There are standard filters for operations like blurring, sharpening, and edge detection. To get the edge, you can use the following 3 x 3 filter to convolute over the image.

##      [,1] [,2] [,3]
## [1,]    1    0   -1
## [2,]    1    0   -1
## [3,]    1    0   -1

The following code implements the convolution process. The result is shown as the middle of figure 12.11.

Edge Detection Example

FIGURE 12.11: Edge Detection Example

Why can kernel_vertical detect vertical edge? Let’s look at a simpler example. The following 8 x 8 matrix where half of the matrix is 10 and the other half is 0. The corresponding image is shown as the left of the figure 12.12.

##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]  200  200  200  200    0    0    0    0
## [2,]  200  200  200  200    0    0    0    0
## [3,]  200  200  200  200    0    0    0    0
## [4,]  200  200  200  200    0    0    0    0
## [5,]  200  200  200  200    0    0    0    0
## [6,]  200  200  200  200    0    0    0    0
## [7,]  200  200  200  200    0    0    0    0
## [8,]  200  200  200  200    0    0    0    0

If we use the above filter kernel_vertical , the output matrix is shown below and the corresponding image is shown as the right of the figure 12.12.

##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    0    0  200  200    0    0
## [2,]    0    0  200  200    0    0
## [3,]    0    0  200  200    0    0
## [4,]    0    0  200  200    0    0
## [5,]    0    0  200  200    0    0
## [6,]    0    0  200  200    0    0
Simple Edge Detection Example

FIGURE 12.12: Simple Edge Detection Example

So the output image has a lighter region in the middle that corresponds to the vertical edge of the input image. When the input image is large, such as the image in figure 12.11 is 1020 x 711, the edge will not seem as thick as it is in this small example. To detect the horizontal edge, you only need to rotate the filter by 90 degrees. The right image in figure 12.11 shows the horizontal edge detection result. You can see how convolution operator detects a specific feature from the image.

The parameters for the convolution operation are the elements in the filter. For a 3x3 filter shown below, the parameters to estimate are \(w_1\) to \(w_9\). So far, we move the filter one step each time when we convolve. You can do more than 1 step as well. For example, you can hop 2 steps each time after the sum of the element-wise product. It is called strided-convolution. Use stride \(s\) means the output is downsized by a factor of \(s\). It is rarely used in practice but it is good to be familiar with the concept.

Convolution Layer Parameters

FIGURE 12.13: Convolution Layer Parameters

12.3.2 Padding Layer

Assume the stride is \(s\) and we have a \(n \times n\) input image to convolve with a \(f \times f\) filter, the output image is \((\frac{n-f}{s} + 1) \times (\frac{n-f}{s} + 1)\). After each convolution, the dimension of the output shrinks. Depending on the size of the input image, the output size may get too small after a few rounds. Also, the pixel at the corner is used less than the pixel in the middle. So it overlooks some information in the image. To overcome these problems, you can add a padding layer during the process. To keep the output the same size as the input image in figure 12.13, you can pad two pixels on each side of the image with 0 (figure 12.14 ).

Padding Layer

FIGURE 12.14: Padding Layer

If the stride is \(s\) and we have a \(n \times n\) input image to convolve with a \(f \times f\) filter. This time we pad \(p\) pixels in each side, then the output size becomes \((\frac{n + 2p -f}{s} + 1) \times (\frac{n + 2p -f}{s} + 1)\). You can specify the value for p and also the pixel value used. Or you can just use 0 to pad and make the output the same size with input.

12.3.3 Pooling Layer

People sometimes use the pooling layer to reduce the size of the representation and make some of the feature detection more robust. If you have a \(4 \times 4\) input, the max and mean pooling operation are shown in the figure 12.15. The process is quite simple. In the example, the filter is \(2 \times 2\) and stride is 2, so break the input into four \(2 \times 2\) regions (shown in the figure with different shaded colors). For max pooling, each of the outputs is the maximum from the corresponding shaded sub-region. Mean pooling layer works in the same way except for getting the mean instead of maximum of the sub-region. The pooling layer has hyperparameters (\(f\) and \(s\)) but it has no parameter for gradient descent to learn.

Pooling Layer

FIGURE 12.15: Pooling Layer

Let’s go through an example of pooling a 2D grayscale image. Hope it gives you some intuition behind what it does. Read the image and convert the original color image (a 3D array) to grayscale (a 2D matrix).

The following function takes an image matrix or array, and apply pooling operation.

Let’s apply both max and mean pooling with filter size 10 (\(f = 10\)) and stride 10 (\(s = 10\)).

You can see the result by plotting the output image (figure 12.16). The top left is the original color picture. The top right is the 2D grayscale picture. The bottom left is the result of max pooling. The bottom right is the result of mean pooling. The max-pooling gives you the value of the largest pixel and the mean-pooling gives the average of the patch. You can consider it as a representation of features, looking at the maximal or average presence of different features. In general, max-pooling works better. You can gain some intuition from the example (figure 12.16). The max-pooling “picks” more distinct features and average-pooling blurs out features evenly.

Example of max and mean pooling

FIGURE 12.16: Example of max and mean pooling

12.3.4 Convolution Over Volume

So far, we have shown different types of layers on 2D inputs. If you have a 3D input (such as a color image), then the filters will have 3 channels too. For example, if you have a \(6 \times 6\) color image, the input dimension is \(6 \times 6 \times 3\). We call them the height, width, and the number of channels. The filter itself has 3 channels corresponding to the red, green, and blue channels of the input. You can consider it as a 3D cube with 27 parameters. Apply each channel of the filter to the corresponding channel of the input. Multiply each of the 27 numbers with the corresponding numbers from the top left region of the color input image and add them up. Add a bias parameter and apply an activation function which gives you the first number of the output image. Then slide it over to calculate the next one. The final output is 2D \(4 \times 4\). If you want to detect features in the red channel only, you can use a filter with the second and third channels to be all 0s. With different choices of the parameters, you can get different feature detectors. You can use more than one filter and each filter has multiple channels. For example, you can use one \(3 \times 3 \times 3\) filter to detect the horizontal edge and another to detect the vertical edge. Figure 12.16 shows an example of one layer with two filters. Each filter has a dimension of \(3 \times 3 \times 3\). The output dimension is \(4 \times 4 \times 2\). The output has two channels because we have two filters on the layer. The total number of parameters is 58 (each filter has one bias parameter \(b\)).

Example of convolution over volume

FIGURE 12.17: Example of convolution over volume

We use the following notations for layer \(l\):

  • Use \(n_W^{[l]}\), \(n_H^{[l]}\) and \(n_C^{[l]}\) to denote the width, height and number of channels of the input
  • \(f^{[l]}\) is the filter size
  • \(p^{[l]}\) is the padding size
  • \(s^{[l]}\) is the stride

The number of filters for layer \(l\) is the number of channels of the output of layer \(l\). Since the output of layer \(l\) is the input of layer \(l+1\), the number of filters for layer \(l\) is \(n_C^{[l+1]}\). The number of channels of the filter of layer \(l\) should be equal to the number of channels of the input of layer \(l\) which is the output of layer \(l-1\) (i.e. \(n_C^{[l-1]}\)). So the dimensions of some key elements of layer \(l\) are:

  • Each filter: \(f^{[l]} \times f^{[l]} \times n^{[l-1]}_C\)
  • Activations: \(a^{[l]} \rightarrow n_H^{[l]} \times n_W^{[l]} \times n_C^{[l]}\)
  • Weights: \(f^{[l]} \times f^{[l]} \times n^{[l-1]}_C \times n^{[l]}_C\)
  • bias: \(b^{[l]}_C\)
  • Input: \(n^{[l-1]}_H \times n^{[l-1]}_W \times n^{[l-1]}_C\)
  • Output: \(n^{[l]}_H \times n^{[l]}_W \times n^{[l]}_C\)

After a series of 3D convolutional layers, we need to ‘flatten’ the 3D tensor to a 1D tensor, and add one or several dense layers to connect the output to the response variable.

Now you know the basic building blocks of CNN. Let’s look at how to use the keras R package to solve the same handwritten digits image recognition problem as in section 12.2.7. You will see the CNN is better at handling image recognition problem.

12.3.5 Image Recognition Using CNN

CNN leverages the relationship among neighbor pixels in the 2D image for better performance. It also avoids generating thousands or millions of features for high resolution images with full color. Now let’s import the MNIST dataset again as we have done some preprocessing specifically for FFNN before. CNN requires different preprocessing steps. Let’s start with a few parameters to be used later.

12.3.5.1 Data preprocessing

For CNN, the input is a \(n_H \times n_W \times n_C\) 3D array with \(n_C\) channels. For example, a greyscale \(n_H \times n_W\) image has only one channel, and the input is \(n_H \times n_W \times 1\) tensor. A \(n_H \times n_W\) 8-bit per channel RGB image has three channels with 3 \(n_H \times n_W\) array with values between 0 and 255, so the input is \(n_H \times n_W \times 3\) tensor. For the problem that we have now, the image is greyscaled, but we need to specifically define there is one channel by reshaping the 2D array into 3D tensor using array_reshape(). The input_shape variable will be used in the CNN model later. For an RGB color image, the number of channels is 3 and we need to replace “1” with “3” for the code cell below if the input image is RGB format.

Here is the structure of the reshaped image, the first dimension is the image index, the 2-4 dimension is a 3D tensor even though there is only one channel.

Same as the FFNN model, we scale the input values to be between 0 and 1 for the same numerical stability consideration in the optimization process.

Encode the response variable to binary vectors.

12.3.5.2 Fit model

CNN model contains a series of 3D convolutional layers which contains a few parameters:

  1. the kernal_size which is typically 3x3 or 5x5;

  2. the number of filters, which is equal to the number of channels of the output;

  3. activation function.

For the first layer, there is also an input_shape parameter which is the input image size and channel. To prevent overfitting and speed up computation, a pooling layer is usually applied after one or a few convolutional layers. A maximum pooling layer with pool_size = 2x2 reduces the size to half. Dropout can be used as well in addition to pooling neighbor values. After a few 3D convolutional layers, we also need to ‘flatten’ the 3D tensor output into 1D tensor, and then add one or a couple of dense layers to connect the output to the target response classes.

Let’s define the CNN model structure. Now we define a CNN model with two convolutional layers, two max-pooling layers, and two dropout layers to mediate overfitting. There are three dense layers after flattening the 3D tensor. The last layer is a dense layer that connects to the response.

Model: "sequential_5"
____________________________________________________________________________________________
Layer (type)                             Output Shape                         Param #       
============================================================================================
conv2d_11 (Conv2D)                       (None, 24, 24, 32)                   832           
____________________________________________________________________________________________
max_pooling2d_9 (MaxPooling2D)           (None, 12, 12, 32)                   0             
____________________________________________________________________________________________
conv2d_12 (Conv2D)                       (None, 8, 8, 64)                     51264         
____________________________________________________________________________________________
max_pooling2d_10 (MaxPooling2D)          (None, 4, 4, 64)                     0             
____________________________________________________________________________________________
dropout_6 (Dropout)                      (None, 4, 4, 64)                     0             
____________________________________________________________________________________________
flatten_5 (Flatten)                      (None, 1024)                         0             
____________________________________________________________________________________________
dense_15 (Dense)                         (None, 120)                          123000        
____________________________________________________________________________________________
dropout_7 (Dropout)                      (None, 120)                          0             
____________________________________________________________________________________________
dense_16 (Dense)                         (None, 84)                           10164         
____________________________________________________________________________________________
dense_17 (Dense)                         (None, 10)                           850           
============================================================================================
Total params: 186,110
Trainable params: 186,110
Non-trainable params: 0
____________________________________________________________________________________________

Similar to before, we need to compile the defined CNN model.

Train the model and save each epochs’s history. Please note, as we are not using GPU, it takes a few minutes to finish. Please be patient while waiting for the results. The training time can be significantly reduced if running on GPU.

The trained model accuracy can be evaluated on the testing dataset which is pretty good.

References

Kwak, Gloria Hyun Jung, and Pan Hui. 2019. “DeepHealth: Deep Learning for Health Informatics Reviews, Challenges, and Opportunities on Medical Imaging, Electronic Health Records, Genomics, Sensing, and Online Communication Health.” 2019.