A Comprehensive Guide to Convolution Neural Network
By: Gaurav Rajpal
Tags: Machine Learning, Convolutional Network, Deep Learning

ROLE OF FILTERS IN CNN.

As we saw in the structure of CNN, convolution layers is used to extract the features and for extracting features it uses filters. So, let us discuss about how the features are extracted using filter now.

Consider below image ,

Source: Analytics Vidhya

In the above image we used various filters like Prewitt or Sobel and obtained the edges. For detail understanding about working on the images and extracting edges you can shoot up at my below blog for theoretical and practical implementation.

Let us understand how filter operation basically works using an animated image.

HOW DOES FILTER OPERATION WORKS ?

Convolution Operation on 5x5 with 3x3 filter

Suppose we have matrix of numbers representing an image and we take 3x3 filter and perform element wise multiplication using the filter over the image. The result which is obtained after performing filter operation is stored in new matrix called as Feature Map.

The filter moves over the image in a manner how we write over the paper i.e. left to right. The nos. of pixels that the filter moves in horizontal direction is called as column stride. The nos. of pixels that the filter moves in vertical direction is called as row stride. This operation is known as convolution operation where filter slides through the image performs element wise operation and generates new matrix called as feature map.

UNDERSTANDING THE DIMENSIONS.

Now we know how the feature map is calculated let us look at the dimensions of input image, filter and feature map.

Source: Analytics Vidhya

In the above figure we have an input image of size (13 x 8) followed by filer of size (3 x 3) and feature map of size (11 x 6 ) obtained by convolution operation.

Consider below images,

Source: Analytics Vidhya

When the filter is used over the first patch of an input image it compares the pixel values on the right and the left on the target pixel 34 and stores the resultant value in feature map.

Source: Analytics Vidhya

Basically feature map contains values against the pixel highlighted in the green box but pixels on the edges are not taken into account.

Source: Analytics Vidhya

If we consider a pixel on an edge i.e. pixel 36 we will notice that there are no pixel surrounding the highlighted pixel and hence it is not contributing in convolution operation and hence size of feature map becomes smaller after every convolution operation.

PADDING STRATEGIES.

As we understood in previous section that pixels on the boundary do not contribute in convolution operation so to resolve that issue let us understand padding strategies.

While building a convolution layer we can set the padding strategies which can be of 2 types.

SAME : Feature map and input image are of same size since zeros are added around the edges of the image.

: Feature map and input image are of same size since zeros are added around the edges of the image. VALID :Feature map and input image differ in size since zeros are not added.

Imagine if we had an image of 1300 x 800 we cannot go and count every single value in output image so you all can refer below formula to calculate height and width of our output i.e. feature map.

Source: Analytics Vidhya.

There are few important things we must note here:

Each convolution layer can have multiple filters.

Values in the filter are not fixed and are learnt during the training process.

It is like MLP where we had parameters like weight matrix which was learnt during backpropagation process here in CNN we have filter values which are learnt during backpropagation.

Using the above formula as discussed let us try to understand the dimensions of the feature map on gray scale images.

Shape with 1 filter and 2D image.

Now instead of single filter, if we use n filters in this case we will have n feature maps stacked together. Values in the filters can be different and are learnt during backpropagation hence we can also have different feature maps of a single input image.

Shape with n filters and 2D image.

Lets us look at the scenario where our input images are having more than one channel i.e. RGB image.

Source: Analytics Vidhya

Using the above image we cannot use our 2D filter for convolution operation as nos. of channels in the filter should be same as nos. of channels in an input image. Interestingly if we use RGB image along with 2D filter, the deep learning frameworks automatically handles it. We do not have to mention the nos. of channels. Note that the output of the operation will be 2D image.

Illustration :

Source: Analytics Vidhya

Now instead of 9 values generating single value in a feature map, we will now have 27 values which will be contributing in generating a single value in feature map.