Pytorch:训练一个简单的分类器(torchvision的使用)

CIFAR数据集

CIFAR10 dataset是一个是个类别的数据集,类别包括:‘airplane’, ‘automobile’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’. 里面的每张图像大小为3 * 32 * 32。
在这里插入图片描述
Pytorch为视觉任务特地创建了一个Torchvision, 里面包括一些常用数据集的加载Imagenet,CIFAR10,MINIST,etc

Jupyter代码如下

import matplotlib.pyplot as plt
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

加载数据

# torchvision dataset的输出是PILImage range[0,1] 我们需要把它进行归一化处理 normalized range[-1,1]
transform = transforms.Compose(  # transform.Compose 将多个transform组合起来用
    [transforms.ToTensor(),     # 把一个取值范围内是[0,255]的Image或shape为(H,W,C)的ndarray转换为(C,H,W)取值为[0,1.]的FloadTensor
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]  # 给定RGB的均值、方差,将会把Tensor正则化 (img-mean)/std
)

# 这个API中 train表示是否为训练集 download表示是否将文件下载到./data目录下 若已经成功下载则无操作
# transform表示原始图片作为输入返回一个转换后的图片
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
# 数据加载器,组合数据集和采样器,并在数据集上提供单进程或者多进程迭代器 num_workers = 0 表示只在主进程中加载
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  # 为了可视化反向正则化
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()


dataiter = iter(trainloader)        # 创建迭代器对象
images, labels = dataiter.next()
# show image
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))

在这里插入图片描述

定义模型

# Define the Neural Network
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.pool = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(16*5*5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        h1 = self.pool(F.relu(self.conv1(x)))
        h2 = self.pool(F.relu(self.conv2(h1)))
        h2 = h2.view(-1, 16*5*5)
        h3 = self.fc1(h2)
        h4 = self.fc2(h3)
        h5 = self.fc3(h4)
        return h5


# 初始化
net = Net()
# 定义Loss Function 和 Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)

训练网络并保存模型

# Train the Network
for epoch in range(2):
    running_loss = 0.
    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inps, labels = data
        # zero the parameter gradient
        optimizer.zero_grad()
        # forward + backward + optimize
        outs = net(inps)
        loss = criterion(outs, labels)
        loss.backward()
        optimizer.step()
        # print loss
        running_loss += loss.item()
        if i % 2000 == 1999:
            print('[%d, %5d] loss: %.3f' % (epoch+1, i+1, running_loss/2000))
            running_loss = 0.

print('Finish Training')
PATH = './cifar_net.pth'
torch.save(net.state_dict(), PATH)

加载模型并测试结果

# Test the Network on the test data
dataiter = iter(testloader)
images, labels = dataiter.next()

# load the model
net = Net()
net.load_state_dict(torch.load(PATH))

out = net(images)
print(out.size())
_, prediction = torch.max(out, 1) # 1表示返回元素在一行中最大的值和列索引
imshow(torchvision.utils.make_grid(images))
print('The prediction of the model is',' '.join('%5s' % classes[prediction[j]] for j in range(4)))

评估结果

# 评估结果1
correct = 0.
total = 0.
with torch.no_grad():  # 不需要计算梯度
    for data in testloader:
        images, labels = data
        out = net(images)
        _,prediction = torch.max(out, 1)
        total += labels.size(0)
        correct += (prediction == labels).sum().item()
        
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct/total))
# 评估结果2
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
        out = net(images)
        _,prediction = torch.max(out, 1)
        c = (prediction == 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 : %.2f %%' % (classes[i], class_correct[i]/class_total[i]))

github地址

代码地址

将模型放到多卡服务器上训练

使用单/多个GPU训练模型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值