Skip to main content

Convolution, Padding, Stride, Pooling and Activation

A Convolutional Neural Network (CNN) is a neural network mainly used for working with images. CNNs learn visual patterns step by step:
A typical CNN looks like:

1. Convolution

Convolution is the main operation in a CNN. A small matrix called a filter or kernel moves across the image and detects patterns. For example, consider this image:
A 2 × 2 filter:
The filter first looks at:
Multiply corresponding values:
Move the filter one position:
Calculate again:
Continue this process:
This output is called a feature map.

Why is Convolution Useful?

Different filters can learn different features:
The CNN learns the filter values automatically during training.

Convolution Output Formula

The output size of a convolution is: O=NF+2PS+1O = \left\lfloor \frac{N - F + 2P}{S} \right\rfloor + 1 where:
  • OO = output size
  • NN = input size
  • FF = filter size
  • PP = padding
  • SS = stride
For example:
Using the formula: O=53+2(0)1+1O = \left\lfloor \frac{5 - 3 + 2(0)}{1} \right\rfloor + 1 O=3O = 3 Therefore:

PyTorch

Output:

2. Padding

Padding means adding extra pixels around the border of an image. Usually, zeros are added. Original image:
With padding of 1:

Why Do We Use Padding?

Padding helps to:
  1. Preserve the image size.
  2. Give border pixels more importance.
  3. Prevent the feature map from becoming too small too quickly.

Without Padding

With Padding

For a 3 × 3 filter, padding 1, and stride 1:
Using the formula: O=53+2(1)1+1O = \left\lfloor \frac{5 - 3 + 2(1)}{1} \right\rfloor + 1 O=5O = 5 So the spatial size stays the same.

PyTorch

Output:

3. Stride

Stride tells us how many pixels the filter moves at each step.

Stride = 1

The filter moves one pixel at a time:
This produces a larger feature map.

Stride = 2

The filter moves two pixels at a time:
This produces a smaller feature map.

Example

Suppose:
Using the formula: O=73+2(0)2+1O = \left\lfloor \frac{7 - 3 + 2(0)}{2} \right\rfloor + 1 O=2+1O = \left\lfloor 2 \right\rfloor + 1 O=3O = 3 Therefore:

PyTorch

Output:

Important Formula

Always remember: O=NF+2PS+1O = \left\lfloor \frac{N - F + 2P}{S} \right\rfloor + 1 This formula is used to calculate the output size of a convolution layer.

4. Pooling

Pooling reduces the spatial size of a feature map. It helps to:
  • Reduce computation
  • Reduce the number of parameters
  • Keep important information
  • Make the model less sensitive to small changes
The two common types are:
  1. Max Pooling
  2. Average Pooling

Max Pooling

Max pooling selects the largest value from each region. For example:
The maximum value is:
Consider this feature map:
Using a 2 × 2 max pooling window:
Take the maximum from each region:
So:

PyTorch

Output:

Average Pooling

Average pooling calculates the average value of each region. For example:
The average is: 1+5+4+84\frac{1 + 5 + 4 + 8}{4} =4.5= 4.5

PyTorch

Easy Difference


5. Activation Function

After convolution, we usually apply an activation function. The most common activation function in CNNs is ReLU. ReLU stands for: Rectified Linear Unit

ReLU Formula

ReLU(x)=max(0,x)ReLU(x) = \max(0, x) This means:
For example:
After ReLU:

Why Do We Need ReLU?

Without activation functions, multiple neural network layers would behave like a linear transformation. ReLU adds non-linearity. This allows the CNN to learn complex patterns.

PyTorch

Output:
You can also use:

6. Convolution + ReLU + Pooling

A common CNN block is:
This block can be repeated multiple times. For example:

PyTorch

Shape Changes

Start with:
First convolution:
ReLU:
Max pooling:
Second convolution:
ReLU:
Second pooling:
Final output:
Including the batch dimension:

7. Understanding Channels

An image can have different numbers of channels.

Grayscale Image

A grayscale image usually has one channel:
For a 28 × 28 image:

RGB Image

An RGB image has three channels:
For a 224 × 224 image:
The three channels are:

Convolution Channels

Consider:
This means:
The layer produces 32 feature maps. So:

8. Complete CNN Example

Here is a simple CNN for MNIST digit classification.

9. Understanding the MNIST Shape Changes

An MNIST image has:
First convolution:
The spatial size stays the same:
Then max pooling:
Second convolution:
Second pooling:
Then flatten:
Number of values: 64×7×7=313664 \times 7 \times 7 = 3136 Therefore:
is used.

10. Important Formulas

Convolution Output Size

O=NF+2PS+1O = \left\lfloor \frac{N - F + 2P}{S} \right\rfloor + 1 where:

Example

O=283+2(1)1+1O = \left\lfloor \frac{28 - 3 + 2(1)}{1} \right\rfloor + 1 O=28O = 28 Therefore:

Pooling Output Size

The same general formula can be used for pooling: O=NF+2PS+1O = \left\lfloor \frac{N - F + 2P}{S} \right\rfloor + 1 For:
O=2822+1O = \left\lfloor \frac{28 - 2}{2} \right\rfloor + 1 O=14O = 14 Therefore:

11. Quick Revision

Convolution

Remember: Convolution = Find features Examples:

Padding

Remember: Padding = Protect the borders Main purpose:

Stride

Remember: Stride = Movement

Pooling

Remember: Pooling = Reduce size

Activation

Remember: ReLU = Remove negative values ReLU(x)=max(0,x)ReLU(x) = \max(0,x)

12. Easy Way to Remember CNN

The five concepts can be remembered as:

Final Summary

Convolution finds useful patterns in an image. Padding adds pixels around the border and can preserve the spatial size. Stride controls how far the filter moves. Pooling reduces the size of feature maps. Activation adds non-linearity so the network can learn complex patterns. The basic CNN flow is:

One-Line Memory Trick

Convolution finds features, Padding handles borders, Stride controls movement, Pooling reduces size, and Activation helps the network learn complex patterns.