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

Keras Preprocessing Layers

~10 min · data

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

Models are 30% of the engineering; data pipelines are the other 70%. Keras 3 has a powerful preprocessing-layer system (image augmentation, text vectorization, normalization) that runs *inside the model graph* — meaning it ships with your saved model. Plus how to plug in tf.data, torch.DataLoader, and grain cleanly.

Preprocessing is a layer, not a script

The thing that breaks most production ML systems isn't the model — it's the gap between how you preprocessed data at training time and how you preprocess it at serving time. Keras 3 closes that gap by making preprocessing a layer: Rescaling, Normalization, RandomFlip, TextVectorization, IntegerLookup and the rest are real layers you stack like any other. Put them inside the model and the saved .keras file carries the exact same transform to production for free.

Adapt before you train

The stateful layers — Normalization and TextVectorization — need to see your data before they're useful. You call .adapt(data) once, and the layer learns its statistics (mean/variance, or the vocabulary) and freezes them as weights. After that the layer is deterministic: it applies the same learned transform forever, including at inference. Stateless layers like Rescaling skip this step — there's nothing to learn from a fixed 1/255 divide. The Code block below shows both kinds and then bakes a rescaler into a model.

Two places to put it

You have a choice: put preprocessing in the model (portable, deploy-safe — the transform travels with the weights) or in the tf.data pipeline (faster training, because the work runs on CPU in parallel with the GPU step). The rule of thumb: keep deploy-critical, learned transforms (normalization, vocabulary) in the model; push cheap, throwaway, training-only work (random augmentation) into the data pipeline where the speed matters and the skew risk is zero.

Code

Adapt stateful layers, then bake them into the model·python
# Normalization: learns mean/variance from data
normalizer = layers.Normalization()
normalizer.adapt(training_data)  # Must see data first

# Rescaling: simple linear transform
rescaler = layers.Rescaling(1.0 / 255.0)

# TextVectorization: text → integer tokens
vectorizer = layers.TextVectorization(
    max_tokens=20000,
    output_sequence_length=200,
)
vectorizer.adapt(text_data)

# Put inside the model
model = keras.Sequential([
    rescaler,                                    # Preprocessing
    layers.Conv2D(32, 3, activation="relu"),  # Model layers
    layers.GlobalAveragePooling2D(),
    layers.Dense(10, activation="softmax"),
])

External links

Exercise

Build a model that includes Rescaling + RandomFlip as the first two layers. Train, save, load. Predict on raw [0,255] images and verify rescaling fires (no error, sane output).

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.