resnet34原理图
时间: 2025-03-14 09:15:52 浏览: 106
### ResNet34 的网络结构及工作原理
#### 网络结构概述
ResNet34 是一种基于残差网络(Residual Network)设计的深层卷积神经网络模型。它属于较浅层的 ResNet 架构之一,采用了 BasicBlock 结构作为其基本组成单元[^1]。整个网络由多个这样的 BasicBlock 堆叠而成。
#### BasicBlock 结构
BasicBlock 是 ResNet 中用于实现残差连接的一种基础模块,在 ResNet34 中被广泛使用。它的主要特点是通过引入 shortcut connection 来缓解梯度消失问题并促进训练过程中的信息流动[^3]。具体来说:
- **输入部分**:接收来自前一层的数据特征图。
- **两个连续的 3×3 卷积操作**:
- 第一个卷积层通常具有相同的通道数,并应用 ReLU 激活函数。
- 第二个卷积层保持相同配置,但不立即激活。
- **Shortcut Connection (跳跃连接)**:将原始输入直接加到第二个卷积后的输出上。如果维度匹配,则无需额外变换;如果不匹配,则可以通过 1×1 卷积调整尺寸[^2]。
最终经过一次 ReLU 激活后得到该 block 的输出。
以下是 Python 实现的一个简化版 `BasicBlock` 定义:
```python
import torch.nn as nn
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_channels, out_channels, stride=1, downsample=None):
super(BasicBlock, self).__init__()
# First convolutional layer with kernel size of 3x3 and specified strides.
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
# Second convolutional layer also using a 3x3 kernel but without any striding here unless necessary.
self.conv2 = nn.Conv2d(out_channels, out_channels * self.expansion, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channels * self.expansion)
# Optional downsampling operation when dimensions do not match between input & output tensors.
self.downsample = downsample
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
```
#### 整体架构描述
ResNet34 总共包含 34 层参数化的权重层(即有学习参数的层)。这些层分布在以下几个阶段中:
| Stage | Number of Blocks | Output Channels |
|-------|------------------|-----------------|
| Conv1 | N/A | 64 |
| Layer1| 3 | 64 |
| Layer2| 4 | 128 |
| Layer3| 6 | 256 |
| Layer4| 3 | 512 |
每个 stage 都是由若干个 BasicBlocks 组成,其中第一个 Block 可能会改变 feature map 的大小或者 channel 数量,因此需要设置合适的步幅(stride) 和降采样(downsample)。
最后接上的全连接层用来分类任务预测类别概率分布情况。
#### 工作原理解析
ResNet 提出了 residual learning framework ,使得即使非常深的网络也可以有效训练出来。核心思想就是让每一层去拟合目标映射 F(x)=H(x)-x 而不是 H(x),这样可以更容易优化更深层次网络的学习过程 。当遇到退化现象时(增加更多层反而性能下降),residual structure 就能够很好地解决问题。
---
###
阅读全文
相关推荐















