【发布时间】: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