数据集:CIFAR-10数据集,
网络:ResNet18模型,
损失函数:交叉熵损失,
优化器:Adam优化器
评价指标:准确率。
transformer = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2023, 0.1994, 0.2010])])
trainset = torchvision.datasets.CIFAR10(root='./cifar10', train=True, download=True, transform=transformer)
devset = torchvision.datasets.CIFAR10(root='./cifar10', train=False, download=True, transform=transformer)
testset = torchvision.datasets.CIFAR10(root='./cifar10', train=False, download=True, transform=transformer)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
image, label = trainset[0]
print(image.size())
image, label = np.array(image), int(label)
plt.imshow(image.transpose(1, 2, 0))
plt.show()
print(classes[label])
import torch
import numpy as np
import matplotlib.pyplot as plt
from torchvision.transforms import transforms
import torchvision
from torch.utils.data import DataLoader
from torchvision.models import resnet18
import torch.nn.functional as F
import torch.optim as opt
from nndl.runner import RunnerV3
from nndl.metric import accuracy
transformer = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2023, 0.1994, 0.2010])])
trainset = torchvision.datasets.CIFAR10(root='./cifar10', train=True, download=True, transform=transformer)
devset = torchvision.datasets.CIFAR10(root='./cifar10', train=False, download=True, transform=transformer)
testset = torchvision.datasets.CIFAR10(root='./cifar10', train=False, download=True, transform=transformer)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
image, label = trainset[0]
print(image.size())
image, label = np.array(image), int(label)
plt.imshow(image.transpose(1, 2, 0))
plt.show()
print(classes[label])
resnet18_model = resnet18(pretrained=True)
# 指定运行设备
torch.cuda.set_device('cuda:0')
# 学习率大小
lr = 0.001
# 批次大小
batch_size = 64
# 加载数据
train_loader = DataLoader(trainset, batch_size=batch_size, shuffle=True)
dev_loader = DataLoader(devset, batch_size=batch_size)
test_loader = DataLoader(testset, batch_size=batch_size)
# 定义网络
model = resnet18_model
# 定义优化器,这里使用Adam优化器以及l2正则化策略,相关内容在7.3.3.2和7.6.2中会进行详细介绍
optimizer = opt.Adam(lr=lr, params=model.parameters(), weight_decay=0.005)
# 定义损失函数
loss_fn = F.cross_entropy
# 定义评价指标
metric = accuracy
# 实例化RunnerV3
runner = RunnerV3(model, optimizer, loss_fn, metric)
# 启动训练
log_steps = 3000
eval_steps = 3000
runner.train(train_loader, dev_loader, num_epochs=30, log_steps=log_steps,
eval_steps=eval_steps, save_path="best_model.pdparams")
Files already downloaded and verified
Files already downloaded and verified
Files already downloaded and verified
torch.Size([3, 32, 32])
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
frog
[Train] epoch: 0/30, step: 0/23460, loss: 7.09256
[Train] epoch: 3/30, step: 3000/23460, loss: 0.68586
[Evaluate] dev score: 0.65970, dev loss: 0.99221
[Evaluate] best accuracy performence has been updated: 0.00000 --> 0.65970
[Train] epoch: 7/30, step: 6000/23460, loss: 0.57793
[Evaluate] dev score: 0.68040, dev loss: 0.93663
[Evaluate] best accuracy performence has been updated: 0.65970 --> 0.68040
[Train] epoch: 11/30, step: 9000/23460, loss: 0.90398
[Evaluate] dev score: 0.72970, dev loss: 0.80892
[Evaluate] best accuracy performence has been updated: 0.68040 --> 0.72970
[Train] epoch: 15/30, step: 12000/23460, loss: 0.54963
[Evaluate] dev score: 0.70290, dev loss: 0.89479
[Train] epoch: 19/30, step: 15000/23460, loss: 0.73634
[Evaluate] dev score: 0.73600, dev loss: 0.80622
[Evaluate] best accuracy performence has been updated: 0.72970 --> 0.73600
[Train] epoch: 23/30, step: 18000/23460, loss: 0.47213
[Evaluate] dev score: 0.73230, dev loss: 0.80564
[Train] epoch: 26/30, step: 21000/23460, loss: 0.52694
[Evaluate] dev score: 0.72850, dev loss: 0.81326
[Evaluate] dev score: 0.73140, dev loss: 0.79042
[Train] Training done!
# 加载最优模型
runner.load_model('best_model.pdparams')
# 模型评价
score, loss = runner.evaluate(test_loader)
print("[Test] accuracy/loss: {:.4f}/{:.4f}".format(score, loss))
[Test] accuracy/loss: 0.7360/0.8062
#获取测试集中的一个batch的数据
for X, label in test_loader:
logits = runner.predict(X)
#多分类,使用softmax计算预测概率
pred = F.softmax(logits)
#获取概率最大的类别
pred_class = torch.argmax(pred[2]).numpy()
label = label[2].data.numpy()
#输出真实类别与预测类别
print("The true category is {} and the predicted category is {}".format(classes[label], classes[pred_class]))
#可视化图片
X=np.array(X)
X=X[1]
plt.imshow(X.transpose(1, 2, 0))
plt.show()
break
resnet18_model = resnet18(pretrained=False)
[Train] epoch: 24/30,step: 15000/18750,loss: 0.38762
[Evaluate] dev score: 0.70030,dev loss: 0.89444
[Evaluate] best accuracy performence has been updated: 0.69470 --> 0.70030
[Train] epoch: 28/30,step: 18000/18750,loss: 0.47557
[Evaluate] dev score: 0.68210,dev loss: 0.99598
[Evaluate]dev score: 0.69590,dev loss: 0.92128
[Train] Training done!