图像分类数据集(Fashion-MNIST)
1. 获取数据集
%matplotlib inline
from IPython import display
from matplotlib import pyplot as plt
from mxnet.gluon import data as gdata
import sys
import time
mnist_train = gdata.vision.FashionMNIST(train=True)
mnist_test = gdata.vision.FashionMNIST(train=False)
Downloading C:\Users\林景\AppData\Roaming\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 C:\Users\林景\AppData\Roaming\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...
Downloading C:\Users\林景\AppData\Roaming\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 C:\Users\林景\AppData\Roaming\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...
训练集中和测试集中的每个类别的图像数分别为6,000和1,000。因为有10个类别,所以训练集和测试集的样本数分别为60,000和10,000。
len(mnist_train), len(mnist_test)
(60000, 10000)
feature, label = mnist_train[0]
变量feature对应?和宽均为28像素的图像。每个像素的数值为0到255之间8位?符号整数(uint8)。它使?三维的NDArray存储。其中的最后?维是通道数。因为数据集中是灰度图像,所以通道数为1。
label, type(label), label.dtype
(2, numpy.int32, dtype('int32'))
Fashion-MNIST中?共包括了10个类别,分别为t-shirt(T恤)、trouser(裤?)、pullover(套衫)、dress(连?裙)、coat(外套)、sandal(凉鞋)、shirt(衬衫)、sneaker(运动鞋)、bag(包)和ankleboot(短靴)。
def get_fashion_mnist_labels(labels):
text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat','sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
return [text_labels[int(i)] for i in labels]
from utils import use_svg_display
def show_fashion_mnist(images, labels):
use_svg_display()
_, figs = plt.subplots(1, len(images), figsize=(12, 12))
for f, img, lbl in zip(figs, images, labels):
f.imshow(img.reshape((28,28)).asnumpy())
f.set_title(lbl)
f.axes.get_xaxis().set_visible(False)
f.axes.get_yaxis().set_visible(False)
X, y = mnist_train[0:9]
show_fashion_mnist(X, get_fashion_mnist_labels(y))
?
?
2. 读取小批量
直接创建DataLoader实例。该实例每次读取?个样本数为batch_size的小批量数据。这?的批量?小batch_size是?个超参数。
在实践中,数据读取经常是训练的性能瓶颈,特别当模型较简单或者计算硬件性能较? 时。Gluon的DataLoader中?个很?便的功能是允许使?多进程来加速数据读取(暂不? 持Windows操作系统)。这?我们通过参数num_workers来设置4个进程读取数据。
此外,我们通过ToTensor实例将图像数据从uint8格式变换成32位浮点数格式,并除以255使得所有像素的数值均在0到1之间。ToTensor实例还将图像通道从最后?维移到最前?维来?便之后介绍的卷积神经?络计算。通过数据集的transform_first函数,我们将ToTensor的变换应?在每个数据样本(图像和标签)的第?个元素,即图像之上。
batch_size = 256
transformer = gdata.vision.transforms.ToTensor()
if sys.platform.startswith('win'):
num_workers = 0
else:
num_workers = 4
train_iter = gdata.DataLoader(mnist_train.transform_first(transformer), batch_size, shuffle=True,num_workers=num_workers)
test_iter = gdata.DataLoader(mnist_test.transform_first(transformer), batch_size, shuffle=False, num_workers=num_workers)
start = time.time()
for X, y in train_iter:
continue
print('%.2f sec'% (time.time() - start))
4.57 sec
|