resnet18网络架构图
时间: 2024-12-31 13:30:35 浏览: 21
### ResNet18 的网络架构
ResNet18 是一种经典的卷积神经网络 (CNN),它通过引入 shortcut 连接来解决深度网络中的梯度消失问题,从而允许训练更深的网络结构。这种设计使得即使在网络加深的情况下也能有效防止性能下降。
#### 架构特点
- **基础模块**:由两个3×3卷积层组成的基础构建单元。
- **残差连接**:每个基本块内含有跳跃连接(skip connection),即所谓的快捷路径,可以绕过一层或多层直接传递信息给后续层[^1]。
#### 层级分布
ResNet18 总共有 18 层权重参数的学习层,具体分为以下几个部分:
| 阶段 | 类型 | 输出尺寸 |
|-----|-------------|
| 初始阶段 | 卷积 + BN + ReLU | H/2 × W/2 |
| Stage 1 | BasicBlock x2 | H/4 × W/4 |
| Stage 2 | BasicBlock x2 | H/8 × W/8 |
| Stage 3 | BasicBlock x2 | H/16 × W/16 |
| Stage 4 | BasicBlock x2 | H/32 × W/32 |
其中 `H` 和 `W` 表示输入图片的高度和宽度;BN代表批标准化操作;ReLU表示激活函数修正线性单位。
#### PyTorch 实现代码片段
以下是基于PyTorch框架的一个简化版ResNet18定义:
```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__()
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)
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)
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
def make_layer(block, planes, blocks, stride=1):
layers = []
downsample = None
if stride != 1 or inplanes != block.expansion * planes:
downsample = nn.Sequential(
nn.Conv2d(inplanes, block.expansion*planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion))
layers.append(block(inplanes, planes, stride, downsample))
for _ in range(1, blocks):
layers.append(block(planes * block.expansion, planes))
global inplanes
inplanes = planes * block.expansion
return nn.Sequential(*layers)
inplanes = 64
resnet18_layers = [
nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,bias=False), # Initial Convolution Layer
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
make_layer(BasicBlock, 64, 2, stride=1), # Stage 1
make_layer(BasicBlock, 128, 2, stride=2), # Stage 2
make_layer(BasicBlock, 256, 2, stride=2), # Stage 3
make_layer(BasicBlock, 512, 2, stride=2) # Stage 4
]
model = nn.Sequential(*resnet18_layers,
nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(),
nn.Linear(512 * BasicBlock.expansion, num_classes))
```
此代码展示了如何创建一个简单的ResNet18模型,在实际应用中可能还需要调整超参数和其他细节设置以适应特定的任务需求。
阅读全文