Convolutional Neural Networks: Simplified
By: Rohan Hirekerur
Tags: Artificial Intelligence, Convolutional Neural Net, Machine Learning, Deep Learning

How exactly do CNNs work?

It’s a technical topic, but I’ll try to explain it in simple terms.

Kernels:

An image is a set of pixels, and to interpret what they depict as a whole, CNNs use something called Kernels. (Also called filters or feature detectors).

Kernels can be termed as the intelligence of CNNs.

Let’s correlate reading a paragraph with kernels. Just as we read a line from left to right, we extract meaning from the text. Similarly, the kernel moves left to right over an image, while extracting features. Also, while reading, we interpret one word at a time and not individual letters, likewise kernel extracts features from a set of pixels at a time. The number of pixels considered at once is determined by the size of the kernel. (A square kernel of size 3*3 is most commonly used).

Kernels in CNN

Each pixel in an image is associated with a value in the range 0 to 255, determining its intensity (Or 3 values per pixel for colored images, one for each of red, green, and blue channels). Similarly, kernels also have a value associated with each of its blocks.

Where do kernels get their values?

As I mentioned before, kernels are the intelligence that extracts features. The values may be initialized randomly or by some predefined function, but they change over time when the network is being trained. The actual intelligence part of our network is these values.

Now you might be wondering, “Hey, that’s all good, but how does the kernel extract features?”

It is as simple as multiplication and addition!

Features are extracted by multiplying corresponding blocks as shown and adding them up. For example value at position [1, 1] in the image is multiplied by the value at position [1, 1] in the kernel. (Typically, this is followed by an activation layer to remove non-linearity, but it’s a vast topic so I won’t include that in this article)

In the following example, in the first step… The resultant feature is given as (7*1) + (2*0) + (3*-1) + …. + (2*-1) = 6

Feature extraction

Strides:

Images contain thousands of pixels, and processing all of them is computationally expensive as well as time-consuming. So we reduce the number of features by altering the Stride parameter.

Stride defines the number of pixels by which the kernel should move in either direction.

In the illustrations you see below, the kernel is moving to the immediate next set of pixels, in this case, the stride is [1, 1]. This tells the kernel to move 1 pixel to the right while moving along the row and 1 pixel down while moving along the column.

Now, to reduce the size, if we change the stride to say [2, 2], the kernel will skip 1 pixel while moving in either direction.

Stride is an important parameter in CNNs because it helps to reduce the size drastically and thus reduce the number of computations. In the above example, we reduce the number of output features from 9 to just 4 by changing the stride parameter.

Pooling:

Video by Kensuke Koike from YouTube

This video accurately demonstrates that we don’t necessarily lose information when we reduce the size of the image, as we can identify that the picture depicts a dog even after the image was reduced in size.

So if even 1/4th part of the actual image is sufficient to deduce what it depicts, we don’t need to process the complete image, even in CNNs. This is where pooling comes into picture.

Pooling is used to reduce the size of the feature map.

Pooling directly reduces the size of the image (feature map) by moving a filter over the features and taking only one value from a block of features. That one value depends on the type of pooling. It may be the maximum value from the block (Max Pooling) or the sum of values (Sum Pooling) or the average of all values (Mean Pooling). Max pooling is the one that’s used most commonly.

Flattening And Prediction:

After a series of convolutions and pooling, we obtain the features and now we need our network to predict something from these features. It’s as if the network now sees eyes, nose, and whiskers, and from this it has to predict that the image is of a cat. To do this, we need to first flatten the matrix.

Flattening

After flattening, we predict our class from the features by using an Artificial Neural Network (ANN).

The main use of convolution is to extract features, and they perform well at it. ANN converts the features into more attributes which will intern give better predictions, hence ANNs are used after convolution. I will explain ANNs in detail in my next article.