【问题标题】:tf.keras.utils.image_dataset_from_directory() can't find images exceptiontf.keras.utils.image_dataset_from_directory() 找不到图像异常
【发布时间】:2022-01-15 03:20:51
【问题描述】:

我有以下代码可以从目录中读取 jpeg 图像:

data_dir_str = "./photo/pults/samsung_small"
data_dir = pathlib.Path(data_dir_str)
image_count = len(list(data_dir.glob('*.*')))
print('images in directory: ' + str(image_count)) # 12 files in directory

sams_pults = list(data_dir.glob('*.jpg'))
img = PIL.Image.open(str(sams_pults[0]))
img_width, img_height = img.size
#img.show()  # This works well - shows image

batch_size = 10

train_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir_str, #data_dir,
  validation_split=0.2,
  subset="training",
  #seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size
)

即使img.show() 可以显示图像,tf.keras.utils.image_dataset_from_directory() 也会给我错误:

在目录 {directory} 中找不到图像。允许的格式: {ALLOWLIST_FORMATS}

【问题讨论】:

  • 所有文件名看起来像 'photo_0.jpg', 'photo_1.jpg', ...
  • 我也尝试将所有选项设置为“无”:batch_size = 10 train_ds = tf.keras.utils.image_dataset_from_directory( data_dir, validation_split=None, #0.8, subset=None, #"training", seed=None, image_size=(img_height, img_width), batch_size=batch_size )

标签: python tensorflow keras


【解决方案1】:

我找到了解决办法:

./photo/pults/samsung_small -> ./photo/pults

路径不需要设置为放置图像的目录,而是上一层。因为目录名是用来分类的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2021-09-30
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多