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

