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: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:2 × 2 filter:
Why is Convolution Useful?
Different filters can learn different features:Convolution Output Formula
The output size of a convolution is: where:- = output size
- = input size
- = filter size
- = padding
- = stride
PyTorch
2. Padding
Padding means adding extra pixels around the border of an image. Usually, zeros are added. Original image:1:
Why Do We Use Padding?
Padding helps to:- Preserve the image size.
- Give border pixels more importance.
- Prevent the feature map from becoming too small too quickly.
Without Padding
With Padding
For a3 × 3 filter, padding 1, and stride 1:
PyTorch
3. Stride
Stride tells us how many pixels the filter moves at each step.Stride = 1
The filter moves one pixel at a time:Stride = 2
The filter moves two pixels at a time:Example
Suppose:PyTorch
Important Formula
Always remember: 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
- Max Pooling
- Average Pooling
Max Pooling
Max pooling selects the largest value from each region. For example:2 × 2 max pooling window:
PyTorch
Average Pooling
Average pooling calculates the average value of each region. For example: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 UnitReLU Formula
This means: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
6. Convolution + ReLU + Pooling
A common CNN block is:PyTorch
Shape Changes
Start with:7. Understanding Channels
An image can have different numbers of channels.Grayscale Image
A grayscale image usually has one channel:28 × 28 image:
RGB Image
An RGB image has three channels:224 × 224 image:
Convolution Channels
Consider:8. Complete CNN Example
Here is a simple CNN for MNIST digit classification.9. Understanding the MNIST Shape Changes
An MNIST image has:10. Important Formulas
Convolution Output Size
where:Example
Pooling Output Size
The same general formula can be used for pooling: For:11. Quick Revision
Convolution
Padding
Stride
Pooling
Activation
12. Easy Way to Remember CNN
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.