Beginner’s guide to Understanding Convolutional Neural Networks
By: Junehao Ching
Tags: Deep Learning, Computer Vision, Convolutional Network, Data Science, Classification

Hey there! Since you clicked into this blog, means that you have heard of convolutional neural networks (CNN) but can’t really grasp the idea of it and are looking for more information about it, right? Let’s dive into this topic in a top-down manner — from the big picture to each component in the CNN.

Definition

A CNN is a class of deep, feed-forward (not recurrent) artificial neural networks that are applied to analyzing visual imagery.

Things to note before reading

Images are just matrices of pixel values. Channel is a conventional term used to refer to a certain component of an image. An image from a standard digital camera will have three channels — red, green, and blue (RGB).

You can imagine those as three 2D-matrices stacked over each other (one for each color).

While a grayscale image has only one channel.

The value of each pixel in the matrix will range from 0 to 255.

CNN is separated into 2 major parts:

Feature learning

Basic components like the convolutional layer and the pooling layer will be introduced here.

Other components like dropout and 1x1 convolution will be talked about as well to give a sense of how does a CNN is able to build deep. Task prediction with learned features

Depends on the task that you would like the CNN to perform — classification, regression, segmentation, etc. To keep this article beginner-friendly, we will only talk about classification.

Convolutional layer

GIF to illustrate convolution. (source)

For simplicity, the images are represented with 0 and 1-pixel values.

In the GIF, a 3x3 yellow box is the filter used to convolve on the image, this filter is an “X” detector, which acts like a sliding window and perform element-wise multiplication to search the “X” pattern in the image. If a certain part in the image looks like “X”, the value of the convolved feature (feature maps/activation map, these terms are used interchangeably) for that part in the next layer will be high.

If to detect more patterns, more types of filters will be needed. Here is the part where the depth of the next layer comes in. Let’s say you use 3 filters, which is “X”, “O” and “I” detector to slide through the images, then the next layer of the network will be depth=3. (more explanation below)

The filter can also be the filter for detecting color contrast, texture, etc. of the images, depending on the prediction task that you want the CNN to learn.

Examples of filters perform operations on images.

Hyperparameters that control the behavior of each convolved layer:

Stride Padding Number of filters (depth of the next layer) Size of the filter

Stride

Stride is the amount of filter shift in the image. The bigger the stride, the smaller the feature map in the next layer.

Example of convolution when Stride = 1.

Example of convolution when Stride = 2.

Time takes to run the filter = ((height*width)of image)/stride

Padding

Example of padding = 2 on a 32x32x3 image.

The padding in the example image also called the same convolution.

The input volume is 32x32x3, with the padding, we got 36x36x3 as the image size. Then we apply convolution with 3 5x5x3 filters and a stride of 1, we will get 32x32x3 as our output volume in the next layer.

The formula for convolution. Output and input indicate the size for height or width.

Constraints on strides. Note again that the spatial arrangement of hyperparameters have mutual constraints. For example, when

the input has size of 10, no zero-padding is used P=0, and the filterSize is 3, then it would be impossible to use stride S=2, since

(W−F+2P)/S+1=(10−3+0)/2+1=4.5, i.e. not an integer, indicating that the neurons don’t “fit” neatly and symmetrically across the input.

Therefore, this setting of the hyperparameters is considered to be invalid, and a ConvNet library could throw an exception or zero pad the rest to make it fit, or crop the input to make it fit, or something.

Number of filters (depth of the next layer)

Example: 6x6x3 image with 4 3x3 filters. After convolving, will get 4x4xn, n depends on the number of filters you use, in other words, it is the number of features detector you use. In this case, n will be 4.

Size of the filter

The size of the filter is a usually odd number so that the filter has the central pixel/central vision so to know the position of the filter. As if the size of the filter is even, then you need some asymmetric padding.