CNN – STEP BY STEP NUMERICAL EXAMPLE
We will cover:
1. Input image
2. Convolution
3. ReLU
4. Max Pooling
5. Flattening
6. Fully Connected Layer
7. Softmax Output
Step 1: Input Image
Assume a 4×4 grayscale image:
1 2 3 0
4 5 6 1
𝑋=[ ]
7 8 9 2
0 1 2 3
Step 2: Convolution Operation
Filter (Kernel): 2×2
1 0
𝐾=[ ]
0 −1
• Stride = 1
• Padding = 0
Output size:
(4 − 2 + 1) × (4 − 2 + 1) = 3 × 3
Convolution Calculation
Position (1,1):
1 2 1 0
[ ]⊙[ ] = (1 × 1) + (2 × 0) + (4 × 0) + (5 × −1) = 1 − 5 = −4
4 5 0 −1
Position (1,2):
2 3
[ ] → (2 × 1) + (3 × 0) + (5 × 0) + (6 × −1) = −4
5 6
Continue similarly.
Feature Map
−4 −4 3
𝑍 = [−4 −4 4]
7 7 6
Step 3: ReLU Activation
𝑅𝑒𝐿𝑈(𝑥) = max(0, 𝑥)
0 0 3
𝐴 = [0 0 4]
7 7 6
Step 4: Max Pooling (2×2, stride = 2)
Take maximum value from each 2×2 region.
0 0 3 4
[ ] → 0[ ]→7
0 0 7 7
Pooled Output
𝑃 = [0 7]
Step 5: Flattening
Convert matrix → vector:
𝐹 = [0, 7]
Step 6: Fully Connected Layer
Assume 2 neurons, weights and bias:
0.5 −0.5
𝑊=[ ] , 𝑏 = [1, − 1]
1.0 0.5
Computation
Neuron 1:
𝑦1 = (0 × 0.5) + (7 × 1.0) + 1 = 8
Neuron 2:
𝑦2 = (0 × −0.5) + (7 × 0.5) − 1 = 2.5
Step 7: Softmax (Classification)
𝑒 𝑦𝑖
𝑆𝑜𝑓𝑡𝑚𝑎𝑥(𝑦𝑖 ) =
∑𝑒 𝑦
𝑒 8 = 2981, 𝑒 2.5 = 12.18
2981
𝑃(𝑐𝑙𝑎𝑠𝑠1) = ≈ 0.996
2993
12.18
𝑃(𝑐𝑙𝑎𝑠𝑠2) = ≈ 0.004
2993
Final Output
Class Probability
Class 1 99.6%
Class 2 0.4%
Predicted Class = Class 1
Summary
• Convolution → Feature extraction
• ReLU → Non-linearity
• Pooling → Dimensionality reduction
• Flatten + FC → Decision making
• Softmax → Probability output
CNN OUTPUT SIZE – STEP BY STEP PROCESS
We will understand output size for:
1. Convolution layer
2. Pooling layer
3. General formula
4. Complete CNN flow example
1. Output Size of CONVOLUTION Layer
Formula (Very Important)
𝑁 − 𝐹 + 2𝑃
Output Size = +1
𝑆
Where:
• 𝑁= Input size
• 𝐹= Filter (kernel) size
• 𝑃= Padding
• 𝑆= Stride
Example 1: Simple Convolution
Input image: 5 × 5
Filter: 3 × 3
Padding: 0
Stride: 1
5 − 3 + 2(0)
+1 =3
1
Output = 3 × 3
Example 2: With Padding
Input: 5 × 5
Filter: 3 × 3
Padding: 1
Stride: 1
5 − 3 + 2(1)
+1 =5
1
Output = 5 × 5
This is called “same convolution”
Example 3: With Stride
Input: 7 × 7
Filter: 3 × 3
Padding: 0
Stride: 2
7−3+0
+1=3
2
Output = 3 × 3
2. Effect of NUMBER OF FILTERS (Depth)
Very important concept
If:
• Input = H × W × D
• Number of filters = K
Then:
• Output depth = K
Example
Input = 32 × 32 × 3
Filters = 10 (size 3×3×3)
Output = 30 × 30 × 10
Each filter creates one feature map
3. Output Size of POOLING Layer
Formula
𝑁−𝐹
+1
𝑆
(No padding usually)
Pooling Example
Input: 4 × 4
Pooling: 2 × 2
Stride: 2
4−2
+1=2
2
Output = 2 × 2
Depth remains SAME
4. Full CNN Output Size Flow
Given CNN Architecture
Input Image: 32 × 32 × 3
Conv Layer
• Filter = 5 × 5
• Stride = 1
• Padding = 0
• Filters = 6
32 − 5 + 0
+ 1 = 28
1
Output = 28 × 28 × 6
Max Pooling
• Pool = 2 × 2
• Stride = 2
28 − 2
+ 1 = 14
2
Output = 14 × 14 × 6
Second Conv Layer
• Filter = 3 × 3
• Stride = 1
• Padding = 0
• Filters = 16
14 − 3
+ 1 = 12
1
Output = 12 × 12 × 16
Flatten Layer
12 × 12 × 16 = 2304 neurons
Convolution:
(𝑁 − 𝐹 + 2𝑃)/𝑆 + 1
Pooling:
(𝑁 − 𝐹)/𝑆 + 1
Depth:
• Conv → Number of filters
• Pool → Same as input
The output size of a convolution layer depends on input size, filter size, padding, and stride. It is
calculated using the formula
(𝑁 − 𝐹 + 2𝑃)/𝑆 + 1.
Pooling layers reduce spatial dimensions but keep depth unchanged.
STEP 4: MAX POOLING (CNN)
What is Max Pooling?
Max Pooling:
• Reduces height & width
• Keeps important (maximum) features
• Makes CNN faster & robust to small shifts
It works independently on each feature map
Inputs to Max Pooling
You must know:
• Input feature map size
• Pooling window size (F × F)
• Stride (S)
(No weights, no learning here )
1. Output Size Formula (Very Important)
𝑁−𝐹
Output size = +1
𝑆
Where:
• 𝑁= Input size
• 𝐹= Pool size
• 𝑆= Stride
2. SIMPLE NUMERICAL EXAMPLE
Input Feature Map (4 × 4)
1 3 2 4
5 6 1 2
𝑋=[ ]
4 2 8 0
1 3 2 6
Pooling Parameters
• Pool size = 2 × 2
• Stride = 2
3. Calculate Output Size
4−2
+1=2
2
Output = 2 × 2
4. Perform MAX POOLING (Step by Step)
Region 1 (Top-Left)
1 3
[ ] ⇒ max = 6
5 6
Region 2 (Top-Right)
2 4
[ ] ⇒ max = 4
1 2
Region 3 (Bottom-Left)
4 2
[ ] ⇒ max = 4
1 3
Region 4 (Bottom-Right)
8 0
[ ] ⇒ max = 8
2 6
5. Final Max Pooled Output
6 4
𝑃=[ ]
4 8
6. Key Observations (Concept Check )
Property Result
Height & Width Reduced
Depth SAME
Parameters NONE
Training NOT learned
Pooling is fixed, no weights, no bias.
7. Max Pooling with STRIDE = 1
Input = 4 × 4
Pool = 2 × 2
Stride = 1
4−2
+1=3
1
Output = 3 × 3
(Overlapping pooling)
8. Multiple Feature Maps Case
Input = 6 × 6 × 3
Apply pooling separately on each channel:
Output = 3 × 3 × 3
Depth unchanged