基于 PaddlePaddle / PyTorch 实现 CIFAR-10 / MNIST 图像分类

该博客介绍了如何使用PaddlePaddle和PyTorch实现CIFAR-10及MNIST数据集的图像分类。详细讨论了两个框架的介绍,数据集的特性,数据加载方法,AlexNet模型的调整及实现,以及在两种框架下的训练过程和结果。
摘要由CSDN通过智能技术生成

1 框架介绍

1.1 PaddlePaddle

PaddlePaddle 官网
PaddlePaddle 中文文档
深度学习平台飞桨(PaddlePaddle)是主流深度学习框架中一款完全国产化的产品,与 Google TensorFlow、Facebook Pytorch 齐名。2016 年飞桨正式开源,是国内首个全面开源开放、技术领先、功能完备的产业级深度学习平台。相比国内其他平台,飞桨是一个功能完整的深度学习平台,也是唯一成熟稳定、具备大规模推广条件的深度学习平台。

1.2 PyTorch

PyTorch 官网
PyTorch 中文文档
PyTorch 是一个基于 Torch 的 Python 开源机器学习库,用于自然语言处理等应用程序。它主要由 Facebook 的人工智能小组开发,不仅能够实现强大的GPU 加速,同时还支持动态神经网络。 PyTorch 提供了两个高级功能: 1. 具有强大的 GPU 加速的张量计算;2. 包含自动求导系统的深度神经网络。

2 数据集介绍

2.1 CIFAR-10

CIFAR-10 官网
CIFAR-10 数据集共有 60000 张彩色图像,其中 50000 张用于训练,10000 张用于测试,图像分辨率为 32×32,分为 10 个类别(airplane,automobile,bird,cat,deer,dog,frog,horse,ship,truck),每个类别有 6000 张图像。
在这里插入图片描述

2.2 MNIST

MNIST 官网
MNIST 数据集共有 70000 张黑白图像,其中训练集包含 60000 张图像,测试集包含 10000 张图像,图像分辨率为 28×28,分为 10 个类别(0~9)
在这里插入图片描述
MNIST 数据集包含四个压缩的二进制文件

文件名 内容
train-images-idx3-ubyte.gz 60000 张训练集图片
train-labels-idx1-ubyte.gz 60000 张训练集图片对应的标签
t10k-images-idx3-ubyte.gz 10000 张测试集图片
t10k-labels-idx1-ubyte.gz 10000 张测试集图片对应的标签

存储图像的二进制文件:

1~4 字节 5~8 字节 9~12 字节 13~16 字节 17~ 字节
文件的 magic number(2051) 图像数量 图像的高度 图像的宽度 像素值

存储标签的二进制文件:

1~4 字节 5~8 字节 9~ 字节
文件的 magic number(2049) 标签数量 标签值

3 数据加载

3.1 CIFAR-10

datasets.py

'''
datasets.py
'''
import os
import gzip
from struct import unpack
import numpy as np

# 从文件中加载 CIFAR-10 数据集
def cifar10(data_path='./data', data_split=None):
    if data_split is None:
        split = ['data_batch', 'test_batch']
    elif data_split == 'train':
        split = ['data_batch', ]
    elif data_split == 'test':
        split = ['test_batch', ]
    else:
        raise ValueError('data_split should be in (train, test)')
    print('loading CIFAR-10 dataset ......')

    data = []
    with tarfile.open(os.path.join(data_path, 'cifar-10-python.tar.gz'), mode='r') as f:
        for item in split:
            images, labels = [], []
            for file_name in f:
                if item in file_name.name:
                    temp = pickle.load(f.extractfile(file_name.name), encoding='bytes')
                    images.append(temp[b'data'])
                    labels += temp[b'labels']
            images = np.concatenate(images, axis=0).reshape(-1, 3, 32, 32)
            labels = np.array(labels).reshape(-1, 1)
            assert images.shape[0] == labels.shape[0], \
                'length of images({}) should be the same as labels({})'.format(images.shape[0], labels.shape[0])
            data.append((images, labels))
    return tuple(data)

3.2 MNIST

datasets.py

'''
datasets.py
'''
import os
import tarfile
import pickle
import numpy as np

# 从文件中加载 MNIST 数据集
def mnist(data_path='./data', data_split=None):
    if data_split is None:
        split = ['train', 't10k']
    elif data_split == 'train':
        split = ['train',]
    elif data_split == 'test':
        split = ['t10k',]
    else:
        raise ValueError('data_split should be in (train, test)')
    print('loading MNIST dataset ......')

    data = []
    for item in split:
        images_file = os.path.join(data_path, item + '-images-idx3-ubyte.gz')
        labels_file = os.path.join(data_path, item + '-labels-idx1-ubyte.gz')
        with gzip.open(images_file, mode='rb') as f:
            magic, nums, rows, cols = unpack('>4I', f.read(16))
            images = np.frombuffer(f.read(), dtype=np.uint8).reshape(nums, 1, rows, cols)
        with gzip.open(labels_file, mode='rb') as f:
            magic, nums = unpack('>2I', f.read(8))
            labels = np.frombuffer(f.read(), dtype=np.uint8).reshape(nums, 1)
        assert images.shape[0] == labels.shape[0], \
            'length of images({}) should be the same as labels({})'.format(images.shape[0], labels.shape[0])
        data.append((images, labels))
    return tuple(data)

3.3 数据加载器

datasets.py

'''
datasets.py
'''
import random
import numpy as np

# 数据加载器
def data_loader(data, batch_size=100, shuffle=False):
    images = (data[0].astype('float32') / 255.0 - 0.5) / 0.5 # 数据归一化: 0~255 -->> -1~1
    labels = data[1].astype('int64')
    assert images.shape[0] == labels.shape[0], \
        'length of images({}) should be the same as labels({})'.format(images.shape[0], labels.shape[0])
    data_ids = list(range(labels.shape[0]))

    def generator():
        if shuffle:
            random.shuffle(data_ids)
        batch_image, batch_label = [], []
        for i in data_ids:
            batch_image.append(images[i])
            batch_label.append(labels[i])
            # 返回批数据
            if len(batch_label) == batch_size:
                yield np.array(batch_image), np.array(batch_label)
                batch_image, batch_label = [], []
        if len(batch_label) > 0:
            yield np.array(batch_image), np.array(batch_label)

    return generator


if __name__ == '__main__':
    for data_name in ('mnist', 'cifar10'):
        train_data, test_data = eval(data_name)()
        print(data_name)
        print('train_data', train_data[0].shape, train_data[1].shape, train_data[0].dtype, train_data[1].dtype)
        print('test_data', test_data[0].shape, test_data[1].shape, test_data[0].dtype, test_data[1].dtype)
        train_loader = data_load
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyTorch可以很容易地实现CIFAR-10多分类任务。以下是一些步骤: 1. 导入必要的库和数据集: ``` import torch import torchvision import torchvision.transforms as transforms transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((.5, .5, .5), (.5, .5, .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=2) 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=2) classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') ``` 2. 定义神经网络模型: ``` import torch.nn as nn import torch.nn.functional as F 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() ``` 3. 定义损失函数和优化器: ``` import torch.optim as optim criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=.001, momentum=.9) ``` 4. 训练模型: ``` for epoch in range(2): # loop over the dataset multiple times running_loss = . for i, data in enumerate(trainloader, ): # get the inputs; data is a list of [inputs, labels] inputs, labels = data # zero the parameter gradients optimizer.zero_grad() # forward + backward + optimize outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() # print statistics running_loss += loss.item() if i % 200 == 1999: # print every 200 mini-batches print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 200)) running_loss = . print('Finished Training') ``` 5. 测试模型: ``` correct = total = with torch.no_grad(): for data in testloader: images, labels = data outputs = net(images) _, predicted = torch.max(outputs.data, 1) total += labels.size() correct += (predicted == labels).sum().item() print('Accuracy of the network on the 10000 test images: %d %%' % ( 100 * correct / total)) ``` 这就是使用PyTorch实现CIFAR-10多分类任务的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值