Reading CNN architectures: LeNet, AlexNet, VGG, ResNet

The layer table in the AlexNet paper is full of numbers like 11 × 11, 96 and 55 × 55. Where do they come from? Some are choices the paper makes. The rest are worked out with a formula. Change the values of a layer and watch the sizes and parameter counts update.

Short answer. The numbers come from three places. ① Filter size, count, stride and padding are choices the paper makes. ② The "output size" column is worked out with (N − K + 2P) ÷ S + 1, and the parameter count with (K·K·C_in + 1)·C_out. ③ In code you can print them with print(model) or torchinfo's summary. But a framework's version can differ from the paper.

× same size × 3 channels
LayerKindFilter KCountStride SPadding POutput sizeParameters

You can edit every number box. Output size is side × side × number of feature maps. For fully connected layers only the number of neurons is shown. A layer that does not divide evenly gets an orange ⚠, and the line below shows the math. ReLU, LRN and dropout do not change sizes or parameter counts, so they are left out. AlexNet is counted as one piece, without the paper's two-GPU split (section 4).

Try this. ① At the start (AlexNet, input 227) the sizes shrink 55, 27, 27, 13, 13, 13, 13, 6, and the total is 62,378,344 parameters. fc6 alone has 37,752,832, more than half. ② Change the input to 224 and conv1 gets a ⚠. (224 − 11 + 2 × 0) ÷ 4 = 53.25 does not divide evenly, so it becomes 54. pool1, pool2 and pool3 get a ⚠ too, and the last size is 5 instead of 6. fc6 now takes 6,400 values instead of 9,216, and the total drops to 50,844,008. conv1 stays at 34,944. ③ Keep 224 and set conv1's padding to 2. The size is 55 again and the last one is 6. The ⚠ stays (54.25 rounded down). This is what torchvision's AlexNet does. ④ Press "VGG (first part)" to get 224, 224, 112, 112, 112, 56. A 3 × 3 layer with stride 1 and padding 1 keeps the size, and only pooling halves it. Set a padding to 0 and that layer loses 2. ⑤ In AlexNet, change conv1's count of 96. The parameter count of conv2 changes too, not just conv1, because conv2 takes those 96 maps as its channels.

Definitions

The text below explains with layer tables and the lab. These are the textbook definitions. K is the filter size, Cin is the number of input channels (feature maps of the layer before), and Cout is the number of filters.

convolutional neural network (CNN)
A neural network that stacks convolutional layers (pooling layers may sit between them) to make feature maps, with fully connected layers at the end. The number of filters in a convolutional layer is the number of feature maps (output channels) it makes, and that becomes the number of input channels of the next layer.
number of parameters
The count of numbers set by training (weights and biases). In a convolutional layer each filter has K·K·Cin weights and one bias, so the count is below. It does not depend on the input size N. A pooling layer has 0.

(K·K·Cin+1)·Cout

fully connected layer
A layer where every value of the input vector x is joined by a weight to every neuron of the output. With n inputs and m outputs, W is an m×n matrix, b is a bias of length m, and there are (n + 1)·m parameters. When it takes the output of a convolutional layer, it flattens the N·N·C values into one row.

𝐲=W𝐱+𝐛

1×1 convolution
A convolution with filter size K = 1. At each position (i, j), it multiplies the vector of channel values xi,j (length Cin) by the same Cout×Cin matrix W used at every position. It keeps the width and height and changes only the number of channels.

𝐲i,j=W𝐱i,j+𝐛

bottleneck block
A residual block that uses a 1×1 convolution to cut the channels from C to c (c < C), passes a 3×3 convolution, uses a 1×1 convolution to bring them back to C, and then adds the block input x through the shortcut. The 3×3 convolution in the middle runs on fewer channels, so it needs fewer parameters and less computation.

1. How to read a layer table

A convolutional neural network (CNN) is a neural network that stacks convolution layers, which slide a small window (a filter) over the image, and pooling layers, which shrink it. At the end come fully connected layers that link every value. Papers list these layers in a table or a figure. One row is one layer.

ColumnMeaningWhere it comes from
KindConvolution, max pooling, fully connectedThe paper
Filter size KSide of the window it looks at. 11 means 11 × 11 cellsThe paper
CountHow many filters. This is also how many feature maps come outThe paper
Stride SHow many cells the window moves each timeThe paper
Padding PHow many rings of zeros go around the edgeThe paper. Some papers leave it out
Output sizeside × side × number of feature mapsWorked out (section 2)
ParametersHow many numbers training decidesWorked out (sections 3 and 4)

Number of feature maps = number of filters. When one filter slides over the whole input, it makes one grid of numbers. That grid is a feature map. So "96 kernels" means 96 maps come out, and the next layer takes them as 96 channels. The very first input has 3 channels: red, green and blue.

2. The size column is worked out

For an input with side N, a filter K, stride S and padding P, the output side is (N − K + 2P) ÷ S + 1 (when the division is not exact, round the quotient down). The convolution page shows where this formula comes from. Pooling uses the same formula and keeps the number of maps. Let's follow AlexNet by hand (the starting values of the lab above).

LayerPaper's valuesMathOutputParameters
input227 × 227 × 30
conv111 × 11, 96 filters, stride 4(227 − 11 + 0) ÷ 4 + 1 = 5555 × 55 × 9634,944
pool13 × 3, stride 2(55 − 3) ÷ 2 + 1 = 2727 × 27 × 960
conv25 × 5, 256 filters(27 − 5 + 4) ÷ 1 + 1 = 2727 × 27 × 256614,656
pool23 × 3, stride 2(27 − 3) ÷ 2 + 1 = 1313 × 13 × 2560
conv33 × 3, 384 filters(13 − 3 + 2) ÷ 1 + 1 = 1313 × 13 × 384885,120
conv43 × 3, 384 filterssame math13 × 13 × 3841,327,488
conv53 × 3, 256 filterssame math13 × 13 × 256884,992
pool33 × 3, stride 2(13 − 3) ÷ 2 + 1 = 66 × 6 × 2560
fc64096flattened: 6 × 6 × 256 = 9,216409637,752,832
fc74096409616,781,312
fc810001000 classes10004,097,000
Total62,378,344

But the paper says 224

Section 3.5 of the AlexNet paper says it "filters the 224 × 224 × 3 input image with 96 kernels of size 11 × 11 × 3 with a stride of 4 pixels". But (224 − 11) ÷ 4 + 1 = 54.25 does not divide evenly. You need 227 to get 55. The paper also does not give the padding, or the stride from conv2 on. What it does give, in Figure 2, is the number of neurons in each layer. Three of them, 186,624, 64,896 and 43,264, match 27 × 27 × 256, 13 × 13 × 384 and 13 × 13 × 256. From those you can work backward to stride 1 and padding 2 and 1. The first layer's number, 253,440, does not match 55 × 55 × 96 = 290,400.

When the division is not exact, frameworks usually drop the fraction. PyTorch's formulas for convolution and max pooling output size use floor, and pooling lets you pick ceiling with ceil_mode=True. So 224 runs without an error, but the sizes quietly change.

3. Building the parameter formula

The output size formula is built on the convolution page, so here we build the parameter formula (K·K·C_in + 1)·C_out. We use AlexNet's first layer: K = 11, C_in = 3, C_out = 96.

  1. Cells in one sheet: K·K. 11 × 11 = 121. One weight per cell.
  2. Times the input channels: K·K·C_in. 121 × 3 = 363. A filter looks at all input channels at once, so one filter is a block with one sheet per channel.
  3. Plus one bias: K·K·C_in + 1. 363 + 1 = 364. The number added after the multiply-adds (the bias) is one per filter.
  4. Times the number of filters: (K·K·C_in + 1)·C_out. 364 × 96 = 34,944. Each filter has its own numbers, so you multiply by the count.

Try this. Press "Next" three times and watch the formula pieces (orange background) and the numbers 121 → 363 → 364 → 34,944 join in. In the layer table above, conv1's parameter cell is also 34,944. Change the input size there and that cell does not move. The formula has no input size N in it.

4. Counting parameters

The AlexNet paper says the network has 60 million parameters. It differs from this page's 62,378,344 because the paper split the network across two GPUs. The filters of conv2, conv4 and conv5 only see the half of the previous layer that sits on the same GPU. That is why the paper writes conv2's filters as 5 × 5 × 48, not 5 × 5 × 96. Count with C_in set to 48, 192 and 192 and you get 60,965,224, which matches the paper's 60 million.

How it was countedConv filter countsTotal parameters
This page (one piece, input 227)96, 256, 384, 384, 25662,378,344
Split across two GPUs like the paper96, 256, 384, 384, 25660,965,224
torchvision's AlexNet (section 8)64, 192, 384, 256, 25661,100,840

Count the 13 convolution layers and 3 fully connected layers of VGG-16 the same way and you get 138,357,544. That matches the 138M in Table 2 of the VGG paper.

5. Key properties

PropertyIn plain wordsIn the lab
Feature maps = filtersOne filter makes one map. That number becomes the next layer's C_inChange conv1's count and conv2's parameters change too
A convolution's parameter count does not depend on input sizeOnly K, C_in and C_out are in it. The same filter is reused at every positionInput 224 still gives conv1 34,944
Fully connected layers depend on input size and hold most parametersThe flattened count N·N·C is in the formulafc6 input 9,216 → 6,400
Uneven division gets rounded downNo error, the size just quietly changesFour ⚠ at 224
3 × 3, stride 1, padding 1 keeps the size(N − 3 + 2) ÷ 1 + 1 = N. Only pooling shrinks itVGG (first part)

6. What each of the four models added

ModelWhat it addedNumbers checked in the original paper
LeNetThe pattern of stacking convolution and pooling (shrinking) in turns, with fully connected layers at the end. Used to read handwritten digitsThis page did not check the original paper, so no numbers are given
AlexNetReLU (after every convolution and fully connected layer), dropout (probability 0.5), data augmentation (random crops and horizontal flips), overlapping pooling (3 × 3, stride 2), LRN (local response normalization), training split across two GPUs60 million parameters. Overlapping pooling lowered top-1 and top-5 error by 0.4% and 0.3% compared with non-overlapping 2 × 2
VGGOnly 3 × 3 filters, stacked deep (16 and 19 layers). Two 3 × 3 layers see a 5 × 5 area, three see 7 × 7. Yet three layers have 27C² weights and one 7 × 7 layer has 49C², so the single layer needs 81% more. Each layer also adds a ReLU, so the network can bend moreVGG-16 has 138M. LRN did not improve accuracy
ResNetShortcuts: add a block's input straight to its output, y = F(x) + x (residual connections). The shortcut adds no parameters. Batch normalization after every convolutionUp to 152 layers. The 34-layer network does 3.6 billion multiply-adds, 18% of VGG-19's 19.6 billion

As the table shows, AlexNet brought many tricks together for the first time, VGG made it simpler with "small filters, many layers", and ResNet used shortcuts to fix the problem that very deep stacks did not train well. ResNet's first layer is 7 × 7, 64 filters, stride 2, with a 112 × 112 output (Table 1 of the paper).

The layers of the four models stacked top to bottom. Green is convolution (filter size, number of filters), orange is pooling, blue is a dense layer, purple is a residual block with a shortcut, and “×2” means the same layer twice. LeNet alternates convolution and pooling twice, AlexNet has five convolutions and three dense layers, VGG-16 has thirteen 3 × 3 convolutions, and ResNet-18 has eight residual blocks and a single dense layer. LeNet-5's numbers are the commonly cited configuration; as the table above says, this page did not check them against the original paper.

7. 1×1 convolution and the blocks built with it

A 1×1 convolution is a convolution whose filter size K is 1. It does not look at neighbouring spots across or down. It only multiplies and adds the channel values stacked at one spot. So it keeps height and width and changes only the number of channels. The Inception module and the bottleneck block below use it to cut channels first, which saves parameters in the expensive 3 × 3 and 5 × 5 convolutions.

Orange is the picked spot and its column, green is output channel 1, blue is output channel 2. The “×number” beside an arrow is the weight it multiplies by. The input values and weights are made up.

At one spot, output channel o is:

y_o = w_o1·x_1 + w_o2·x_2 + … + w_oC·x_C + b_o

The parameter count is the formula from section 3, (K·K·C_in + 1)·C_out, with K = 1. Here it is next to 3 × 3, using the lab's C = 4 and C′ = 2.

FilterFormulaIn the lab
1 × 1(1·1·C + 1)·C′(4 + 1) × 2 = 10
3 × 3(3·3·C + 1)·C′(36 + 1) × 2 = 74

Leave out the bias and 1 × 1 has C·C′ while 3 × 3 has 9·C·C′, one ninth.

Inception module (GoogLeNet)

The GoogLeNet paper puts 1 × 1, 3 × 3 and 5 × 5 convolutions and a 3 × 3 max pooling side by side on the same input, and concatenates the branch outputs along the channels into one output. This is the Inception module. Because 3 × 3 and 5 × 5 are expensive, a 1 × 1 in front of them cuts the channels first, and the pooling branch gets a 1 × 1 projection after the pooling (Figure 2 (b) in the paper). Without the cut, the paper says, “even a modest number of 5×5 convolutions can be prohibitively expensive”.

Input (previous layer output) 1×11×1 reduce1×1 reduce 3×3max pool 3×35×51×1projection Concatenate along channels
The Inception module (the version with reductions). The same input splits into four branches that are then concatenated along the channels. Orange boxes are 1 × 1 convolutions: the first branch's 1 × 1, the 1 × 1 that cuts channels before 3 × 3 and 5 × 5, and the 1 × 1 projection after pooling. Green is a 3 × 3 or 5 × 5 convolution, grey is max pooling. Filter counts per box are left out.

Here is how much it saves, with small made-up channel counts. The 32 input channels and each branch's filter count are the values in the table below, not the paper's. “32 → 16” means 32 channels in, 16 out. The pooling branch is the same on both sides, so only the cut before 3 × 3 and 5 × 5 is compared. The formula is section 4's (K·K·C_in + 1)·C_out.

BranchNo 1 × 1 cut first1 × 1 cut first
1 × 11 × 1, 32 → 16: (1·1·32 + 1) × 16 = 528Same: 528
3 × 33 × 3, 32 → 16: (3·3·32 + 1) × 16 = 4,6241 × 1, 32 → 8: 264. 3 × 3, 8 → 16: (3·3·8 + 1) × 16 = 1,168. Sum 1,432
5 × 55 × 5, 32 → 8: (5·5·32 + 1) × 8 = 6,4081 × 1, 32 → 4: 132. 5 × 5, 4 → 8: (5·5·4 + 1) × 8 = 808. Sum 940
Pooling3 × 3 max pool (0) → 1 × 1, 32 → 8: 264Same: 264
Total11,8243,164

Both sides output 16 + 16 + 8 + 8 = 48 channels, yet the parameters drop to 3,164 ÷ 11,824 ≈ 27%. The biggest drop is the 5 × 5 branch (6,408 → 940).

Basic block and bottleneck block (ResNet)

Figure 5 of the ResNet paper shows two kinds of block side by side. On the left is ResNet-34's basic block, two 3 × 3 layers. On the right is the bottleneck block of ResNet-50/101/152, three layers of 1 × 1, 3 × 3 and 1 × 1, where the 1 × 1 layers cut the channels and then raise them again (restore). So the middle 3 × 3 runs on fewer channels. In both blocks a shortcut adds the block's input x to its output (residual connections).

x (C channels) 3×3, C → C3×3, C → C shortcut 1×1stride 2 + output
Basic block (ResNet-34): two 3 × 3 layers.
x (C channels) 1×1 reduce: C → c3×3, c → c1×1 restore: c → C shortcut 1×1stride 2 + output
Bottleneck block (ResNet-50/101/152): a 1 × 1 cuts C to c, then a 3 × 3, then a 1 × 1 restores C. c is smaller than C.

Orange is 1 × 1, green is 3 × 3 convolution. The purple dashed box is the projection (1 × 1, stride 2) put on the shortcut only in blocks where the size changes; other blocks add x as it is (+).

Where height, width or the channel count changes, x cannot be added as it is. The paper gives two options: (A) an identity shortcut with the extra channels padded with zeros, and (B) a projection shortcut done by 1 × 1 convolutions. When the shortcut crosses feature maps of two sizes, it uses a stride of 2. More in the section on differing dimensions of the residual connections page.

8. Printing it from code

import torchvision
from torchinfo import summary                  # pip install torchinfo

model = torchvision.models.alexnet()           # structure only, no trained weights
print(model)                                   # list of layers: Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2)) ...
summary(model, input_size=(1, 3, 224, 224))    # per layer: Output Shape, Param #, then Total params

print(model) lists the layers with their filter size, count, stride and padding. torchinfo's summary runs one fake input through the model and shows each layer's output shape and parameter count. It does what the lab above does.

Code can differ from the paper. As its docs say, torchvision's AlexNet follows a later paper, "One weird trick", not the original. Its filter counts are 64, 192, 384, 256, 256. It puts padding 2 on conv1 to get 55 from 224, and it has no LRN and no GPU split. Its parameter count is 61,100,840, as listed by torchvision, and this page's formulas give the same number. So when you quote "AlexNet's parameter count", say which version you mean.

9. Which one to use when

If you were building something new today, which would you pick? Here is how the four compare.

OptionGoodBadUse it when
A small LeNet-style network (conv and pool two or three times, then fully connected)Few parameters, trains quickly even on a CPU. You can count the layer table by handNot enough for large color images or many classesSmall grayscale images, practice, small devices
AlexNetOnly 8 learned layers (5 conv, 3 fully connected), easy to readMixes in choices nobody picks today: a large first filter (11 × 11, stride 4), LRN, the GPU split. Most parameters sit in the fully connected layersLearning. Not a starting point for new work
VGGRepeats 3 × 3 only, the simplest structureMany parameters (VGG-16 has 138,357,544) and heavy computeExplaining structure, or a first network of your own
ResNetShortcuts let deep stacks train. Fewer filters and less compute than VGG (the 34-layer net is 18% of VGG-19). Trained weights are available in frameworksBlocks and shortcuts make the layer table long. Shortcuts where the size changes need extra handlingThe starting point among these four. Take a trained one, replace the last layer, and train again (transfer learning)

Where it is used

Frequently asked questions

Where do the numbers in a layer table come from?

From three places. Filter size, count, stride and padding are choices the paper makes, so you find them in the paper's text or tables. Output size and parameter count are worked out from those choices. If there is code, print(model) or torchinfo's summary shows each layer's shape and parameter count. Keep in mind that a framework's version can differ from the paper.

Is the AlexNet input 224 or 227?

The paper says 224 × 224 × 3. But with filter 11 and stride 4, (224 − 11) ÷ 4 + 1 = 54.25, which does not divide evenly. You need 227 to get 55. So some write-ups change it to 227, and torchvision keeps 224, adds padding 2 and drops the fraction to land on 55.

Does a layer with more parameters also do more work?

No. AlexNet's conv1 has only 34,944 parameters, but it does 363 multiply-adds for each of its 55 × 55 × 96 outputs, which is 105,415,200 in total. fc6 has 37,752,832 parameters but does 9,216 × 4,096 = 37,748,736 multiply-adds. A convolution reuses the same filter at every position, so it has few parameters and a lot of work.

Words

Word used hereTextbook term
filter, windowfilter, kernel
filter sizekernel size
stepstride
ring of zeros around the edgepadding, zero padding
feature map, channelfeature map, channel
the added numberbias
numbers set by trainingparameters, weights
shrinkingpooling, max pooling, subsampling
layer that links every valuefully connected layer, dense layer, Linear in PyTorch
laying out in one lineflatten
shortcutresidual connection, shortcut connection
local response normalizationLRN
overlapping poolingoverlapping pooling
number of multiply-addsmultiply-adds, FLOPs (papers count them slightly differently)
taking a trained model and training it againtransfer learning, fine-tuning
1×1 convolution1×1 convolution, pointwise convolution
cutting channels, restoring themdimension reduction, restoring
concatenating along the channelsconcatenation, concat
bottleneck blockbottleneck block
projection shortcutprojection shortcut

Sources

← Pooling and receptive fieldsBatch norm and layer norm →