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?
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:
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:
Resolution & width invariant: Five stride-2 pools halve 224\to112\to56\to28\to14\to7 while channels double 64\to128\to256\to512, 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).
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\to4096\to4096\to10) 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 isNone: 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 layerreturn [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:
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 branchesdef__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)
Test your understanding of blocks, bottlenecks, and pooling heads:
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?
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 1283\times 3 conv?
Answers:
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.
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.