通常,我们会遇到处理图像,文本,音频或视频数据时,可以使用Python标准包将数据加载到 NumPy 数组中。 然后,您可以将该数组转换为torch.*Tensor
格式的数据。
另外,pytorch针对视觉,还创建了一个torchvision的包,其中包含常见的数据集集(例如Imagenet,GIFAR10,MNIST)的数据加载器以及用于图像(即torchvision.datasets
和torch.utils.data.DataLoader
)的数据转换器。
本文讲解用CIFAR10数据,完成分类问题。它具有以下类别:“飞机”,“汽车”,“鸟”,“猫”,“鹿”,“狗”,“青蛙”,“马”,“船”,“卡车”。 CIFAR-10 中的图像尺寸为3x32x32,即尺寸为32x32像素的 3 通道彩色图像。
步骤:
import torch
import torchvision
import torchvision.transforms as transforms
# transforms将三通道(0,1)区间的数据转换成(-1,1)的数据
TorchVision
数据集的输出是[0, 1]范围的PILImage图像。 我们将它们转换为归一化范围[-1, 1]的张量。
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=2)
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')
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./data\cifar-10-python.tar.gz
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting ./data\cifar-10-python.tar.gz to ./data
Files already downloaded and verified
展示部分图片:
import matplotlib.pyplot as plt
import numpy as np
# functions to show an image
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
bird cat cat deer
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.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(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
net = Net()
这里我们使用分类交叉熵损失和带有动量的 SGD。
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
只需要遍历数据迭代器,然后将输入馈送到网络并进行优化即可。
for epoch in range(2): # 训练两次
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
[1, 2000] loss: 2.175
[1, 4000] loss: 1.889
[1, 6000] loss: 1.697
[1, 8000] loss: 1.594
[1, 10000] loss: 1.527
[1, 12000] loss: 1.476
[2, 2000] loss: 1.383
[2, 4000] loss: 1.362
[2, 6000] loss: 1.330
[2, 8000] loss: 1.348
[2, 10000] loss: 1.316
[2, 12000] loss: 1.316
Finished Training
保存模型:
# 保存在当前文件夹下,pth文件
PATH = './cifar_net.pth'
torch.save(net.state_dict(), PATH)
我们已经在训练数据集中对网络进行了 2 次训练。 但是我们需要检查网络是否学到了什么。
我们将通过预测神经网络输出的类别标签并根据实际情况进行检查来进行检查。 如果预测正确,则将样本添加到正确预测列表中。
# 显示测试集中的图像
dataiter = iter(testloader)
images, labels = dataiter.next()
# print images
imshow(torchvision.utils.make_grid(images))
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
GroundTruth: cat ship ship plane
加载模型:
net = Net()
net.load_state_dict(torch.load(PATH))
outputs = net(images)
print(outputs)
tensor([[-2.1010, -3.0878, 1.4055, 3.2283, 0.0129, 1.6000, 3.7093, -1.6727,
-1.0253, -2.4035],
[ 4.8515, 5.2974, -1.5895, -2.5089, -2.3749, -4.2794, -4.5515, -4.6212,
8.2107, 3.2727],
[ 2.6539, 1.8801, 0.0768, -1.7280, -0.6224, -2.2073, -2.3949, -1.8895,
3.7800, 0.6642],
[ 2.4435, 0.5671, -0.2488, -0.9293, -0.1690, -1.8434, -1.9284, -1.1686,
3.3121, 0.1381]], grad_fn=)
上面的输出是四个test例子在10个类别上的能量,能量越高,网络就认为是该类概率越大。
_, predicted = torch.max(outputs, 1)
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4)))
Predicted: frog ship ship ship
网络在整个数据集上的表现:
correct = 0
total = 0
with torch.no_grad(): # 不需要梯度
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
Accuracy of the network on the 10000 test images: 54 %
具体看一些网络在那些类上表现良好,在那些类上表现不佳:
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs, 1)
c = (predicted == labels).squeeze()
for i in range(4):
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1
for i in range(10):
print('Accuracy of %5s : %2d %%' % (
classes[i], 100 * class_correct[i] / class_total[i]))
Accuracy of plane : 58 %
Accuracy of car : 81 %
Accuracy of bird : 42 %
Accuracy of cat : 33 %
Accuracy of deer : 37 %
Accuracy of dog : 34 %
Accuracy of frog : 82 %
Accuracy of horse : 51 %
Accuracy of ship : 78 %
Accuracy of truck : 46 %
判断GPU是否可用
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Assuming that we are on a CUDA machine, this should print a CUDA device:
print(device)
cpu
如过GPU可用的话:
# 把网络传到GPU
net.to(device)
# 把输入和lables传入GPU
inputs, labels = data[0].to(device), data[1].to(device)