【问题标题】:Display MNIST image using matplotlib [duplicate]使用 matplotlib 显示 MNIST 图像
【发布时间】:2017-07-10 06:29:15
【问题描述】:

我正在使用 tensorflow 导入一些 MNIST 输入数据。我按照这个教程...https://www.tensorflow.org/get_started/mnist/beginners

我正在导入它们......

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

我希望能够显示训练集中的任何图像。我知道图片的位置是mnist.train.images,所以我尝试访问第一张图片并像这样显示它......

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')

我尝试将图像转换为 28 x 28 numpy 数组,因为我知道每个图像都是 28 x 28 像素。

但是,当我运行代码时,我得到的只是以下内容......

显然我做错了什么。当我打印出矩阵时,一切看起来都不错,但我认为我错误地重塑了它。

【问题讨论】:

    标签: python numpy matplotlib tensorflow mnist


    【解决方案1】:

    这是使用 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()
    

    【讨论】:

    • 这对我有用,谢谢.. 请编辑以包含几个导入行,其中定义了 npmnistplt 等,以便搜索快速答案的人可以快速复制和逐字粘贴您的内容。谢谢
    • 这也适用于使用 PyTorch 导入 MNIST 数据时。
    【解决方案2】:

    对于那些想用 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()
    

    【讨论】:

      【解决方案3】:

      以下代码显示了从用于训练神经网络的 MNIST 数字数据库中显示的示例图像。它使用了来自 stackflow 的各种代码,避免了 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()
      

      mnist的定义在:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/datasets/mnist.py

      导致我需要显示 MNINST 图像的张量流神经网络位于:https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/examples/tutorials/mnist/mnist_deep.py

      由于我只编写了两个小时的 Python 程序,我可能犯了一些新错误。欢迎指正。

      【讨论】:

        【解决方案4】:

        您将浮点数组 (as described in the docs) 转换为 uint8,如果它们不是 1.0,则将它们截断为 0。您应该将它们舍入或将它们用作浮点数或乘以 255。

        我不确定,为什么你看不到白色背景,但我还是建议使用定义明确的灰度。

        【讨论】:

          猜你喜欢
          • 2018-09-22
          • 2018-02-26
          • 2020-08-22
          • 1970-01-01
          • 2013-07-27
          • 1970-01-01
          • 2022-11-16
          • 2017-08-30
          • 2016-06-06
          相关资源
          最近更新 更多