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

Practical: Image Pipeline with Augmentation

~10 min · data

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

Everything in this track, in one script

This is the capstone: a complete vision pipeline that folds together every piece you've learned in this track. Preprocessing as layers (lesson 1), augmentation that's on for training and off for inference (lesson 2), a backend-agnostic fit() (lesson 3), and a folder-loaded dataset with cache/prefetch (lesson 4) — all collapsed into one model definition plus one fit() call.

Why the preprocessing lives inside the model

Look at where Rescaling and the augmentation block sit: they're the first layers, ahead of any Conv2D. That placement is the whole point. The model now expects raw [0, 255] images as input and does its own normalization internally. There's no separate preprocessing script to keep in sync, and no chance of train/serve skew — the saved .keras file is the preprocessing. Augmentation, being a Random* block, automatically goes quiet at inference, so production sees clean rescaling and nothing else.

Deploy = copy one file

Because the transform travels with the weights, deployment is as boring as it should be: copy the .keras file to the serving box, keras.saving.load_model(), and call predict() on raw images. No preprocessing code ships alongside it, no environment to reproduce, no drift. That's the engineering payoff of treating data as a first-class part of the model rather than a script bolted on the side.

Code

End-to-end: preprocessing + augmentation baked into the model·python
import keras
from keras import layers

# Data augmentation layers (only active during training)
augmentation = keras.Sequential([
    layers.RandomFlip("horizontal"),
    layers.RandomRotation(0.1),
    layers.RandomZoom(0.1),
])

# Model with preprocessing built in
model = keras.Sequential([
    keras.Input(shape=(224, 224, 3)),
    layers.Rescaling(1.0 / 255.0),   # Normalize
    augmentation,                       # Augment
    layers.Conv2D(32, 3, activation="relu"),
    layers.MaxPooling2D(2),
    layers.Conv2D(64, 3, activation="relu"),
    layers.MaxPooling2D(2),
    layers.GlobalAveragePooling2D(),
    layers.Dense(128, activation="relu"),
    layers.Dense(5, activation="softmax"),
])

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
model.fit(train_ds, validation_data=val_ds, epochs=20)

External links

Exercise

Build the full pipeline: folder of dog/cat images → image_dataset_from_directory → cache/prefetch → model with Rescaling + augmentation + small CNN → train → save. Deploy by loading the .keras file and predicting on raw images.

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.