【问题标题】:Loading images with image_dataset_from_directory() from keras从 keras 使用 image_dataset_from_directory() 加载图像
【发布时间】:2021-04-23 04:33:00
【问题描述】:

图片文件夹结构如下:

train/
...batch0/
......file00.jpg
......file01.jpg
...batch1/
......file10.jpg
......file11.jpg

目录名称batch0batch1 不是标签,标签位于单独的文件中。问题是将这些图像加载到数据集中。函数image_dataset_from_directory('/batch0')image_dataset_from_directory('/batch1') 不起作用。

错误:

ValueError: Expected the lengths of labels to match the number of files in the target directory. len(labels) is 2 while we found 0 files in ../train/batch0/. 

【问题讨论】:

  • 为什么要在文件夹本身中将图像分成批次?它抛出的错误是什么?
  • 我有两种不成功的情况,一种成功的情况。 1)image_dataset_from_directory('../train/batch0') Found 0 files belonging to 0 classes. 2)labels_list=[0, 1] image_dataset_from_directory(train_dir, labels=labels_list) ValueError: Expected the lengths of labels to match the number of files in the target directory. len(labels) is 2 while we found 0 files in ../train/batch0/. 3)成功情况:image_dataset_from_directory('../train/') Found 4 files belonging to 2 classes.
  • 啊,你需要使用相对路径,就像在成功的情况下一样,但问题是image_dataset_from_directory 假定目录是不同的类而不是批次。
  • image_dataset_from_directory有模式,可以通过参数labels开启/关闭。标签:“推断”(标签是从目录结构生成的),或者与目录中找到的图像文件数量相同大小的整数标签列表/元组。

标签: tensorflow keras tensorflow-datasets


【解决方案1】:

看起来这应该在 tf-nightly 版本中修复。 错误信息已更新为,

如果您希望获得仅包含图像(无标签)的数据集, 通过labels_mode=None

不是labels=None,而是通过label_mode=None

train/
...batch0/
......file00.jpg
......file01.jpg
...batch1/
......file10.jpg
......file11.jpg

import pathlib
data_dir = pathlib.Path('/content/train/')

import tensorflow as tf
batch_size = 16
img_height = 180
img_width = 180
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  label_mode=None,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

输出

Found 4 files belonging to 2 classes.
Using 3 files for training.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多