Image normalization
Convert uint8 or floating-point image pixels from [0, 255] to [0, 1].
Keras · Class
Keras' Rescaling layer applies the same linear transformation to every input value: multiply by scale, then add offset. It is commonly used to convert image values such as [0, 255] into a model-friendly range such as [0, 1] or [-1, 1].
Rescaling applies an affine transformation independently to every input value: the value is multiplied by scale and then offset is added. It accepts integer or floating-point inputs, normally produces floating-point outputs, and performs the same transformation during training and inference.
keras.layers.Rescaling(scale, offset=0.0, **kwargs)| Name | Type | Description |
|---|---|---|
scale | Float | Floating-point multiplier applied to each input value. |
offset | Float | Floating-point value added after scaling; defaults to 0.0. |
**kwargs | Keyword arguments | Base layer keyword arguments, such as name and dtype. |
A layer that outputs the transformed values; by default, outputs are floating-point.
import keras
from keras import layers
inputs = keras.Input(shape=(224, 224, 3), dtype="uint8")
outputs = layers.Rescaling(scale=1.0 / 255)(inputs)
model = keras.Model(inputs, outputs)
x = keras.ops.ones((1, 224, 224, 3), dtype="uint8") * 255
y = model(x)
print(y.dtype, keras.ops.max(y))This model converts pixel values from 0–255 integers to floating-point values in the 0–1 range.
Convert uint8 or floating-point image pixels from [0, 255] to [0, 1].
Prepare inputs for models whose expected preprocessing range is [-1, 1].
Embed deterministic numeric preprocessing directly in a model or input pipeline.
It computes an elementwise affine transform: each input value becomes input * scale + offset. For example, scale=1/255 maps [0,255] to [0,1].
Follow-up: How would you map [0,255] to [-1,1]? Use scale=1/127.5 and offset=-1.
Use scale=1/255 for normalization to [0,1]. The layer applies this operation during both training and inference.
Follow-up: Would a training flag disable it during inference? No; the documentation says the rescaling is applied in both modes.
The constructor is keras.layers.Rescaling(scale, offset=0.0, **kwargs). scale and offset are floating-point transformation parameters, while kwargs accepts base-layer options such as name and dtype.
Follow-up: What can be passed through kwargs? Base Layer keyword arguments, including name and dtype.
keras.layers.Resizingkeras.layers.Normalizationkeras.layers.RandomFlipkeras.Layer