高级CNN架构:ResNet、EfficientNet与视觉Transformer
高级CNN架构:ResNet、EfficientNet与视觉Transformer
CNN的发展历程
卷积神经网络(CNN)在计算机视觉领域取得了巨大成功。从LeNet到ResNet,CNN架构不断演进,性能持续提升。
ResNet:残差学习
问题:深度网络的退化
随着网络层数增加,训练误差反而上升,这不是过拟合,而是优化困难。
解决方案:残差连接
ResNet通过引入跳跃连接(skip connection),让网络学习残差函数而非直接映射。
import torch
import torch.nn as nn
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super().__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, 3, stride, 1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, 1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
# 快捷连接
self.shortcut = nn.Sequential()
if stride != 1 or in_channels != out_channels:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1, stride, bias=False),
nn.BatchNorm2d(out_channels)
)
def forward(self, x):
residual = self.shortcut(x)
out = self.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += residual # 残差连接
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, num_classes=1000):
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, 7, 2, 3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2, 1)
)
self.layer1 = self._make_layer(64, 64, 2, stride=1)
self.layer2 = self._make_layer(64, 128, 2, stride=2)
self.layer3 = self._make_layer(128, 256, 2, stride=2)
self.layer4 = self._make_layer(256, 512, 2, stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512, num_classes)
def _make_layer(self, in_channels, out_channels, num_blocks, stride):
layers = [ResidualBlock(in_channels, out_channels, stride)]
for _ in range(1, num_blocks):
layers.append(ResidualBlock(out_channels, out_channels))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
return x
ResNet的变体
- ResNet-18/34:基本残差块
- ResNet-50/101/152:瓶颈残差块(1x1 -> 3x3 -> 1x1)
DenseNet:密集连接
DenseNet将每一层与前面所有层连接,促进特征重用。
class DenseBlock(nn.Module):
def __init__(self, in_channels, growth_rate, num_layers):
super().__init__()
self.layers = nn.ModuleList()
for i in range(num_layers):
self.layers.append(
nn.Sequential(
nn.BatchNorm2d(in_channels + i * growth_rate),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels + i * growth_rate, growth_rate, 3, 1, 1, bias=False)
)
)
def forward(self, x):
features = [x]
for layer in self.layers:
x = torch.cat(features, dim=1)
x = layer(x)
features.append(x)
return x
class Transition(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.transition = nn.Sequential(
nn.BatchNorm2d(in_channels),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.AvgPool2d(2, 2)
)
def forward(self, x):
return self.transition(x)
EfficientNet:复合缩放
EfficientNet通过同时缩放网络的深度、宽度和分辨率,实现更好的性能-效率平衡。
复合缩放公式
depth: d = α^φ
width: w = β^φ
resolution: r = γ^φ
约束:α · β^2 · γ^2 ≈ 2
基础网络(MBConv)
class MBConv(nn.Module):
def __init__(self, in_channels, out_channels, stride, expand_ratio):
super().__init__()
hidden_dim = in_channels * expand_ratio
self.use_residual = (stride == 1 and in_channels == out_channels)
layers = []
if expand_ratio != 1:
layers.extend([
nn.Conv2d(in_channels, hidden_dim, 1, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True)
])
layers.extend([
nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True),
nn.Conv2d(hidden_dim, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels)
])
self.conv = nn.Sequential(*layers)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
if self.use_residual:
return x + self.conv(x)
return self.conv(x)
Vision Transformer (ViT)
ViT将Transformer应用于图像分类,证明了纯Transformer架构也能在视觉任务上取得优异性能。
核心思想
将图像分割成固定大小的patch,每个patch作为一个token输入Transformer。
class PatchEmbedding(nn.Module):
def __init__(self, img_size, patch_size, in_channels, d_model):
super().__init__()
self.num_patches = (img_size // patch_size) ** 2
self.proj = nn.Conv2d(in_channels, d_model, kernel_size=patch_size, stride=patch_size)
self.cls_token = nn.Parameter(torch.randn(1, 1, d_model))
self.pos_embedding = nn.Parameter(torch.randn(1, self.num_patches + 1, d_model))
def forward(self, x):
batch_size = x.size(0)
x = self.proj(x) # (B, D, H/P, W/P)
x = x.flatten(2).transpose(1, 2) # (B, num_patches, D)
cls_tokens = self.cls_token.expand(batch_size, -1, -1)
x = torch.cat([cls_tokens, x], dim=1)
x += self.pos_embedding
return x
class VisionTransformer(nn.Module):
def __init__(self, img_size=224, patch_size=16, in_channels=3,
d_model=768, num_heads=12, num_layers=12, num_classes=1000):
super().__init__()
self.patch_embedding = PatchEmbedding(img_size, patch_size, in_channels, d_model)
encoder_layer = nn.TransformerEncoderLayer(
d_model=d_model,
nhead=num_heads,
dim_feedforward=d_model * 4,
dropout=0.1,
activation='gelu'
)
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
self.norm = nn.LayerNorm(d_model)
self.head = nn.Linear(d_model, num_classes)
def forward(self, x):
x = self.patch_embedding(x)
x = self.transformer(x)
x = self.norm(x)
x = x[:, 0] # 取CLS token
x = self.head(x)
return x
架构对比
| 架构 | 核心思想 | 参数量 | Top-1准确率 |
|---|---|---|---|
| ResNet-50 | 残差连接 | 25M | 76.1% |
| DenseNet-121 | 密集连接 | 8M | 74.8% |
| EfficientNet-B0 | 复合缩放 | 5M | 77.1% |
| ViT-B/16 | Patch嵌入+Transformer | 86M | 77.9% |
总结
从ResNet的残差学习到EfficientNet的复合缩放,再到Vision Transformer的架构革新,CNN架构不断演进。理解这些架构的设计思想对于构建高效的计算机视觉系统至关重要。