.. _sec_fashion_mnist: 图像分类数据集 ============== MNIST数据集 :cite:`LeCun.Bottou.Bengio.ea.1998` 是图像分类中广泛使用的数据集之一,但作为基准数据集过于简单。 我们将使用类似但更复杂的Fashion-MNIST数据集 :cite:`Xiao.Rasul.Vollgraf.2017`\ 。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %matplotlib inline import sys from mxnet import gluon from d2l import mxnet as d2l d2l.use_svg_display() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %matplotlib inline import torch import torchvision from torch.utils import data from torchvision import transforms from d2l import torch as d2l d2l.use_svg_display() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %matplotlib inline import tensorflow as tf from d2l import tensorflow as d2l d2l.use_svg_display() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %matplotlib inline import warnings from d2l import paddle as d2l warnings.filterwarnings("ignore") import sys import paddle from paddle.vision import transforms d2l.use_svg_display() .. raw:: html
.. raw:: html
读取数据集 ---------- 我们可以通过框架中的内置函数将Fashion-MNIST数据集下载并读取到内存中。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python mnist_train = gluon.data.vision.FashionMNIST(train=True) mnist_test = gluon.data.vision.FashionMNIST(train=False) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Downloading /opt/mxnet/datasets/fashion-mnist/train-images-idx3-ubyte.gz from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/fashion-mnist/train-images-idx3-ubyte.gz... Downloading /opt/mxnet/datasets/fashion-mnist/train-labels-idx1-ubyte.gz from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/fashion-mnist/train-labels-idx1-ubyte.gz... [07:01:09] ../src/storage/storage.cc:196: Using Pooled (Naive) StorageManager for CPU Downloading /opt/mxnet/datasets/fashion-mnist/t10k-images-idx3-ubyte.gz from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/fashion-mnist/t10k-images-idx3-ubyte.gz... Downloading /opt/mxnet/datasets/fashion-mnist/t10k-labels-idx1-ubyte.gz from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/fashion-mnist/t10k-labels-idx1-ubyte.gz... .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python # 通过ToTensor实例将图像数据从PIL类型变换成32位浮点数格式, # 并除以255使得所有像素的数值均在0~1之间 trans = transforms.ToTensor() mnist_train = torchvision.datasets.FashionMNIST( root="../data", train=True, transform=trans, download=True) mnist_test = torchvision.datasets.FashionMNIST( root="../data", train=False, transform=trans, download=True) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python mnist_train, mnist_test = tf.keras.datasets.fashion_mnist.load_data() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python trans = transforms.ToTensor() mnist_train = paddle.vision.datasets.FashionMNIST(mode="train", transform=trans) mnist_test = paddle.vision.datasets.FashionMNIST(mode="test", transform=trans) .. raw:: html
.. raw:: html
Fashion-MNIST由10个类别的图像组成, 每个类别由\ *训练数据集*\ (train dataset)中的6000张图像 和\ *测试数据集*\ (test dataset)中的1000张图像组成。 因此,训练集和测试集分别包含60000和10000张图像。 测试数据集不会用于训练,只用于评估模型性能。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python len(mnist_train), len(mnist_test) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (60000, 10000) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python len(mnist_train), len(mnist_test) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (60000, 10000) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python len(mnist_train[0]), len(mnist_test[0]) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (60000, 10000) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python len(mnist_train), len(mnist_test) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (60000, 10000) .. raw:: html
.. raw:: html
每个输入图像的高度和宽度均为28像素。 数据集由灰度图像组成,其通道数为1。 为了简洁起见,本书将高度\ :math:`h`\ 像素、宽度\ :math:`w`\ 像素图像的形状记为\ :math:`h \times w`\ 或(\ :math:`h`,\ :math:`w`\ )。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python mnist_train[0][0].shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (28, 28, 1) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python mnist_train[0][0].shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output torch.Size([1, 28, 28]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python mnist_train[0][0].shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (28, 28) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python mnist_train[0][0].shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [1, 28, 28] .. raw:: html
.. raw:: html
Fashion-MNIST中包含的10个类别,分别为t-shirt(T恤)、trouser(裤子)、pullover(套衫)、dress(连衣裙)、coat(外套)、sandal(凉鞋)、shirt(衬衫)、sneaker(运动鞋)、bag(包)和ankle boot(短靴)。 以下函数用于在数字标签索引及其文本名称之间进行转换。 .. raw:: latex \diilbookstyleinputcell .. code:: python def get_fashion_mnist_labels(labels): #@save """返回Fashion-MNIST数据集的文本标签""" text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat', 'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot'] return [text_labels[int(i)] for i in labels] 我们现在可以创建一个函数来可视化这些样本。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5): #@save """绘制图像列表""" figsize = (num_cols * scale, num_rows * scale) _, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize) axes = axes.flatten() for i, (ax, img) in enumerate(zip(axes, imgs)): ax.imshow(img.asnumpy()) ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) if titles: ax.set_title(titles[i]) return axes .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5): #@save """绘制图像列表""" figsize = (num_cols * scale, num_rows * scale) _, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize) axes = axes.flatten() for i, (ax, img) in enumerate(zip(axes, imgs)): if torch.is_tensor(img): # 图片张量 ax.imshow(img.numpy()) else: # PIL图片 ax.imshow(img) ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) if titles: ax.set_title(titles[i]) return axes .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5): #@save """绘制图像列表""" figsize = (num_cols * scale, num_rows * scale) _, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize) axes = axes.flatten() for i, (ax, img) in enumerate(zip(axes, imgs)): ax.imshow(img.numpy()) ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) if titles: ax.set_title(titles[i]) return axes .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5): """绘制图像列表""" figsize = (num_cols * scale, num_rows * scale) _, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize) axes = axes.flatten() for i, (ax, img) in enumerate(zip(axes, imgs)): if paddle.is_tensor(img): # 图片张量 ax.imshow(img.numpy()) else: # PIL图片 ax.imshow(img) ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) if titles: ax.set_title(titles[i]) return axes .. raw:: html
.. raw:: html
以下是训练数据集中前几个样本的图像及其相应的标签。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X, y = mnist_train[:18] print(X.shape) show_images(X.squeeze(axis=-1), 2, 9, titles=get_fashion_mnist_labels(y)); .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (18, 28, 28, 1) .. figure:: output_image-classification-dataset_e45669_80_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X, y = next(iter(data.DataLoader(mnist_train, batch_size=18))) show_images(X.reshape(18, 28, 28), 2, 9, titles=get_fashion_mnist_labels(y)); .. figure:: output_image-classification-dataset_e45669_83_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = tf.constant(mnist_train[0][:18]) y = tf.constant(mnist_train[1][:18]) show_images(X, 2, 9, titles=get_fashion_mnist_labels(y)); .. figure:: output_image-classification-dataset_e45669_86_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X, y = next(iter(paddle.io.DataLoader(mnist_train, batch_size=18))) show_images(X.reshape([18, 28, 28]), 2, 9, titles=get_fashion_mnist_labels(y)); .. figure:: output_image-classification-dataset_e45669_89_0.svg .. raw:: html
.. raw:: html
读取小批量 ---------- 为了使我们在读取训练集和测试集时更容易,我们使用内置的数据迭代器,而不是从零开始创建。 回顾一下,在每次迭代中,数据加载器每次都会读取一小批量数据,大小为\ ``batch_size``\ 。 通过内置数据迭代器,我们可以随机打乱了所有样本,从而无偏见地读取小批量。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python batch_size = 256 def get_dataloader_workers(): #@save """在非Windows的平台上,使用4个进程来读取数据""" return 0 if sys.platform.startswith('win') else 4 # 通过ToTensor实例将图像数据从uint8格式变换成32位浮点数格式,并除以255使得所有像素的数值 # 均在0~1之间 transformer = gluon.data.vision.transforms.ToTensor() train_iter = gluon.data.DataLoader(mnist_train.transform_first(transformer), batch_size, shuffle=True, num_workers=get_dataloader_workers()) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python batch_size = 256 def get_dataloader_workers(): #@save """使用4个进程来读取数据""" return 4 train_iter = data.DataLoader(mnist_train, batch_size, shuffle=True, num_workers=get_dataloader_workers()) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python batch_size = 256 train_iter = tf.data.Dataset.from_tensor_slices( mnist_train).batch(batch_size).shuffle(len(mnist_train[0])) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python batch_size = 256 def get_dataloader_workers(): #@save """使用4个进程来读取数据""" return 4 train_iter = paddle.io.DataLoader(dataset=mnist_train, batch_size=batch_size, shuffle=True, return_list=True, num_workers=get_dataloader_workers()) .. raw:: html
.. raw:: html
我们看一下读取训练数据所需的时间。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python timer = d2l.Timer() for X, y in train_iter: continue f'{timer.stop():.2f} sec' .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output '1.89 sec' .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python timer = d2l.Timer() for X, y in train_iter: continue f'{timer.stop():.2f} sec' .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output '3.37 sec' .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python timer = d2l.Timer() for X, y in train_iter: continue f'{timer.stop():.2f} sec' .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output '0.31 sec' .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python timer = d2l.Timer() for X, y in train_iter: continue f'{timer.stop():.2f} sec' .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output '5.65 sec' .. raw:: html
.. raw:: html
整合所有组件 ------------ 现在我们定义\ ``load_data_fashion_mnist``\ 函数,用于获取和读取Fashion-MNIST数据集。 这个函数返回训练集和验证集的数据迭代器。 此外,这个函数还接受一个可选参数\ ``resize``\ ,用来将图像大小调整为另一种形状。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def load_data_fashion_mnist(batch_size, resize=None): #@save """下载Fashion-MNIST数据集,然后将其加载到内存中""" dataset = gluon.data.vision trans = [dataset.transforms.ToTensor()] if resize: trans.insert(0, dataset.transforms.Resize(resize)) trans = dataset.transforms.Compose(trans) mnist_train = dataset.FashionMNIST(train=True).transform_first(trans) mnist_test = dataset.FashionMNIST(train=False).transform_first(trans) return (gluon.data.DataLoader(mnist_train, batch_size, shuffle=True, num_workers=get_dataloader_workers()), gluon.data.DataLoader(mnist_test, batch_size, shuffle=False, num_workers=get_dataloader_workers())) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def load_data_fashion_mnist(batch_size, resize=None): #@save """下载Fashion-MNIST数据集,然后将其加载到内存中""" trans = [transforms.ToTensor()] if resize: trans.insert(0, transforms.Resize(resize)) trans = transforms.Compose(trans) mnist_train = torchvision.datasets.FashionMNIST( root="../data", train=True, transform=trans, download=True) mnist_test = torchvision.datasets.FashionMNIST( root="../data", train=False, transform=trans, download=True) return (data.DataLoader(mnist_train, batch_size, shuffle=True, num_workers=get_dataloader_workers()), data.DataLoader(mnist_test, batch_size, shuffle=False, num_workers=get_dataloader_workers())) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def load_data_fashion_mnist(batch_size, resize=None): #@save """下载Fashion-MNIST数据集,然后将其加载到内存中""" mnist_train, mnist_test = tf.keras.datasets.fashion_mnist.load_data() # 将所有数字除以255,使所有像素值介于0和1之间,在最后添加一个批处理维度, # 并将标签转换为int32。 process = lambda X, y: (tf.expand_dims(X, axis=3) / 255, tf.cast(y, dtype='int32')) resize_fn = lambda X, y: ( tf.image.resize_with_pad(X, resize, resize) if resize else X, y) return ( tf.data.Dataset.from_tensor_slices(process(*mnist_train)).batch( batch_size).shuffle(len(mnist_train[0])).map(resize_fn), tf.data.Dataset.from_tensor_slices(process(*mnist_test)).batch( batch_size).map(resize_fn)) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def load_data_fashion_mnist(batch_size, resize=None): """下载Fashion-MNIST数据集,然后将其加载到内存中""" trans = [transforms.ToTensor()] if resize: trans.insert(0, transforms.Resize(resize)) trans = transforms.Compose(trans) mnist_train = paddle.vision.datasets.FashionMNIST(mode="train", transform=trans) mnist_test = paddle.vision.datasets.FashionMNIST(mode="test", transform=trans) return (paddle.io.DataLoader(dataset=mnist_train, batch_size=batch_size, shuffle=True, return_list=True, num_workers=get_dataloader_workers()), paddle.io.DataLoader(dataset=mnist_test, batch_size=batch_size, return_list=True, shuffle=True, num_workers=get_dataloader_workers())) .. raw:: html
.. raw:: html
下面,我们通过指定\ ``resize``\ 参数来测试\ ``load_data_fashion_mnist``\ 函数的图像大小调整功能。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python train_iter, test_iter = load_data_fashion_mnist(32, resize=64) for X, y in train_iter: print(X.shape, X.dtype, y.shape, y.dtype) break .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (32, 1, 64, 64) (32,) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python train_iter, test_iter = load_data_fashion_mnist(32, resize=64) for X, y in train_iter: print(X.shape, X.dtype, y.shape, y.dtype) break .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output torch.Size([32, 1, 64, 64]) torch.float32 torch.Size([32]) torch.int64 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python train_iter, test_iter = load_data_fashion_mnist(32, resize=64) for X, y in train_iter: print(X.shape, X.dtype, y.shape, y.dtype) break .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (32, 64, 64, 1) (32,) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python train_iter, test_iter = load_data_fashion_mnist(32, resize=64) for X, y in train_iter: print(X.shape, X.dtype, y.shape, y.dtype) break .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [32, 1, 64, 64] paddle.float32 [32, 1] paddle.int64 .. raw:: html
.. raw:: html
我们现在已经准备好使用Fashion-MNIST数据集,便于下面的章节调用来评估各种分类算法。 小结 ---- - Fashion-MNIST是一个服装分类数据集,由10个类别的图像组成。我们将在后续章节中使用此数据集来评估各种分类算法。 - 我们将高度\ :math:`h`\ 像素,宽度\ :math:`w`\ 像素图像的形状记为\ :math:`h \times w`\ 或(\ :math:`h`,\ :math:`w`\ )。 - 数据迭代器是获得更高性能的关键组件。依靠实现良好的数据迭代器,利用高性能计算来避免减慢训练过程。 练习 ---- 1. 减少\ ``batch_size``\ (如减少到1)是否会影响读取性能? 2. 数据迭代器的性能非常重要。当前的实现足够快吗?探索各种选择来改进它。 3. 查阅框架的在线API文档。还有哪些其他数据集可用? .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html