Keras image_dataset_from_directory not finding images
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
tf.keras.utils.image_dataset_from_directory loads images from a directory tree into a tf.data.Dataset, automatically labeling them by subfolder name. When it reports "Found 0 files," the cause is almost always a wrong directory path, unsupported file extensions, an incorrect directory structure (images sitting directly in the root instead of class subfolders), or file permission issues. Fixing the directory layout and verifying the path resolves the problem in most cases.
Expected Directory Structure
The function expects images organized into class subdirectories:
If images are placed directly in the root folder without subdirectories, the function finds 0 files:
Fix 1: Correct the Directory Path
The most common cause is a wrong or relative path:
Fix 2: Add Class Subdirectories
If all images are in a single folder, move them into class subdirectories:
For binary classification with a single class, use labels='inferred' with the label_mode parameter, or set labels=None for unlabeled data:
Fix 3: Check Supported File Extensions
By default, the function looks for .jpg, .jpeg, .png, .bmp, and .gif files. Other formats like .tiff, .webp, or .svg are silently skipped:
Fix 4: Handle Hidden Files and Corrupted Images
Hidden files (like .DS_Store on macOS) and corrupted images can cause issues:
Fix 5: Verify File Permissions
On Linux/macOS, restrictive permissions prevent the function from reading files:
Debugging Checklist
Common Pitfalls
- Images in root directory without class subfolders: The function requires at least one level of subdirectories for class labels. Place images inside named subfolders, or use
labels=Nonefor unlabeled datasets. - Using the deprecated
tf.keras.preprocessingpath: In TensorFlow 2.9+, usetf.keras.utils.image_dataset_from_directoryinstead oftf.keras.preprocessing.image_dataset_from_directory, which is deprecated. - Relative paths resolve against the wrong directory: Jupyter notebooks and scripts may have different working directories. Use
os.path.abspath()to verify the full path before passing it. - Uppercase file extensions not recognized: Files named
image.JPGorphoto.PNGare supported (the function is case-insensitive), but corrupted extensions like.jpg_or.jpeg2are not. - Symlinks or mounted drives not followed: On some systems, symbolic links or network-mounted directories may not be traversed. Copy the files locally or verify the symlinks resolve correctly.
Summary
image_dataset_from_directoryrequires images in class subdirectories, not directly in the root folder- Use absolute paths or verify
os.getcwd()to ensure the path resolves correctly - Only
.jpg,.jpeg,.png,.bmp, and.gifextensions are supported by default - Remove hidden files (
.DS_Store) and validate images with PIL before loading - Use the debugging checklist to systematically identify why 0 files are found
Related reading
- Keras Image Preprocessing
- Keras Image segmentation using grayscale masks and ImageDataGenerator class
- Keras ImageDataGenerator flow directory with 3D CNN data format error?
- Keras images with no subfolders
- Keras ImageDataGenerator Fit causes memory leak
- Keras ImageDataGenerator for multiple inputs and image based target output
- 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.