GPU代码
'构建网络'
# import torch
# import torch.nn as nn
# import torch.nn.functional as F
#
# class NET(nn.Module):
#
# def __init__(self):
# super(NET, self).__init__()
# # 1 input image channel, 6 output channels, 5x5 square convolution
# # self
# self.conv1 = nn.Conv2d(1, 6, 5)
# self.conv2 = nn.Conv2d(6, 16, 5)
# # an affine operation: y = Wx +b
# self.fc1 = nn.Linear(16 * 5 * 5, 120)
# self.fc2 = nn.Linear(120, 84)
# self.fc3 = nn.Linear(84, 10)
#
# def forward(self, x):
# # Max pooling over a (2, 2) window
# x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# # If the size is a square you can only specify a single number
# x = F.max_pool2d(F.relu(self.conv2(x)), 2)
# x = x.view(-1, self.num_flat_features(x))
# x = F.relu(self.fc1(x))
# x = F.relu(self.fc2(x))
# x = self.fc3(x)
# return x
#
# def num_flat_feature(self, x):
# size = x.size()[1:] # all dimension expect the batch dimension
# num_feature = 1
# for s in size:
# num_feature *= s
# return num_feature
#
#
# net = NET()
# print(net)
# params = list(net.parameters())
# print(len(params))
# print(params[0].size()) # conv1's weight
# input = torch.randn(1, 1, 32, 32)
# out = net(input)
# print(out)
#
# # 缓存清零
# net.zero_grad()
# out.backward(torch.randn(1, 10))
'训练一个分类器'
'''
训练一个图像分类器
依次按照下列顺序进行:
使用torchvision加载和归一化CIFAR10训练集和测试集
定义一个卷积神经网络
定义损失函数
在训练集上训练网络
在测试集上测试网络
'''
#1.读取和归一化 CIFAR10
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
#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=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=0)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
#
#
#
#展示一些训练图像
# def imshow(img):
# img = img / 2 + 0.5 #unnormalize
# npimg = img.numpy()
# plt.imshow(np.transpose(npimg, (1, 2, 0)))
# # 获取随机数据
# dataiter = iter(trainloader)
# images, labels = dataiter.next()
#
# #display images
# imshow(torchvision.utils.make_grid(images))
# #显示图像标签
# print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
# 2.定义一个卷积神经网络
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()
net.to(device)
# 3.定义损失函数和优化器
# 使用交叉熵作为损失函数,使用带动量的随机梯度下降。
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
inputs, labels = inputs.to(device), labels.to(device)
# 梯度置0
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.在测试集上测试网络
'''
我们在整个训练集上进行了2次训练,
但是我们需要检查网络是否从数据集中学习到有用的东西。
通过预测神经网络输出的类别标签与实际情况标签进行对比来进行检测。
如果预测正确,我们把该样本添加到正确预测列表。
第一步,显示测试集中的图片并熟悉图片内容。
'''
dataiter = iter(testloader)
images, labels = dataiter.next()
images, labels = images.to(device), labels.to(device)
# 显示图片
# imshow(torchvision.utils.make_grid(images))
print('GroudTruth:', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
outputs = net(images)
'''
输出是10个标签的能量。 一个类别的能量越大,神经网络越认为它是这个类别。
所以让我们得到最高能量的标签。
'''
_, 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
images,labels = images.to(device), labels.to(device)
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))
'''
结果看起来不错,至少比随机选择要好,随机选择的正确率为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.to(device), labels.to(device)
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]))
结果
D:\Anaconda3\python.exe F:/Workspaces/pytorch_learn/net.py
cuda:0
Files already downloaded and verified
Files already downloaded and verified
[1, 2000] loss: 0.000
[1, 4000] loss: 0.000
[1, 6000] loss: 0.000
[1, 8000] loss: 0.000
[1, 10000] loss: 0.000
[1, 12000] loss: 0.000
[2, 2000] loss: 0.000
[2, 4000] loss: 0.000
[2, 6000] loss: 0.000
[2, 8000] loss: 0.000
[2, 10000] loss: 0.000
[2, 12000] loss: 0.000
Finished Training
GroudTruth: cat ship ship plane
Predicted: cat ship car ship
Accuracy of the network on the 10000 test images: 54 %
Accuracy of plane : 45 %
Accuracy of car : 65 %
Accuracy of bird : 39 %
Accuracy of cat : 47 %
Accuracy of deer : 42 %
Accuracy of dog : 47 %
Accuracy of frog : 53 %
Accuracy of horse : 65 %
Accuracy of ship : 83 %
Accuracy of truck : 57 %
Process finished with exit code 0
总结
没注意到GPU的速度提升很多?那是因为网络非常的小。
实践: 尝试增加你的网络的宽度(第一个nn.Conv2d的第2个参数,第二个nn.Conv2d的第一个参数,它们需要是相同的数字),看看你得到了什么样的加速。
实现的目标:
深入了解了PyTorch的张量库和神经网络
训练了一个小网络来分类图片
BUG
- expected object of backend CPU but got backend CUDA for argument #2 'weight
有变量转化到CUDA
net.to(device)
inputs, labels = inputs.to(device), labels.to(device)
images, labels = images.to(device), labels.to(device)
images,labels = images.to(device), labels.to(device)
images, labels = images.to(device), labels.to(device)
共五处
- BrokenPipeError: [Errno 32] Broken pipe
um_workers=2 改为 um_workers=0。Windows多线程的问题
详情见此博主