liveBook · Manning

Image_dataset_from_directory

As explained in Deep Learning with Python, Third Edition

A Keras utility function that builds a dataset from image files organized in class-specific subdirectories.

image_dataset_from_directory turns an image-folder layout into a ready-to-use dataset. It reads files, infers class labels from subdirectories, decodes JPEG images, resizes them, shuffles them, and returns batches of tensors. section 8.2.4

Create training, validation, and test datasets

Organize images under separate train, validation, and test directories, with one subdirectory per class. Then call the utility for each split, choosing a shared image size and batch size. The resulting datasets yield image batches shaped like (32, 180, 180, 3) and label batches shaped like (32, ready for model.fit(). section 8.2.4

Listing 8.9 Using image_dataset_from_directory to read images from directories
from keras.utils import image_dataset_from_directory

batch_size = 64
image_size = (180, 180)
train_dataset = image_dataset_from_directory(
    new_base_dir / "train", image_size=image_size, batch_size=batch_size
)
validation_dataset = image_dataset_from_directory(
    new_base_dir / "validation", image_size=image_size, batch_size=batch_size
)
test_dataset = image_dataset_from_directory(
    new_base_dir / "test", image_size=image_size, batch_size=batch_size
)

Use the complete listing to configure matching datasets for training, validation, and test images. section 8.2.4

Keep these practical rules in mind:

  • Use class-specific subdirectories when labels should be inferred automatically. section 8.2.4
  • Set image_size to produce a shared spatial shape and batch_size to control the number of images yielded together. section 8.2.4
  • Set labels=None when loading unlabeled images, such as images used for generation rather than classification. section 17.2.1
  • Use crop_to_aspect_ratio when fixed-size images should avoid distortion; rebatch with drop_remainder when uniform batch sizes are required. section 17.2.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains directory indexing, decoding, resizing, shuffling, batching, and Dataset iteration without detailing the utility’s implementation.
Examples
Uses class-split image directories, 180×180 classification batches, and 128×128 aspect-preserving Oxford Flowers batches.
What it assumes
Assumes image files are organized on disk and introduces the preprocessing steps needed before model input.
What you can do after
Apply it via Keras to create batched image datasets and pass them directly to model.fit() or model.evaluate().
How it connects
Connects Keras preprocessing with tf.data.Dataset, model.fit(), image classification, and image generation.

Also explained in

Compare how 2 books explain image_dataset_from_directory →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.