TensorFlow - Show image from MNIST DataSet

自闭症网瘾萝莉.ら 提交于 2019-12-17 16:16:18

问题


I'm trying to learn TensorFlow and I implemented the MNIST example from the the following link: http://openmachin.es/blog/tensorflow-mnist I want to be able to actually view the training/test images. So I'm trying to add code that will show the first train picture of the first batch:

x_i = batch_xs[0]
image = tf.reshape(x_i,[28,28])

Now, because the Data is in float32 type (with values in [0,1] range), I tried to convert it to uint16 and then to encode it to png in order to show the image. I tried using tf.image.convert_image_dtype and tf.image.encode_png, but with no success. Can you guys please help me understand how can I convert the raw Data to an image and show the image?


回答1:


After reading the tutorial you can do it all in numpy no need for TF:

import matplotlib.pyplot as plt
first_array=batch_xs[0]
#Not sure you even have to do that if you just want to visualize it
#first_array=255*first_array
#first_array=first_array.astype("uint8")
plt.imshow(first_array)
#Actually displaying the plot if you are not in interactive mode
plt.show()
#Saving plot
plt.savefig("fig.png")

You can also use PIL or whatever visualization tool you are into.




回答2:


X = X.reshape([28, 28]);
plt.gray()
plt.imshow(X)

this works.




回答3:


On top of the codes in the tutorial MNIST for ML beginners, you can visualize the image in the mnist dataset:

import matplotlib.pyplot as plt
batch = mnist.train.next_batch(1)
plotData = batch[0]
plotData = plotData.reshape(28, 28)
plt.gray() # use this line if you don't want to see it in color
plt.imshow(plotData)
plt.show()

enter image description here




回答4:


Pass a numpy array representing an MNIST image to the function below and it will display a figure using matplotlib.

def displayMNIST(imageAsArray):
    imageAsArray = imageAsArray.reshape(28, 28);
    plt.imshow(imageAsArray, cmap='gray')
    plt.show()



回答5:


In tensorflow 2.0:

import matplotlib.pyplot as plt
import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

plt.imshow(x_train[0], cmap='gray_r')


来源:https://stackoverflow.com/questions/38308378/tensorflow-show-image-from-mnist-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!