您正在将一个float数组( 如docs中所述 )投射到uint8
,如果它们不是1.0
,则将其截断为0。您应该将它们取整或将它们用作浮点数或乘以255。
我不确定,为什么您看不到白色背景,但是无论如何,我建议您使用定义良好的灰度。
0
您正在将一个float数组( 如docs中所述 )投射到uint8
,如果它们不是1.0
,则将其截断为0。您应该将它们取整或将它们用作浮点数或乘以255。
我不确定,为什么您看不到白色背景,但是无论如何,我建议您使用定义良好的灰度。
0
对于那些想要使用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()
0
这是使用matplotlib显示图像的完整代码
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()
0
以下代码显示了从MNIST数字数据库显示的用于训练神经网络的示例图像。它使用来自堆栈流的各种代码,并避免了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()
mnist的定义位于: https : //github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/datasets/mnist.py
导致我需要显示MNINST图像的Tensorflow神经网络位于: https : //github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/examples/tutorials/mnist/mnist_deep.py
由于我仅对Python进行了两个小时的编程,因此我可能会犯一些newby错误。请随时纠正。
0
我正在使用tensorflow导入一些MNIST输入数据。我遵循了本教程... https://www.tensorflow.org/get_started/mnist/beginners
我正这样导入它们...
我希望能够显示训练集中的任何图像。我知道图像的位置是
mnist.train.images
,所以我尝试访问第一个图像并像这样显示它...我尝试将图像转换为28 x 28 numpy数组,因为我知道每个图像都是28 x 28像素。
但是,当我运行代码时,得到的只是以下内容...
显然我做错了。当我打印出矩阵时,一切看起来都不错,但是我认为我在错误地重塑它。