How can I explore and modify the created dataset from 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
image_dataset_from_directory gives you a tf.data.Dataset, not a static list of images. That means exploration and modification happen through dataset operations such as take, map, filter, unbatch, batch, cache, and prefetch, rather than by mutating some in-memory collection directly.
Inspect What the Dataset Actually Contains
Start by treating the dataset as a stream of batches. Check its metadata and inspect a batch before changing anything.
element_spec tells you the tensor structure, while class_names tells you how folder names map to label indices.
Visualize Samples Early
A quick visualization often catches label mistakes, broken images, and unexpected resizing.
This is often more informative than inspecting tensor shapes alone.
Modify the Dataset with map
Most transformations belong in map. This is where you normalize images, cast dtypes, change labels, or attach augmentation.
This keeps preprocessing tied directly to the dataset pipeline instead of scattering it around the training loop.
Add Augmentation Carefully
If you want to modify the images for training, wrap augmentation into the dataset pipeline. Keep augmentation only on the training dataset, not validation or test data.
This changes the images produced by the dataset without touching the original files.
Filter or Remap Data
Because the result is a tf.data.Dataset, you can filter classes or remap labels using dataset operators.
Or remap labels:
This is useful when merging classes, dropping classes, or adapting a dataset to a different output structure.
Be Deliberate About Unbatching
unbatch is powerful, but it changes the dataset shape and affects ordering and performance. Use it only when the transformation genuinely needs per-example handling.
After unbatching and modifying, you usually need to batch again before training. That means it is easy to accidentally lose the batching and shuffling behavior you expected if you are not explicit.
Improve Throughput with Cache and Prefetch
Once the dataset pipeline is correct, make it efficient.
For datasets that do not fit comfortably in memory, cache to disk or skip caching altogether. prefetch is usually a good default because it overlaps input work with model execution.
Validation Pipelines Should Differ from Training Pipelines
Create validation data with the same resizing and normalization, but without random augmentation.
Keeping the pipelines aligned except for augmentation prevents evaluation drift.
Common Pitfalls
A common mistake is treating the dataset like a mutable Python list. tf.data.Dataset is transformed functionally, not edited in place item by item.
Another mistake is applying augmentation to validation data, which makes metrics noisy and misleading.
Developers also often unbatch and rebatch without thinking about how that changes shuffle behavior and performance.
Finally, do not skip the initial inspection step. Many training problems come from dataset structure errors that were visible before the first epoch even started.
Summary
- '
image_dataset_from_directoryreturns atf.data.Dataset, so exploration happens by iterating and inspection.' - Use
take,class_names, andelement_specto understand the dataset first. - Use
map,filter,unbatch, andbatchto modify the pipeline. - Keep augmentation only on the training path.
- Add
cacheandprefetchafter correctness so the input pipeline stays efficient.

