工具方法和一些设置
这里有一个帮助显示图片的方法和解决图片不显示问题的设置。
import matplotlib.pyplot as plt
import numpy as np
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
def imshow(img):
"""
展示图片
:param img:
"""
img = img / 2 + 0.5
npImg = img.numpy()
plt.imshow(np.transpose(npImg, (1, 2, 0)))
plt.show()
创建模型
模型结果如图:
由于CIFAR10数据为彩色图片,因此第一层卷积
c
o
n
v
1
conv1
conv1 将输入设置为 3图像通道。
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
self.pool = nn.MaxPool2d(2, 2)
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(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
导入CIFAR10数据
导入数据并对数据进行归一化,便于模型学习。
import torch.utils.data
import torchvision.datasets
import torchvision.transforms as transforms
def load_data():
"""
载入数据并转换为 DataLoader
:return: trainloader, testloader
"""
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=4,
shuffle=True,
num_workers=0
)
testset = torchvision.datasets.CIFAR10(
root='./data', train=False, download=True, transform=transform
)
testloader = torch.utils.data.DataLoader(
testset, batch_size=4, shuffle=False, num_workers=2
)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
return trainloader, testloader, classes
训练模型
使用交叉熵损失和随机梯度下降训练模型。
import torch.optim as optim
def train(trainloader, _iter=2, lr=0.001):
"""
训练
:param trainloader:
:return:net
"""
net = Net()
loss_function = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=lr, momentum=0.9)
losses = []
for epoch in range(_iter):
running_loss = 0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = loss_function(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 2000 == 1999:
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
losses.append(running_loss / 2000)
running_loss = 0
print('Finished Training')
plt.plot(list(range(len(losses))), losses)
plt.show()
return net
测试样例模型
使用训练后的模型对测试集的数据进行预测。
def test(net, testloader, classes):
"""
测试模型
:param net:
:param testloader:
:param classes:
:return:
"""
dataiter = iter(testloader)
images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
print('正确分类:', ' '.join([classes[i] for i in labels]))
outputs = net(images)
print('预测分类:', ' '.join([classes[i] for i in outputs.argmax(dim=1)]))
测试集总体评估
在测试集上对模型总体效率进行评估。
def eval(net, testloader):
correct = 0
total = 0
for data in testloader:
imgs, labels = data
outputs = net(imgs)
preds = outputs.argmax(dim=1)
total += labels.size(0)
correct += (preds == labels).sum()
print('在整个测试集上的正确率为:%d %%' % (100 * correct / total))
对每个类进行评估
对模型在每个类的识别能力进行评估。
def eval_for_each_class(net, testloader, classes):
class_correct = list(0 for i in range(len(classes)))
class_total = list(0 for i in range(len(classes)))
for data in testloader:
imgs, labels = data
outputs = net(imgs)
preds = outputs.argmax(dim=1)
c = (preds == labels)
for index, label in enumerate(labels):
class_correct[label] += c[index].item()
class_total[label] += 1
for i, c in enumerate(classes):
print('%5s 类的正确率为:%2d %%' %
(c, 100 * class_correct[i] / class_total[i]))
测试代码
用于走完训练和测试流程。
if __name__ == '__main__':
trainloader, testloader, classes = load_data()
final_net = train(trainloader)
test(final_net, testloader, classes)
eval(final_net, testloader)
eval_for_each_class(final_net, testloader, classes)
结果展示
训练损失曲线
可以看出后面的线条已经较为平缓,损失函数已经开始收敛。
测试样例模型结果
正确分类: cat ship ship plane
预测分类: cat ship ship ship
可以看出有3个样例预测正确,1个样例预测错误。
测试集总体评估结果
在整个测试集上的正确率为:56 %
各类评估结果
plane 类的正确率为:64 %
car 类的正确率为:58 %
bird 类的正确率为:38 %
cat 类的正确率为:25 %
deer 类的正确率为:51 %
dog 类的正确率为:43 %
frog 类的正确率为:74 %
horse 类的正确率为:62 %
ship 类的正确率为:80 %
truck 类的正确率为:63 %
总结
由上面的各项结果可以看出,此次训练的LetNet模型准确率远低于现在的模型。这是由于LetNet模型是结构较为简单经典模型,且只进行了简单的训练。
虽然结果并不理想,但通过这次练习了解到了用pytorch进行训练的基本流程和实现。
完整代码
import matplotlib.pyplot as plt
import numpy as np
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
def imshow(img):
"""
展示图片
:param img:
"""
img = img / 2 + 0.5
npImg = img.numpy()
plt.imshow(np.transpose(npImg, (1, 2, 0)))
plt.show()
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
self.pool = nn.MaxPool2d(2, 2)
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(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
import torch.utils.data
import torchvision.datasets
import torchvision.transforms as transforms
def load_data():
"""
载入数据并转换为 DataLoader
:return: trainloader, testloader
"""
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=4,
shuffle=True,
num_workers=0
)
testset = torchvision.datasets.CIFAR10(
root='./data', train=False, download=True, transform=transform
)
testloader = torch.utils.data.DataLoader(
testset, batch_size=4, shuffle=False, num_workers=2
)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
return trainloader, testloader, classes
import torch.optim as optim
def train(trainloader, _iter=2, lr=0.001):
"""
训练
:param trainloader:
:return:net
"""
net = Net()
loss_function = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=lr, momentum=0.9)
losses = []
for epoch in range(_iter):
running_loss = 0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = loss_function(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 2000 == 1999:
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
losses.append(running_loss / 2000)
running_loss = 0
print('Finished Training')
plt.plot(list(range(len(losses))), losses)
plt.show()
return net
def test(net, testloader, classes):
"""
测试模型
:param net:
:param testloader:
:param classes:
:return:
"""
dataiter = iter(testloader)
images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
print('正确分类:', ' '.join([classes[i] for i in labels]))
outputs = net(images)
print('预测分类:', ' '.join([classes[i] for i in outputs.argmax(dim=1)]))
def eval(net, testloader):
correct = 0
total = 0
for data in testloader:
imgs, labels = data
outputs = net(imgs)
preds = outputs.argmax(dim=1)
total += labels.size(0)
correct += (preds == labels).sum()
print('在整个测试集上的正确率为:%d %%' % (100 * correct / total))
def eval_for_each_class(net, testloader, classes):
class_correct = list(0 for i in range(len(classes)))
class_total = list(0 for i in range(len(classes)))
for data in testloader:
imgs, labels = data
outputs = net(imgs)
preds = outputs.argmax(dim=1)
c = (preds == labels)
for index, label in enumerate(labels):
class_correct[label] += c[index].item()
class_total[label] += 1
for i, c in enumerate(classes):
print('%5s 类的正确率为:%2d %%' %
(c, 100 * class_correct[i] / class_total[i]))
if __name__ == '__main__':
trainloader, testloader, classes = load_data()
final_net = train(trainloader)
test(final_net, testloader, classes)
eval(final_net, testloader)
eval_for_each_class(final_net, testloader, classes)
|