transformers做cifar10图像分类任务保存loss曲线和准确率曲线并保存模型pytorch
时间: 2024-02-11 16:08:28 浏览: 94
好的,您可以按照以下步骤进行:
1. 导入必要的库:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
```
2. 定义超参数:
```python
EPOCHS = 20
BATCH_SIZE = 128
LEARNING_RATE = 0.001
```
3. 加载数据集并进行预处理:
```python
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 = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)
testloader = DataLoader(testset, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
```
4. 定义模型:
```python
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(torch.relu(self.conv1(x)))
x = self.pool(torch.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
```
5. 定义损失函数和优化器:
```python
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=LEARNING_RATE)
```
6. 训练模型并记录loss和accuracy:
```python
train_losses = []
train_accs = []
test_losses = []
test_accs = []
for epoch in range(EPOCHS):
# training
train_loss = 0.0
train_acc = 0.0
net.train()
for i, data in enumerate(trainloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
train_loss += loss.item()
train_acc += accuracy_score(labels.numpy(), torch.argmax(outputs, axis=1).numpy())
train_loss /= len(trainloader)
train_acc /= len(trainloader)
train_losses.append(train_loss)
train_accs.append(train_acc)
# testing
test_loss = 0.0
test_acc = 0.0
net.eval()
with torch.no_grad():
for i, data in enumerate(testloader, 0):
inputs, labels = data
outputs = net(inputs)
loss = criterion(outputs, labels)
test_loss += loss.item()
test_acc += accuracy_score(labels.numpy(), torch.argmax(outputs, axis=1).numpy())
test_loss /= len(testloader)
test_acc /= len(testloader)
test_losses.append(test_loss)
test_accs.append(test_acc)
print('Epoch %d | Train Loss: %.3f | Train Acc: %.3f | Test Loss: %.3f | Test Acc: %.3f' %
(epoch + 1, train_loss, train_acc, test_loss, test_acc))
```
7. 画出loss和accuracy曲线:
```python
plt.plot(train_losses, label='Train Loss')
plt.plot(test_losses, label='Test Loss')
plt.legend()
plt.show()
plt.plot(train_accs, label='Train Acc')
plt.plot(test_accs, label='Test Acc')
plt.legend()
plt.show()
```
8. 保存模型:
```python
PATH = './cifar_net.pth'
torch.save(net.state_dict(), PATH)
```
这样就完成了对CIFAR-10数据集的图像分类任务,并保存了loss和accuracy曲线以及模型。
阅读全文