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

Data Augmentation Layers

~11 min · augmentation, regularization

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

Free regularization that actually works

Data augmentation is the single most effective regularizer for image models — it teaches the model that "rotated cat" and "flipped cat" are still cats. Keras provides preprocessing layers you can drop directly into your model graph or apply via tf.data.

The standard augmentation set for image classification:

  • RandomFlip('horizontal') — most images are symmetric horizontally
  • RandomRotation(0.1) — small rotations (±10%)
  • RandomZoom(0.1) — slight zoom in/out
  • RandomBrightness(0.1), RandomContrast(0.1)

Don't include vertical flip for natural images — upside-down cats are not cats. Don't include color shifts that change semantics — a green stop sign is not a stop sign.

Augmentation only runs during training. The layers respect the training flag automatically — no augmentation during inference. This means you can include them as part of the model itself, simplifying deployment.

Code

Augmentation을 model 안에·python
import tensorflow as tf
from tensorflow import keras
from keras import layers

augmentation = keras.Sequential([
    layers.RandomFlip('horizontal'),
    layers.RandomRotation(0.1),
    layers.RandomZoom(0.1),
    layers.RandomBrightness(0.1),
    layers.RandomContrast(0.1),
], name="augmentation")

model = keras.Sequential([
    keras.Input(shape=(224, 224, 3)),
    augmentation,                          # only active when training=True
    layers.Rescaling(1./255),
    layers.Conv2D(32, 3, activation='relu', padding='same'),
    layers.MaxPooling2D(),
    layers.Conv2D(64, 3, activation='relu', padding='same'),
    layers.GlobalAveragePooling2D(),
    layers.Dropout(0.4),
    layers.Dense(10),
])

# At inference, augmentation is bypassed automatically
preds = model(test_images, training=False)

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.