简体   繁体   中英

Keras: flow_from_directory() or flow() using filenames instead of directories

I'm importing images to Keras from a directory. The code looks something like this:

gen = datagen.flow_from_directory (
    DIRECTORY,
    target_size = (IMAGE_WIDTH, IMAGE_HEIGHT),
    batch_size = BATCH_SIZE,
    class_mode = "binary"
)

However, my data is organized not by folders, but by filenames. If I couldn't change this structure, is there any way to flow while separating by a regex (say, ending with ".jpg" vs. ".png", or starting with "numeral_7" vs "numeral_8")?

According to the official documentation , you can add argument "classes". Which is an optional list of class subdirectories (eg ['dogs', 'cats']). Default: None.

If not provided, the list of classes will be automatically inferred from the subdirectory names/structure under directory, where each subdirectory will be treated as a different class (and the order of the classes, which will map to the label indices, will be alphanumeric). The dictionary containing the mapping from class names to class indices can be obtained via the attribute class_indices.

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