3. Modern CNN Architectures
The Evolution of Classic CNN Architectures
The history of convolutional neural networks is marked by a quest for deeper representations. Starting with LeNet, which introduced the fundamental stack of convolutional, pooling, and fully connected layers, the field was revolutionized by AlexNet in 2012. AlexNet utilized ReLU activation functions and Dropout to mitigate vanishing gradients and overfitting, respectively. These innovations allowed networks to scale significantly in size.
Following AlexNet, the VGG architecture shifted the paradigm toward architectural simplicity by utilizing small 3x3 kernels stacked repeatedly. This design choice demonstrated that increasing the depth of the network while maintaining a small receptive field per layer was more effective than using larger, sparse filters. Despite their success, these models suffer from the degradation problem, where deeper models exhibit higher training error as optimization becomes increasingly difficult.
import torch.nn as nn # Simple VGG-style block: Small kernels repeated class VGGBlock(nn.Module): def __init__(self, in_channels, out_channels): super().__init__() self.conv = nn.Sequential( nn.Conv2d(in_channels, out_channels, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(out_channels, out_channels, 3, padding=1), nn.ReLU(inplace=True) ) def forward(self, x): return self.conv(x)
Residual Learning and Skip Connections
The degradation problem occurs because deep networks are difficult to optimize. ResNet solved this by introducing skip connections, which allow gradients to flow through the network without passing through every non-linear transformation. By reformulating the layers to learn a residual mapping $$F(x) = H(x) - x$$, the network effectively learns the difference between the identity and the target mapping, which is much simpler than learning the direct representation H(x).
To maintain efficiency, ResNet utilizes bottleneck blocks for deeper variants like ResNet-50. These blocks use 1x1 convolutions to reduce dimensionality, followed by a 3x3 convolution, and another 1x1 convolution to restore dimensions. This reduces computational complexity significantly while maintaining deep feature representations.
import torch class ResidualBlock(nn.Module): def __init__(self, channels): super().__init__() self.conv = nn.Sequential( nn.Conv2d(channels, channels, 3, padding=1), nn.ReLU(), nn.Conv2d(channels, channels, 3, padding=1) ) def forward(self, x): # Skip connection: Add original input to output return torch.relu(self.conv(x) + x)
MobileNet and Depthwise Separable Convolutions
MobileNet architectures are designed for efficient, low-power inference. The core innovation is the use of depthwise separable convolutions, which decompose a standard convolution into two distinct steps: a depthwise convolution that performs spatial filtering on each input channel independently, and a pointwise (1x1) convolution that combines these features across channels.
This approach drastically reduces the parameter count and computational cost (FLOPs) compared to standard convolutions while maintaining competitive accuracy. It is highly optimized for deployment on mobile devices where memory access and throughput are significant bottlenecks.
import torch.nn as nn class DepthwiseSeparable(nn.Module): def __init__(self, in_ch, out_ch): super().__init__() self.depthwise = nn.Conv2d(in_ch, in_ch, 3, groups=in_ch, padding=1) self.pointwise = nn.Conv2d(in_ch, out_ch, 1) def forward(self, x): return self.pointwise(self.depthwise(x))
Knowledge Check
Ready to test your understanding of 3. Modern CNN Architectures?