TensorFlow - 显示来自MNIST DataSet的图像

Jon*_*nyK 16 python image mnist tensorflow

我正在尝试学习TensorFlow,我从以下链接实现了MNIST示例:http://openmachin.es/blog/tensorflow-mnist 我希望能够实际查看训练/测试图像.所以我正在尝试添加代码,以显示第一批的第一张火车图片:

x_i = batch_xs[0]
image = tf.reshape(x_i,[28,28])
Run Code Online (Sandbox Code Playgroud)

现在,因为Data是float32类型(值在[0,1]范围内),我试图将它转换为uint16,然后将其编码为png以显示图像.我试过用tf.image.convert_image_dtype and tf.image.encode_png,但没有成功.你能帮我理解如何将原始数据转换为图像并显示图像?

jea*_*ean 10

阅读完教程后,您可以在numpy中完成所有操作,不需要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")
Run Code Online (Sandbox Code Playgroud)

您还可以使用PIL或您使用的任何可视化工具.


小智 9

X = X.reshape([28, 28]);
plt.gray()
plt.imshow(X)
Run Code Online (Sandbox Code Playgroud)

这很有效.