Keras images with no subfolders
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Keras does not require image subfolders in every situation. What matters is how labels are obtained. If you want labels inferred from the directory structure, subfolders are required. If labels come from somewhere else, or if you are doing inference only, a flat directory can work perfectly well.
Why Subfolders Matter in Some APIs
Classic helpers such as flow_from_directory() and modern helpers such as image_dataset_from_directory(..., labels="inferred") treat subfolder names as class labels. That means a layout like this is expected:
If all files live directly in one folder, there is no class information to infer from the path. So the problem is not that Keras dislikes flat folders. The problem is that folder-based label inference has nothing to infer.
Flat Directory for Inference Only
If you just want predictions and do not need labels, a flat folder is straightforward. In modern TensorFlow, you can load unlabeled data by setting labels=None.
In this mode, the directory structure is ignored. Keras just walks the files and yields image batches.
Flat Directory with Explicit Labels
If you do have labels, you must supply them explicitly instead of asking Keras to infer them from folders. One clean option is a table of filenames and labels.
This works with a flat folder because the labels come from the dataframe, not from subfolder names.
Modern Alternative: tf.data
For new TensorFlow code, tf.data often gives the most control and scales better than older generator APIs.
This is usually the best route when labels come from CSV files, databases, or filename parsing logic.
Labels from Filenames
If the class is encoded in the filename, build the label table before training.
Once you have paths and labels, you can feed them into tf.data, a dataframe-backed generator, or your own custom loader.
When image_dataset_from_directory Can Still Help
The modern image_dataset_from_directory utility is more flexible than many people realize. If you set labels=None, it works for unlabeled prediction data in a flat folder. If you want labeled supervised training from a flat folder, though, you should usually build a tf.data pipeline or use explicit metadata rather than forcing the directory API to behave like a database.
That distinction is the core answer:
- No subfolders is fine for unlabeled inference
- No subfolders is also fine for training if you provide labels yourself
- No subfolders is not fine for automatic class inference from folder names
Common Pitfalls
One common mistake is calling flow_from_directory() on a flat directory and expecting class labels to be discovered magically. Folder-based generators cannot infer labels that are not encoded in the folder tree.
Another issue is using a flat folder with labels stored elsewhere but never joining the two sources explicitly. Keras needs a clear mapping from each file to its target label.
Developers also sometimes load the entire dataset into RAM just because the folder structure is inconvenient. Streaming with tf.data is usually cleaner and more memory-efficient.
Finally, when labels come from filenames, validate the naming rules. A small inconsistency in filenames can silently poison the training set.
Summary
- Keras does not require image subfolders in every workflow.
- Subfolders are required only when labels are inferred from the directory structure.
- For unlabeled inference, a flat directory works with loaders such as
image_dataset_from_directory(..., labels=None). - For supervised training from a flat directory, provide labels explicitly through metadata or a
tf.datapipeline. - Keep the file-to-label mapping explicit so the training pipeline stays reproducible and debuggable.
Related reading
- keras implementation of Levenberg-Marquardt optimization algorithm as a custom optimizer
- Keras inconsistent prediction time
- Keras initialize large embeddings layer with pretrained embeddings
- Keras input_shape for conv2d and manually loaded images
- Keras Lambda layer has no output tensor shape, error when compiling model
- Keras load_model with custom objects doesn't work properly
- Kinect pattern recognition
- Large Scale Image Classifier
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.