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

Image Augmentation

~9 min · data

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

Free data, generated on the fly

Augmentation is the cheapest way to make a small dataset bigger: instead of collecting more photos, you show the model a slightly different version of each photo every epoch — flipped, rotated, zoomed, recolored. The model never sees the exact same input twice, so it can't just memorize the training set. It's forced to learn the invariances you actually care about (a cat is still a cat upside-down-ish, lighter, or off-center).

On during training, off at inference

The key behavior to internalize: Random* layers only fire when training=True. During fit() they inject randomness; during predict() and evaluate() they pass the input straight through untouched. That's exactly what you want — you augment to make learning harder, not to scramble the images you're scoring. Because they're plain layers, you can stack them in their own Sequential block and drop that block into any model.

Tune the strength to your data

There's no universal setting. Start mild (rotation 0.05, zoom 0.05), watch validation accuracy climb, then push harder. The failure mode is over-augmentation: if the transforms are so aggressive that even your training accuracy stalls, you've made the task harder than the model can learn — back off. Match the augmentation to reality too: RandomFlip("horizontal") is great for cats, wrong for handwritten digits (a flipped 2 isn't a 2).

Code

A stack of built-in augmentation layers·python
# Built-in Keras augmentation layers
data_augmentation = keras.Sequential([
    layers.RandomFlip("horizontal"),
    layers.RandomRotation(0.1),        # ±10% of full rotation
    layers.RandomZoom(0.1),            # ±10% zoom
    layers.RandomContrast(0.1),        # ±10% contrast
])

# Applied only during training (training=True)
# At inference time, augmentation is a no-op

External links

Exercise

Train CIFAR-10 with three augmentation levels: none, mild (flip + rotation 0.1), heavy (flip + rotation 0.3 + zoom 0.2 + contrast 0.3). Plot training and validation curves on one chart. Identify the sweet spot.

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.