【问题标题】:TensorFlow - Show image from MNIST DataSetTensorFlow - 显示来自 MNIST 数据集的图像
【发布时间】:2016-11-13 11:38:56
【问题描述】:

我正在尝试学习 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,但没有成功。 你们能帮我理解如何将原始数据转换为图像并显示图像吗?

【问题讨论】:

标签: python image tensorflow mnist


【解决方案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 或任何您喜欢的可视化工具。

【讨论】:

  • 我想通过 TensorFlow 以某种 windows 可读格式保存图像,然后在 windows 图像查看器中打开图像。我该怎么做?
  • 你只需要在我写的行之后使用plt.savefig 来保存绘图,但你原来的问题只是为了展示。
  • 也许你对matplotlib不熟悉我会修改我的答案,让它更适合你的需求。
  • 我没有可用的 matplotlib,这就是为什么我想将图像(使用 TensorFlow)转换为以后可以在 Windows 中打开的格式。这可能吗?
  • 在使用 Python 进行科学用途时没有安装 matplotlib/numpy 对我来说有点像在没有球拍的情况下打网球,当然你可以设法将球弹回一两次,但你很快就会累这个的。如果您仍然坚持,您可以随时将tf.image_summary 与 Tensorboard 一起用于可视化。
【解决方案2】:
X = X.reshape([28, 28]);
plt.gray()
plt.imshow(X)

这行得通。

【讨论】:

    【解决方案3】:

    在 MNIST for ML 初学者教程中的代码之上,您可以可视化 mnist 数据集中的图像:

    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】:

      将表示 MNIST 图像的 numpy 数组传递给下面的函数,它将使用 matplotlib 显示一个图形。

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

      【讨论】:

      • 除非您解释您的答案以及它如何成为该功能的更好版本,否则它可能会被否决或删除。你必须提供一点上下文。欢迎来到堆栈溢出。感谢您回答问题。
      • 虽然这可能会回答作者的问题,但它缺少一些解释性文字和/或文档链接。如果没有围绕它们的一些短语,原始代码 sn-ps 并不是很有帮助。您可能还会发现how to write a good answer 非常有帮助。请编辑您的答案 - From Review
      【解决方案5】:

      在张量流 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')
      

      【讨论】:

        【解决方案6】:

        我们可以使用subplotsmatplotlib.pyplot

         # plotting the first 9 images in the train set of MNIST
         fig , axs = plt.subplots(3, 3)
         cnt = 0
         for i in range(3):
             for j in range(3):
                 axs[i, j].imshow(X_train[cnt])
                 cnt += 1
        

        【讨论】:

          猜你喜欢
          • 2021-03-10
          • 1970-01-01
          • 2016-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-06
          • 2017-11-02
          • 2021-08-14
          相关资源
          最近更新 更多