How to calculate output shape & parameters in a CNN?

Output Feature Size Formula

nout=nin+2pks+1

Parameters Formula

Parameters=(kw×kh×Cin+1)×Cout

Where:

Example

ML_AI/images/conv-3.png

Given:

Parameters

(Given)kw=3,kh=3,Cin=3,Cout=2(Formula)Parameters=(kw×kh×Cin+1)×Cout(Result)=(3×3×3+1)×2=56

Output Features

(Given)nin=6,p=0,s=1,k=3(Formula)nout=nin+2pks+1(Result)=6+2×031+1=4(Output)Features=4×4 per filter

How does a typical CNN architecture for image classification look like?

ML_AI/images/conv-4.png

Layer Operation Formula Inputs
(nin,k,p,s)
Output Size
(nout​)
Output Volume Channels
cin,cout
Parameters Calculation Weights Biases Total Parameters
0 Input Image 227x227x3 0 0 0
1 CONV1 nin=227, k=11,
p=0, s=4
55 55 × 55 × 96 cin=3
cout=96
(11 × 11 × 3 + 1) × 96 34,848 96 34,944
2 POOL1 nin=55, k=3,
p=0, s=2
27 27 × 27 × 96 0 (No learnable weights) 0 0 0
3 CONV2 nin=27, k=5,
p=2, s=1
27 27 × 27 × 256 cin=96
cout=256
(5 × 5 × 96 + 1) × 256 614,400 256 614,656
4 POOL2 nin=27, k=3,
p=0, s=2
13 13 × 13 × 256 0 (No learnable weights) 0 0 0
5 CONV3 nin=13, k=3,
p=1, s=1
13 13 × 13 × 384 cin=256,
cout=384
(3 × 3 × 256 + 1) × 384 884,736 384 885,120
6 CONV4 nin=13, k=3,
p=1, s=1
13 13 × 13 × 384 cin=384,
cout=384
(3 × 3 × 384 + 1) × 384 1,327,104 384 1,327,488
7 CONV5 nin=13, k=3,
p=1, s=1
13 13 × 13 × 256 cin=384,
cout=256
(3 × 3 × 384 + 1) × 256 884,736 256 884,992
8 POOL3 nin=13, k=3,
p=0, s=2
6 6 × 6 × 256 0 (No learnable weights) 0 0 0
9 FC1 Flattened Input: 9,216 - 4096 × 1 (9216 + 1) × 4096 37,748,736 4,096 37,752,832
10 FC2 Input: 4096 - 4096 × 1 (4096 + 1) × 4096 16,777,216 4,096 16,781,312
11 Softmax Input: 4096 - 1000 × 1 (4096 + 1) × 1000 4,096,000 1000 4,097,000
12 Output 1000 × 1 0 0 0