Keras image_dataset_from_directory 未找到图像

Llu*_*s C 5 keras tensorflow image-preprocessing tensorflow-datasets

我想从我有大约 5000 张图像的目录中加载数据(类型“png”)。但是它返回一个错误,说当明显有图像时没有图像。这段代码:

width=int(wb-wa)
height=int(hb-ha)
directory = '/content/drive/My Drive/Colab Notebooks/Hair/Images'
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    directory, labels=densitat, label_mode='int',
    color_mode='rgb', batch_size=32, image_size=(width, height), shuffle=True, seed=1,
    validation_split=0.2, subset='training', follow_links = False)
Run Code Online (Sandbox Code Playgroud)

返回:

ValueError: Expected the lengths of `labels` to match the number of files in the target directory. len(labels) is 5588 while we found 0 files in /content/drive/My Drive/Colab Notebooks/Hair/Images.
Run Code Online (Sandbox Code Playgroud)

我可以看到图像: 带有图像的文件夹结构的 Colab 视图

问题出在哪儿?我需要使用这个函数来批量加载数据,因为我有一个大数据集

Llu*_*s C 8

我找到了答案,所以我发帖以防它可能对某人有所帮助。

问题是路径,因为我使用的是带有图像的文件夹的路径,而我应该使用目录(上面的一个文件夹)。

directory = '/content/drive/My Drive/Colab Notebooks/Hair'
Run Code Online (Sandbox Code Playgroud)

请注意,'/Hair' 是包含我的图像的文件夹。


小智 5

如果上面接受的解决方案不能解决您的问题,可能是因为您正在尝试加载带有.tif扩展名的 TIFF 图像。事实证明,唯一允许的格式image_dataset_from_directory('.bmp', '.gif', '.jpeg', '.jpg', '.png')

  • 那么我们如何从目录加载`.tif`文件呢? (2认同)