How to obtain filenames during prediction while using tf.keras.preprocessing.image_dataset_from_directory?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you use image_dataset_from_directory for prediction, the tricky part is not generating predictions. It is keeping each prediction aligned with the correct file path. The most important rule is that the dataset order must be stable, otherwise the filenames and prediction outputs stop matching.
Use shuffle=False for Predictable Ordering
If you plan to pair predictions with filenames, create the dataset without shuffling.
With shuffle=False, the dataset yields images in a deterministic order based on the directory walk used by the loader. That makes it possible to match the prediction array back to the file paths.
Use the Dataset File Paths
Datasets created by this utility expose the source file list through file_paths.
This is often the simplest solution. The prediction outputs follow dataset iteration order, and file_paths follows the same original order when shuffling is disabled.
Why Order Breaks So Easily
If you create the dataset with shuffle=True, or you later apply transformations that change ordering semantics, you can no longer assume that zip(file_paths, predictions) is correct. That is the central mistake in many prediction pipelines.
The filename problem is really an ordering problem.
When You Need Labels and Filenames Together
If you are evaluating predictions as well as recording filenames, you may want both labels and file paths. One practical pattern is to keep the dataset for images and labels, while separately storing the ordered file_paths list from the same dataset object.
As long as order is stable, the indices still line up.
Alternative: Build a Custom tf.data Pipeline
If you need even tighter control, build the dataset from file paths manually. That lets you carry filenames as part of each dataset element instead of managing them outside the pipeline.
This is more flexible when the filename itself is part of the downstream logic.
Verify Alignment with a Small Batch First
Before running a large prediction job, it is worth checking one or two batches manually. Print the first few file paths, inspect the corresponding images, and verify that the model outputs line up with what you expect. That quick sanity check catches ordering mistakes early, before an entire prediction export becomes mislabeled.
Common Pitfalls
- Leaving
shuffle=Trueand expecting filenames to line up with predictions. - Assuming the problem is filename access when the real issue is unstable dataset order.
- Losing alignment after additional transformations that change batching or ordering behavior.
- Mixing labeled and unlabeled prediction pipelines without checking index consistency.
- Rebuilding the path list independently instead of using the same ordered source as the dataset.
Summary
- The key to obtaining filenames during prediction is stable ordering.
- Use
shuffle=Falsewhen creating the dataset if you want filenames to align with predictions. dataset.file_pathsis usually the easiest way to retrieve the ordered filenames.- For more control, build a custom
tf.datapipeline that carries paths explicitly. - This is fundamentally an order-alignment problem, not only a filename-access problem.

