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.
| Layer | Kind | Filter K | Count | Stride S | Padding P | Output size | Parameters |
|---|
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.
- 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.
- 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.
- 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.
| Column | Meaning | Where it comes from |
|---|---|---|
| Kind | Convolution, max pooling, fully connected | The paper |
| Filter size K | Side of the window it looks at. 11 means 11 × 11 cells | The paper |
| Count | How many filters. This is also how many feature maps come out | The paper |
| Stride S | How many cells the window moves each time | The paper |
| Padding P | How many rings of zeros go around the edge | The paper. Some papers leave it out |
| Output size | side × side × number of feature maps | Worked out (section 2) |
| Parameters | How many numbers training decides | Worked 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).
| Layer | Paper's values | Math | Output | Parameters |
|---|---|---|---|---|
| input | 227 × 227 × 3 | 0 | ||
| conv1 | 11 × 11, 96 filters, stride 4 | (227 − 11 + 0) ÷ 4 + 1 = 55 | 55 × 55 × 96 | 34,944 |
| pool1 | 3 × 3, stride 2 | (55 − 3) ÷ 2 + 1 = 27 | 27 × 27 × 96 | 0 |
| conv2 | 5 × 5, 256 filters | (27 − 5 + 4) ÷ 1 + 1 = 27 | 27 × 27 × 256 | 614,656 |
| pool2 | 3 × 3, stride 2 | (27 − 3) ÷ 2 + 1 = 13 | 13 × 13 × 256 | 0 |
| conv3 | 3 × 3, 384 filters | (13 − 3 + 2) ÷ 1 + 1 = 13 | 13 × 13 × 384 | 885,120 |
| conv4 | 3 × 3, 384 filters | same math | 13 × 13 × 384 | 1,327,488 |
| conv5 | 3 × 3, 256 filters | same math | 13 × 13 × 256 | 884,992 |
| pool3 | 3 × 3, stride 2 | (13 − 3) ÷ 2 + 1 = 6 | 6 × 6 × 256 | 0 |
| fc6 | 4096 | flattened: 6 × 6 × 256 = 9,216 | 4096 | 37,752,832 |
| fc7 | 4096 | 4096 | 16,781,312 | |
| fc8 | 1000 | 1000 classes | 1000 | 4,097,000 |
| Total | 62,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.
- Cells in one sheet:
K·K. 11 × 11 = 121. One weight per cell. - 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. - Plus one bias:
K·K·C_in + 1. 363 + 1 = 364. The number added after the multiply-adds (the bias) is one per filter. - 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
- Convolution:
(K·K·C_in + 1)·C_out. C_in is the number of feature maps from the layer before. - Fully connected:
(number of inputs + 1)·number of neurons. The number of inputs is the layer before flattened into one line,N·N·C. For fc6 that is(9,216 + 1) × 4,096 = 37,752,832. - Pooling, ReLU, dropout: 0. Nothing to learn. AlexNet's LRN has four constants, but the paper picked them with a validation set. They are not learned.
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 counted | Conv filter counts | Total parameters |
|---|---|---|
| This page (one piece, input 227) | 96, 256, 384, 384, 256 | 62,378,344 |
| Split across two GPUs like the paper | 96, 256, 384, 384, 256 | 60,965,224 |
| torchvision's AlexNet (section 8) | 64, 192, 384, 256, 256 | 61,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
| Property | In plain words | In the lab |
|---|---|---|
| Feature maps = filters | One filter makes one map. That number becomes the next layer's C_in | Change conv1's count and conv2's parameters change too |
| A convolution's parameter count does not depend on input size | Only K, C_in and C_out are in it. The same filter is reused at every position | Input 224 still gives conv1 34,944 |
| Fully connected layers depend on input size and hold most parameters | The flattened count N·N·C is in the formula | fc6 input 9,216 → 6,400 |
| Uneven division gets rounded down | No error, the size just quietly changes | Four ⚠ at 224 |
| 3 × 3, stride 1, padding 1 keeps the size | (N − 3 + 2) ÷ 1 + 1 = N. Only pooling shrinks it | VGG (first part) |
6. What each of the four models added
| Model | What it added | Numbers checked in the original paper |
|---|---|---|
| LeNet | The pattern of stacking convolution and pooling (shrinking) in turns, with fully connected layers at the end. Used to read handwritten digits | This page did not check the original paper, so no numbers are given |
| AlexNet | ReLU (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 GPUs | 60 million parameters. Overlapping pooling lowered top-1 and top-5 error by 0.4% and 0.3% compared with non-overlapping 2 × 2 |
| VGG | Only 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 more | VGG-16 has 138M. LRN did not improve accuracy |
| ResNet | Shortcuts: add a block's input straight to its output, y = F(x) + x (residual connections). The shortcut adds no parameters. Batch normalization after every convolution | Up 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).
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
x_1 … x_C: the channel values at one spot (the column). C is the number of input channels, 4 in the lab.w_oc: the weight output channel o multiplies input channel c by.b_o: output channel o's bias.y_o: output channel o's value at that spot. o runs from 1 to C′, and C′ = 2 in the lab.
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.
| Filter | Formula | In the lab |
|---|---|---|
| 1 × 1 | (1·1·C + 1)·C′ | (4 + 1) × 2 = 10 |
| 3 × 3 | (3·3·C + 1)·C′ | (36 + 1) × 2 = 74 |
C,C′: input channels, output channels (the number of filters).1·1,3·3: cells in one sheet of the filter.+ 1: one bias per filter.
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”.
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.
| Branch | No 1 × 1 cut first | 1 × 1 cut first |
|---|---|---|
| 1 × 1 | 1 × 1, 32 → 16: (1·1·32 + 1) × 16 = 528 | Same: 528 |
| 3 × 3 | 3 × 3, 32 → 16: (3·3·32 + 1) × 16 = 4,624 | 1 × 1, 32 → 8: 264. 3 × 3, 8 → 16: (3·3·8 + 1) × 16 = 1,168. Sum 1,432 |
| 5 × 5 | 5 × 5, 32 → 8: (5·5·32 + 1) × 8 = 6,408 | 1 × 1, 32 → 4: 132. 5 × 5, 4 → 8: (5·5·4 + 1) × 8 = 808. Sum 940 |
| Pooling | 3 × 3 max pool (0) → 1 × 1, 32 → 8: 264 | Same: 264 |
| Total | 11,824 | 3,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).
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.
| Option | Good | Bad | Use 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 hand | Not enough for large color images or many classes | Small grayscale images, practice, small devices |
| AlexNet | Only 8 learned layers (5 conv, 3 fully connected), easy to read | Mixes in choices nobody picks today: a large first filter (11 × 11, stride 4), LRN, the GPU split. Most parameters sit in the fully connected layers | Learning. Not a starting point for new work |
| VGG | Repeats 3 × 3 only, the simplest structure | Many parameters (VGG-16 has 138,357,544) and heavy compute | Explaining structure, or a first network of your own |
| ResNet | Shortcuts 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 frameworks | Blocks and shortcuts make the layer table long. Shortcuts where the size changes need extra handling | The starting point among these four. Take a trained one, replace the last layer, and train again (transfer learning) |
Where it is used
- Checking papers and model docs. Recount sizes and parameters with the formulas, and missing padding or a different implementation shows up.
- Estimating memory. At 32-bit floats (4 bytes) per parameter, AlexNet takes
62,378,344 × 4 = 249,513,376bytes, about 250 MB. - Changing the input size. Convolution layers still fit, but the number going into the fully connected layers changes and no longer matches (at 224, fc6 gets 6,400). torchvision's AlexNet puts
AdaptiveAvgPool2d((6, 6))before the fully connected layers so they always get 6 × 6. - Designing your own model. Before stacking layers, check where the sizes stop dividing evenly and which layer holds most parameters.
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 here | Textbook term |
|---|---|
| filter, window | filter, kernel |
| filter size | kernel size |
| step | stride |
| ring of zeros around the edge | padding, zero padding |
| feature map, channel | feature map, channel |
| the added number | bias |
| numbers set by training | parameters, weights |
| shrinking | pooling, max pooling, subsampling |
| layer that links every value | fully connected layer, dense layer, Linear in PyTorch |
| laying out in one line | flatten |
| shortcut | residual connection, shortcut connection |
| local response normalization | LRN |
| overlapping pooling | overlapping pooling |
| number of multiply-adds | multiply-adds, FLOPs (papers count them slightly differently) |
| taking a trained model and training it again | transfer learning, fine-tuning |
| 1×1 convolution | 1×1 convolution, pointwise convolution |
| cutting channels, restoring them | dimension reduction, restoring |
| concatenating along the channels | concatenation, concat |
| bottleneck block | bottleneck block |
| projection shortcut | projection shortcut |
Sources
- Alex Krizhevsky, Ilya Sutskever, Geoffrey E. Hinton, ImageNet Classification with Deep Convolutional Neural Networks, NeurIPS 2012. Section 3.5 (layers, 224 × 224 × 3, filter sizes and counts, neuron counts in Figure 2), sections 3.1, 3.3, 3.4 (ReLU, LRN, overlapping pooling), section 4 (60 million parameters, data augmentation, dropout 0.5).
- Karen Simonyan, Andrew Zisserman, Very Deep Convolutional Networks for Large-Scale Image Recognition, arXiv:1409.1556. Section 2.1 (3 × 3, stride 1, padding 1, 2 × 2 pooling), Tables 1 and 2 (configurations and parameter counts), section 2.3 (27C² and 49C²).
- Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun, Deep Residual Learning for Image Recognition, arXiv:1512.03385. Equation (1) y = F(x) + x, sections 3.3 and 3.4 (compute, batch normalization), Table 1, Figure 5 (basic block and bottleneck block: three layers of 1×1, 3×3, 1×1, with the 1×1 layers reducing and then restoring dimensions), shortcuts when dimensions increase ((A) identity with zero padding, (B) projection by 1×1 convolutions, stride 2 when crossing two sizes).
- Szegedy et al., Going deeper with convolutions, arXiv:1409.4842. The Inception module (1×1, 3×3, 5×5 convolutions and a parallel pooling path, 1×1 reductions before the expensive 3×3 and 5×5, a 1×1 projection after pooling, branch outputs concatenated, without reductions even a modest number of 5×5 convolutions can be prohibitively expensive), Figure 2 (a) naïve version and (b) with dimension reductions.
- PyTorch: torchvision's AlexNet code (filter counts, padding, num_params), Conv2d and MaxPool2d docs (floor in the output size, ceil_mode), torchinfo.