我怎样才能产生一个正确的MNIST形象?

2024-06-17 13:28:00 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,伙计们,我一直在做一个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,它会很好地工作:


Tags: fromimageimportnumpyimginputdatapil
1条回答
网友
1楼 · 发布于 2024-06-17 13:28:00

将数据乘以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.

相关问题 更多 >