Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 5: Computer Vision and CNNs

The CNN architecture tour

P5.940 min●●○ IntermediateReviewed Dec 3, 2026

Why this matters for machine learning

Many image models in use are one of a few designs, or a small change to one. Knowing what each design added, and what it costs, lets you read a model card and choose a backbone instead of guessing.

  • Transfer learning starts from a pretrained backbone such as a ResNet or an EfficientNet, so the choice of backbone decides accuracy, memory and speed. (P5.10: Transfer learning and fine-tuning)
  • Detection networks reuse these designs as feature extractors, and segmentation networks start from their feature maps (P5.13). (P5.12: Detection architectures)
  • ConvNeXt is the convolutional network that Vision Transformers are compared with, and the comparison shows which design differences matter. (P5.15: Vision Transformers)

Before this: Multi-channel convolutions and 1×1 convs, Pooling and receptive fields, Residual connections

After this lesson you can

  • State, for nine architectures, the problem each addressed and the one idea it added
  • Count the weights of a VGG stage, a ResNet bottleneck, an Inception module and a depthwise-separable block
  • Explain global average pooling and why it replaced large dense heads
  • Choose a backbone by parameters, multiply-adds and measured latency

In 2012 a convolutional network called AlexNet won the ImageNet classification contest with a top-5 error of 15.3% on the test set, while the second-best entry had 26.2%. Top-5 error is the share of test images whose true class is not among the model’s five highest-scoring classes. Three years later an ensemble of residual networks with up to 152 layers reached 3.57% in the same contest. In those three years the number of layers went from 8 to 152.

Between the two results lie a handful of design ideas that image models still use. This lesson walks through nine networks in the order they appeared, with the problem each faced, the idea it added, a drawing of its block, its paper and its parameter count. The building blocks are taught in P5.5 (1×11\times1 convolutions, bottlenecks, depthwise-separable convolutions) and P4.20 (residual connections). Here you meet the networks that made them famous.

Scope: classification networks only. Detection and segmentation designs are P5.12 and P5.13, Vision Transformers are P5.15, and training a CNN yourself is P5.7.

How to read the tour

Each design gets four things: the problem that stopped earlier networks, the idea that helped, a repeating block (drawn with tensor shapes in the widget), and the size in parameters.

Parameter counts come from torchvision, the vision library of PyTorch (version 0.24.1). We build each model with weights=None, which gives the untrained network, and add up p.numel() over its parameters. We call the result “the torchvision version”. Two networks are not in torchvision: LeNet-5, which we build ourselves, and the first MobileNet, which we build from the table in its paper.

ImageNet results come from the original papers, with the exact measure. The contest entries below are ensembles: the predictions of several networks are averaged, which lowers the error but costs several times the compute.

See the nine designs

Pick a design on the timeline. The Block view draws its characteristic block, with shapes as (channels, height, width) for one image. The Compare view puts the nine parameter counts on one log scale, where each gridline is 10 times the one before.

LeNet-5, 1998: 61,706 parameters

LeNet-5, 1998

LeCun, Bottou, Bengio, Haffner. Gradient-Based Learning Applied to Document Recognition. Proceedings of the IEEE, November 1998.

Key idea. Convolution and subsampling layers alternate, then dense layers classify: weight sharing makes a network for 32×32 digit images small enough to train.

61,706 parameters (61.7K): a modern variant built for this lesson (the paper counts 60,000 trainable parameters).

The whole network. Shapes are (channels × height × width) for one image.
input image1×32×32conv 5×5, 66×28×28average pool 2×26×14×14conv 5×5, 1616×10×10average pool 2×216×5×5conv 5×5, 120120×1×1dense 8484dense 1010 digit scores

A modern variant: tanh, average pooling, and a dense output layer. The paper uses a sparse connection table in the third convolution and a different output layer.

The nine designs of the tour, in order of year. Block view: boxes are layers, thin arrows are data flow, the dashed line is a shortcut around the layers. Blue boxes are convolutions, dashed blue boxes are dense layers, grey boxes are pooling, normalization or activations, dotted boxes are inputs, and dark-outlined boxes join branches by adding or concatenating. Compare view: the nine parameter counts on a log scale, where each gridline is 10 times the one before. Arrow keys move along the timeline.

Notice that the bars do not rise with the years: VGG-16 (2014) is the largest.

LeNet-5 (1998): the pattern

Problem. Recognize handwritten digits from pixels without hand-designed features.

Idea. Alternate convolution layers with subsampling layers, then finish with dense layers (LeCun, Bottou, Bengio and Haffner, Proceedings of the IEEE, November 1998). The input is a 32×3232\times32 grey image, and the paper counts 7 layers without it. Because each kernel is shared over the whole image (P5.2), the network has only 60,000 trainable parameters, although the paper counts 340,908 connections.

The original has details that modern networks dropped: trainable subsampling coefficients, a third convolution that connects each map to only some of the maps before it, and an output layer that compares 84 numbers with stored patterns. The widget draws a modern variant with plain average pooling, a full third convolution and a dense output layer. It has 156+2,416+48,120+10,164+850=61,706156+2{,}416+48{,}120+10{,}164+850=61{,}706 parameters. The original’s 60,000 are the sum 156+12+1,516+32+48,120+10,164156+12+1{,}516+32+48{,}120+10{,}164 over its layers C1, S2, C3, S4, C5 and F6, without the stored patterns of the output layer.

AlexNet (2012): scale and the tricks that made it work

Problem. Why not earlier? The authors point to two things that had only just become available: labelled datasets with millions of images, and GPUs fast enough to train big CNNs. The task was to train a large CNN on 1.2 million high-resolution images in 1,000 classes without overfitting and without waiting months.

Idea. A deeper CNN, with five convolutional and three dense layers, plus training changes (Krizhevsky, Sutskever and Hinton, NIPS 2012, now NeurIPS):

  • ReLU instead of tanh. In their test, a four-layer CNN on CIFAR-10 reached 25% training error six times faster.
  • GPUs. The network was split over two GTX 580 GPUs and trained for five to six days.
  • Dropout (P4.17) in the first two dense layers. Without it the network showed substantial overfitting.
  • Data augmentation (P4.18). Random 224×224224\times224 crops of 256×256256\times256 images and horizontal flips enlarged the training set by a factor of 2048, and a random change of colour intensities added more.
  • Overlapping max-pooling and local response normalization, a layer later replaced by batch normalization (P4.19).

The paper reports 60 million parameters. The torchvision version, which follows the variant in the “One weird trick” paper (Krizhevsky, 2014), has 61,100,840, with 64, 192, 384, 256 and 256 channels. In the 2012 contest the authors’ entry, an average of several CNNs, had a top-5 test error of 15.3%, against 26.2% for the second-best entry.

Where the parameters are. The torchvision AlexNet has 58,631,144 parameters in its three dense layers and only 2,469,696 in its five convolutions. So 96% of the weights sit in the dense head, 37,752,832 of them in the first dense layer alone (256×6×6=9,216256\times6\times6=9{,}216 inputs to 4,096 units). Remember this when we reach global average pooling.

VGG (2014): small kernels, stacked

Problem. AlexNet used large kernels (11×1111\times11 and 5×55\times5). Is there a simpler recipe that goes deeper?

Idea. Use only 3×33\times3 convolutions, in stages of two or three, each stage followed by a max pool (Simonyan and Zisserman, arXiv 2014, ICLR 2015). Two 3×33\times3 layers in a row, with stride 1 and no pooling between, see a 5×55\times5 patch of the input, and three see 7×77\times7 (receptive fields are P5.6). What do you gain by three small layers instead of one big one? The paper gives two reasons: three nonlinearities instead of one, and fewer weights. With CC channels in and out, the weight counts are

three 3×3:3⋅(32C2)=27C2,one 7×7:72C2=49C2,\begin{aligned} \text{three }3\times3:&\quad 3\cdot(3^2C^2)=27C^2,\\ \text{one }7\times7:&\quad 7^2C^2=49C^2, \end{aligned}

which is 81% more for the single large layer. At C=64C=64 these are 110,592 and 200,704 (weights only, no biases).

VGG-16 has 13 convolutions in five stages of 2, 2, 3, 3 and 3 layers, with 64, 128, 256, 512 and 512 channels, and three dense layers: 16 weight layers. The torchvision version has 138,357,544 parameters, of which 123,642,856 (89%) are in the dense head, 102,764,544 in the first dense layer alone (7×7×512=25,0887\times7\times512=25{,}088 inputs to 4,096 units). In the 2014 contest VGG came second in classification, with a top-5 test error of 7.3% for an ensemble of 7 networks (6.8% for two networks, found after the deadline), and first in localisation.

GoogLeNet and Inception (2014): branches, and 1×1 reductions

Problem. Which kernel size is best, 1×11\times1, 3×33\times3 or 5×55\times5? And how can a network get deeper and wider without the cost exploding?

Idea. Do not choose. An Inception module runs a 1×11\times1, a 3×33\times3, a 5×55\times5 and a pooling branch side by side on the same input and concatenates their outputs along the channels (Szegedy et al., arXiv 2014, CVPR 2015). To keep the cost down, 1×11\times1 convolutions come first in the 3×33\times3 and 5×55\times5 branches and shrink the channels, the same trick P5.5 shows for ResNet.

Take the paper’s module 3a, which reads 192 channels. Its 3×33\times3 branch has 128 outputs and its 5×55\times5 branch has 32. If both read the 192 channels directly, they need 192⋅128⋅9+192⋅32⋅25=374,784192\cdot128\cdot9+192\cdot32\cdot25=374{,}784 weights. With 1×11\times1 reductions to 96 and 16 channels first, they need 144,896144{,}896, which is 2.59 times fewer (weights only).

GoogLeNet is the name of the contest entry, a 22-layer network built from such modules. Its other choices:

  • Average pooling before the last layer instead of big dense layers. The paper reports about 0.6% better top-1 accuracy.
  • Two auxiliary classifiers on middle layers, used only in training, to strengthen the gradient signal that reaches early layers (P4.16).
  • The paper says it uses 12 times fewer parameters than Krizhevsky et al. and a budget of 1.5 billion multiply-adds. Counts differ between implementations: the torchvision version has 6,624,904 parameters without the auxiliary classifiers, 9 times fewer than torchvision’s AlexNet, and it uses a 3×33\times3 convolution in the third branch where the paper uses 5×55\times5.

It won the 2014 contest with a top-5 error of 6.67%, for an ensemble of 7 networks that each looked at 144 crops of the image.

ResNet (2015): add the input

Problem. Plain networks stopped improving with depth, and even their training error got worse (the degradation problem of P4.20).

Idea. Let each block learn a correction to its input: y=x+F(x)\mathbf{y}=\mathbf{x}+F(\mathbf{x}) (He, Zhang, Ren and Sun, arXiv 2015, CVPR 2016). P4.20 explains why the added x\mathbf{x} makes deep networks trainable. ResNet-18 and ResNet-34 use a basic block of two 3×33\times3 convolutions. ResNet-50, 101 and 152 use a bottleneck block: a 1×11\times1 convolution that shrinks the channels, a 3×33\times3 convolution, and a 1×11\times1 convolution that expands them again.

For a block with 256 channels the bottleneck goes 256 to 64, 3×33\times3 at 64, then 64 to 256, with 16,384+36,864+16,384=69,63216{,}384+36{,}864+16{,}384=69{,}632 weights. Two 3×33\times3 layers at 256 channels would have 1,179,648, and one 3×33\times3 layer 589,824 (weights only: these are P5.5’s numbers without the biases P5.5 counted). So the bottleneck has about 17 times fewer weights than a pair of 3×33\times3 layers with the same 256-channel input and output.

When a stage halves the image size and doubles the channels, the shortcut cannot be a plain copy. The block then uses a projection shortcut, a 1×11\times1 convolution with stride 2 on the shortcut path (in the torchvision ResNet-50 the second stage has one that maps 256 to 512 channels with stride 2). ResNet-50 has 3,4,6,33,4,6,3 blocks in its four stages, batch normalization (P4.19) after every convolution, and ends with global average pooling and one dense layer.

The paper’s deepest ImageNet network has 152 layers. A single ResNet-152 had a top-5 error of 4.49% on the validation set, and an ensemble reached 3.57% on the test set, first place in 2015. The torchvision counts are 11,689,512 (ResNet-18), 25,557,032 (ResNet-50) and 60,192,808 (ResNet-152).

DenseNet (2016): keep everything

Problem. Each layer of a plain network sees only the maps of the layer before it. Can later layers reuse earlier features directly, and can the gradient reach early layers by a short path?

Idea. Give each layer the maps of all earlier layers in its block, joined by concatenation. The authors argue that this strengthens feature propagation and encourages feature reuse (Huang, Liu, van der Maaten and Weinberger, arXiv 2016, CVPR 2017):

xℓ=Hℓ([x0,x1,…,xℓ−1]).\mathbf{x}_\ell = H_\ell\bigl([\mathbf{x}_0,\mathbf{x}_1,\dots,\mathbf{x}_{\ell-1}]\bigr).

A block of LL layers then has L(L+1)/2L(L+1)/2 direct connections, where a plain stack has LL. A residual block adds (shapes must match); a dense layer concatenates, so the earlier maps stay as they are and the channel count grows.

Each layer adds only kk new maps, the growth rate, with k=32k=32 on ImageNet. A layer with cc input channels applies a 1×11\times1 convolution to make 4k=1284k=128 maps, then a 3×33\times3 convolution to make k=32k=32, and appends them to its input, giving c+32c+32 channels. A transition layer between blocks (a 1×11\times1 convolution and 2×22\times2 average pooling) halves the channel count. DenseNet-121 has four blocks of 6, 12, 24 and 16 layers, with channel counts 64→25664\to256, 128→512128\to512, 256→1024256\to1024 and 512→1024512\to1024 (the Python cell below prints them). Its 7,978,856 parameters in the torchvision version are about a third of ResNet-50’s.

MobileNet (2017): cheap enough for a phone

Problem. The networks above need billions of multiply-adds, too many for a phone.

Idea. Replace each ordinary 3×33\times3 convolution by a depthwise-separable pair (Howard et al., arXiv 2017): a depthwise 3×33\times3 convolution that filters each channel on its own, then a pointwise 1×11\times1 convolution that mixes the channels (P5.5 shows the counts). The paper’s cost ratio is

1N+1DK2,\frac{1}{N}+\frac{1}{D_K^2},

where NN is the number of output channels and DKD_K the kernel size, so a 3×33\times3 pair costs 8 to 9 times less than a full convolution. At 256 channels in and out, a full 3×33\times3 layer has 589,824 weights and the pair has 2,304+65,536=67,8402{,}304+65{,}536=67{,}840, 8.7 times fewer.

The network has a first full 3×33\times3 convolution, then 13 such pairs, with stride-2 depthwise layers to halve the size, then average pooling and one dense layer. Almost all the work is now in the 1×11\times1 layers: 95% of the multiply-adds and 75% of the parameters (the paper’s Table 2). A width multiplier and a resolution multiplier shrink the network uniformly to fit a budget. Version 1 has 4.2 million parameters and 569 million multiply-adds in the paper, and building its Table 1 in PyTorch gives 4,231,976 parameters.

EfficientNet (2019): scale all three together

Problem. To get a better network, people made one dimension bigger: more layers (depth), more channels (width), or larger images (resolution). The gain shrinks quickly when only one of them grows.

Idea. Compound scaling (Tan and Le, ICML 2019): grow all three together, with one coefficient φ\varphi that says how much more compute you can spend:

depth d=αφ,width w=βφ,resolution r=γφ,\begin{aligned} \text{depth } d&=\alpha^{\varphi},\\ \text{width } w&=\beta^{\varphi},\\ \text{resolution } r&=\gamma^{\varphi}, \end{aligned}

with α⋅β2⋅γ2≈2\alpha\cdot\beta^2\cdot\gamma^2\approx2 and α,β,γ≥1\alpha,\beta,\gamma\ge1. The squares appear because the cost of a convolution layer grows with the number of layers, with the square of the width (input and output channels both grow) and with the square of the resolution (height and width both grow). So the cost grows by about 2φ2^{\varphi}. A small grid search on the base network found α=1.2\alpha=1.2, β=1.1\beta=1.1, γ=1.15\gamma=1.15, and 1.2⋅1.12⋅1.152=1.921.2\cdot1.1^2\cdot1.15^2=1.92 is a little under 2 (the cell below tries φ=1,2,3\varphi=1,2,3).

The base network, EfficientNet-B0, was found by an architecture search. Its block is the mobile inverted bottleneck of MobileNetV2 (Sandler et al., CVPR 2018: thin ends, a wide middle with a depthwise convolution) plus squeeze-and-excitation, a small gate that rescales each channel using the average of that channel over the image. B1 to B7 are B0 scaled up. In the paper’s table, B0 has 5.3 million parameters, 0.39 billion FLOPs and 77.1% top-1 accuracy on the ImageNet validation set, and B7 has 66 million parameters, 37 billion FLOPs and 84.3%. The torchvision counts are 5,288,548 and 66,347,960.

ConvNeXt (2022): a ResNet that learned from Transformers

Problem. After Vision Transformers appeared in 2020 (P5.15), Transformers overtook ConvNets on ImageNet classification. Was the gain from attention, or from other changes made at the same time?

Idea. Take a ResNet-50 and move it one change at a time toward the Transformer design, keeping only convolutions (Liu et al., arXiv 2022, CVPR 2022). The paper’s roadmap, with ImageNet-1K top-1 accuracy on the validation set:

  1. A newer training recipe (300 epochs instead of 90, the AdamW optimizer, stronger augmentation) on the unchanged ResNet-50: 76.1% to 78.8%.
  2. Blocks per stage from 3,4,6,33,4,6,3 to 3,3,9,33,3,9,3 (79.4%), and a patchify stem, one 4×44\times4 convolution with stride 4 (79.5%).
  3. Depthwise 3×33\times3 convolutions with wider channels, an inverted bottleneck (a hidden layer four times wider than the block’s input and output, like a Transformer’s feed-forward layer), and a 7×77\times7 kernel size: 80.6%.
  4. GELU instead of ReLU, fewer activation and normalization layers, LayerNorm instead of batch normalization (P4.19) and separate downsampling layers: about 82%.

The roadmap ends at 82.0% (82.1% for ConvNeXt-T in the comparison table, from the final training runs), against 81.3% for Swin-T of similar size. Depthwise convolutions alone first lowered the accuracy, to 78.3% in the paper’s figure. The torchvision ConvNeXt-Tiny has 28,589,128 parameters and about 4.5 billion multiply-adds. The first step is the surprise: the authors conclude that a significant part of the gap between older CNNs and Transformers may be due to how they were trained.

The head: global average pooling

The end of a classifier turns feature maps into class scores. AlexNet and VGG flatten the last maps and use big dense layers. Later networks use global average pooling: average each channel’s map over all its positions, so CC maps become CC numbers, then one dense layer (pooling is P5.6, the dense layer is P4.5). A ResNet-50 ends with 2,048 maps of size 7×77\times7. Flattening them gives 7⋅7⋅2048=100,3527\cdot7\cdot2048=100{,}352 numbers, and a dense layer from there to 4,096 units would have 411,045,888 weights. Average pooling gives 2,048 numbers, and the dense layer to 1,000 classes has 2,049,000.

That is why the head is 8% of ResNet-50’s parameters, against 96% for AlexNet and 89% for VGG-16. Each channel’s average can be read as how strongly that feature is present anywhere in the image, and the layer shapes no longer depend on the image size. GoogLeNet’s paper credits the idea to the Network-in-Network paper and reports the 0.6% gain mentioned above.

What changed, and what stayed

Across the tour networks got deeper, made trainable by ReLU, batch normalization (P4.19) and shortcuts (P4.20), while each layer got cheaper (stacked 3×33\times3 convolutions, 1×11\times1 reductions, bottlenecks, depthwise-separable layers) and the heads shrank to global average pooling. Scaling rules (EfficientNet) and training recipes (ConvNeXt’s first step) now matter as much as layer types, and ideas travel both ways: ConvNeXt borrows from Transformers (P5.15).

How to choose in practice

  1. Start from a pretrained model, not from random weights (P5.10), and pick the backbone by three numbers.
  2. Parameters set the memory for the weights, 4 bytes each in 32-bit floating point: about 102 MB for ResNet-50, 553 MB for VGG-16 and 14 MB for MobileNetV2.
  3. Compute and latency. Multiply-adds and the measured time on your device are not the same (see the box below).
  4. Match the input. Pretrained weights expect a certain image size and the per-channel normalization of their training (P5.1).
Go deeper FLOPs, parameters and latency

Three numbers describe a network, and they do not move together. Counting the multiply-adds of every convolution and linear layer for one 224×224224\times224 image (with forward hooks in PyTorch):

  • AlexNet: 61.1 million parameters, 0.71 billion multiply-adds.
  • VGG-16: 138.4 million parameters, 15.47 billion multiply-adds.
  • ResNet-50: 25.6 million parameters, 4.09 billion.
  • ConvNeXt-T: 28.6 million parameters, 4.46 billion (the paper says 4.5).
  • MobileNetV2: 3.5 million parameters, 0.30 billion.

The parameter counts set the memory for the weights, 4 bytes each in 32-bit floating point.

Parameters are not compute. A convolution reuses each weight at every position, so a layer on a large map can need many multiply-adds with few weights. AlexNet has 2.4 times the parameters of ResNet-50 but 5.7 times fewer multiply-adds. Compute is not time either, and multiply-adds set the work per image while latency is the measured time on your device. The ConvNeXt paper notes that, at similar FLOPs, models with depthwise convolutions are known to be slower and to use more memory than models with only dense convolutions. MobileNet’s authors point out that their design puts nearly all the work into 1×11\times1 convolutions, which map directly onto fast matrix multiplication. Latency also depends on the hardware, the library, the batch size and the memory traffic. Resolution matters too: the ConvNeXt paper’s comparison table lists EfficientNet-B7 at 600×600600\times600 pixels with 37.0 billion FLOPs, and ConvNeXt-T at 224×224224\times224 with 4.5 billion.

So treat parameters and multiply-adds as a first filter and measure latency on the target device before you commit. (“FLOPs” is used in two ways: some papers count one multiply-add as one FLOP, as the ResNet paper does, others count it as two.)

Go deeper Inception v3: splitting a kernel into a column and a row

P5.3 showed that some kernels, like the Gaussian blur and the Sobel filter, split into a column and a row. Inception v3 (Szegedy, Vanhoucke, Ioffe, Shlens and Wojna, arXiv 2015, CVPR 2016) uses the same idea for learned layers: a 3×33\times3 convolution is replaced by a 3×13\times1 convolution followed by a 1×31\times3 convolution. With CC channels in and out, that costs 6C26C^2 weights instead of 9C29C^2, which the paper puts as 33% cheaper. Replacing a 3×33\times3 layer by two 2×22\times2 layers would save only 11% (8C28C^2 against 9C29C^2). The paper also uses 1×71\times7 followed by 7×17\times1 on medium-sized maps. At C=64C=64 the 3×33\times3 layer has 36,864 weights and the pair 24,576.

The pair is not the same as a hand-made separable kernel. With one channel and no activation in between, the pair can only represent kernels that split exactly (rank 1, P5.3). With many channels between the two layers, the pair can represent more. The MobileNet paper adds that this kind of spatial factorization saves little on top of depthwise convolutions, because very little computation is spent in the depthwise layers.

Depth: three ways to see it

Intuition

A CNN is a factory line of identical workstations, and each design in the tour redesigned the workstation. VGG used two or three small tools in a row. Inception gave it several tools working on the same part at once. ResNet added a conveyor that carries the part past the station. DenseNet kept a copy of every earlier part on the table. MobileNet split each tool into a cheap per-channel tool and a mixer. EfficientNet asked how long, how wide and how fine the line should be.

The picture breaks in two places. The stations are trained together, not designed one at a time. And a faster station on paper is not always faster in the real factory, because the hardware likes some layouts better than others.

Math

Weights of a convolution (no bias): Cout Cin k2C_{\text{out}}\,C_{\text{in}}\,k^2, divided by the number of groups for a grouped convolution. A depthwise convolution has Ck2Ck^2 weights.

Compound scaling. A convolution layer costs about CinCoutHWk2C_{\text{in}}C_{\text{out}}HWk^2 multiply-adds. Scaling the depth by dd, the channels by ww and the height and width by rr multiplies the cost by d w2r2d\,w^2r^2. With d=αφd=\alpha^\varphi, w=βφw=\beta^\varphi, r=γφr=\gamma^\varphi this is (αβ2γ2)φ(\alpha\beta^2\gamma^2)^\varphi, which the constraint αβ2γ2≈2\alpha\beta^2\gamma^2\approx2 turns into about 2φ2^\varphi.

Code

Counting the parameters of the torchvision models and the size of two heads.

from torchvision import models

def count(m):
    return sum(p.numel() for p in m.parameters())

names = ["alexnet", "vgg16", "googlenet", "resnet50", "densenet121",
         "mobilenet_v2", "efficientnet_b0", "convnext_tiny"]
for name in names:
    extra = {"aux_logits": False} if name == "googlenet" else {}
    model = getattr(models, name)(weights=None, **extra)
    print(f"{name:16s}{count(model):>12,}")

vgg = models.vgg16(weights=None)
print("VGG-16 dense head:", f"{count(vgg.classifier):,}")
print("ResNet-50 head   :", f"{count(models.resnet50(weights=None).fc):,}")

Output from a local run with PyTorch 2.9.1 and torchvision 0.24.1:

alexnet           61,100,840
vgg16            138,357,544
googlenet          6,624,904
resnet50          25,557,032
densenet121        7,978,856
mobilenet_v2       3,504,872
efficientnet_b0    5,288,548
convnext_tiny     28,589,128
VGG-16 dense head: 123,642,856
ResNet-50 head   : 2,049,000

The aux_logits=False argument leaves out the two auxiliary classifiers of GoogLeNet, which are used only in training. The shape of a pretrained model for your own task is the subject of P5.10.

Try it in Python

The first cell counts the weights of the blocks in this lesson. The second runs the compound-scaling arithmetic, the DenseNet channel growth and the two heads. Both use only NumPy and plain arithmetic, with weights counted without biases.

Weights of the building blocks

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Running the cell gives 69,632 weights for the bottleneck against 1,179,648 for two 3×33\times3 layers, and 144,896 for Inception 3a against 374,784.

Compound scaling, dense growth and heads

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Running the cell gives a cost of ×1.92\times1.92, ×3.69\times3.69, ×7.08\times7.08 for φ=1,2,3\varphi=1,2,3, and 411,045,888 against 2,049,000 weights for the two heads.

Common mistakes

Quiz

Check your understanding

  1. In a ResNet-50 bottleneck block that reads 256 channels, the first layer is a 1×11\times1 convolution from 256 to 64 channels. What is its job?

  2. In a DenseNet with growth rate 32, a layer receives 128 channels. How many channels does the next layer in the same block receive?

  3. A ResNet-50 ends with 2,048 feature maps of size 7×77\times7. After global average pooling, how many weights (without bias) does the dense layer to 1,000 classes have?

Exercises

  1. Two 3×33\times3 layers with 128 channels in and out, against one 5×55\times5 layer with the same channels (weights only). Which has fewer weights?
    Show answerTwo 3×33\times3 layers: 2⋅9⋅1282=294,9122\cdot9\cdot128^2=294{,}912. One 5×55\times5 layer: 25⋅1282=409,60025\cdot128^2=409{,}600, about 39% more. Both see a 5×55\times5 patch of the input.
  2. A bottleneck block reads 128 channels, shrinks to 32, applies a 3×33\times3 convolution at 32 channels and expands back to 128. Compare its weights with one 3×33\times3 layer from 128 to 128 channels (weights only).
    Show answerBottleneck: 128⋅32+32⋅32⋅9+32⋅128=4,096+9,216+4,096=17,408128\cdot32+32\cdot32\cdot9+32\cdot128=4{,}096+9{,}216+4{,}096=17{,}408. One 3×33\times3 layer: 9⋅1282=147,4569\cdot128^2=147{,}456. The bottleneck has about 8.5 times fewer weights.
  3. DenseNet-121’s third block starts with 256 channels, has 24 layers and growth rate 32. How many channels does layer 10 read, and how many does the block output?
    Show answerLayer 10 reads 256+32⋅9=544256+32\cdot9=544 channels. The block outputs 256+32⋅24=1024256+32\cdot24=1024 channels.
  4. How much memory do the weights of ResNet-50 (25,557,032 parameters) and of VGG-16 (138,357,544) need in 32-bit floating point? What does this number leave out?
    Show answerAbout 4×25,557,032≈1024\times25{,}557{,}032\approx102 MB and 4×138,357,544≈5534\times138{,}357{,}544\approx553 MB (decimal megabytes). It leaves out the activations stored during a forward pass, which depend on the image size and batch size, and during training also the gradients and optimizer state.

Next: P5.10 turns these networks into starting points: transfer learning and fine-tuning reuse a pretrained backbone for your own images.

Key takeaways

  • LeNet-5 (1998) set the pattern: convolution and pooling layers, then dense layers. AlexNet (2012) won ImageNet by a wide margin with a deeper network trained on GPUs, with ReLU, dropout and augmentation.
  • VGG stacks 3×33\times3 convolutions (27C227C^2 weights against 49C249C^2 for a 7×77\times7). Inception uses parallel branches and 1×11\times1 reductions. ResNet adds the input to the output. DenseNet concatenates all earlier maps.
  • MobileNet uses depthwise-separable convolutions, 8 to 9 times cheaper. EfficientNet scales depth, width and resolution together. ConvNeXt modernizes a ResNet with Transformer-style choices.
  • Global average pooling replaced dense heads, which held 96% of AlexNet's parameters and 8% of ResNet-50's.
  • Choose by parameters, multiply-adds and measured latency. The newest design is not automatically best: a better recipe alone lifted ResNet-50 from 76.1% to 78.8%.
Found a mistake? Tell us