tensorflow
keras
image processing
machine learning
filenames

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.

python
1import tensorflow as tf
2
3batch_size = 32
4img_size = (224, 224)
5
6dataset = tf.keras.preprocessing.image_dataset_from_directory(
7    "images",
8    labels=None,
9    image_size=img_size,
10    batch_size=batch_size,
11    shuffle=False,
12)

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.

python
1file_paths = dataset.file_paths
2predictions = model.predict(dataset)
3
4for path, pred in zip(file_paths, predictions):
5    print(path, pred)

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.

python
1dataset = tf.keras.preprocessing.image_dataset_from_directory(
2    "images",
3    labels="inferred",
4    image_size=(224, 224),
5    batch_size=32,
6    shuffle=False,
7)
8
9file_paths = dataset.file_paths
10predictions = model.predict(dataset)

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.

python
1paths = tf.constant(sorted(file_paths))
2path_ds = tf.data.Dataset.from_tensor_slices(paths)
3
4
5def load_image(path):
6    image = tf.io.read_file(path)
7    image = tf.image.decode_jpeg(image, channels=3)
8    image = tf.image.resize(image, [224, 224])
9    return image, path
10
11predict_ds = path_ds.map(load_image).batch(32)

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=True and 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=False when creating the dataset if you want filenames to align with predictions.
  • dataset.file_paths is usually the easiest way to retrieve the ordered filenames.
  • For more control, build a custom tf.data pipeline that carries paths explicitly.
  • This is fundamentally an order-alignment problem, not only a filename-access problem.

Course illustration
Course illustration

All Rights Reserved.