TensorFlow - 显示来自 MNIST 数据集的图像

新手上路,请多包涵

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

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

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

原文由 JonyK 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 566
1 个回答

阅读本教程后,您可以在 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")

您还可以使用 PIL 或您喜欢的任何可视化工具。

原文由 jeandut 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题