Phase 6: Computer Vision
Module 14: CNN Fundamentals
What You Will Learn
In this module, you will learn:
- What is a Convolutional Neural Network (CNN)?
- Convolution Operation
- Kernel (Filter)
- Feature Map
- Padding
- Stride
- Pooling
- Batch Normalization
- Dropout
- Building a CNN from Scratch
- Best Practices
What is a Convolutional Neural Network (CNN)?
A Convolutional Neural Network (CNN) is a type of deep learning model designed for image and video data. Unlike a fully connected neural network, a CNN focuses on local patterns such as edges, textures, and shapes.
CNNs are widely used for:
- Image Classification
- Object Detection
- Face Recognition
- Medical Image Analysis
- OCR
- Autonomous Driving
- Satellite Image Processing
CNN Architecture
1Input Image 2 │ 3 ▼ 4Convolution 5 │ 6 ▼ 7Activation (ReLU) 8 │ 9 ▼ 10Pooling 11 │ 12 ▼ 13Convolution 14 │ 15 ▼ 16Pooling 17 │ 18 ▼ 19Flatten 20 │ 21 ▼ 22Fully Connected Layer 23 │ 24 ▼ 25Output
Why Not Use Fully Connected Networks?
Suppose an RGB image has size:
1224 × 224 × 3
Total input values:
1224 × 224 × 3 = 150,528
Connecting every pixel to just 100 neurons requires:
1150,528 × 100 2= 315,052,800 weights
This is computationally expensive.
CNNs solve this by sharing weights using convolution.
What is Convolution?
A Convolution is an operation that scans an image using a small matrix called a kernel (or filter).
Instead of looking at the entire image, it looks at one small region at a time.
Example:
1Input Image 2 31 2 3 44 5 6 57 8 9
Kernel
11 0 20 1
The kernel slides across the image and computes new values.
Convolution Process
1Image 2┌─────────────┐ 3│ │ 4│ ███ │ 5│ │ 6└─────────────┘ 7 │ 8Kernel slides → 9 │ 10 ▼ 11Feature Map
Each movement extracts useful information from the image.
What is a Kernel?
A Kernel is a small matrix used to detect patterns.
Typical kernel sizes:
- 3 × 3
- 5 × 5
- 7 × 7
Example
13 × 3 Kernel 2 31 0 -1 41 0 -1 51 0 -1
This kernel detects vertical edges.
Convolution Example
1import torch 2import torch.nn as nn 3 4image = torch.randn(1, 1, 5, 5) 5 6conv = nn.Conv2d( 7 in_channels=1, 8 out_channels=1, 9 kernel_size=3 10) 11 12output = conv(image) 13 14print(output.shape)
Output
1torch.Size([1, 1, 3, 3])
What is a Filter?
A Filter is another name for a learnable kernel.
Each filter learns to detect a different feature.
Example:
1Filter 1 → Horizontal Edges 2 3Filter 2 → Vertical Edges 4 5Filter 3 → Corners 6 7Filter 4 → Texture
If we use:
1nn.Conv2d( 2 3, 3 64, 4 kernel_size=3 5)
The layer learns:
164 Different Filters
Feature Map
The output of a convolution layer is called a Feature Map (or activation map).
Example
1Input Image 2 3↓ 4 5Convolution 6 7↓ 8 9Feature Map
If we use:
1conv = nn.Conv2d(3, 32, 3) 2 3output = conv(images) 4 5print(output.shape)
Output
1torch.Size([Batch, 32, H, W])
Each channel corresponds to one learned feature.
Padding
Without padding, the output size becomes smaller after every convolution.
Example
Input
132 × 32
Kernel
13 × 3
Output
130 × 30
Zero Padding
Padding adds zeros around the image.
1Original 2 3□□□□ 4 5After Padding 6 7000000 80□□□□0 90□□□□0 10000000
Padding Example
1conv = nn.Conv2d( 2 3, 3 16, 4 kernel_size=3, 5 padding=1 6)
Input
132 × 32
Output
132 × 32
Stride
Stride determines how far the kernel moves after each operation.
Stride = 1
1████ 2 ████ 3 ████
Stride = 2
1████ 2 3 ████ 4 5 ████
Larger strides reduce output size.
Stride Example
1conv = nn.Conv2d( 2 3, 3 16, 4 kernel_size=3, 5 stride=2 6)
Output Size Formula
For a convolution layer:
1Output Size 2 3(H - K + 2P) 4 5---------------- + 1 6 7 S
Where:
- H = Input size
- K = Kernel size
- P = Padding
- S = Stride
Example:
1Input = 32 2 3Kernel = 3 4 5Padding = 1 6 7Stride = 1 8 9Output = 32
Pooling
Pooling reduces the spatial size of feature maps.
Benefits:
- Faster training
- Lower memory usage
- Better generalization
Max Pooling
Chooses the maximum value.
Example
1Input 2 31 5 43 2 5 6↓ 7 8Max Pool 9 105
Average Pooling
Chooses the average value.
Example
1Input 2 32 4 46 8 5 6↓ 7 8Average 9 105
MaxPool Example
1pool = nn.MaxPool2d( 2 kernel_size=2, 3 stride=2 4) 5 6x = torch.randn(1, 3, 32, 32) 7 8output = pool(x) 9 10print(output.shape)
Output
1torch.Size([1, 3, 16, 16])
Average Pool Example
1pool = nn.AvgPool2d(2) 2 3output = pool(x) 4 5print(output.shape)
Batch Normalization
BatchNorm normalizes activations during training.
Benefits:
- Faster convergence
- Stable gradients
- Higher learning rates
- Improved accuracy
BatchNorm Example
1batch_norm = nn.BatchNorm2d(32) 2 3x = torch.randn(8, 32, 64, 64) 4 5output = batch_norm(x) 6 7print(output.shape)
Dropout
Dropout randomly disables neurons during training.
Example
Without Dropout
1● ● ● ● ● ●
With Dropout
1● ○ ● ○ ● ○
This helps reduce overfitting.
Dropout Example
1dropout = nn.Dropout( 2 p=0.5 3) 4 5x = torch.randn(16, 128) 6 7output = dropout(x) 8 9print(output.shape)
Building Blocks of a CNN
1Conv2D 2 │ 3 ▼ 4BatchNorm 5 │ 6 ▼ 7ReLU 8 │ 9 ▼ 10MaxPool 11 │ 12 ▼ 13Dropout
This pattern is repeated multiple times.
CNN from Scratch
1import torch 2import torch.nn as nn 3 4class SimpleCNN(nn.Module): 5 6 def __init__(self): 7 super().__init__() 8 9 self.features = nn.Sequential( 10 11 nn.Conv2d( 12 in_channels=3, 13 out_channels=32, 14 kernel_size=3, 15 padding=1 16 ), 17 nn.BatchNorm2d(32), 18 nn.ReLU(), 19 20 nn.MaxPool2d(2), 21 22 nn.Conv2d( 23 32, 24 64, 25 kernel_size=3, 26 padding=1 27 ), 28 nn.BatchNorm2d(64), 29 nn.ReLU(), 30 31 nn.MaxPool2d(2) 32 ) 33 34 self.classifier = nn.Sequential( 35 36 nn.Flatten(), 37 38 nn.Linear( 39 64 * 56 * 56, 40 256 41 ), 42 43 nn.ReLU(), 44 45 nn.Dropout(0.5), 46 47 nn.Linear( 48 256, 49 10 50 ) 51 ) 52 53 def forward(self, x): 54 55 x = self.features(x) 56 57 x = self.classifier(x) 58 59 return x 60 61 62model = SimpleCNN() 63 64print(model)
Test the CNN
1x = torch.randn( 2 8, 3 3, 4 224, 5 224 6) 7 8output = model(x) 9 10print(output.shape)
Output
1torch.Size([8, 10])
CNN Layer Summary
| Layer | Purpose |
|---|---|
| Conv2d | Extract local features |
| ReLU | Add non-linearity |
| BatchNorm2d | Normalize activations |
| MaxPool2d | Downsample feature maps |
| AvgPool2d | Average downsampling |
| Flatten | Convert feature maps to vectors |
| Linear | Classification |
| Dropout | Reduce overfitting |
Common Conv2d Parameters
1nn.Conv2d( 2 in_channels=3, 3 out_channels=64, 4 kernel_size=3, 5 stride=1, 6 padding=1, 7 dilation=1, 8 groups=1, 9 bias=True 10)
| Parameter | Description |
|---|---|
| in_channels | Number of input channels |
| out_channels | Number of filters |
| kernel_size | Size of the convolution kernel |
| stride | Step size |
| padding | Number of pixels added to borders |
| dilation | Spacing between kernel elements |
| groups | Controls grouped/depthwise convolutions |
| bias | Whether to learn a bias term |
Best Practices
- Use 3×3 kernels as the default choice for most CNNs.
- Apply padding=1 with 3×3 kernels to preserve spatial dimensions.
- Use BatchNorm2d after convolution layers for stable training.
- Apply ReLU after BatchNorm.
- Use MaxPool2d to gradually reduce spatial dimensions.
- Add Dropout in fully connected layers to reduce overfitting.
- Increase the number of filters in deeper layers (e.g., 32 → 64 → 128).
- Use AdaptiveAvgPool2d instead of hardcoding flatten sizes when building flexible CNN architectures.
Practice Project: CNN from Scratch on CIFAR-10
Objective
Build a CNN classifier for the CIFAR-10 dataset.
Tasks
-
Load the CIFAR-10 dataset using
torchvision.datasets.CIFAR10. -
Apply image normalization.
-
Create a CNN with:
- Two convolutional blocks
- BatchNorm
- ReLU
- MaxPooling
-
Add fully connected layers with Dropout.
-
Train the model using:
CrossEntropyLossAdamoptimizer
-
Evaluate accuracy on the test dataset.
-
Save the trained model using
torch.save().
Module Summary
In this module, you learned:
- ✅ What a Convolutional Neural Network (CNN) is and why it is effective for image data.
- ✅ How Convolution, Kernels, and Filters extract meaningful visual features.
- ✅ How Feature Maps represent learned patterns.
- ✅ The roles of Padding and Stride in controlling output dimensions.
- ✅ How Max Pooling and Average Pooling reduce spatial size and computational cost.
- ✅ Why Batch Normalization improves training stability.
- ✅ How Dropout helps prevent overfitting.
- ✅ How to build a complete CNN from scratch using PyTorch.
After mastering these fundamentals, you'll be ready to explore advanced CNN architectures such as LeNet, AlexNet, VGG, ResNet, DenseNet, and modern vision models like Vision Transformers (ViTs).