为什么我的图像加载到数据集中都是白色的?

Why are my images loaded into a dataset all white?

我使用我的图像创建了一个数据集:

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

dataset = tf.keras.preprocessing.image_dataset_from_directory(
    <directory>,
    label_mode=None,
    seed=1,
    subset='training',
    validation_split=0.1,
    image_size=(900, 900))

images = next(iter(dataset))
print(tf.shape(images))

我得到输出: 找到属于 1 类 的 209 个文件。 使用 189 个文件进行训练。 tf.Tensor([ 32 900 900 3], 形状=(4,), dtype=int32)

现在我想看一张图片:

plt.imshow(images[19])
plt.show()

作为输出我得到: 使用 RGB 数据将输入数据剪切到 imshow 的有效范围([0..1] 用于浮点数或 [0..255] 用于整数)。

还有一个纯白色图像作为输出。

我确定加载到数据集中的图像不是纯白色的。有人可以帮助我吗?

你的dataset是一个tf.data.Dataset所以你可以使用这个可视化功能。 https://www.tensorflow.org/datasets/api_docs/python/tfds/visualization/show_examples

tfds.visualization.show_examples(
    ds: tf.data.Dataset,
    ds_info: tfds.core.DatasetInfo,
    **options_kwargs
)

这样实现的:

import tensorflow_datasets as tfds

ds, info = tfds.load(<your dataset name>, split='train', with_info=True)
fig = tfds.show_examples(ds, info)

不是您问题的直接答案,但坦率地说,我更喜欢使用 ImageDataGenerator.flow_from_directory,它允许您重新缩放图像、增强图像并且生成器的输出易于使用。文档为 here. 对于您的应用程序,代码为:

data_gen=tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255,
    validation_split=.01)
train_gen=data_gen.flow_from_directory(
    directory= r'c:\your_directory',
    target_size=(900,900),
    class_mode="categorical",
    batch_size=32,
    shuffle=True,
    seed=123,    
    subset='training')
valid_gen=data_gen.flow_from_directory(
    directory= r'c:\your_directory',
    target_size=(900,900),
    class_mode="categorical",
    batch_size=32,
    shuffle=False,
    seed=123,    
    subset='validation')
tr_images, tr_labels=next (train_gen) # generate a batch of 32 training images, and labels
val_images, val_labels=next(valid_gen)
print('tr_images.shape = ', tr_images.shape)
# result will be tr_images.shape =  (32, 900, 900, 3)
image1=tr_images[0]
plt.imshow(image1)
plt.show()
# result will be showing the image
# other useful things available
class_dict=train_gen.class_indices # a dictionary where key is the text class name and value is integer label of the class
print (class_dict)
labels= train_gen.labels # a sequential list of all the generator labels
file_names= test_gen.filenames  # a sequential list of all the generator file names
# hope this helps

将它们传输到 numpy 并转换为 uint8。 这是我用来检查输入和输出的函数,我使用的批量大小为 32,但只打印了其中的 8 个。

def generate_images(test_input):
  prediction = decoder(encoder(test_input), training=True)
  plt.figure(figsize=(15, 15))
  for i in range(8):
    plt.subplot(4, 8, i*2+1)
    plt.imshow(test_input[i].numpy().astype("uint8") )
    plt.subplot(4, 8, i*2+2)
    plt.imshow(prediction[i] )
  plt.axis('off')
  plt.show() 

从数据集调用它

for imgs, labels in train_ds.take(1):
  generate_images( imgs)

我已经尝试使用上述方法,但 none 中的方法按我想要的方式工作。您可以做的是将 interpolation 更改为 "nearest" 默认值为 "bilinear":

试试这个:

ds = tf.keras.preprocessing.image_dataset_from_directory(
    directory = directory, # path to images
    ....
    color_mode="rgb",
    interpolation="nearest",
    follow_links=False,
)

然后显示图像:

for image, label in ds:
    pass

plt.imshow(image[0])