张芷铭的个人博客

ResNet:深度残差学习的革命性架构

定义

残差网络(Residual Network, ResNet)是由微软研究院提出的深度卷积神经网络架构,其核心创新在于残差学习(Residual Learning)机制。通过引入跳跃连接(Skip Connection),解决了深度神经网络中的梯度消失/爆炸问题,使得网络深度可以显著增加(超过1000层)。数学表达为:

$$ y = F(x, {W_i}) + x $$

其中$x$是输入,$F$是残差函数,$y$是输出。

发展历史

  • 2012年:AlexNet在ImageNet竞赛夺冠,开启CNN时代
  • 2014年:VGGNet证明深度增加提升性能,但出现梯度问题
  • 2015年:何恺明团队发表论文《Deep Residual Learning for Image Recognition》,提出ResNet
  • 2016年:ResNet-152在ImageNet达到3.57%错误率,首次超越人类水平(5.1%)
  • 后续发展:衍生出ResNeXt、Wide ResNet等变体

核心原理

残差块设计

graph LR
    x --> Conv1 --> ReLU --> Conv2 --> + --> ReLU --> y
    x --> ----------------------------+

标准残差块包含:

  1. 两个3×3卷积层(带BatchNorm)
  2. 恒等映射(Identity Mapping)的跳跃连接
  3. 最后使用ReLU激活

数学表达式: $$ y = \text{ReLU}(F(x) + x) $$

网络架构演进

版本深度关键改进
ResNet-1818基础残差块
ResNet-3434增加块重复次数
ResNet-5050引入瓶颈结构(Bottleneck)
ResNet-101101深层架构优化
ResNet-152152当前常用最大标准版本

瓶颈结构公式: $$ y = W_2 \cdot \text{ReLU}(W_1 \cdot x) + x $$ 其中$W_1$是1×1降维卷积,$W_2$是3×3卷积

适用场景

计算机视觉任务

  1. 图像分类:ImageNet等基准测试
  2. 目标检测:Faster R-CNN的骨干网络
  3. 语义分割:FCN+ResNet组合
  4. 姿态估计:HRNet的基础架构

实际应用领域

  • 医学影像分析(X光分类)
  • 自动驾驶(场景理解)
  • 工业质检(缺陷检测)

实践经验

训练技巧

  1. 学习率策略
    • 初始学习率0.1
    • 每30个epoch除以10
  2. 权重初始化
    1
    
    nn.init.kaiming_normal_(conv.weight, mode='fan_out')
    
  3. 数据增强
    • 随机裁剪(224×224)
    • 水平翻转
    • 颜色抖动

调参建议

  • Batch Size:256(需多GPU并行)
  • 优化器:SGD with momentum=0.9
  • 权重衰减:1e-4

PyTorch实现

残差块代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class BasicBlock(nn.Module):
    expansion = 1
    
    def __init__(self, in_planes, planes, stride=1):
        super().__init__()
        self.conv1 = nn.Conv2d(in_planes, planes, 3, stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, 3, stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        
        # 下采样匹配维度
        self.shortcut = nn.Sequential()
        if stride != 1 or in_planes != self.expansion*planes:
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_planes, self.expansion*planes, 1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*planes)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.bn2(self.conv2(out))
        out += self.shortcut(x)
        return F.relu(out)

完整网络构建

1
2
3
4
5
6
7
def make_layer(block, planes, num_blocks, stride):
    layers = []
    layers.append(block(self.in_planes, planes, stride))
    self.in_planes = planes * block.expansion
    for _ in range(1, num_blocks):
        layers.append(block(self.in_planes, planes, stride=1))
    return nn.Sequential(*layers)

改进方向

主流变体架构

  1. ResNeXt:分组卷积增强特征多样性 $$ y = \sum_{i=1}^C \mathcal{T}_i(x) + x $$
  2. Wide ResNet:增加通道数替代加深
  3. Res2Net:多尺度特征融合

最新进展

  • 2020年:ResNet-D(改进下采样模块)
  • 2021年:ResNet-RS(针对TPU优化)
  • 2022年:ConvNeXt(受Swin Transformer启发)

参考文献

💬 评论