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

Data Augmentation Layers

~9 min · keras-cv

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

Augmentation is free regularization

The cheapest way to make a vision model generalize is to show it more variety — not more images, but more versions of the images you have. Flip, rotate, crop, recolor: each transform teaches the model that a cat is still a cat upside-down or in shadow. KerasCV's layers do this on the fly, on the GPU, as part of the input pipeline — so it costs almost nothing and the original dataset never grows on disk.

Where KerasCV pulls ahead of basic flips and rotations is the batch-level augmentations that mix two samples together:

AugmentationDescription
CutMixCuts a random patch from one image and pastes it onto another, mixing labels proportionally
MixUpLinear interpolation between two images and their labels
RandAugmentApplies a random policy of N transforms from a predefined set — no manual tuning
MosaicAugmentationCombines 4 images into a mosaic (used in YOLO training)

Why mixing labels works

MixUp and CutMix do something that feels wrong at first: they produce images that are partly a cat and partly a dog, with a label to match (0.7 cat, 0.3 dog). This forces the model to stop being overconfident — it learns smooth decision boundaries instead of memorizing sharp ones, which is exactly what reduces overfitting. They're standard in SOTA classification recipes for that reason. The catch: they only apply during training, and they're more hyperparameter-sensitive than vanilla augmentation.

Code

Batch-mixing and policy-based augmenters·python
# CutMix and MixUp
augmenter = keras_cv.layers.CutMix(alpha=1.0)
augmenter = keras_cv.layers.MixUp(alpha=0.2)

# RandAugment
augmenter = keras_cv.layers.RandAugment(
    value_range=(0, 255), augmentations_per_image=3
)
Wire augmentation into the tf.data input pipeline·python
# Apply during training only, on the input pipeline
train_ds = train_ds.map(
    lambda x, y: augmenter({"images": x, "labels": y}),
    num_parallel_calls=tf.data.AUTOTUNE,
)
# No augmentation on the validation set — measure the real thing
val_ds = val_ds  # left untouched

External links

Exercise

Train CIFAR-10 with three augmentation strategies on identical model + schedule: vanilla (flip + rotation), MixUp(0.2), and CutMix(1.0). Plot the train AND val accuracy curves for all three to epoch 50. The interesting signal isn't just final val accuracy — it's the train/val gap. Note which strategy narrows that gap most, and whether MixUp's gain shows up early or late.
Hint
MixUp will look WORSE on training accuracy (mixed labels are hard to fit) while being BETTER on validation — that divergence is the regularization working, not a bug.

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.