使用matplotlib显示MNIST图像 [英] Display MNIST image using matplotlib

查看:561
本文介绍了使用matplotlib显示MNIST图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tensorflow导入一些MNIST输入数据.我遵循了本教程... https://www.tensorflow.org/get_started/mnist/beginners

I am using tensorflow to import some MNIST input data. I followed this tutorial...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)

我希望能够显示训练集中的任何图像.我知道图像的位置是mnist.train.images,所以我尝试访问第一个图像并像这样显示它...

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...

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')

我尝试将图像转换为28 x 28 numpy数组,因为我知道每个图像都是28 x 28像素.

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.

但是,当我运行代码时,我得到的只是以下内容...

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.

推荐答案

您正在投射浮点数数组(

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.

我不确定,为什么您看不到白色背景,但是我还是建议您使用定义良好的灰度.

I am not sure, why you don't see the white background, but i would suggest to use a well defined gray scale anyway.

这篇关于使用matplotlib显示MNIST图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆