简体   繁体   English

如何生成合适的MNIST图像?

[英]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

This answer helps. 这个答案有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用经过mnist训练的模型来预测图像 - How can i use my mnist trained model to predict an image 如何预处理我的图像,以便 SVM 可以像处理 MNIST 数据集一样处理它 - How can I preprocess my image so it can be processed by a SVM in the same way it processes the MNIST dataset Tensorflow:如果我有训练有素的 MNIST model,如何检测图像中的手写数字? - Tensorflow: how can I detect handwriting numbers in an image if I have a trained MNIST model? 如何在python中调整图像的大小以降低MNIST时尚数据等灰度低分辨率? - How can I resize an image in python to gray scale low resolution like MNIST fashion data? 如何在 tensorflow 2.1.0 中将 mnist 数据集转换为 tfrecords - How can I convert mnist dataset into tfrecords in tensorflow 2.1.0 我如何将 KNN 训练到 Fashion MNIST 上? - How can I train KNN onto Fashion MNIST? 如何在 TensorFlow 数据集中使用 Mnist 数据集? - how I can work with Mnist dataset in the TensorFlow-datasets? 如何将 mnist 数据转换为 RGB 格式? - How can i convert mnist data to RGB format? 如何在 mnist tensorflow python 中获得预测的 class 标签? - How can i get the predicted class labels in mnist tensorflow python? 如何居中/检测 MNIST 手写数字预测的数字? - How can I centre/detect the digits for MNIST Handwritten Digit Prediction?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM