Python 使用 matplotlib 显示 MNIST 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42353676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display MNIST image using matplotlib
提问by buydadip
I am using tensorflow to import some MNIST input data. I followed this tutorial...https://www.tensorflow.org/get_started/mnist/beginners
我正在使用 tensorflow 导入一些 MNIST 输入数据。我跟着本教程... https://www.tensorflow.org/get_started/mnist/beginners
I am importing them as so...
我正在导入它们...
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
I want to be able to display any of the images from the training set. I know the location of the images is mnist.train.images
, so I try to access the first images and display it like so...
我希望能够显示训练集中的任何图像。我知道图像的位置是mnist.train.images
,所以我尝试访问第一张图像并像这样显示它......
with tf.Session() as sess:
#access first image
first_image = mnist.train.images[0]
first_image = np.array(first_image, dtype='uint8')
pixels = first_image.reshape((28, 28))
plt.imshow(pixels, cmap='gray')
I a attempt to convert the image to a 28 by 28 numpy array because I know that each image is 28 by 28 pixels.
我尝试将图像转换为 28 x 28 的 numpy 数组,因为我知道每个图像都是 28 x 28 像素。
However, when I run the code all I get is the following...
但是,当我运行代码时,我得到的只是以下内容...
Clearly I am doing something wrong. When I print out the matrix, everything seems to look good, but I think I am incorrectly reshaping it.
显然我做错了什么。当我打印出矩阵时,一切看起来都不错,但我认为我错误地重塑了它。
采纳答案by allo
You are casting an array of floats (as described in the docs) to uint8
, which truncates them to 0, if they are not 1.0
. You should either round them or use them as floats or multiply with 255.
您正在将一组浮点数(如文档中所述)投射uint8
到 ,如果它们不是 ,则将它们截断为 0 1.0
。您应该将它们舍入或将它们用作浮点数或乘以 255。
I am not sure, why you don't see the white background, but i would suggest to use a well defined gray scale anyway.
我不确定,为什么你看不到白色背景,但我建议无论如何使用定义明确的灰度。
回答by Vinh Trieu
Here is the complete code for showing image using matplotlib
这是使用 matplotlib 显示图像的完整代码
from matplotlib import pyplot as plt
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot = True)
first_image = mnist.test.images[0]
first_image = np.array(first_image, dtype='float')
pixels = first_image.reshape((28, 28))
plt.imshow(pixels, cmap='gray')
plt.show()
回答by wheresmypdp10
The following code shows example images displayed from the MNIST digit database used for training neural networks. It uses a variety of pieces of code from around stackflow and avoids pil.
以下代码显示了用于训练神经网络的 MNIST 数字数据库中显示的示例图像。它使用了来自 stackflow 的各种代码片段并避免了 pil。
# Tested with Python 3.5.2 with tensorflow and matplotlib installed.
from matplotlib import pyplot as plt
import numpy as np
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)) * 255).astype(np.uint8)
plt.imshow(two_d, interpolation='nearest')
return plt
# Get a batch of two random images and show in a pop-up window.
batch_xs, batch_ys = mnist.test.next_batch(2)
gen_image(batch_xs[0]).show()
gen_image(batch_xs[1]).show()
The definition of mnist is at: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/datasets/mnist.py
mnist 的定义在:https: //github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/datasets/mnist.py
The tensorflow neural network that led me to the need to display the MNINST images is at: https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/examples/tutorials/mnist/mnist_deep.py
导致我需要显示 MNINST 图像的 tensorflow 神经网络位于:https: //github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/examples/tutorials/mnist/mnist_deep.py
Since I have only been programming Python for two hours, I might have made some newby errors. Please feel free to correct.
由于我只编写了两个小时的 Python 程序,我可能会犯一些新的错误。请随时纠正。
回答by WhatAMesh
For those of you who want to do it with PIL.Image:
对于那些想用 PIL.Image 做的人:
import numpy as np
import PIL.Image as pil
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('mnist')
testImage = (np.array(mnist.test.images[0], dtype='float')).reshape(28,28)
img = pil.fromarray(np.uint8(testImage * 255) , 'L')
img.show()