本实验来自Pytorch官方教程中文版
# 导包
import torch
import torchvision
import torchvision.transforms as transforms
# device = torch.device("cuda:0") # 指定GPU编号
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device) # cuda:0
# 指定转换类型
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# 在torchvision中下载CIFAR10的数据集(训练集和测试集)
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)
# 10种图片的类别 classes是tuple
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# 查看GPU情况
print(torch.cuda.is_available()) # 判断 GPU 是否可用
print(torch.cuda.device_count()) # 判断有多少 GPU
print(torch.cuda.get_device_name(0)) # 返回 gpu 名字,设备索引默认从 0 开始
print(torch.cuda.current_device()) # 返回当前设备索引
# 随机选出4张图
import matplotlib.pyplot as plt
import numpy as np
# 定义函数-> 显示图片
def imshow(img):
img = img / 2 + 0.5 # unnormalize
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))
# 显示图片对应的标签labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
# 卷积+池化+全连接+输出最终10种结果的概率(10种照片)
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) #生成卷积核1
self.pool = nn.MaxPool2d(2, 2) # 生成池化用的卷积核 2次池化都用它
self.conv2 = nn.Conv2d(6, 16, 5) #生成卷积核2
self.fc1 = nn.Linear(16 * 5 * 5, 120) # 全连接层1
self.fc2 = nn.Linear(120, 84) #全连接层2
self.fc3 = nn.Linear(84, 10) # 全连接层3
def forward(self, x):
x = self.pool(F.relu(self.conv1(x))) # 卷积核1+第一步池化
x = self.pool(F.relu(self.conv2(x))) # 卷积核2+第二步池化
x = x.view(-1, 16 * 5 * 5) # 改变输出结果的shape,变为10个通道(10个类别)
x = F.relu(self.fc1(x)) # 全连接1
x = F.relu(self.fc2(x)) # 全连接2
x = self.fc3(x) # 全连接3
return x # 获取神经网络的结果
net = Net()
net = net.to(device) # 上传到GPU
print(net)
# 导入求 损失函数 的包
import torch.optim as optim
criterion = nn.CrossEntropyLoss() # 交叉熵损失函数
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) # 0.9是固定的
# 计算2个epoch 即训练2次数据集
for epoch in range(2):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
#获取训练集数据
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device) # 上传到GPU
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs) # 前向传播,得到神经网络的结果outputs
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')
# 随机选取4张图片,评估训练的效果
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
# 随机获取训练集中的4张图片
dataiter = iter(trainloader)
images, labels = dataiter.next()
# 显示4张图片
imshow(torchvision.utils.make_grid(images)) # 在这里使用tuple不能上传到GPU
# 输出4张图片的标签labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
images = images.cuda() #上传到GPU
outputs = net(images)
print(outputs.shape)
_, predicted = torch.max(outputs,1) # 此函数看文末的博客链接
# 输出对4张图片的预测结果
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
images, labels = images.cuda(), labels.cuda()
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0) # tensor.size(0)看文末博客链接
correct += (predicted == labels).sum().item() #(x==y).sum.item()看文末博客链接
# 输出整体的训练结果
print('Accuracy of the network on the 10000 test images: %d %%' % (
100 * correct / total))
预测的正确结果达到54%
# 计算所有种类(10种)的预测概率
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
images, labels = images.cuda(), labels.cuda() # 上传到GPU
outputs = net(images)
_, predicted = torch.max(outputs, 1)
c = (predicted == labels).squeeze() # .squeeze()看文末博客链接
for i in range(4):
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1
# 输出10个种类的预测概率
for i in range(10):
print('Accuracy of %5s : %2d %%' % (
classes[i], 100 * class_correct[i] / class_total[i]))
10个种类的预测概率
torch.max()
x.item()
x.sum()
x.size(0)
x.squeeze()
上述函数见此->博客