Display MNIST image using matplotlib

49,645

Solution 1

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.

Solution 2

Here is the complete code for showing image using 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()

Solution 3

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.

# 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

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

Since I have only been programming Python for two hours, I might have made some newby errors. Please feel free to correct.

Solution 4

For those of you who want to do it with 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()
Share:
49,645
buydadip
Author by

buydadip

$$BANK$$

Updated on May 25, 2020

Comments

  • buydadip
    buydadip almost 4 years

    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.

  • Somo  S.
    Somo S. over 6 years
    this worked for me thanks.. kindly edit to include the few import lines where np, mnist , plt etc are defined so that someone searching for a quick answer can quickly copy and paste what you have verbatim. Thanks
  • Stefan
    Stefan over 3 years
    This also works when the MNIST data is imported using PyTorch.