【问题标题】:Matplotlib plot becomes blank after tf.image.resizeMatplotlib 图在 tf.image.resize 后变为空白
【发布时间】:2021-11-23 06:51:17
【问题描述】:

我有一些用于 tensorflow 数据集的代码。 它以前工作得很好,它可能仍然有效。但我不这么认为

img = parse_image(img_paths[0])
img = tf.image.resize(img, [224, 224])
plt.imshow(img)

只输出一个空白的224x224 画布。

img = parse_image(img_paths[0])
plt.imshow(img)

正确输出图像。

img_paths 是带有路径名的字符串列表

我试过了:

img = parse_image(img_paths[0])
img = tf.image.resize([img], [224, 224])
plt.imshow(img[0])

img = parse_image(img_paths[0])
img = tf.image.resize(img, [224, 224])
plt.imshow(img.numpy())

img = parse_image(img_paths[0])
img = tf.image.resize([img], [224, 224])
plt.imshow(img.numpy()[0])

形状是正确的,这段代码以前也有效。 并且可能仍然有效,我想我可能无法正确使用它了(我写它已经有一段时间了)。

感谢您提供的任何提示或想法?当然还有解决方案;-)

【问题讨论】:

    标签: python tensorflow matplotlib image-processing tensorflow-datasets


    【解决方案1】:

    嗯,

    我在别处看到了一些东西并添加了这一行:

    img = tf.image.convert_image_dtype(img, tf.float32)
    

    在调整大小之前,它起作用了。

    这非常奇怪,因为我以前不需要这条线。可能是因为版本更新?

    无论哪种方式都有效:

    img = parse_image(train_img_paths[0])
    img = tf.image.convert_image_dtype(img, tf.float32)
    img = tf.image.resize(img, [224, 224])
    plt.imshow(img)
    

    【讨论】:

      【解决方案2】:

      “问题”在于 Matplotlib。当您使用 Tensorflow 调整大小时,它会将您的输入变为浮动。 Matplotlib 接受两种图像格式,0-255 之间的整数和 0 到 1 之间的浮点数。如果您在大于 1 的浮点数上调用 plt.imshow(),它将剪切所有值,您将看到一个白色图像。我怀疑这就是你得到的。

      tf.image.convert_image_dtype 有一个saturate 参数,其默认值使得0-255 整数范围变为0-1 浮点数。这就是它“有效”的原因,因为 Matplotlib 理解这种格式。在此之后,Tensorflow 调整大小操作也将其保持在 0-1 之间,所以它可以工作。

      【讨论】:

      • 谢谢,这对我来说很有意义,我之前使用我的代码的数据集具有标准化的像素值。非常感谢!
      • Matplotlib imshow 肯定 not 只接受 0-1 之间的浮点数。它在 vmin 和 vmax 之间进行标准化,其中这些是任何 float64。 matplotlib.org/stable/api/_as_gen/…
      • 根据您的链接,这仅适用于灰度图像,即没有通道尺寸
      猜你喜欢
      • 2022-10-19
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多