卷积神经网络 残差网络34
时间: 2024-12-30 14:21:06 浏览: 4
### 关于ResNet-34架构及其实现
#### 架构概述
ResNet-34 是一种具有34层深度的残差网络,由微软研究院开发。该网络通过引入跳跃连接解决了深层网络训练过程中遇到的梯度消失或爆炸问题[^1]。
#### 主要特点
- **跳跃连接**:允许信息绕过某些层次结构直接传递给后面的层,从而缓解了随着层数增加而产生的退化问题。
- **模块化设计**:整个网络可以看作是由多个基本单元组成的堆叠体,这些基本单元被称为瓶颈(bottleneck)或者两层构建块(two-layer block),具体取决于所使用的版本。
对于 ResNet-34 而言,采用的是较为简单的两层构建块形式:
```python
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_channels, out_channels, stride=1, downsample=None):
super(BasicBlock, self).__init__()
# 定义两个连续的3x3卷积层
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
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.relu = nn.ReLU(inplace=True)
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
```
上述代码展示了如何定义一个基础的 `BasicBlock` 类来创建每一对相邻的卷积层之间的关系,并实现了前向传播逻辑。当输入特征图尺寸发生变化时(例如步幅大于1的情况),则会应用额外的操作以匹配维度大小。
#### 整体框架搭建
基于以上描述的基础组件,完整的 ResNet-34 可以按照如下方式组装起来:
```python
import torch.nn as nn
def make_layer(block, planes, blocks, stride=1):
layers = []
# 创建第一个block实例可能需要调整通道数/空间分辨率
downsample = None
if stride != 1 or inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers.append(block(inplanes, planes, stride, downsample))
inplanes = planes * block.expansion
# 添加剩余blocks到layers列表中
for _ in range(1, blocks):
layers.append(block(inplanes, planes))
return nn.Sequential(*layers)
class ResNet(nn.Module):
def __init__(self, num_classes=1000):
global inplanes
inplanes = 64
super().__init__()
self.in_planes = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = make_layer(BasicBlock, 64, 3)
self.layer2 = make_layer(BasicBlock, 128, 4, stride=2)
self.layer3 = make_layer(BasicBlock, 256, 6, stride=2)
self.layer4 = make_layer(BasicBlock, 512, 3, stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * BasicBlock.expansion, num_classes)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(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
```
这段代码片段说明了怎样利用之前定义好的 `make_layer()` 函数以及 `BasicBlock` 来构造不同阶段的不同数量的基本块组合成最终的 ResNet-34 结构。最后一部分包含了全局平均池化层和全连接分类器用于输出预测类别概率分布。
阅读全文