使用深度学习库pytorch完成图像分类任务,搭建卷积神经网络的经典模型AlexNet,完成对cifar-10数据的分类任务,记录训练过程中的损失和准确率以及测试集的损失和准确率,并将其可视化,分析结果。 文件夹cifar-10-batches-py包含我们的图像分类问题的数据集,data_batch_1,2..5为训练数据,test_batch为测试数据,为了方便图片转成张量,使用torchvision加载和处理数据集。图像加载预处理示例代码见ex5-1.py。
时间: 2024-02-13 08:59:42 浏览: 74
抱歉,我作为一个语言模型无法执行文件读取和代码运行的操作。但是,我可以提供一些思路和代码示例供您参考。
思路:
1. 导入必要的库和数据集
2. 定义网络模型,这里是AlexNet
3. 定义损失函数和优化器
4. 训练模型并记录训练过程中的损失和准确率
5. 测试模型并记录测试集的损失和准确率
6. 可视化训练过程中的损失和准确率以及测试集的损失和准确率
代码示例:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
# 定义数据集
transform = 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)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128,
shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=128,
shuffle=False, num_workers=2)
# 定义网络模型AlexNet
class AlexNet(nn.Module):
def __init__(self):
super(AlexNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2)
self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2)
self.conv2 = nn.Conv2d(64, 192, kernel_size=5, padding=2)
self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2)
self.conv3 = nn.Conv2d(192, 384, kernel_size=3, padding=1)
self.conv4 = nn.Conv2d(384, 256, kernel_size=3, padding=1)
self.conv5 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.pool3 = nn.MaxPool2d(kernel_size=3, stride=2)
self.fc1 = nn.Linear(256 * 2 * 2, 4096)
self.fc2 = nn.Linear(4096, 4096)
self.fc3 = nn.Linear(4096, 10)
def forward(self, x):
x = self.pool1(torch.relu(self.conv1(x)))
x = self.pool2(torch.relu(self.conv2(x)))
x = torch.relu(self.conv3(x))
x = torch.relu(self.conv4(x))
x = self.pool3(torch.relu(self.conv5(x)))
x = x.view(-1, 256 * 2 * 2)
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
# 实例化模型、定义损失函数和优化器
net = AlexNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
# 定义损失和准确率记录器
train_loss = []
train_acc = []
test_loss = []
test_acc = []
# 训练模型
for epoch in range(5):
running_loss = 0.0
correct = 0
total = 0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
train_loss.append(running_loss / len(trainloader))
train_acc.append(100 * correct / total)
# 测试模型
test_running_loss = 0.0
test_correct = 0
test_total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
loss = criterion(outputs, labels)
test_running_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
test_total += labels.size(0)
test_correct += (predicted == labels).sum().item()
test_loss.append(test_running_loss / len(testloader))
test_acc.append(100 * test_correct / test_total)
print('Epoch %d: Train Loss: %.3f | Train Acc: %.3f %% | Test Loss: %.3f | Test Acc: %.3f %%'
% (epoch + 1, train_loss[-1], train_acc[-1], test_loss[-1], test_acc[-1]))
# 可视化训练过程中的损失和准确率以及测试集的损失和准确率
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.plot(train_loss, label='Train Loss')
plt.plot(test_loss, label='Test Loss')
plt.legend()
plt.title('Loss')
plt.subplot(122)
plt.plot(train_acc, label='Train Acc')
plt.plot(test_acc, label='Test Acc')
plt.legend()
plt.title('Accuracy')
plt.show()
```
这是一个简单的示例,你可以根据你的实际需求进行修改和调整。
阅读全文