C.W.K.
Stream
Lesson 04 of 06 · published

Image Loading

~9 min · data

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Folders are your labels

keras.utils.image_dataset_from_directory is the standard on-ramp from a disk of image files to a batched, training-ready dataset. The trick is that it reads your folder layout as the label scheme: one subdirectory per class, images inside. Point it at the parent folder and it infers the class names, loads the images, resizes them to a uniform image_size, batches, and shuffles — all from one call. No manual label arrays, no glob loops.

Split once, with a shared seed

The validation_split + subset + seed trio is how you carve train and validation out of the same folder. The catch most people miss: you must call it twice — once with subset="training", once with subset="validation" — using the same seed both times. The seed is what guarantees the two calls partition the data consistently instead of overlapping. Pick label_mode="categorical" for one-hot targets (pairs with categorical_crossentropy) or "int" for integer labels (pairs with sparse_categorical_crossentropy).

Then make it fast

Loading from disk every epoch is slow. Chain .cache() to hold decoded images after the first pass, and .prefetch(tf.data.AUTOTUNE) to prepare the next batch on the CPU while the GPU chews on the current one. Together they often cut epoch time in half or more. Order matters: cache before any random augmentation (so the cache stores raw images and augmentation still varies each epoch), prefetch last.

Code

Load a labeled image dataset from a folder tree·python
# Folder structure:
# data/
#   cats/
#     cat001.jpg
#     cat002.jpg
#   dogs/
#     dog001.jpg
#     dog002.jpg

train_ds = keras.utils.image_dataset_from_directory(
    "data/",
    image_size=(224, 224),   # Resize all images
    batch_size=32,
    validation_split=0.2,
    subset="training",
    seed=42,
    label_mode="categorical", # or "int" for integer labels
)

External links

Exercise

Create a folder of 100 random images organized by class. Use image_dataset_from_directory to load them. Print one batch's shape. Add cache + prefetch and time one epoch.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.