Build an image-classification input pipeline
Organized class folders
Keras · Module
Creates a dataset from images stored in a directory, optionally inferring labels from class-specific subdirectories. It decodes, resizes, shuffles, batches, and can split the files for training and validation.
This utility scans a directory for supported image files, optionally infers class labels from subdirectories, decodes and resizes images, and returns batched datasets. It can also split one directory into training and validation subsets, shuffle the files, and emit TensorFlow or Grain datasets.
keras.utils.image_dataset_from_directory(
directory,
labels="inferred",
label_mode="int",
class_names=None,
color_mode="rgb",
batch_size=32,
image_size=(256, 256),
shuffle=True,
seed=None,
validation_split=None,
subset=None,
interpolation="bilinear",
follow_links=False,
crop_to_aspect_ratio=False,
pad_to_aspect_ratio=False,
data_format=None,
format="tf",
verbose=True,
)| Name | Type | Description |
|---|---|---|
directory | str | Root directory containing the image files; inferred labels require class subdirectories. |
labels | "inferred" | None | list/tuple[int] | "inferred", None, or an integer-label list/tuple matching the discovered image files. |
label_mode | str | None | Label encoding: "int", "binary", "categorical", or None. |
class_names | list[str] | None | Optional explicit class order. |
color_mode | str | Image color representation, such as "rgb", "rgba", or "grayscale". |
batch_size | int | None | Number of samples per batch; None disables batching. |
image_size | tuple[int, int] | Output image dimensions as height and width. |
shuffle | bool | Whether to randomize file order. |
seed | int | None | Seed used for deterministic shuffling and splitting. |
validation_split | float | None | Fraction reserved for validation. |
subset | str | None | Which split to return. |
interpolation | str | Image resizing interpolation method. |
By default, a tf.data.Dataset object; with format="grain", a grain.IterDataset object. The dataset yields image batches and labels unless labels are disabled.
import keras
train_ds, val_ds = keras.utils.image_dataset_from_directory(
"PetImages",
validation_split=0.2,
subset="both",
seed=1337,
image_size=(180, 180),
batch_size=32,
)
for images, labels in train_ds.take(1):
print(images.shape, labels.shape)Creates reproducible training and validation datasets from class-organized image folders.
Organized class folders
One directory with a reserved fraction
Images without targets
TensorFlow-independent input iteration
The function scans directory for supported image files and, with labels="inferred", treats each subdirectory as a class. It returns batches containing decoded, resized images and corresponding labels; the default label mode is integer.
Follow-up: How would you create unlabeled inputs? Set label_mode=None, which makes the dataset yield images without labels.
validation_split reserves a fraction of the files for validation, while subset selects the requested portion. Supplying a seed makes the split and shuffling reproducible.
Follow-up: How can both partitions be returned in one call? Use subset="both" together with validation_split and a seed.
The default result is a tf.data.Dataset, with batches of size 32 by default. Setting format="grain" instead returns a grain.IterDataset and removes the TensorFlow dependency.
Follow-up: What happens with batch_size=None? Batching is disabled, so the dataset yields individual samples.
keras.utils.image_dataset_from_directorykeras.utils.load_imgkeras.utils.img_to_arraykeras.utils.save_imgkeras.utils.array_to_img