Introduction to CNN & Image Classification Using CNN in PyTorch
By: Amey Band
Tags: Cnn, Image Classification, Deep Learning, Data Science, Machine Learning

Convolutional Neural Network is the type of Neural Network that is most often applied to image processing problems. The major application of CNN is the object identification in an image but we can use it for natural language processing too. Once you will go through the complete article, you will get to know why CNN is most effective in these fast-growing areas.

CNN works differently as they treat the data in the spatial aspect. In the neural network, you know that when we provide input to the input layer, the number of neurons in this layer is equal to the number of pixels in the case of images. In CNN, instead of neurons being connected to every neuron in the previous layer, they are only connected to the neurons close to it.

There are some important layers in CNN: 1. Convolution Layer 2. Pooling Layer 3. Fully-connected Layer

Here we will understand the concepts behind the two main layers,

(i) Convolution Layer

Simple Convolution of a (5x5) matrix with a (3x3) kernel

Convolution refers to the filtering process that happens in this type of neural network. Mathematically, convolution is described as the function derived from two given functions by integration which expresses how the shape of one function is modified by the other.

The convolution layer’s output shape is affected by:

Kernel Size: the size of the filter.

the size of the filter. Input Dimensions: input image size

input image size Padding: we can add layers of 0s to the outside of the image in order to make sure that the kernel properly passes over the edges of the image.

we can add layers of 0s to the outside of the image in order to make sure that the kernel properly passes over the edges of the image. Strides: the rate at which the kernel passes over the input image. A stride of 2 moves the kernel in 2-pixel increments.

Read more about the convolution parameters here.

Consider the above image, the size of the image is (5x5) and the filter’s size is (3x3). After the input image processes through the convolution layer, the output image we obtain has the dimension of (3x3).

Where n_in denotes the dimension of the input image, f denotes the window size, and s denotes the stride.

The function for convolution in pyTorch is torch.nn.Connv2d().

(ii) Pooling Layer

Source: Photo via bouvet.no

The pooling layer in CNN progressively reduces the spatial size of the representation to lower the number of parameters in the convolutional neural network.

From the above image, you conclude that there are three types of pooling methods:

Max Pooling : Extract the maximum value of the patch from the feature map.

: Extract the maximum value of the patch from the feature map. Average Pooling: Extract the average of all patches in the feature map.

Extract the average of all patches in the feature map. Sum Pooling: Extract the sum of all patches in the feature map.

Max-pooling enables the network to concentrate on a few neurons rather than all of them which has a regularizing effect on the network, and likely to overfit the training data.

The function for max-pooling in pyTorch is torch.nn.MaxPool2d().

For detailed information, go through the articles mentioned in the reference section below.

(iii) Fully-connected Layer

Fully connected layers are an essential component of Convolutional Neural Networks (CNNs), which have been proven very successful in recognizing and classifying images for computer vision. The CNN process begins with convolution and pooling, breaking down the image into features, and analyzing them independently.

I would like you will go through the article to get more understanding about fully-connected layers.