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

Preprocessing Layers

~10 min · layers

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

Preprocessing as a layer, not a script

The single most common production bug in ML is train/serve skew: your training pipeline normalizes pixels by 255 and lowercases text, but the serving code forgets one step, and the model quietly predicts garbage on inputs that look subtly different from what it trained on. Keras 3's answer is to make preprocessing a layer that lives inside the model graph — so when you save the model, the preprocessing saves with it, and there's no second script to keep in sync.

The catalog

Numeric: Rescaling(1./255) scales pixels into [0, 1]; Normalization() standardizes features to zero mean / unit variance. Text: TextVectorization tokenizes raw strings into integer sequences. Categorical: CategoryEncoding and StringLookup turn categories and strings into model-ready tensors. There are image-augmentation layers (RandomFlip, RandomRotation) in the same family.

The adapt() step

Stateless layers like Rescaling need nothing — the factor is fixed. But Normalization and TextVectorization learn statistics from your data first: you call layer.adapt(train_data) once so the layer computes mean/variance or builds its vocabulary. After adapting, the layer behaves like any other and carries those learned statistics into deployment automatically.

Code

Preprocessing layers: numeric, text, and categorical·python
# Numeric preprocessing
layers.Rescaling(1.0/255)             # Normalize pixel values
layers.Normalization()                  # Standardize features (.adapt() first)

# Text preprocessing
layers.TextVectorization(
    max_tokens=10000,
    output_mode="int",
    output_sequence_length=200,
)

# Category encoding
layers.CategoryEncoding(num_tokens=10)
layers.StringLookup(vocabulary=["cat", "dog", "bird"])

External links

Exercise

Build a CNN whose first two layers are Rescaling(1./255) and RandomFlip('horizontal'). Train on CIFAR-10. Save the model, reload it in a fresh process, and call predict() on raw uint8 [0,255] pixels — confirm the rescaling is applied automatically and you get sane predictions without re-implementing any preprocessing. That round-trip is the whole point of the lesson.
Hint
RandomFlip is active only during training, so it won't affect your inference predictions — but Rescaling runs in both modes, which is exactly what saves you from train/serve skew.

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.