Answer a question

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)

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

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

enter image description here

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.

Answers

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.

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐