Search

Search papers and lessons

VGG Network

Level 3 · 10 min read

VGG is a convolutional network built from one repeated block, stacked 3 by 3 convolutions followed by max pooling, showing that depth alone, with small filters, makes image classifiers much better.

On this page

  • Colour = topic mix
  • Calculus
  • Architectures
  • Linear algebra
  • Neural network basics
  • Probability
  • Losses & regularization
  • Sign in to see your progress

TL;DR

VGG is a Convolutional Neural Network built by repeating one simple pattern: two or more small 3 by 3 convolutions with ReLU activations, then a Max Pooling step that halves the grid. Stacking that pattern 16 or 19 layers deep, with a fixed design rule at every level, gave a large accuracy gain on the ImageNet classification benchmark in 2014.

Where you'll see it: as a feature extractor for transfer learning, as the perceptual-loss network in many image-generation and style-transfer systems, and as the reference "plain deep network" in papers about residual connections.

Intuition

A recipe, not a new idea

Earlier convolutional networks, such as the ones that won ImageNet in 2012 and 2013, used a mix of filter sizes, strides and normalisation choices. Each design was a one-off. The VGG authors, Karen Simonyan and Andrew Zisserman at Oxford, asked a narrower question: if you fix everything except depth, how much does depth alone help?

Their answer was to use the same tiny building block everywhere. Every convolution is 3 by 3 with stride 1 and padding 1, so it keeps the grid size. Every pooling step is 2 by 2 with stride 2, so it halves the grid. Depth is then just a count of how many times you repeat the block.

flowchart LR
    I[224 x 224 x 3 image] --> B1[2 conv 3x3, 64 channels]
    B1 --> P1[max pool: 112 x 112]
    P1 --> B2[2 conv 3x3, 128 channels]
    B2 --> P2[max pool: 56 x 56]
    P2 --> B3[3 conv 3x3, 256 channels]
    B3 --> P3[max pool: 28 x 28]
    P3 --> B4[3 conv 3x3, 512 channels]
    B4 --> P4[max pool: 14 x 14]
    P4 --> B5[3 conv 3x3, 512 channels]
    B5 --> P5[max pool: 7 x 7]
    P5 --> H[fully connected layers and softmax]

The shape of the network is a set of blocks. Each block keeps its spatial size, and each pool halves it while the channel count doubles.

Why small filters are enough

A 3 by 3 filter sees a small neighbourhood. Stacking two of them means the second filter sees a 3 by 3 patch of first-layer outputs, and each of those already saw a 3 by 3 patch of the input. The combined view is a 5 by 5 region of the input. Three stacked 3 by 3 layers give a 7 by 7 view. So a stack of small filters covers the same area as one big filter, but with more non-linear steps in between, and with fewer weights. The math below makes the weight count precise.

The math

Notation

Let a convolutional layer take CinC_{\text{in}} input channels and produce CoutC_{\text{out}} output channels with filters of side kk. The number of weights in that layer, ignoring biases, is

W=k2⋅Cin⋅Cout.W = k^2 \cdot C_{\text{in}} \cdot C_{\text{out}}.

The receptive field of a layer is the square of input pixels that can affect one output value. For a stack of nn convolutions of side kk with stride 1, the receptive field side is

r=1+n(k−1).r = 1 + n(k - 1).

For k=3k = 3, this gives r=2n+1r = 2n + 1. Two layers see a 5 by 5 region, and three see a 7 by 7 region.

Output sizes through the network

With padding 1 and a 3 by 3 kernel, a convolution keeps the height and width. A 2 by 2 max pool with stride 2 maps a side of length HH to H/2H/2. Starting from a 224 by 224 input, the grid goes 224, 112, 56, 28, 14, and finally 7 after the fifth pool. The last convolutional output is therefore 7×7×5127 \times 7 \times 512, which flattens to 25,08825{,}088 numbers.

Parameter counts

The classifier at the end is three fully connected layers, with 4096, 4096 and 1000 outputs. The first of these maps 25,08825{,}088 inputs to 4,0964{,}096 outputs, so it holds

25,088⋅4,096=102,760,44825{,}088 \cdot 4{,}096 = 102{,}760{,}448

weights. The second fully connected layer holds 4,096⋅4,096=16,777,2164{,}096 \cdot 4{,}096 = 16{,}777{,}216 weights, and the last holds 4,096⋅1,000=4,096,0004{,}096 \cdot 1{,}000 = 4{,}096{,}000. Together these are about 123.6 million weights. The convolutional layers of VGG-16 together hold about 14.7 million. The total is about 138 million, and the fully connected layers account for roughly 89 percent of it.

Training objective

The network outputs 1000 scores, one per ImageNet class, and a Softmax turns them into probabilities. Training minimises the cross-entropy loss with Stochastic Gradient Descent and momentum. Dropout with probability 0.5 is applied to the first two fully connected layers during training. The authors also used weight decay and a learning rate that was divided by 10 when validation accuracy stopped improving.

Worked examples

Example
Counting weights: two 3 by 3 layers against one 5 by 5 layer

Take 64 input channels and 64 output channels, and compare two ways to get a 5 by 5 receptive field.

  1. One 5 by 5 convolution: W=52⋅64⋅64=25⋅4,096=102,400W = 5^2 \cdot 64 \cdot 64 = 25 \cdot 4{,}096 = 102{,}400 weights.
  2. Two 3 by 3 convolutions: each has W=32⋅64⋅64=9⋅4,096=36,864W = 3^2 \cdot 64 \cdot 64 = 9 \cdot 4{,}096 = 36{,}864 weights, so the pair has 2⋅36,864=73,7282 \cdot 36{,}864 = 73{,}728 weights.
  3. Ratio: 73,728/102,400=0.7273{,}728 / 102{,}400 = 0.72. The stack uses about 28 percent fewer weights.
  4. Between the two layers there is a ReLU, so the stack can represent a more complex function of the same 5 by 5 region than a single linear filter can.
  5. Interpretation: VGG's choice is cheaper and more expressive for the same view of the image, which is why it used only 3 by 3 filters.
Example
Tracing the shape through one block

Take a 28×28×25628 \times 28 \times 256 input to the fourth block of VGG-16, which has three 3 by 3 convolutions with 512 output channels.

  1. First convolution: input 256 channels, output 512. Size stays 28×2828 \times 28 because of padding 1. Weights: 9⋅256⋅512=1,179,6489 \cdot 256 \cdot 512 = 1{,}179{,}648.
  2. Second convolution: input 512, output 512. Weights: 9⋅512⋅512=2,359,2969 \cdot 512 \cdot 512 = 2{,}359{,}296.
  3. Third convolution: same as the second, another 2,359,2962{,}359{,}296 weights.
  4. After the block, a max pool halves the grid to 14×14×51214 \times 14 \times 512.
  5. Interpretation: the block adds about 5.9 million weights and halves the spatial area for the next block. Each pool widens the view of every layer after it, so deeper features depend on larger parts of the input.

Tradeoffs and failure modes

VGG is simple to explain and easy to modify, which made it a standard tool for years. It has real costs.

SituationProblemBetter choice
Deploying on a phone or edge deviceAbout 138 million weights and high memory useMobileNet-style separable convolutions, or a smaller network
Training a very deep plain stack from scratchGradients fade in the deepest layers, and training stallsResidual connections, as in [[residual-connection
Building a classifier with a large fixed-size inputFully connected layers force one input sizeA global average pooling head
Needing a feature extractor onlyThe large fully connected head is wastedUse the convolutional part and discard the classifier
Wanting high accuracy with low computeCompute per image is high at 224 by 224A more efficient architecture from later work

Two failure modes are worth knowing. First, the large fully connected layers make the model heavy and prone to overfitting, which is why dropout was needed. Second, the plain deep network is hard to optimise past a certain depth without careful initialisation. The VGG authors trained the shallow 11-layer net first and used its weights to initialise the first four convolutional layers and the last three fully connected layers of the deeper ones; they later found that Glorot-style random initialisation works without this pre-training.

History

Simonyan and Zisserman submitted the VGG work in 2014. It placed second in the classification track of the ILSVRC 2014 challenge (7.3 percent top-5 test error with an ensemble of 7 models), and first in the localisation track. The paper's main contribution was the systematic study of depth: the authors compared configurations from 11 to 19 weight layers (named A to E, plus a variant of A with local response normalisation), built from the same blocks, with 1 by 1 convolutions in one 16-layer variant. Error fell as depth grew from 11 to 19 layers, but the authors report that it saturated at 19, and the 16 and 19 layer versions became the names people still use.

VGG caught on for two reasons. It was simple to implement with the tools of the time, and its pretrained weights were released publicly, so researchers used it as a feature extractor for many tasks. It also showed that a single design rule could scale, which set the pattern for later architecture papers. The gap between VGG and the residual networks that followed in 2015 is one of the clearest lessons in deep learning: plain stacks have a depth limit that skip connections remove.

Check yourself

QuestionRecall: what are the filter size, stride and padding of every convolution in VGG?Show answer

Every convolution uses a 3 by 3 filter with stride 1 and padding 1. Padding 1 keeps the height and width unchanged, so only the pooling steps change the grid size.

QuestionComputation: how many weights does one 3 by 3 convolution with 128 input channels and 128 output channels have, ignoring biases?Show answer

W=32⋅128⋅128=9⋅16,384=147,456W = 3^2 \cdot 128 \cdot 128 = 9 \cdot 16{,}384 = 147{,}456 weights.

QuestionWhy: why does VGG use two or three 3 by 3 layers in a row rather than one larger filter?Show answer

A stack of small filters covers the same receptive field as one large filter. Two 3 by 3 layers see a 5 by 5 region, and three see a 7 by 7 region. The stack uses fewer weights: for equal channel counts, two 3 by 3 layers use 18C218C^2 weights against 25C225C^2 for one 5 by 5 layer. The stack also has a non-linearity between its layers, so it can represent more complicated functions of the same region.

QuestionApplied: you want to use a pretrained VGG-16 as a feature extractor for medical images of size 512 by 512. Which parts of the network do you keep, and what happens to the feature map size?Show answer

Keep the convolutional part and discard the three fully connected layers, because their input size is fixed to 7×7×5127 \times 7 \times 512. The convolutional part is size-flexible. A 512 by 512 input passes five pools, so the final map is 16×16×51216 \times 16 \times 512 rather than 7×7×5127 \times 7 \times 512. You can then use global average pooling over the spatial grid to get a 512-dimensional feature vector, or attach a new head. The weights from ImageNet still transfer, but you should check that the object scale in the medical images matches what the filters were trained to see.

Sources