【问题标题】:How can I generate a proper MNIST image?如何生成正确的 MNIST 图像?
【发布时间】:2017-02-26 17:04:19
【问题描述】:

大家好,我一直在从事一个 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 数组被重新整形为 [28, 28] 时无法查明它来自 [784]。有什么线索吗?

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

【问题讨论】:

  • 我读过那篇文章,但它并没有真正解释我的图像看起来不像数字?
  • 嗯,很明显,如果我使用 matplotlib 而不是 PIL,图像将正确显示......我想知道为什么。无论如何,我会暂时使用 matplotlib

标签: python numpy tensorflow mnist


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 2017-12-26
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    相关资源
    最近更新 更多