如何使用pytorch创建残差网络?
时间: 2024-09-19 18:01:49 浏览: 64
在PyTorch中,创建一个残差网络(Residual Network,简称ResNet)通常涉及以下几个步骤:
1. 导入必要的库:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
```
2. 定义基本块(Basic Block)或更深的模块(如Bottleneck Block),这是ResNet的核心部分:
- 对于基础块,你可以这样做:
```python
class BasicBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, stride=stride)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
if stride != 1 or in_channels != out_channels:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride),
nn.BatchNorm2d(out_channels)
)
else:
self.shortcut = None
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)
if self.shortcut is not None:
shortcut = self.shortcut(residual)
else:
shortcut = residual
return out + shortcut
```
- 如果你需要更复杂的Bottleneck Block,可以在每个卷积层之间添加更多的内核和BN层。
3. 构建网络结构,比如常见的ResNet-50有4个阶段:
```python
num_blocks_per_stage = [3, 4, 6, 3]
layers = [2, 2, 2, 2] # 每个stage包含的blocks数
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=1000):
super(ResNet, self).__init__()
self.in_channels = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self.make_layer(block, 64, layers[0])
self.layer2 = self.make_layer(block, 128, layers[1], stride=2)
self.layer3 = self.make_layer(block, 256, layers[2], stride=2)
self.layer4 = self.make_layer(block, 512, layers[3], stride=2)
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512, num_classes)
def make_layer(self, block, out_channels, blocks, stride=1):
strides = [stride] + [1] * (blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_channels, out_channels, stride))
self.in_channels = out_channels
return nn.Sequential(*layers)
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.avg_pool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
```
4. 创建并初始化模型实例:
```python
model = ResNet(BasicBlock, layers)
# 可能还需要设置优化器和损失函数
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
criterion = nn.CrossEntropyLoss()
```
现在,你已经准备好了一个基本的ResNet模型。训练时记得调整学习率策略和其他超参数。
阅读全文