.. _sec_fashion_mnist:
图像分类数据集
==============
MNIST数据集 :cite:`LeCun.Bottou.Bengio.ea.1998`
是图像分类中广泛使用的数据集之一,但作为基准数据集过于简单。
我们将使用类似但更复杂的Fashion-MNIST数据集
:cite:`Xiao.Rasul.Vollgraf.2017`\ 。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
%matplotlib inline
import mindspore
import mindspore.dataset as ds
from mindspore.dataset import GeneratorDataset
from d2l import mindspore 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
读取数据集
----------
我们可以通过框架中的内置函数将Fashion-MNIST数据集下载并读取到内存中。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
mnist_train = d2l.FashionMnist('../../data/FashionMNIST', kind='train', remove_finished=False)
mnist_test = d2l.FashionMnist('../../data/FashionMNIST', kind='t10k')
mnist_train = ds.GeneratorDataset(source=mnist_train, column_names=['img', 'label'], shuffle=False)
mnist_test = ds.GeneratorDataset(source=mnist_test, column_names=['img', 'label'], shuffle=False)
.. 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
Fashion-MNIST由10个类别的图像组成, 每个类别由\ *训练数据集*\ (train
dataset)中的6000张图像 和\ *测试数据集*\ (test
dataset)中的1000张图像组成。
因此,训练集和测试集分别包含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), 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
.. 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
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
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
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 isinstance(img, mindspore.Tensor):
ax.imshow(img.asnumpy())
else:
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)):
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:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
mnist_train = mnist_train.batch(batch_size=18)
X, y = next(iter(mnist_train.create_tuple_iterator(output_numpy=False)))
show_images(X.reshape(18, 28, 28), 2, 9, titles=get_fashion_mnist_labels(y.asnumpy()));
.. figure:: output_image-classification-dataset_e45669_50_0.png
.. 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_53_0.svg
.. raw:: html
.. raw:: html
读取小批量
----------
为了使我们在读取训练集和测试集时更容易,我们使用内置的数据迭代器,而不是从零开始创建。
回顾一下,在每次迭代中,数据加载器每次都会读取一小批量数据,大小为\ ``batch_size``\ 。
通过内置数据迭代器,我们可以随机打乱了所有样本,从而无偏见地读取小批量。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
# 由于mindspore.dataset的设计,在进行batch操作后,产生的数据处理pipeline不会再改变,所以这里不进行二次batch操作,
# 直接使用已有的dataset测试性能
train_iter = mnist_train
.. 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:: 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.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
'0.37 sec'
.. raw:: html
.. raw:: html
整合所有组件
------------
现在我们定义\ ``load_data_fashion_mnist``\ 函数,用于获取和读取Fashion-MNIST数据集。
这个函数返回训练集和验证集的数据迭代器。
此外,这个函数还接受一个可选参数\ ``resize``\ ,用来将图像大小调整为另一种形状。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
import mindspore.dataset.transforms as transforms
import mindspore.dataset.vision as vision
def load_data_fashion_mnist(batch_size, resize=None, works=1):
"""将Fashion-MNIST数据集加载到内存中。"""
data_path = "../../data/FashionMNIST/"
mnist_train = d2l.FashionMnist(data_path, kind='train', remove_finished=False)
mnist_test = d2l.FashionMnist(data_path, kind='t10k')
mnist_train = ds.GeneratorDataset(source=mnist_train, column_names=['image', 'label'], shuffle=False)
mnist_test = ds.GeneratorDataset(source=mnist_test, column_names=['image', 'label'], shuffle=False)
trans = [vision.Rescale(1.0 / 255.0, 0), vision.HWC2CHW()]
type_cast_op = transforms.TypeCast(mindspore.int32)
if resize:
trans.insert(0, vision.Resize(resize))
mnist_train = mnist_train.map(trans, input_columns=["image"])
mnist_test = mnist_test.map(trans, input_columns=["image"])
mnist_train = mnist_train.map(type_cast_op, input_columns=['label'])
mnist_test = mnist_test.map(type_cast_op, input_columns=['label'])
mnist_train = mnist_train.batch(batch_size, num_parallel_workers=works)
mnist_test = mnist_test.batch(batch_size, num_parallel_workers=works)
return mnist_train, mnist_test
.. 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
下面,我们通过指定\ ``resize``\ 参数来测试\ ``load_data_fashion_mnist``\ 函数的图像大小调整功能。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
mnist_train, mnist_test = load_data_fashion_mnist(32, resize=64)
for X, y in mnist_train.create_tuple_iterator(output_numpy=True):
print(X.shape, X.dtype, y.shape, y.dtype)
break
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
(32, 1, 64, 64) float32 (32,) int32
.. 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
我们现在已经准备好使用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
.. raw:: html
`讨论 `__
.. raw:: html
.. raw:: html
`讨论 `__
.. raw:: html
.. raw:: html