VGG: Very Deep Convolutional Networks for Large-Scale Image Recognition
Stacking more 3×3 convolution layers, with everything else held fixed, lowers ImageNet error up to 19 layers.
Simonyan and Zisserman built six networks that share one input size, one filter size, one pooling scheme and one classifier, and differ only in depth, from 11 weight layers to 19. Top-5 validation error fell from 10.4% to 8.0%, and the 16- and 19-layer versions, VGG-16 and VGG-19, were reused as image feature extractors for years.
Explaining the paperVery Deep Convolutional Networks for Large-Scale Image RecognitionVGG-16 fits in nine lines of code. Its 138 million weights sit mostly in one fully-connected layer, its 15.5 billion multiply-adds mostly in the convolutions, and it placed second at ImageNet 2014.
The experiment: change only the depth
In 2014 image recognition was measured on ImageNet's ILSVRC benchmark: 1.3 million training photos in 1000 classes. Convolutional networks had taken it over two years earlier, when AlexNet won with an 11×11 first-layer filter applied at stride 4. The 2013 entries improved on it partly by shrinking that first layer to 7×7 at stride 2, while also changing other parts of the network. When a new entry changes the filter size, the stride and the layer count together and reports a lower error, the gain cannot be assigned to any one of them.
Simonyan and Zisserman fixed everything except depth. Every network in the paper shares this scaffold:
- The input is a 224×224 RGB crop. The only preprocessing is subtracting the mean RGB value of the training set from each pixel.
- Every convolution is 3×3 with stride 1 and one pixel of padding (apart from three 1×1 layers in configuration C), so it leaves height and width unchanged.
- Five max-pooling layers do all of the spatial downsampling. Each keeps the largest value in every non-overlapping 2×2 block (stride 2), halving height and width.
- The channel count starts at 64 and doubles after each pooling until it reaches 512.
- The classifier is the same in every network: fully-connected layers of 4096, 4096 and 1000 units, then a softmax that turns the 1000 scores into class probabilities.
- Every hidden layer is followed by a ReLU, .
The only free choice left is how many 3×3 layers sit between consecutive poolings. The paper builds configurations A to E, from 11 weight layers (8 conv + 3 fully-connected) to 19 (16 conv + 3 fully-connected). A weight layer is a layer with learned parameters; pooling, ReLU and softmax are not counted. Configurations D and E are the networks now called VGG-16 and VGG-19.
The paper scores them with top-1 and top-5 error. Top-1 error is the fraction of images whose highest-scoring class is wrong. Top-5 error, ILSVRC's main metric, is the fraction whose correct class is not among the five highest-scoring classes, which forgives a network that ranks "husky" just above the correct "malamute". On the validation set, at one test scale, top-5 error falls from 10.4% for A to 8.0% for E.
A handful of ideas explain the design: what one padded 3×3 convolution computes; how stacking them widens the region of the image each neuron depends on; why three 3×3 layers are better than one 7×7 layer; where VGG-16's weights and arithmetic sit; and the training and test procedures that made a 19-layer network workable in 2014.
How a 3×3 convolution keeps the image size
A convolution layer holds a bank of small filters. Each filter is a block of weights of shape , where is the number of input channels. At one position the filter lies over a 3×3 patch of the input, through all of its channels, multiplies each weight by the value under it, sums the products, adds a bias, and writes one number. The filter then moves one pixel and repeats. Running every filter at every position gives the output: one channel per filter.
VGG's first layer makes this concrete. The input has 3 channels (RGB), so each filter has weights plus a bias, and one output number costs 27 multiply-adds. The layer has 64 filters, so it holds parameters and turns the 224×224×3 image into a 224×224×64 feature map. Because the same filter weights are reused at every position, the parameter count does not depend on the image size:
A layer deep in the network, with 256 channels in and 256 out, has parameters, whether the feature map is 56×56 or 1000×1000.
The output's size follows from how many positions the filter can occupy. For an input of width , filter size , padding on each side and stride :
Without padding, a 3×3 window centered on an edge pixel would hang off the image, so the window's center can only reach pixels one in from the border, and each side loses one pixel: an 8×8 input gives a 6×6 output. Each of VGG-16's 13 conv layers would trim its feature map by 2 pixels in width and in height. Padding the input with a ring of zeros, one pixel wide, lets the window center on every real pixel. In Figure 1, switch padding on and off, then move the filter size to 5 and 7 and watch how much the output shrinks.
With VGG's , , , equation (2) gives . In general, a stride-1 convolution keeps the size when , which is a whole number only for odd . The paper gives its reason for 3×3: it is the smallest filter that captures the notion of left and right, up and down, and center. With the convolutions fixed at full resolution, every reduction in size happens at one of the five poolings, so each stage of the network works on one known resolution.
Stacking 3×3 filters: the receptive field
The receptive field of a neuron is the set of input pixels that can affect its value. A neuron in the first 3×3 layer has a 3×3 receptive field. A neuron in the second layer reads a 3×3 block of first-layer neurons. The two outer neurons in each row of that block sit one pixel left and one pixel right of center, and each of them reads one pixel further out, so the second-layer neuron depends on a 5×5 patch of the input. Each further 3×3 layer adds one pixel on every side, so after layers, with no pooling between them, the side of the receptive field is
Two layers reach 5×5 and three reach 7×7, the two cases the paper states. Drag the slider in Figure 2 to add layers one at a time.
Pooling speeds this up. After a 2×2 pool with stride 2, neighboring cells of the feature map are 2 input pixels apart, so the next 3×3 conv widens the field by 2 pixels per side instead of 1; after two pools, by 4. Tracing VGG-16 stage by stage, with the input-pixel spacing between neighboring cells:
- Stage 1 (): two convs give 3, then 5. The pool adds and doubles it: 6, .
- Stage 2: two convs add 4 each, giving 14. Pool: 16, .
- Stage 3: three convs add 8 each, giving 40. Pool: 44, .
- Stage 4: three convs add 16 each, giving 92. Pool: 100, .
- Stage 5: three convs add 32 each, giving 196: a conv5_3 neuron depends on a 196×196 window of the 224×224 input.
Why three 3×3 layers beat one 7×7
If three stacked 3×3 layers reach the same 7×7 patch as one 7×7 layer, which should a network use? The paper compares the two with channels in and out, and finds two differences.
The first is the number of weights. A 3×3 layer has , so three of them have . A 7×7 layer has :
At VGG's widest stage, , that is 7,077,888 weights for the stack against 12,845,056 for the single layer. Since , the paper says the 7×7 layer has 81% more weights than the stack. Measured from the other side, the stack has about 45% fewer weights than the 7×7 layer.
The second is the number of non-linearities. Each conv is followed by a ReLU, so the stack applies three ReLUs where the 7×7 layer applies one. The paper says this makes the decision function more discriminative. Figure 3 sets the two options side by side for receptive fields from 3×3 to 11×11.
The ReLUs also decide whether the stack is a 7×7 convolution in disguise. Convolution is associative: filtering an image with , then , then gives the same result as filtering once with the single kernel , whose size is per side. So with no non-linearity between them, three 3×3 convs compute exactly one 7×7 linear filter, and only a restricted family of them, because numbers generate a map that would otherwise have free entries. A ReLU between the layers sets every negative intermediate value to zero, the composition no longer reduces to one kernel, and the stack can compute functions that no 7×7 conv computes.
Figure 4 runs this in one dimension, where the whole computation fits on screen. Three random 3-tap filters are applied in sequence to a signal, and their composed 7-tap kernel is applied once to the same signal. With the ReLUs off, the dashed amber curve lies exactly on the teal one for any filters you draw; with them on, the readout gives the largest gap between the two outputs.
The paper describes the stack as a regularised 7×7 filter: the 7×7 layer is forced to decompose into three 3×3 filters with non-linearities between them. It also tests the claim directly. Configuration B was rebuilt with each pair of 3×3 convs replaced by one 5×5 conv, which has the same 5×5 receptive field by equation (3) but half as many layers and ReLUs. On a center crop, the shallower net's top-1 error was 7% higher than B's.
The 1×1 convolution in configuration C
Configuration C is B with a 1×1 convolution added at the end of stages 3, 4 and 5. The "1×1" refers to the spatial footprint only. At each pixel the layer multiplies the channel vector by a learned matrix of shape and applies a ReLU:
It mixes channels and never reads a neighboring pixel, so it leaves the receptive field unchanged. At 512 channels in and out it has parameters, against 2,359,808 for a 3×3 layer of the same width. The idea comes from the Network in Network paper (Lin et al., 2014). The paper uses it as a way to add non-linearity without changing what each neuron can see.
B, C and D form a small experiment of their own. C is B plus three 1×1 layers, and the best single-scale top-5 error falls from 9.9% to 8.8%, so the extra non-linearity helps. D has the same 16 layers as C with those three layers made 3×3, and the error falls again, to 8.1%, so spatial context helps more than non-linearity alone. The community name "VGG-16" refers to D.
The VGG-16 architecture, layer by layer
Follow one 224×224 image through VGG-16. The five pools halve the spatial size each time: 224, 112, 56, 28, 14, and finally 7. The channel count doubles after each pool, 64, 128, 256, 512, and then stops: stages 4 and 5 both have 512 channels, not 512 and 1024. The final feature map is 7×7×512, a vector of 25,088 numbers once flattened, and the classifier reads it.
Doubling the channels as the side halves keeps the work per layer level. A 3×3 conv with channels in and out, on an map, does multiply-adds. Halving divides by 4, and doubling multiplies by 4. A 128-channel conv at 112×112 and a 256-channel conv at 56×56 each do 1.85 billion multiply-adds. The cap at 512 breaks the pattern: the stage 5 convs at 14×14 do a quarter of that.
Most of the weights are outside the conv layers. FC1 maps 25,088 inputs to 4096 outputs, so it has weights, about 74% of VGG-16's 138 million parameters. The three fully-connected layers together hold about 89%. The 13 conv layers that give the network its depth hold about 14.7 million, roughly 10%.
Arithmetic is distributed the other way. A conv layer repeats its multiply-adds at every position of its feature map, while an FC layer does exactly one multiply-add per weight. The second conv of stage 1 has 36,928 parameters and runs at all 50,176 positions of the 224×224 map, for 1.85 billion multiply-adds. FC1, with about 2,800 times as many weights, does 0.10 billion. Across the network, the 13 conv layers do 15.35 billion multiply-adds per image (99.2%) and the three FC layers 0.12 billion. Figure 5 shows both splits; switch the tab to move the bar from weights to compute.
The whole network in pseudocode, with the tensor shape after each stage:
# VGG-16 (configuration D): 13 conv layers + 3 FC layers.
# convs(x, k, n): n stacked 3x3 convs, stride 1, pad 1, k filters, each + ReLU.
# pool(x): 2x2 max-pool, stride 2 (halves height and width).
def vgg16(x): # x: [B, 3, 224, 224], mean RGB subtracted
x = pool(convs(x, 64, 2)) # -> [B, 64, 112, 112]
x = pool(convs(x, 128, 2)) # -> [B, 128, 56, 56]
x = pool(convs(x, 256, 3)) # -> [B, 256, 28, 28]
x = pool(convs(x, 512, 3)) # -> [B, 512, 14, 14]
x = pool(convs(x, 512, 3)) # -> [B, 512, 7, 7]
x = flatten(x) # -> [B, 25088]
x = dropout(relu(fc(x, 4096))) # FC1: 25088 x 4096 = 102,760,448 weights
x = dropout(relu(fc(x, 4096))) # FC2: 4096 x 4096
return fc(x, 1000) # FC3: 1000 logits -> softmaxLater networks removed most of the classifier's weights: ResNet ends with a global average pool and a single 1000-way FC layer.
Configurations A to E and their errors
The paper's Table 1 lists six networks. A has 11 weight layers. A-LRN is A plus one local response normalisation layer, a per-position rescaling across neighboring channels used in AlexNet. B has 13. C has 16, three of them 1×1. D has 16, all 3×3. E has 19. Their parameter counts are 133, 133, 133, 134, 138 and 144 million: going from 11 to 19 layers adds only 11 million weights, because the added layers are convs and the 123.6-million-weight classifier is shared. Click a column in Figure 6, or use the slider, to read a configuration's depth, size and best single-scale top-5 error.
Three results come out of the table:
- Error falls with depth: top-5 10.4% for A, 9.9% for B, 8.8% for C, 8.1% for D, 8.0% for E (top-1 29.6, 28.7, 27.3, 25.6, 25.5).
- LRN does not help: A-LRN scores 10.5% against A's 10.4%, so the deeper networks leave it out.
- The gain stops at 19 layers. E improves on D by 0.1 point, and the paper says error saturates at this depth on ImageNet, adding that deeper models might help on larger datasets.
The best rows for C, D and E come from networks trained with scale jittering (covered in the next section), which A, A-LRN and B did not get. Compared at the same fixed training scale, , the top-5 errors are 10.4 (A), 9.9 (B), 9.4 (C), 8.8 (D) and 9.0 (E), so at that setting E is slightly worse than D.
VGG reports only that adding layers past 19 stopped helping. A year later, He et al. trained much deeper plain networks and found that they reached higher error on the training set than shallower ones, so the extra layers made optimisation worse rather than causing overfitting. They called this degradation and fixed it with ResNet's identity shortcuts.
Training a 19-layer network before batch norm
VGG was submitted in September 2014, five months before batch normalization (February 2015). The paper notes that a bad initialisation can stall learning in a deep net because the gradients become unstable. Its random initialisation draws every weight from a Gaussian with mean 0 and variance (standard deviation 0.1), and sets biases to 0. That variance ignores layer width. The initialisation of Glorot and Bengio sets the variance to , and for a 3×3 conv with 512 channels in and out, , which gives about , 46 times smaller than .
The paper avoids training a deep net from that starting point. It first trains configuration A, which is shallow enough to train from random weights. Each deeper configuration then copies A's first four conv layers and its three FC layers, initialises the layers in between randomly, and trains everything, the copied layers included, with no reduced learning rate. A footnote added after submission reports that Glorot and Bengio's random initialisation makes this pre-initialisation unnecessary; the published networks were still trained with it.
The rest of the recipe follows AlexNet. The loss is the multinomial logistic loss (softmax cross-entropy), minimised by mini-batch gradient descent with momentum 0.9 and batch size 256. Regularisation is L2 weight decay with multiplier and dropout on FC1 and FC2, which zeroes each unit with probability 0.5 during training. The learning rate starts at and is divided by 10 whenever validation accuracy stops improving, three times in all. Training stops after 370K iterations: 370,000 × 256 ≈ 95 million crops, or 74 passes over the 1.3 million images. Each crop is flipped horizontally at random and given a random RGB colour shift. On four NVIDIA Titan Black GPUs, one network took two to three weeks.
# One SGD iteration, as in Sec. 3.1 (scale-jittered model).
# Every weight in the network receives a gradient; nothing is frozen.
xs, ys = [], []
for img, label in sample(train_set, 256): # batch 256
S = randint(256, 512) # scale jittering
img = resize_shorter_side(img, S) # keep aspect ratio
x = random_crop(img, 224) # one 224x224 crop per image
x = color_shift(random_hflip(x)) # flip + RGB colour shift
xs.append(x - MEAN_RGB); ys.append(label)
loss = cross_entropy(vgg(xs), ys) # multinomial logistic loss
loss += 5e-4 * l2_penalty(weights) # weight decay
step(momentum=0.9, lr=lr) # lr: 1e-2, then /10 three times
# dropout 0.5 on FC1 and FC2 only; stop after 370K iterations (74 epochs)The crop is cut from a training image that has first been rescaled, keeping its aspect ratio, so that its shorter side is pixels. The paper calls the training scale. At the crop spans the whole shorter side and sees the full scene. At it spans 224/512, or 44%, of the shorter side, a close-up in which an object fills more of the 224×224 input. The paper trains some networks at a fixed or , and others with drawn at random from for every image, which it calls scale jittering. Drag in Figure 7, or press Jitter to draw training crops the way an iteration does.
Jittering gives one network practice at many object sizes. For configuration D tested at one scale, top-5 error is 8.8% trained at , 8.7% at , and 8.1% with jittered over . To save time, the jittered networks were fine-tuned from the fixed network of the same configuration, which was itself initialised from the network.
Dense evaluation: running on any image size
A trained VGG expects a 224×224 input, because FC1 needs a vector of exactly 25,088 numbers. At test time the paper removes that restriction by rewriting the FC layers as convolutions, a technique taken from OverFeat (Sermanet et al., 2014). FC1's 102,760,448 weights are the same numbers as 4096 filters of shape 7×7×512: on a 7×7×512 input, a 7×7 conv with those filters computes FC1's output exactly. FC2 and FC3 become 1×1 convs in the same way. After the rewrite the network contains only convolutions and pooling, and accepts any input size.
On a larger image, the network produces a grid of predictions. The test image is rescaled to shorter side , the test scale. The five pools divide the side by 32, and the 7×7 conv that replaced FC1 removes 6 more, so the class-score map has side
At , : one cell, the original network. At , : a 6×6 map of 36 overlapping 1000-way predictions, which the paper averages (sum-pools) into one. It also averages the scores of the image and its horizontal flip. Drag in Figure 8.
A single pass over the full image replaces many passes over crops, and the network can be run at several scales. The jittered models were tested at with the class probabilities averaged, which takes configuration D from 8.1% to 7.5% top-5. Networks trained at a fixed were tested only at , and , because a large gap between training and test scale hurt them.
The paper also runs the older multi-crop evaluation: 50 crops per scale (a 5×5 grid, each crop also flipped) at three scales, 150 forward passes per image. For D, multi-crop alone gives 7.5% top-5, the same as dense, and averaging the two gives 7.2%. The paper attributes this to how the two methods handle borders. A crop's feature maps are padded with zeros at its edges, while dense evaluation reads the real neighboring pixels there, so the two see different context.
Results: second in classification, first in localisation
VGG's ILSVRC-2014 classification entry was an ensemble of 7 networks, averaging their class probabilities. It scored 7.3% top-5 error on the test set and placed second. The winner, GoogLeNet, scored 6.7%, also with 7 networks. GoogLeNet was developed independently and also used depth (22 weight layers) and small filters. After the deadline, an ensemble of only D and E, using dense and multi-crop evaluation, reached 6.8%, and the best single network, E, reached 7.0%. For scale, the 2012 winner's 5-net ensemble scored 16.4% on the same test.
VGG won the localisation track, which asks for a bounding box around the object as well as its class, with 25.3% error. Its localisation network is a very deep ConvNet whose last FC layer predicts a box (center, width and height) instead of class scores.
The authors released configurations D and E, and the paper tests how their features carry to other datasets. It removes the 1000-way layer, uses the 4096-dimensional output of FC2 as an image descriptor, keeps all network weights frozen, and trains a linear SVM on each new dataset. With D and E combined this gave 89.7% mean average precision on PASCAL VOC-2007 and 86.2% mean class recall on Caltech-256, 8.6 points above the previous best on Caltech-256. VGG-16 also served as the backbone of detectors such as Faster R-CNN.
Later architectures kept VGG's 3×3 stages. U-Net builds each level from two 3×3 convs; ResNet's plain baselines follow VGG's rules (3×3 filters, channels doubling as the map halves) and add shortcuts to go past 100 layers. The Vision Transformer later replaced the convolution stack with attention over image patches.
Questions you might still have
Did VGG win ImageNet 2014?
No. GoogLeNet won the classification track with 6.7% top-5 test error from 7 networks; VGG placed second with 7.3%, also from 7 networks. VGG won the separate localisation track with 25.3% error. After the deadline a two-network VGG ensemble reached 6.8%, and the best single VGG network reached 7.0%, but neither was the competition entry.
Is a stack of three 3×3 convs the same as one 7×7 conv?
Only in which input pixels it can see: both cover a 7×7 patch. The stack has a ReLU after each layer, three in all against one for the 7×7 layer, so it computes functions no single 7×7 conv can. Remove the two inner ReLUs and the three convolutions compose into one 7×7 linear filter, and a restricted one, since 27C² weights generate it instead of 49C².
If the conv layers hold so few weights, why is VGG slow?
Weights and arithmetic sit in different layers. About 89% of the 138M weights are in the three fully-connected layers, FC1 alone holding 102.76M. But a conv layer repeats its multiply-adds at every position of its feature map, so the 13 conv layers do about 15.35 billion multiply-adds per 224×224 image, 99.2% of the total, while the FC layers do one multiply-add per weight, about 0.12 billion.
Why does VGG use 3×3 filters and not 2×2?
The paper picks 3×3 as the smallest filter that captures left/right, up/down and center. An even-sized filter has no center pixel, and the stride-1 padding that keeps the size unchanged, P = (F − 1)/2, is a whole number only for odd F.
What is top-5 error?
The fraction of test images whose correct class is not among the network's five highest-scoring classes, out of 1000. It was ILSVRC's main metric. Top-1 error counts an image as wrong unless the single best guess is correct. VGG-16's best single-scale validation numbers are 25.6% top-1 and 8.1% top-5.
Why not keep adding layers past 19?
In the paper, error stops improving at 19 layers on ImageNet; the authors suggest deeper nets might help on larger datasets. A year later the ResNet paper showed that much deeper plain networks of this kind reach higher training error than shallower ones, and added identity shortcuts that let networks train past 100 layers. The ResNet explainer covers that.
Can I use the released VGG weights as a feature extractor?
That is how the paper tested transfer: drop the 1000-way layer, take the 4096-dimensional output of the second FC layer as the image descriptor, keep the network weights frozen, and train a linear SVM on the new dataset. With configs D and E combined this gave 89.7% mean average precision on PASCAL VOC-2007 and 86.2% on Caltech-256.
Footnotes & further reading
- The paper: Simonyan & Zisserman, Very Deep Convolutional Networks for Large-Scale Image Recognition (Visual Geometry Group, Oxford; ICLR 2015). The released models D and E are the original VGG-16 and VGG-19.
- The ConvNet baseline VGG deepens: Krizhevsky, Sutskever & Hinton, ImageNet Classification with Deep Convolutional Neural Networks (AlexNet, 2012).
- The 1×1 convolution as a channel mixer: Lin, Chen & Yan, Network In Network (2014).
- Dense, fully-convolutional evaluation: Sermanet et al., OverFeat (2014).
- The concurrent very deep network that won ILSVRC-2014 classification: Szegedy et al., Going Deeper with Convolutions (GoogLeNet, 2014).
- The degradation problem and identity shortcuts: He, Zhang, Ren & Sun, Deep Residual Learning for Image Recognition (ResNet, 2015), explained here.
- The initialisation noted post-submission: Glorot & Bengio, Understanding the difficulty of training deep feedforward neural networks (AISTATS 2010).
- Batch normalization, which postdates VGG: Ioffe & Szegedy, Batch Normalization (2015), explained here.
How could this explainer be improved? Found an error, or something unclear? I read every message.