《Gluon 动手学深度学习》显示图像数据集Fashion-MNIST

%matplotlib inline
import sys
sys.path.append('..')
import gluonbook as gb
from mxnet.gluon import data as gdata
import time

mnist_train=gdata.vision.FashionMNIST(train = True)
mnist_test=gdata.vision.FashionMNIST(train = False)
len(mnist_train),len(mnist_test)

feature,label =mnist_train[0]
feature.shape,feature.dtype


# 本函数已保存在 gluonbook 包中方便以后使用。
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]

# 本函数已保存在 gluonbook 包中方便以后使用。
def show_fashion_mnist(images, labels):
    gb.use_svg_display()
    # 这里的 _ 表示我们忽略(不使用)的变量。
    _, figs = gb.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)
        #print(lbl)
        #print(img.T)

        
X, y = mnist_train[0:10]
show_fashion_mnist(X, get_fashion_mnist_labels(y))

结果为:

使用gluonbook显示图像非常方便快捷,主要涉及到以下几个方面:

显示图像的模块:%matplotlib inline

显示图像的函数:show_fashion_mnist()

显示图像标签及图像每个像素值:        

print(lbl)
print(img.T)

猜你喜欢

转载自blog.csdn.net/qq_42189368/article/details/83743325