Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Module

keras.utils.image_dataset_from_directory

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.

How it works

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.

Syntax

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,
)

Parameters

NameTypeDescription
directorystrRoot 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_modestr | NoneLabel encoding: "int", "binary", "categorical", or None.
class_nameslist[str] | NoneOptional explicit class order.
color_modestrImage color representation, such as "rgb", "rgba", or "grayscale".
batch_sizeint | NoneNumber of samples per batch; None disables batching.
image_sizetuple[int, int]Output image dimensions as height and width.
shuffleboolWhether to randomize file order.
seedint | NoneSeed used for deterministic shuffling and splitting.
validation_splitfloat | NoneFraction reserved for validation.
subsetstr | NoneWhich split to return.
interpolationstrImage resizing interpolation method.

Returns

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.

Key points

  • Use it to turn an on-disk image collection into a model-ready dataset.
  • The loader recognizes JPEG, PNG, BMP, and GIF files, including both common JPEG extensions.
  • With labels="inferred", immediate subdirectory names define classes and are mapped to integer IDs.
  • Each returned batch contains 32 samples unless batch_size is changed.
  • Choose the Grain format when a TensorFlow-free iterable dataset is preferred.
  • The default output integrates with TensorFlow data pipelines.

Common mistakes

  • When supplying labels manually, align them with the alphanumeric ordering of paths discovered under the directory.
  • Animated GIF inputs do not preserve animation; only their first frame is loaded.
  • Set batch_size to None when the model or input pipeline must receive individual samples instead of batches.

Example: Load training and validation images

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.

When to use it

Build an image-classification input pipeline

Organized class folders

Create training and validation datasets

One directory with a reserved fraction

Load images for generation or inference

Images without targets

Produce a Grain dataset

TensorFlow-independent input iteration

Interview questions

QHow are labels inferred?

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.

QHow do you make a reproducible train/validation split?

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.

QWhat datasets can the function return?

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_directory
  • keras.utils.load_img
  • keras.utils.img_to_array
  • keras.utils.save_img
  • keras.utils.array_to_img

Source: img_to_array function | Keras. Checked 2026-10-15.