训练一个分类器
我们将按顺序做以下步骤:
- 通过torchvision加载CIFAR10里面的训练和测试数据集,并对数据进行标准化
- 定义卷积神经网络
- 定义损失函数
- 利用训练数据训练网络
- 利用测试数据测试网络
1. 加载CIFAR10数据
import torch
import torchvision
import torchvision.transforms as transforms
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')
可视化训练数据
import matplotlib.pyplot as plt
import numpy as np
def imshow(img):
img = img / 2 + 0.5
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0))) # 矩阵转置
plt.show()
#随机获取训练图片
dataiter = iter(trainloader)
images, labels = dataiter.next()
#显示图片
imshow(torchvision.utils.make_grid(images))
#打印图片标签
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
输出:
2.定义卷积神经网络
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) # 输入变为3 通道
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()
3.定义损失函数和优化器
交叉熵函数 随机梯度下降
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr = 0.001, momentum = 0.9)
4.训练数据
遍历数据迭代器,将数据喂给网络和优化器
for epoch in range(2):
running_loss = 0.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()
if i %2000 == 1999:
print('[%d, %5d] loss: %.3f' % (epoch +1, i + 1 ,running_loss / 2000))
running_loss = 0.0
print('Finished Training')
5.测试
显示测试集的ground-truth
dataiter = iter(testloader)
images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
训练的网络给出的预测
outputs = net(images)
_, predicted = torch.max(outputs, 1)
print('Predicted:',' '.join('%5s' % classes[predicted[j]] for j in range(4) ))
接着,我们查看网络在整个数据集上的表现
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('Accuray of the network on the 10000 test images: %d %%' % (100 * correct / total))
查看哪些类表现良好
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]))
数据增强
随机改变训练样本可以降低模型对某些属性的依赖,从而提高模型的泛化能力。
# 旋转剪裁
# 水平翻转 左右
torchvision.transforms.RandomHorizontalFlip()
# 垂直翻转 上下
torchvision.transforms.RandomVerticalFlip()
# 随机裁剪出一块面积为原面积 10%∼100% 的区域,且该区域的宽和高之比随机取自 0.5∼2 ,然后再将该区域的宽和高分别缩放到200像素
torchvision.transforms.RandomResizedCrop(200, scale=(0.1, 1), ratio=(0.5, 2))
# -----------------------------------------------------
# 变换颜色
# 亮度(brightness)、对比度(contrast)、饱和度(saturation)和色调(hue)
# 亮度随机变化为原图亮度的 50% ( 1−0.5 ) ∼150% ( 1+0.5 )
torchvision.transforms.ColorJitter(brightness=0.5, contrast=0, saturation=0, hue=0)
# 变化色调
torchvision.transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0.5)
# ---------------------------------------------------
# 通过Compose实例将上面定义的多个图像增广方法叠加起来
torchvision.transforms.Compose( [ ] )