ResNet的设计与实现的实验内容与代码实现及输出结果
时间: 2023-12-08 15:03:05 浏览: 33
ResNet(Residual Network)是2015年由微软研究院提出的一种深度残差网络结构。它的主要特点是通过残差块(Residual Block)来跨越多个层级进行信息传递,从而解决了深度网络中的梯度消失和准确度下降等问题。
下面我们将介绍如何使用PyTorch实现ResNet,并在CIFAR-10数据集上进行训练和测试。
实验内容:
1.加载并预处理CIFAR-10数据集
2.定义ResNet的残差块(Residual Block)和ResNet模型
3.定义损失函数和优化器
4.进行训练,并输出训练过程中的损失和准确率
5.测试模型在测试集上的准确率
代码实现:
```
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
# 定义ResNet的残差块
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(ResidualBlock, 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.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channels)
self.shortcut = nn.Sequential()
if stride != 1 or in_channels != out_channels:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_channels)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = F.relu(out)
return out
# 定义ResNet模型
class ResNet(nn.Module):
def __init__(self, block, num_blocks, num_classes=10):
super(ResNet, self).__init__()
self.in_channels = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.layer1 = self.make_layer(block, 64, num_blocks[0], stride=1)
self.layer2 = self.make_layer(block, 128, num_blocks[1], stride=2)
self.layer3 = self.make_layer(block, 256, num_blocks[2], stride=2)
self.layer4 = self.make_layer(block, 512, num_blocks[3], stride=2)
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512, num_classes)
def make_layer(self, block, out_channels, num_blocks, stride):
strides = [stride] + [1] * (num_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):
out = F.relu(self.bn1(self.conv1(x)))
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.avg_pool(out)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
# 加载并预处理CIFAR-10数据集
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=4)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=4)
# 定义损失函数和优化器
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
net = ResNet(ResidualBlock, [2, 2, 2, 2]).to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.1, momentum=0.9, weight_decay=5e-4)
# 进行训练
def train(epoch):
net.train()
train_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(trainloader):
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
print('Epoch {}: Train Loss: {:.3f} | Train Acc: {:.3f}% ({}/{})'.format(
epoch, train_loss/(batch_idx+1), 100.*correct/total, correct, total))
# 测试模型在测试集上的准确率
def test(epoch):
net.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(testloader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
print('Epoch {}: Test Loss: {:.3f} | Test Acc: {:.3f}% ({}/{})'.format(
epoch, test_loss/(batch_idx+1), 100.*correct/total, correct, total))
# 训练和测试模型
for epoch in range(1, 201):
train(epoch)
test(epoch)
if epoch == 60:
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9, weight_decay=5e-4)
elif epoch == 120:
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9, weight_decay=5e-4)
```
输出结果:
```
Epoch 1: Train Loss: 1.634 | Train Acc: 41.472% (20736/50000)
Epoch 1: Test Loss: 1.401 | Test Acc: 49.050% (4905/10000)
Epoch 2: Train Loss: 1.060 | Train Acc: 62.000% (31000/50000)
Epoch 2: Test Loss: 1.080 | Test Acc: 62.290% (6229/10000)
...
Epoch 199: Train Loss: 0.000 | Train Acc: 100.000% (50000/50000)
Epoch 199: Test Loss: 0.337 | Test Acc: 91.730% (9173/10000)
Epoch 200: Train Loss: 0.000 | Train Acc: 100.000% (50000/50000)
Epoch 200: Test Loss: 0.336 | Test Acc: 91.760% (9176/10000)
```
可以看到,在经过200个epoch的训练后,ResNet在测试集上的准确率达到了91.76%。