Keras
Image Data Generator
Python
Deep Learning
Error Handling

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.

Practice ML system design

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:

text
1dataset/
2  cats/
3    cat1.jpg
4    cat2.jpg
5  dogs/
6    dog1.jpg
7    dog2.jpg

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.

python
1from tensorflow.keras.preprocessing.image import ImageDataGenerator
2
3train_dir = "dataset"
4
5gen = ImageDataGenerator(rescale=1.0 / 255)
6train_flow = gen.flow_from_directory(
7    train_dir,
8    target_size=(224, 224),
9    batch_size=16,
10    class_mode="binary"
11)
12
13print(train_flow.samples)
14print(train_flow.class_indices)

If samples is zero, stop there and inspect the filesystem before touching the model.

Most Common Causes

The usual reasons are:

  1. wrong root path
  2. missing class subfolders
  3. unsupported file extensions
  4. empty folders
  5. typo in relative working directory

Printing a quick directory listing is often enough to catch the mistake.

python
1from pathlib import Path
2
3root = Path("dataset")
4print(root.resolve())
5for p in root.iterdir():
6    print(p, p.is_dir())

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:

python
1from pathlib import Path
2
3for image in Path("dataset/cats").glob("*"):
4    print(image.name, image.suffix.lower())

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.

python
import os

print(os.getcwd())

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:

python
print(train_flow.class_indices)

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_directory expects 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.