Normalize pixels to [0, 1]
Image models receive integer RGB pixels in the [0, 255] range.
TensorFlow · Class
A Keras preprocessing layer that converts every input value with input * scale + offset. It is commonly used to normalize image pixels, such as mapping [0, 255] to [0, 1] or [-1, 1].
Rescaling transforms every input value using the affine rule output = input × scale + offset. It accepts integer or floating-point inputs, normally produces floating-point outputs, and performs the same transformation while training and inferring.
tf.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 | Additional base-layer keyword arguments, including options such as name and dtype. |
A tensor containing the rescaled values; outputs are floating point by default.
scale, then shifted by offset.scale=1./255.scale=1./127.5 and offset=-1.import tensorflow as tf
layer = tf.keras.layers.Rescaling(1.0 / 255.0)
images = tf.constant([[[[0, 128, 255]]]], dtype=tf.uint8)
scaled = layer(images)
print(scaled.numpy()) # [[[ [0.0, 0.5019608, 1.0] ]]]This runnable example converts integer pixel values from the 0–255 range to floating-point values in the 0–1 range.
Image models receive integer RGB pixels in the [0, 255] range.
A model expects inputs centered in a symmetric range.
Preprocessing should be part of the input pipeline rather than separate application code.
tf.keras.layers.Rescaling perform?Rescaling is a preprocessing layer that applies an affine transformation independently to each input value: multiply by scale, then add offset. For input x, the result is x * scale + offset.
Follow-up: How would you map [0, 255] to [-1, 1]? Use scale=1./127.5 and offset=-1, which gives 0 → -1 and 255 → 1.
Use scale=1./255 when converting image values from [0, 255] to [0, 1]. To map the same source range to [-1, 1], use scale=1./127.5 with offset=-1.
Follow-up: Why use a layer rather than manual preprocessing? Embedding it in the model can simplify deployment, while applying it through dataset mapping is also supported.
The constructor accepts scale, an offset multiplier, offset, an additive shift defaulting to 0.0, and standard base-layer keyword arguments such as name and dtype. Inputs may be integer or floating point, and outputs are floating point by default.
Follow-up: What operational behavior should be tested? Verify the output dtype and confirm that the same transformation is applied during both training and inference.
tf.keras.layers.Layertf.keras.layers.Normalizationtf.data.Dataset.map