Blocks, Bottlenecks, and Branches: VGG, NiN, GoogLeNet

Dive into Deep Learning · §7.2

Blocks, Bottlenecks, and Branches: VGG, NiN, GoogLeNet
repeatable 3\times 3 blocks · 1\times 1 channel mixing · global average pooling · multi-branch Inception

Two Structural Flaws in AlexNet’s Layer-by-Layer Design

Motivation & Design Bottlenecks

  • Ad-hoc layer lists: Choosing 11 \times 11, 5 \times 5, and 3 \times 3 kernels layer by layer offers no systematic template for 20+ layer networks.
  • 43M-parameter dense head: Holds 92% of AlexNet’s weights for only 5% of its MACs.
  • Three solutions: VGG (3 \times 3 blocks), NiN (1 \times 1 convs + Global Avg Pool), and GoogLeNet (multi-branch 1 \times 1 bottlenecks in a stem–body–head layout).

Why Stack Narrow 3×3 Convolutions Over Large Kernels?

VGG: Receptive Field per FLOP

  • Matched receptive field: Two 3 \times 3 layers span 5 \times 5; three 3 \times 3 layers span 7 \times 7 (r = 2k+1).
  • Fewer weights & MACs: Two 3 \times 3 use 18c^2 vs. 25c^2 (28% fewer); three 3 \times 3 use 27c^2 vs. 49c^2 (45% fewer).
  • Richer nonlinearity: Inserts k ReLU activations instead of 1.

Implementing a Reusable VGG Block

VGG: Modular Block Code

A VGG block repeats num_convs padded 3\times 3 convolutions (p=1, preserving height and width) followed by 2\times 2 stride-2 max-pooling that halves spatial dimensions:

def vgg_block(num_convs, out_channels):
    layers = []
    for _ in range(num_convs):
        layers.append(nn.LazyConv2d(out_channels, kernel_size=3, padding=1))
        layers.append(nn.ReLU())
    layers.append(nn.MaxPool2d(kernel_size=2, stride=2))
    return nn.Sequential(*layers)

Network Topology as Data: The VGG Family

VGG: Parameterized Architecture

Because every VGG block is defined by a (num_convs, out_channels) tuple, an entire architecture family is specified by passing an arch list to one class:

  • VGG-11 specification: arch = ((1, 64), (1, 128), (2, 256), (2, 512), (2, 512)) (8 convs + 3 dense layers = 11 layers).
  • Resolution & width invariant: Five stride-2 pools halve 224 \to 112 \to 56 \to 28 \to 14 \to 7 while channels double 64 \to 128 \to 256 \to 512, keeping per-layer compute h w c^2 roughly constant across stages.
  • VGG-16 and VGG-19: Simply change arch to (2, 2, 3, 3, 3) or (2, 2, 4, 4, 4) blocks without altering class code.

VGG-11 Shapes and the 103M-Parameter Dense Bottleneck

VGG: Resource Ledger

Inspecting VGG-11 on a 224\times 224 input shows that modularizing the convolutional trunk left the classifier head even more bloated than AlexNet’s:

  • Convolutional trunk: 8 layers hold 9.2\text{M} parameters (7%) and execute 7.43\text{G} multiply-adds (98% of compute—8\times AlexNet).
  • Flattened dense head: The final 512 \times 7 \times 7 map flattens to 25{,}088 features, making the first dense matrix 25{,}088 \times 4096 = 102.8\text{M} weights (119.6\text{M} across all three dense layers, or 93% of VGG-11’s 128.8M parameters).
Sequential             (1, 64, 112, 112)
Sequential             (1, 128, 56, 56)
Sequential             (1, 256, 28, 28)
Sequential             (1, 512, 14, 14)
Sequential             (1, 512, 7, 7)
Flatten                (1, 25088)
Linear                 (1, 4096)
ReLU                   (1, 4096)
Dropout                (1, 4096)
Linear                 (1, 4096)
ReLU                   (1, 4096)
Dropout                (1, 4096)
Linear                 (1, 10)

Training Quarter-Width VGG-11 on Fashion-MNIST

VGG: Experimental Evidence

Halving channel widths fourfold to (16, 32, 64, 128, 128) cuts compute to 512\text{M} multiply-adds (half of AlexNet) and trains cleanly at peak learning rate \eta_0 = 0.1:

  • Accuracy vs. compute: Quarter-width VGG reaches \approx 93\% validation accuracy in 10 epochs—2 points ahead of AlexNet at half the arithmetic (and 91\text{--}92\% at AlexNet’s \eta_0 = 0.03).
  • The head problem persists: Shrinking the trunk cuts convolutional weights to 0.6\text{M}, yet the dense head (6272 \to 4096 \to 4096 \to 10) still holds 42.5\text{M} of the 43.1\text{M} parameters (99%).

NiN: Per-Pixel Channel Mixing with 1×1 Convolutions

Network in Network (NiN)

def nin_block(out_channels, kernel_size, strides, padding, num_classes=None):
    layers = [
        nn.LazyConv2d(out_channels, kernel_size, strides, padding), nn.ReLU(),
        nn.LazyConv2d(out_channels, kernel_size=1), nn.ReLU()]
    if num_classes is None:
        layers += [nn.LazyConv2d(out_channels, kernel_size=1), nn.ReLU()]
    else:  # Last block: one map of scores per class, no ReLU
        layers.append(nn.LazyConv2d(num_classes, kernel_size=1))
    return nn.Sequential(*layers)
  • Shared per-pixel MLP: Follows each k \times k spatial conv with two 1 \times 1 convs + ReLU across channels at every (x, y).

Global Average Pooling Eliminates the 43M-Parameter Head

NiN: Parameter-Free Classification Head

Instead of flattening feature maps into massive dense layers, NiN projects the final block directly to K class channels and averages each channel spatially:

  • Zero-parameter spatial aggregation: The final NiN block emits one h \times w feature map per class (K=10), and global average pooling computes the mean of each 5\times 5 map to yield the 10 class logits: z_k = \frac{1}{h w} \sum_{x=1}^h \sum_{y=1}^w X_{k, x, y}
  • Built-in translation invariance: Averaging over all (x,y) coordinates makes class scores invariant to where an object appears.
  • 14× parameter reduction: Total parameters drop from AlexNet’s 46.8\text{M} to 3.4\text{M} (798\text{M} MACs).

Critical Pitfall: Never Put a ReLU on Final Logit Maps

NiN: Training & Implementation Pitfall

When num_classes is passed to the final nin_block, omitting the ReLU after the final 1\times 1 convolution is essential for trainability:

  • The dead-class failure mode: If a ReLU followed the final 1\times 1 layer, any class channel whose 5\times 5 spatial entries all became negative would be clamped to 0 everywhere, producing logit z_k = 0 and gradient \partial L / \partial W = 0 identically—permanently killing that class.
  • Training NiN on Fashion-MNIST: At peak learning rate \eta_0 = 0.03, NiN reaches \approx 89\% validation accuracy (training loss \approx 0.30, underfitting slightly in 10 epochs without overfitting).

Controlled Ablation: Global Average Pooling on VGG

Head Ablation: VGG vs. PooledVGG

class PooledVGG(VGG):
    def head(self):  # Global average pooling and one fully connected layer
        return [nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(),
                nn.LazyLinear(self.num_classes)]

Controlled Ablation: Global Average Pooling on VGG (cont.)

Head Ablation: VGG vs. PooledVGG

Training the dense-headed VGG at peak rate 0.03:

Controlled Ablation: Global Average Pooling on VGG (result)

Head Ablation: VGG vs. PooledVGG

Training PooledVGG at the same rate, then both final validation accuracies:

{'VGG': 0.914, 'PooledVGG': 0.891}

GoogLeNet: Multi-Scale Parallel Branches and Bottlenecks

GoogLeNet & The Inception Block

Objects in natural images appear at wildly different spatial scales. Rather than committing to a single kernel size per layer, GoogLeNet’s Inception block runs four parallel branches and concatenates their outputs along the channel axis:

  • Four parallel spatial scales:
    1. 1\times 1 conv (c_1 ch): pixelwise channel features.
    2. 1\times 1 reduction \to 3\times 3 conv (c_{2,1} \to c_{2,2} ch).
    3. 1\times 1 reduction \to 5\times 5 conv (c_{3,1} \to c_{3,2} ch).
    4. 3\times 3 stride-1 max-pool \to 1\times 1 conv (c_4 ch).
  • 1×1 dimensionality-reduction bottlenecks: Squeeze input channels c_{\textrm{i}} into a narrow intermediate subspace before expensive 3\times 3 and 5\times 5 filters.

Implementing the Four-Branch Inception Block

GoogLeNet: Block Code

“Same” padding keeps all four branch outputs the same size for channel concatenation:

class Inception(nn.Module):
    # c1--c4 are the numbers of output channels of the four branches
    def __init__(self, c1, c2, c3, c4):
        super().__init__()
        self.b1_1 = nn.LazyConv2d(c1, kernel_size=1)
        self.b2_1 = nn.LazyConv2d(c2[0], kernel_size=1)
        self.b2_2 = nn.LazyConv2d(c2[1], kernel_size=3, padding=1)
        self.b3_1 = nn.LazyConv2d(c3[0], kernel_size=1)
        self.b3_2 = nn.LazyConv2d(c3[1], kernel_size=5, padding=2)
        self.b4_1 = nn.MaxPool2d(kernel_size=3, stride=1, padding=1)
        self.b4_2 = nn.LazyConv2d(c4, kernel_size=1)

    def forward(self, x):
        b1 = F.relu(self.b1_1(x))
        b2 = F.relu(self.b2_2(F.relu(self.b2_1(x))))
        b3 = F.relu(self.b3_2(F.relu(self.b3_1(x))))
        b4 = F.relu(self.b4_2(self.b4_1(x)))
        return torch.cat((b1, b2, b3, b4), dim=1)

Why 1×1 Bottlenecks Cut Spatial Filter Cost Tenfold

GoogLeNet: Bottleneck Arithmetic

  • Direct 5 \times 5 branch (192 \to 32): Costs 25 \cdot 192 \cdot 32 = 153{,}600 weights (120.4\textrm{M} MACs at 28 \times 28).
  • With 1 \times 1 bottleneck (192 \to 16 \to 32): Costs 3{,}072 + 12{,}800 = 15{,}872 weights (12.4\textrm{M} MACs)—a 9.7\times reduction in both parameters and compute.

The Stem–Body–Head Macro Blueprint

GoogLeNet: Macro Architecture

GoogLeNet established the three-part macro template inherited by ResNet, MobileNet, ConvNeXt, and RegNet:

  • Rapidly downsampling stem: A 7\times 7 stride-2 conv, stride-2 max-pool, 1\times 1 conv, 3\times 3 conv, and second stride-2 max-pool reduce spatial resolution 8\times while expanding to 192 channels.
  • Three-stage Inception body: Stacks 2, 5, and 2 Inception blocks separated by stride-2 max-pooling (192 \to 480 \to 832 \to 1024 channels across 22 total weight layers).
  • Global average pooling head: Averages the 1024 final channels into a 1024-vector feeding one 1024 \to 10 linear layer.
Sequential             (1, 192, 12, 12)
Sequential             (1, 480, 12, 12)
MaxPool2d              (1, 480, 6, 6)
Sequential             (1, 832, 6, 6)
MaxPool2d              (1, 832, 3, 3)
Sequential             (1, 1024, 3, 3)
Sequential             (1, 10)

Training GoogLeNet and Comparing All Five Architectures

Empirical Comparison

Trained on 96\times 96 Fashion-MNIST at \eta_0 = 0.03, GoogLeNet matches AlexNet’s 91% validation accuracy with 8\times fewer parameters (6.0\text{M} vs. 46.8\text{M}):

Model Input Params MACs Peak \eta_0 Val Acc
LeNet (30 ep) 28^2 0.06M 0.4M 2.0 88–90%
AlexNet 224^2 46.8M 938M 0.03 91%
Quarter-VGG 224^2 43.1M 512M 0.1 / 0.03 93% / 91–92%
PooledVGG 224^2 0.6M 470M 0.03 89–90%
NiN 224^2 3.4M 798M 0.03 89%
GoogLeNet 96^2 6.0M 276M 0.03 91%

Concept Check: Block Design & Bottleneck Arithmetic

Concept Check

Test your understanding of blocks, bottlenecks, and pooling heads:

  1. Stacked 3\times 3 vs. 7\times 7: A stage operates at c = 256 channels. How many parameters does a stack of three 3\times 3 convolutions require compared with a single 7\times 7 convolution (ignoring biases), and what receptive field do both span?
  2. Inception 3\times 3 bottleneck: In GoogLeNet’s first Inception block (c_{\textrm{i}} = 192), branch 2 reduces 192 \to 96 with a 1\times 1 conv before a 3\times 3 conv emitting 128 channels. By what factor does the 1\times 1 reduction shrink the branch’s weight count compared with a direct 192 \to 128 3\times 3 conv?

Answers:

  1. Both span a 7\times 7 receptive field. Three 3\times 3 layers use 3 \cdot 9 \cdot 256^2 = 1{,}769{,}472 weights versus 49 \cdot 256^2 = 3{,}211{,}264 for one 7\times 7 layer—saving 1.44\text{M} weights (45\%) while adding two extra ReLU nonlinearities.
  2. Direct 3\times 3: 9 \cdot 192 \cdot 128 = 221{,}184 weights. With the 1\times 1 bottleneck: 192 \cdot 96 + 9 \cdot 96 \cdot 128 = 18{,}432 + 110{,}592 = 129{,}024 weights (a 1.71\times reduction).

Summary: Key Takeaways from VGG, NiN, and GoogLeNet

Summary

  • VGG (3 \times 3 blocks): Parameterizes network topology via (num_convs, c) stage tuples.
  • NiN (1 \times 1 + GAP): Mixes channels per pixel and eliminates >90\% of weights by replacing dense heads with Global Average Pooling.
  • GoogLeNet (Stem–Body–Head): Multi-scale Inception branches with 1 \times 1 bottlenecks inside the universal 3-part backbone layout.