如何生成正确的MNIST图像?

时间:2017-02-26 17:04:19

标签: python numpy tensorflow mnist

嘿伙计们,所以我一直在研究tensorflow项目,我想从MNIST数据库中获取测试图像。下面是我将原始数据(ubyte?)转换为2d numpy的代码的要点:

from PIL import Image
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot = True)
def gen_image(arr):
   two_d = np.reshape(arr, (28, 28))
   img = Image.fromarray(two_d, 'L')
   return img

batch_xs, batch_ys = mnist.test.next_batch(1)
gen_image(batch_xs[0]).show()

然而,当img显示here时,它看起来不像普通数字,所以我认为我必须搞砸到某处,但除了将numpy数组重新变形为[来自[784]的28,28]。有线索吗?

编辑:显然,如果我使用matplotlib而不是PIL,它可以正常工作:

1 个答案:

答案 0 :(得分:2)

将数据乘以255并转换为np.uint8 (uint8 for mode 'L')让它有效。

def gen_image(arr):
    two_d = (np.reshape(arr, (28, 28)) * 255).astype(np.uint8)
    img = Image.fromarray(two_d, 'L')
    return img

This answer helps.

相关问题