resnet50网络结构图 各阶段输出
时间: 2025-05-22 10:51:43 浏览: 21
### ResNet50 架构详解
ResNet50 是一种深度残差网络,具有 50 层的深度。其设计通过引入残差块解决了深层神经网络中的梯度消失问题。
#### 架构概述
ResNet50 的主要组成部分如下:
- **输入层**
输入图像大小通常为 \(224 \times 224\) 像素[^2]。
- **初始卷积层**
使用一个 \(7 \times 7\), 步幅为 2, 输出通道数为 64 的卷积核处理输入图片,随后是一个最大池化操作 (kernel size=3×3, stride=2),此时输出尺寸变为 \(56\times56\times64\)。
- **四个阶段(Stage)**
整体分为四个阶段,每个阶段由多个瓶颈(bottleneck)模块组成。这些模块内部采用1x1、3x3 和另一个 1x1 卷积组合来减少计算量并增加非线性变换能力。
| 阶段 | 模块数量 | 特征图尺寸 |
| --- | -------- | ---------- |
| Stage 2 | 3 | \(56 \times 56\) |
| Stage 3 | 4 | \(28 \times 28\) |
| Stage 4 | 6 | \(14 \times 14\) |
| Stage 5 | 3 | \(7 \times 7\) |
每经过一次下采样(步长为2的最大池化或特定卷积),空间维度减半而通道数目翻倍直到最后达到 2048 维。
- **全局平均池化与分类器**
在最后一个卷积层之后应用全局平均池化得到固定长度向量作为全连接层前馈输入;接着是用于 ImageNet 数据集上1000类别的 softmax 分类头。
```python
import torch.nn as nn
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
```
阅读全文
相关推荐


















