pytorch之Fashion MNIST 图像分类

  |   0 评论   |   0 浏览

背景

学习一下如何使用pytorch来处理问题。

这里用Fashion MNIST例子。

数据集介绍

本文略过,可以参见前面的文章。

初体验

FC网络

输入图像大小为28x28,设计如下全连接网络:

INPUT(784) + FC1(1000) + Relu1 + FC2(500) + Relu2 + FC3(200) + Relu3 + FC4(10) + log_softmax

from __future__ import print_function   # 从future版本导入print函数功能
import argparse                         # 加载处理命令行参数的库
import torch                            # 引入相关的包
import torch.nn as nn                   # 指定torch.nn别名nn
import torch.nn.functional as F         # 引用神经网络常用函数包,不具有可学习的参数
import torch.optim as optim
from torchvision import datasets, transforms  # 加载pytorch官方提供的dataset


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(784, 1000)  # 784表示输入神经元数量,1000表示输出神经元数量
        self.fc2 = nn.Linear(1000, 500)
        self.fc3 = nn.Linear(500, 200)
        self.fc4 = nn.Linear(200, 10)

    def forward(self, x):
        x = x.view(-1, 28*28)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = self.fc4(x)
        # Applies a softmax followed by a logarithm, output batch * classes tensor
        return F.log_softmax(x, dim=1)


def train(args, model, device, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad()
        output = model(data)
        # negative log likelihood loss(nll_loss), sum up batch cross entropy
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()                        # 根据parameter的梯度更新parameter的值
        #print(epoch, batch_idx, type(batch_idx))
        if batch_idx % args.log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))


def test(args, model, device, test_loader):
    model.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():  # 无需计算梯度
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            # sum up batch loss
            test_loss += F.nll_loss(output, target, reduction='sum').item()
            # get the index of the max log-probability
            pred = output.argmax(dim=1, keepdim=True)
            correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(test_loader.dataset)

    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))


def main():
    # Training settings
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch-size', type=int, default=64, metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--epochs', type=int, default=10, metavar='N',
                        help='number of epochs to train (default: 10)')
    parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--momentum', type=float, default=0.5, metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda', action='store_true', default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed', type=int, default=1, metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--log-interval', type=int, default=10, metavar='N',
                        help='how many batches to wait before logging training status')

    parser.add_argument('--save-model', action='store_true', default=True,
                        help='For Saving the current Model')
    args = parser.parse_args()
    use_cuda = not args.no_cuda and torch.cuda.is_available()

    torch.manual_seed(args.seed)

    device = torch.device("cuda" if use_cuda else "cpu")

    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
    train_loader = torch.utils.data.DataLoader(
        datasets.FashionMNIST('./fashionmnist_data/', train=True, download=True,
                              transform=transforms.Compose([
                                  transforms.ToTensor(),
                                  transforms.Normalize((0.1307,), (0.3081,))
                              ])),
        batch_size=args.batch_size, shuffle=True, **kwargs)

    test_loader = torch.utils.data.DataLoader(
        datasets.FashionMNIST('./fashionmnist_data/', train=False, transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307,), (0.3081,))
        ])),
        batch_size=args.test_batch_size, shuffle=True, **kwargs)

    model = Net().to(device)

    # optimizer存储了所有parameters的引用,每个parameter都包含gradient
    optimizer = optim.SGD(model.parameters(), lr=args.lr,
                          momentum=args.momentum)

    scheduler = optim.lr_scheduler.MultiStepLR(
        optimizer, milestones=[12, 24], gamma=0.1)  # 学习率按区间更新

    for epoch in range(1, args.epochs + 1):
        train(args, model, device, train_loader, optimizer, epoch)
        test(args, model, device, test_loader)

    if (args.save_model):
        torch.save(model.state_dict(), "mnist_fc.pt")


# 当.py文件直接运行时,该语句及以下的代码被执行,当.py被调用时,该语句及以下的代码不被执行
if __name__ == '__main__':
    main()

结果

Using downloaded and verified file: ./fashionmnist_data/FashionMNIST/raw/train-images-idx3-ubyte.gz
Extracting ./fashionmnist_data/FashionMNIST/raw/train-images-idx3-ubyte.gz to ./fashionmnist_data/FashionMNIST/raw
Using downloaded and verified file: ./fashionmnist_data/FashionMNIST/raw/train-labels-idx1-ubyte.gz
Extracting ./fashionmnist_data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to ./fashionmnist_data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to ./fashionmnist_data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz
4423680it [00:05, 746242.74it/s]
Extracting ./fashionmnist_data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to ./fashionmnist_data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to ./fashionmnist_data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz
8192it [00:00, 16499.39it/s]
Extracting ./fashionmnist_data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to ./fashionmnist_data/FashionMNIST/raw
Processing...
/Users/xuqian/Library/Python/3.7/lib/python/site-packages/torchvision/datasets/mnist.py:469: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at  ../torch/csrc/utils/tensor_numpy.cpp:141.)
  return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)
Done!
Train Epoch: 1 [0/60000 (0%)]	Loss: 2.293269
Train Epoch: 1 [640/60000 (1%)]	Loss: 2.262661
Train Epoch: 1 [1280/60000 (2%)]	Loss: 2.207895
Train Epoch: 1 [1920/60000 (3%)]	Loss: 2.168133
…………
Train Epoch: 10 [57600/60000 (96%)]	Loss: 0.288563
Train Epoch: 10 [58240/60000 (97%)]	Loss: 0.152729
Train Epoch: 10 [58880/60000 (98%)]	Loss: 0.212459
Train Epoch: 10 [59520/60000 (99%)]	Loss: 0.240333

Test set: Average loss: 0.3357, Accuracy: 8802/10000 (88%)

即训练结果top1准确率为88%,模型大小为5.3M。

CNN网络

FC网络参数量太大,而CNN网络考虑到图像的局部关联特性,使用卷积网络,参数量大大减小,设计如下CNN:

conv(1, 20, 5) + Relu + conv(20, 50, 5) + flatten + Relu + FC(10) + log_softmax

from __future__ import print_function   # 从future版本导入print函数功能
import argparse                         # 加载处理命令行参数的库
import torch                            # 引入相关的包
import torch.nn as nn                   # 指定torch.nn别名nn
import torch.nn.functional as F         # 引用神经网络常用函数包,不具有可学习的参数
import torch.optim as optim
from torchvision import datasets, transforms  # 加载pytorch官方提供的dataset


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # 1表示输入通道,20表示输出通道,5表示conv核大小,1表示conv步长
        self.conv1 = nn.Conv2d(1, 20, 5, 1)
        self.conv2 = nn.Conv2d(20, 50, 5, 1)
        self.fc1 = nn.Linear(4 * 4 * 50, 500)
        self.fc2 = nn.Linear(500, 10)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = x.view(-1, 4 * 4 * 50)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)


def train(args, model, device, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad()
        output = model(data)
        # negative log likelihood loss(nll_loss), sum up batch cross entropy
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()                        # 根据parameter的梯度更新parameter的值
        #print(epoch, batch_idx, type(batch_idx))
        if batch_idx % args.log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))


def test(args, model, device, test_loader):
    model.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():  # 无需计算梯度
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            # sum up batch loss
            test_loss += F.nll_loss(output, target, reduction='sum').item()
            # get the index of the max log-probability
            pred = output.argmax(dim=1, keepdim=True)
            correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(test_loader.dataset)

    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))


def main():
    # Training settings
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch-size', type=int, default=64, metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--epochs', type=int, default=10, metavar='N',
                        help='number of epochs to train (default: 10)')
    parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--momentum', type=float, default=0.5, metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda', action='store_true', default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed', type=int, default=1, metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--log-interval', type=int, default=10, metavar='N',
                        help='how many batches to wait before logging training status')

    parser.add_argument('--save-model', action='store_true', default=True,
                        help='For Saving the current Model')
    args = parser.parse_args()
    use_cuda = not args.no_cuda and torch.cuda.is_available()

    torch.manual_seed(args.seed)

    device = torch.device("cuda" if use_cuda else "cpu")

    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
    train_loader = torch.utils.data.DataLoader(
        datasets.FashionMNIST('./fashionmnist_data/', train=True, download=True,
                              transform=transforms.Compose([
                                  transforms.ToTensor(),
                                  transforms.Normalize((0.1307,), (0.3081,))
                              ])),
        batch_size=args.batch_size, shuffle=True, **kwargs)

    test_loader = torch.utils.data.DataLoader(
        datasets.FashionMNIST('./fashionmnist_data/', train=False, transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307,), (0.3081,))
        ])),
        batch_size=args.test_batch_size, shuffle=True, **kwargs)

    model = Net().to(device)

    # optimizer存储了所有parameters的引用,每个parameter都包含gradient
    optimizer = optim.SGD(model.parameters(), lr=args.lr,
                          momentum=args.momentum)

    scheduler = optim.lr_scheduler.MultiStepLR(
        optimizer, milestones=[12, 24], gamma=0.1)  # 学习率按区间更新

    for epoch in range(1, args.epochs + 1):
        train(args, model, device, train_loader, optimizer, epoch)
        test(args, model, device, test_loader)

    if (args.save_model):
        torch.save(model.state_dict(), "mnist_cnn.pt")


# 当.py文件直接运行时,该语句及以下的代码被执行,当.py被调用时,该语句及以下的代码不被执行
if __name__ == '__main__':
    main()

结果

Train Epoch: 1 [0/60000 (0%)]	Loss: 2.293269
Train Epoch: 1 [640/60000 (1%)]	Loss: 2.262661
Train Epoch: 1 [1280/60000 (2%)]	Loss: 2.207895
Train Epoch: 1 [1920/60000 (3%)]	Loss: 2.168133
…………
Train Epoch: 10 [56960/60000 (95%)]	Loss: 0.345022
Train Epoch: 10 [57600/60000 (96%)]	Loss: 0.293099
Train Epoch: 10 [58240/60000 (97%)]	Loss: 0.098339
Train Epoch: 10 [58880/60000 (98%)]	Loss: 0.137070
Train Epoch: 10 [59520/60000 (99%)]	Loss: 0.154875

Test set: Average loss: 0.2843, Accuracy: 8996/10000 (90%)

即训练结果top1准确率为90%,模型大小为1.7M。

参考