[英]How can I generate a proper MNIST image?
Hey guys so I've been working on a tensorflow project and I want to take a took at the test images from the MNIST database. 大家好,所以我一直在做一个tensorflow项目,我想从MNIST数据库中获取测试图像。 Below is the gist of my code for converting the original data(ubyte?) into 2d numpy: 下面是我将原始数据(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()
However when the img gets shown here , it looks nothing like a normal number so I figure I must have messed up somewhere, but can't pinpoint it other than when the numpy array gets reshaped to [28, 28] from [784]. 但是,当在这里显示img时,它看起来似乎不像是正常数字,因此我认为我一定在某个地方搞砸了,但是除了numpy数组从[784]重塑为[28,28]之外,它无法查明。 Any clues? 有什么线索吗?
EDIT: So apparently if I use matplotlib instead of PIL it works fine: 编辑:所以很显然,如果我使用matplotlib而不是PIL可以正常工作:
Multiply the data by 255 and convert to np.uint8 (uint8 for mode 'L') have it work. 将该数据乘以255,然后转换为np.uint8 (对于“ L”模式为uint8)可以正常工作。
def gen_image(arr):
two_d = (np.reshape(arr, (28, 28)) * 255).astype(np.uint8)
img = Image.fromarray(two_d, 'L')
return img
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.