Keras image_dataset_from_directory找不到图像

问题描述

我想从我有大约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)

返回:

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.

我可以看到图像: Colab view of the folder structure with the images

问题出在哪里?我需要使用此功能来批量加载数据,因为我的数据集很大

解决方法

我找到了答案,因此我正在发布,以免对某人有所帮助。

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

directory = '/content/drive/My Drive/Colab Notebooks/Hair'

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

,

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