简体   繁体   中英

Keras flow_from_directory autoencoder training

I'm trying to train an autoencoder in Keras and I've my own dataset organized as follow:

  • dataset:
    • train:
      • img1.jpg
      • 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. In this case, all the images are in the same folder, without any label. When I execute the code, I got the following error: "Found 0 images belonging to 0 classes."

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. 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. 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

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'. 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...

train_path = 'dataset/train/'

As @Gerry P suggested, I set class_mode=None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM