【问题标题】:Why am I getting TypeError "Image data cannot be converted to float"?为什么我收到 TypeError“图像数据无法转换为浮点数”?
【发布时间】:2019-03-24 07:34:40
【问题描述】:

在加载图像时,我试图通过在 pyplot 中打印它们来确保它们被正确加载,但我遇到了问题。如何将这些图像加载到 Tensorflow 并使用 pyplot 的 imshow()(或其他方式)检查它们?

图像数据是单通道(黑白)jpeg。它最初加载为具有未知形状和 uint8 dtype 的张量。我已经尝试确保将张量重塑为正确的形状并转换为 float32。我还尝试确保将值从 0.0 - 1.0 缩放为浮点数,并在 imshow() 函数中使用 Gray cmapping。

import tensorflow as tf
import matplotlib.pyplot as plt

def load_and_preprocess_jpeg(imagepath):
    img = tf.read_file(imagepath)
    img_tensor = tf.image.decode_jpeg(img)
    img_tensor.set_shape([792,1224,1])
    img_tensor = tf.reshape(img_tensor, [792,1224])
    img_tensor = tf.cast(img_tensor, tf.float32, name='ImageCast')
    #img_tensor /= 255.0 #Tried with and without
    return img_tensor

def read_data(all_filenames):
    path_Dataset = tf.data.Dataset.from_tensor_slices(all_filenames)
    image_Dataset = path_Dataset.map(load_and_preprocess_jpeg)
    plt.figure(figsize=(8,8))
    temp_DS = image_Dataset.take(4)
    itera = temp_DS.make_one_shot_iterator()
    for n in range(4):
        image = itera.get_next()
        plt.subplot(2,2,n+1)
        plt.imshow(image)
        plt.grid(False)
        plt.xticks([])
        plt.yticks([])

我的堆栈跟踪:

File "<stdin>", line 1, in <module>
line 34, in read_data
  plt.imshow(image)
matplotlib\pyplot.py, line 3205, in imshow
  **kwargs)
matplotlib\__init__.py, line 1855, in inner
  return func(ax, *args, **kwargs)
matplotlib\axes\_axes.py, line 5487, in imshow
  im.set_data(X)
matplotlib\image.py, line 649, in set_data
  raise TypeError("Image data cannot be converted to float")

【问题讨论】:

  • 尝试在load_and_preprocess_jpeg 内的不同位置调用.imshow(),看看哪里出了问题。一定要在return 语句之前立即验证它是否有效。如果是这样,那么在temp_DS 旅程中发生了一些事情。
  • 我认为.imshow() 在这段代码中的任何时候都不起作用。

标签: python-3.x tensorflow matplotlib


【解决方案1】:

您正在尝试绘制张量。为了绘制图像,您必须先运行会话。试试下面的代码:

import tensorflow as tf
import matplotlib.pyplot as plt

def load_and_preprocess_jpeg(imagepath):
    img = tf.read_file(imagepath)
    img_tensor = tf.image.decode_jpeg(img)
    img_tensor = tf.image.resize_images(img_tensor, [img_size,img_size])
    img_tensor = tf.cast(img_tensor, tf.float32, name='ImageCast')
    img_tensor /= 255.0 
    return img_tensor

path_Dataset = tf.data.Dataset.from_tensor_slices(all_filenames)
image_Dataset = path_Dataset.map(load_and_preprocess_jpeg)
temp_DS = image_Dataset.take(4)
itera = temp_DS.make_one_shot_iterator()
image = itera.get_next()

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    while True:
        try:
            image_to_plot = sess.run(image)
            plt.figure(figsize=(8,8))
            plt.subplot(2,2,n+1)
            plt.imshow(image_to_plot)
            plt.grid(False)
            plt.xticks([])
            plt.yticks([])

        except tf.errors.OutOfRangeError:
            break 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2023-03-30
    • 2017-11-12
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多