Keras Image data generator throwing no files found error?
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
The ImageDataGenerator "No files found" error almost always means the generator could not match your directory structure, filenames, or extension rules. The model itself is usually fine. The quickest way to fix it is to verify the path, confirm the class-folder layout, and test the generator against a tiny known-good dataset before training.
What flow_from_directory Expects
flow_from_directory is opinionated about layout. It expects a root directory containing one subdirectory per class.
Example:
If you point the generator at a folder full of images with no class subfolders, it can report zero files.
Minimal Working Example
Start with the smallest possible working setup.
If samples is zero, stop there and inspect the filesystem before touching the model.
Most Common Causes
The usual reasons are:
- wrong root path
- missing class subfolders
- unsupported file extensions
- empty folders
- typo in relative working directory
Printing a quick directory listing is often enough to catch the mistake.
This tells you what the Python process actually sees, which is more useful than what the IDE sidebar suggests.
Check File Extensions and Hidden Files
flow_from_directory looks for image files with recognized extensions. If your files are unusual formats, uppercase-only extensions in a pipeline that renamed poorly, or non-image placeholders, they may be ignored.
A quick inspection helps:
Also make sure the files are real images, not text files with image-like names.
Relative Paths vs Working Directory
Many "No files found" errors happen because the notebook or script is running from a different directory than expected.
If dataset is not relative to the current working directory, use an absolute path or construct the path relative to the script location.
Use a Tiny Sanity Dataset
Before debugging a large training tree, create two class folders with one image each and test the generator there. If the small dataset works, the problem is in the real dataset layout, not in Keras itself.
This is much faster than debugging a full production folder tree blind.
Check Class Discovery Explicitly
When the generator does find files, it also derives class names from subfolder names. Printing the discovered mapping is a fast sanity check:
If the mapping is empty or missing an expected class, the directory tree is still not shaped the way Keras expects.
Prefer image_dataset_from_directory in Newer Pipelines
For newer TensorFlow workflows, many teams prefer tf.keras.utils.image_dataset_from_directory because it integrates more naturally with tf.data. Even if you stay with ImageDataGenerator, that newer API is a useful diagnostic comparison when folder structure is in doubt.
Common Pitfalls
- Pointing the generator at a folder that contains images but no per-class subfolders.
- Running the script from a different working directory than expected.
- Assuming files are valid images without checking their extensions and contents.
- Debugging model code before printing
train_flow.samples. - Testing only the full dataset instead of a tiny known-good directory tree.
Summary
- "No files found" is usually a filesystem or layout problem, not a model problem.
- '
flow_from_directoryexpects one subfolder per class under the root directory.' - Check path resolution, folder contents, and file extensions before training.
- Print generator sample counts immediately after construction.
- Use a tiny sanity dataset to isolate directory issues quickly.
Related reading
- Keras Image data generator throwing no files found error?
- Keras image_dataset_from_directory not finding images
- Keras Image Preprocessing
- Keras Image segmentation using grayscale masks and ImageDataGenerator class
- Keras ImageDataGenerator Fit causes memory leak
- Keras ImageDataGenerator flow directory with 3D CNN data format error?
- Keras LSTM neural net TypeError LSTM missing 1 required positional argument 'Y
- Keras Making a neural network to find a number's modulus
.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.