【问题标题】:Keras image_dataset_from_directory not finding imagesKeras image_dataset_from_directory 找不到图像
【发布时间】:2020-09-15 17:05:57
【问题描述】:

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

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

【问题讨论】:

    标签: tensorflow keras tensorflow-datasets image-preprocessing


    【解决方案1】:

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

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

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

    请注意,“/Hair”是我的图片所在的文件夹。

    【讨论】:

    • 我在使用 .tif 文件时遇到了类似的问题;但是,移动到上面的目录并没有帮助。我使用了parent = ltifs_lib[0].parents[0].parents[0].parents[0].__str__()(其中ltifs_lib[0].glob("**/*.tif")pathlib 给出的第一个tif 的路径)在0 到3 次之间应用.parents[0]。没有帮助。此外,我提供了一个带有标签的列表。
    【解决方案2】:

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

    【讨论】:

    • 那么我们如何从目录中加载.tif文件呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多