PyTorch - Cifar 数据集

文章目录

    • 项目说明
      • cifar-10 数据集介绍
    • 代码实现
      • 构建数据集、加载器
      • 构建 卷积网络
      • 训练数据
      • 构建 VGG 加深网络
      • 训练
      • 测试


项目说明


cifar-10 数据集介绍

cifar-10 数据集由 60000 张分辨率为 32x32 彩色图像组成;
共分为 10 类,每类包含 6000 张图像;这十类为:含飞机、汽车、鸟、猫、鹿、狗、青蛙、马、船以及卡车。这些类是完全相互排斥的。
cifar-10 数据集有 50000 个训练图像和 10000 个测试图像。

数据集分为五个训练批次和一个测试批次,每个批次包含 10000 张图像;测试批次恰好包含从每个类中随机选择的 1000 张图像,训练批次以随机顺序包含其余图像,但某些训练批处理可能包含来自一个类的图像多于另一个类的图像,在它们之间,训练批次正好包含来自每个类的 5000 张图像。


你也可以从这里下载:https://www.cs.toronto.edu/~kriz/cifar.html


下面是数据集中所包含的类以及每个类中的 10 个随机图像。


代码实现

引入头文件

import os
import time

import torch

import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable

import torchvision
import torchvision.transforms as transforms

import matplotlib.pyplot as plt
import numpy as np

防止网站证书失效、下载失败之类的

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

(作者个人)设置下载目录 task_dir


from config import *
task = 'cifar10'
task_dir = get_task_dir(task)

构建数据集、加载器

transform = transforms.Compose([
        transforms.RandomHorizontalFlip(),
        transforms.RandomGrayscale(),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 
trainset = torchvision.datasets.CIFAR10(root=task_dir, train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=100, shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root=task_dir, 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')

查看数据集

trainset, testset 

(Dataset CIFAR10
     Number of datapoints: 50000
     Root location: /Users/luyi/Documents/nlp_data/cifar10
     Split: Train
     StandardTransform
 Transform: Compose(
                RandomHorizontalFlip(p=0.5)
                RandomGrayscale(p=0.1)
                ToTensor()
                Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
            ),
            
 Dataset CIFAR10
     Number of datapoints: 10000
     Root location: /Users/luyi/Documents/nlp_data/cifar10
     Split: Test
     StandardTransform
 Transform: Compose(
                RandomHorizontalFlip(p=0.5)
                RandomGrayscale(p=0.1)
                ToTensor()
                Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
            ))

trainloader, testloader

(<torch.utils.data.dataloader.DataLoader at 0x7feff0d7ce10>,
 <torch.utils.data.dataloader.DataLoader at 0x7feff0d7c6d0>)

构建 卷积网络


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()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

显示图片的方法

def imshow(img):
    img = img / 2 + 0.5
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()



训练数据

def train():
    for epoch in range(20):
        timestart = time.time()
        running_loss = 0.0
        for i,data in enumerate(trainloader, 0):
            inputs, labels = data
            inputs, labels = Variable(inputs), Variable(labels)
            optimizer.zero_grad()
            outputs = net(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()

            if i % 500 == 499:
                print('[%d ,%5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 500))
                running_loss = 0.0

        print('-- epoch %d cost %3f sec' % (epoch + 1, time.time()-timestart))

    print('==== Finished Training')
 


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)))

构建 VGG 加深网络



class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, 3, padding=1)
        self.conv2 = nn.Conv2d(64, 64, 3, padding=1)
        self.pool1 = nn.MaxPool2d(2, 2)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu1 = nn.ReLU()

        self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
        self.conv4 = nn.Conv2d(128, 128, 3, padding=1)
        self.pool2 = nn.MaxPool2d(2, 2, padding=1)
        self.bn2 = nn.BatchNorm2d(128)
        self.relu2 = nn.ReLU()

        self.conv5 = nn.Conv2d(128, 128, 3, padding=1)
        self.conv6 = nn.Conv2d(128, 128, 3, padding=1)
        self.conv7 = nn.Conv2d(128, 128, 1, padding=1)
        self.pool3 = nn.MaxPool2d(2, 2, padding=1)
        self.bn3 = nn.BatchNorm2d(128)
        self.relu3 = nn.ReLU()

        self.conv8 = nn.Conv2d(128, 256, 3, padding=1)
        self.conv9 = nn.Conv2d(256, 256, 3, padding=1)
        self.conv10 = nn.Conv2d(256, 256, 1, padding=1)
        self.pool4 = nn.MaxPool2d(2, 2, padding=1)
        self.bn4 = nn.BatchNorm2d(256)
        self.relu4 = nn.ReLU()

        self.conv11 = nn.Conv2d(256, 512, 3, padding=1)
        self.conv12 = nn.Conv2d(512, 512, 3, padding=1)
        self.conv13 = nn.Conv2d(512, 512, 1, padding=1)
        self.pool5 = nn.MaxPool2d(2, 2, padding=1)
        self.bn5 = nn.BatchNorm2d(512)
        self.relu5 = nn.ReLU()

        self.fc14 = nn.Linear(512 * 4 * 4, 1024)
        self.drop1 = nn.Dropout2d()
        self.fc15 = nn.Linear(1024, 1024)
        self.drop2 = nn.Dropout2d()
        self.fc16 = nn.Linear(1024, 10)


    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.pool1(x)
        x = self.bn1(x)
        x = self.relu1(x)

        x = self.conv3(x)
        x = self.conv4(x)
        x = self.pool2(x)
        x = self.bn2(x)
        x = self.relu2(x)

        x = self.conv5(x)
        x = self.conv6(x)
        x = self.conv7(x)
        x = self.pool3(x)
        x = self.bn3(x)
        x = self.relu3(x)

        x = self.conv8(x)
        x = self.conv9(x)
        x = self.conv10(x)
        x = self.pool4(x)
        x = self.bn4(x)
        x = self.relu4(x)

        x = self.conv11(x)
        x = self.conv12(x)
        x = self.conv13(x)
        x = self.pool5(x)
        x = self.bn5(x)
        x = self.relu5(x)
        # print(" x shape ",x.size())
        x = x.view(-1, 512 * 4 * 4)
        x = F.relu(self.fc14(x))
        x = self.drop1(x)
        x = F.relu(self.fc15(x))
        x = self.drop2(x)
        x = self.fc16(x)

        return x


训练

 def train_sgd(model, device):

    optimizer = optim.SGD(model.parameters(), lr=0.01)
    path = 'mnist_vgg_weights.tar'
    initepoch = 0

    if os.path.exists(path) is not True:
        loss = nn.CrossEntropyLoss() 
    else:
        # 如果存在已保存的权重,则加载
        checkpoint = torch.load(path)
        model.load_state_dict(checkpoint['model_state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
        initepoch = checkpoint['epoch']
        loss = checkpoint['loss']

    for epoch in range(initepoch, 20):  # loop over the dataset multiple times
        timestart = time.time()

        running_loss = 0.0
        total = 0
        correct = 0
        for i, data in enumerate(trainloader, 0):
            # get the inputs
            inputs, labels = data
            inputs, labels = inputs.to(device), labels.to(device)

            optimizer.zero_grad()

            outputs = model(inputs)
            l = loss(outputs, labels)
            l.backward()
            optimizer.step()

            running_loss += l.item()

            if i % 500 == 499:
                print('[%d, %5d] loss: %.4f' %
                      (epoch, i, running_loss / 500))
                running_loss = 0.0
                _, predicted = torch.max(outputs.data, 1)
                total += labels.size(0)
                correct += (predicted == labels).sum().item()
                print('Accuracy of the network on the %d tran images: %.3f %%' % (total,
                                                                                  100.0 * correct / total))
                total = 0
                correct = 0
                torch.save({'epoch': epoch,
                            'model_state_dict': net.state_dict(),
                            'optimizer_state_dict': optimizer.state_dict(),
                            'loss': loss
                            }, path) 

        print('epoch %d cost %3f sec' % (epoch, time.time() - timestart))

    print('Finished Training') 

测试

 
def test(model, device):
    correct = 0
    total = 0
    with torch.no_grad():
        for data in testloader:
            images, labels = data
            images, labels = images.to(device), labels.to(device)
            outputs = model(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: %.3f %%' % (100.0 * correct / total))

def classify(self, device):
    class_correct = list(0. for i in range(10))
    class_total = list(0. for i in range(10))
    for data in testloader:
        images, labels = data
        images, labels = images.to(device), labels.to(device)
        outputs = self(images)
        _, predicted = torch.max(outputs.data, 1)
        c = (predicted == labels).squeeze()
        for i in range(4):
            label = labels[i]
            class_correct[label] += c[i]
            class_total[label] += 1

    for i in range(10):
        print('Accuracy of %5s : %2d %%' % (classes[i], 100 * class_correct[i] / class_total[i]))


训练

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = Net()
model 

model = model.to(device)
train_sgd(model, device) 

test(model, device)

classify(device)

你可能感兴趣的:(ML/DL,pytorch,cifar)