1

画像を読み込むときに、pyplot で印刷して正しく読み込まれていることを確認しようとしていますが、問題が発生しています。これらの画像を Tensorflow にロードし、pyplot でimshow()(または他の方法で) チェックするにはどうすればよいですか?

画像データは1チャンネル(白黒)のjpegです。最初は不明な shape と uint8 dtype を持つ Tensor としてロードされます。Tensor を正しい形状に再形成し、float32 にキャストすることを確認しました。また、値が 0.0 から 1.0 にスケーリングされていることを確認し、関数内でグレーの cmapping を使用しようとしましたimshow()

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")
4

1 に答える 1