简体   繁体   English

Keras flow_from_directory 自动编码器训练

[英]Keras flow_from_directory autoencoder training

I'm trying to train an autoencoder in Keras and I've my own dataset organized as follow:我正在尝试在 Keras 中训练自动编码器,并且我自己的数据集组织如下:

  • dataset:数据集:
    • train:火车:
      • img1.jpg img1.jpg
      • etc ETC
    • valid:有效的:
    • test:测试:

I've seen how to use flow_from_directory for a classification task, where the dataset is organized in labels and subdirectories.我已经了解了如何将 flow_from_directory 用于分类任务,其中数据集被组织在标签和子目录中。 In this case, all the images are in the same folder, without any label.在这种情况下,所有图像都在同一个文件夹中,没有任何 label。 When I execute the code, I got the following error: "Found 0 images belonging to 0 classes."执行代码时,出现以下错误:“找到属于 0 个类的 0 个图像。”

This is my code snippet:这是我的代码片段:

train_path = 'dataset/train/'
train_gen = train_data_gen.flow_from_directory(
    train_path,
    class_mode = 'Input',
    target_size = IMAGE_SIZE,
    color_mode = 'grayscale',
    batch_size = BS,
    seed = SEED,
    shuffle = 'Yes'
)

How can I fix it?我该如何解决?

The problem is with the structure of your data.问题在于数据的结构。 When you use flow_from_directory with the directory as train_path = 'dataset/train/' it looks in that directory for subdirectories which would be your classes.当您将 flow_from_directory 与目录作为 train_path = 'dataset/train/' 一起使用时,它会在该目录中查找将是您的类的子目录。 From what you show as your directory structure there are no class sub directories in dataset/train so the generator returns no files and no classes.从您显示的目录结构来看,dataset/train 中没有 class 子目录,因此生成器不返回任何文件和类。 For example assume you are building a classifier to classify dogs and cats.例如,假设您正在构建一个分类器来对狗和猫进行分类。 In you train directory you would have two sub directories one to hold the images of cats and the other to hold images of dogs.在您的训练目录中,您将有两个子目录,一个用于保存猫的图像,另一个用于保存狗的图像。 flow_from_directory has a parameter class_mode, description is provided below flow_from_directory 有一个参数 class_mode,描述如下

class_mode: One of "categorical", "binary", "sparse", "input", or None.
 Default: "categorical". Determines the type of label arrays that are
 returned: - "categorical" will be 2D one-hot encoded labels, - "binary" 
will be 1D binary labels, "sparse" will be 1D integer labels,
 - "input" will be images identical to input images (mainly used to 
work with autoencoders). - If None, no labels are returned 
(the generator will only yield batches of image data, 
which is useful to use with model.predict()). 
Please note that in case of class_mode None,
 the data still needs to reside in a subdirectory of directory for it to work correctly.

so in flow_from_directory set class_mode='input'.所以在 flow_from_directory 中设置 class_mode='input'。 That way you will just get the images without any labels.这样你就可以得到没有任何标签的图像。

I solved the problem;我解决了这个问题; the solution may be useful for someone: I added a subfolder in which all the images are stored.该解决方案可能对某人有用:我添加了一个存储所有图像的子文件夹。 So the dataset is structured in this way:所以数据集的结构是这样的:

dataset:数据集:

  • train:火车:
    • images:图片:
      • img.jpg...图片.jpg...

train_path = 'dataset/train/' train_path = '数据集/训练/'

As @Gerry P suggested, I set class_mode=None正如@Gerry P 建议的那样,我设置了 class_mode=None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras -.flow_from_directory(目录) - Keras - .flow_from_directory(directory) Keras ImageDataGenerator 方法 flow_from_directory - Keras ImageDataGenerator method flow_from_directory 使用Keras的flow_from_directory和FCNN - using Keras' flow_from_directory with FCNN 验证准确性低且训练准确度高-keras imagedatagenerator flow_from_directory类别分类 - Low validation accuracy with good training accuracy - keras imagedatagenerator flow_from_directory categorical classification Keras:Plot 使用 flow_from_directory 训练数据的图像增强样本 - Keras: Plot Image augmentations samples for training data using flow_from_directory Keras:使用文件名而不是目录的flow_from_directory()或flow() - Keras: flow_from_directory() or flow() using filenames instead of directories 使用 flow_from_directory 将图像增强拟合到训练数据 - Fit Image augmentations to training data using flow_from_directory Keras:从flow_from_directory获取图像和标签数组 - Keras: Obtain array of images and labels from flow_from_directory 在 Keras 中为 flow_from_directory 使用多个目录 - Use multiple directories for flow_from_directory in Keras 无法让 keras flow_from_directory 使用 .gif 数据 - Unable to get keras flow_from_directory to work with .gif data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM