利用python中的 PIL(python Image Library) 模块进行显示
import sys, os
sys.path.append(os.pardir)
import numpy as np
from dataset.mnist import load_mnist
from PIL import Image
def img_show(img):
pil_img = Image.fromarray(np.uint8(img)) # 数组图像数据转化为PIL用的数据对象
pil_img.show()
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False) # 读入已经pickle的mnist数据
img = x_train[0]
label = t_train[0]
print(label) # 5
print(img.shape) # (784,)
img = img.reshape(28, 28) # 把图像形状变成原来的尺寸,因为flatten为True,故把图像读成了一维(1列)形式。要显示图像就要还原
print(img.shape) # (28, 28)
img_show(img)
flatten=True 时读入的图像是一维的(一列),显示图像时,需要将其变成原来的28*28像素,故要用到reshap()