使用Pytorch对数据集CIFAR-10进行分类,主要是以下几个步骤:
#数据加载和预处理
#使用CIFAR-10数据进行分类实验
import torch as t
import torchvision as tv
import torchvision.transforms as transforms
from torchvision.transforms import ToPILImage
show = ToPILImage() # 可以把Tensor转成Image,方便可视化
#定义对数据的预处理
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)), #归一化
])
#训练集
trainset = tv.datasets.CIFAR10(
root = './data/',
train = True,
download = True,
transform = transform
)
trainloader = t.utils.data.DataLoader(
trainset,
batch_size = 4,
shuffle = True,
num_workers = 2,
)
#测试集
testset = tv.datasets.CIFAR10(
root = './data/',
train = False,
download = True,
transform = transform,
)
testloader = t.utils.data.DataLoader(
testset,
batch_size = 4,
shuffle = False,
num_workers = 2,
)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
初次下载需要一些时间,运行结束后,显示如下:
import torch.nn as nn
import torch.nn.functional as F
import time
start = time.time()#计时
#定义网络结构
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.fc1 = nn.Linear(16*5*5,120)
self.fc2 = nn.Linear(120,84)
self.fc3 = nn.Linear(84,10)
def forward(self,x):
x = F.max_pool2d(F.relu(self.conv1(x)),2)
x = F.max_pool2d(F.relu(self.conv2(x)),2)
x = x.view(x.size()[0],-1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
print(net)
显示net结构如下:
#定义优化和损失
loss_func = nn.CrossEntropyLoss() #交叉熵损失函数
optimizer = t.optim.SGD(net.parameters(),lr = 0.001,momentum = 0.9)
#训练网络
for epoch in range(2):
running_loss = 0
for i,data in enumerate(trainloader,0):
inputs,labels = data
outputs = net(inputs)
loss = loss_func(outputs,labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss +=loss.item()
if i%2000 ==1999:
print('epoch:',epoch+1,'|i:',i+1,'|loss:%.3f'%(running_loss/2000))
running_loss = 0.0
end = time.time()
time_using = end - start
print('finish training')
print('time:',time_using)
结果如下:
下一步进行使用测试集进行网络测试:
#测试网络
correct = 0 #定义的预测正确的图片数
total = 0#总共图片个数
with t.no_grad():
for data in testloader:
images,labels = data
outputs = net(images)
_,predict = t.max(outputs,1)
total += labels.size(0)
correct += (predict == labels).sum()
print('测试集中的准确率为:%d%%'%(100*correct/total))
结果如下:
简单的网络训练确实要比10%的比例高一点:)
在GPU中训练:
#在GPU中训练
device = t.device('cuda:0' if t.cuda.is_available() else 'cpu')
net.to(device)
images = images.to(device)
labels = labels.to(device)
output = net(images)
loss = loss_func(output,labels)
loss