How can Tensorflow be used to build normalization layer using Python?

TensorFlow can be used to build a normalization layer by converting pixel values from the range [0, 255] to [0, 1] using the Rescaling layer. This preprocessing step is essential for neural networks to process image data effectively.

A neural network that contains at least one convolutional layer is known as a Convolutional Neural Network (CNN). Transfer learning allows us to use pre-trained models from TensorFlow Hub without training from scratch on large datasets.

We are using Google Colaboratory to run the code below. Google Colab provides free access to GPUs and requires no setup for running Python code in the browser.

Creating a Normalization Layer

The normalization layer rescales pixel values from [0, 255] to [0, 1] range, which is the standard input format for most image classification models ?

import tensorflow as tf
import numpy as np

# Example dataset (assuming train_ds is already loaded)
# For demonstration, let's create sample class names
class_names_list = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
class_names = np.array(class_names_list)

print("It contains 5 classes")
print(class_names)

print("A normalization layer is built")
normalization_layer = tf.keras.layers.Rescaling(1./255)

# Apply normalization to dataset
# train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
print("Normalization layer created successfully")
It contains 5 classes
['daisy' 'dandelion' 'roses' 'sunflowers' 'tulips']
A normalization layer is built
Normalization layer created successfully

How Rescaling Works

The Rescaling layer multiplies each pixel value by the specified factor. For images with pixel values in range [0, 255], using factor 1./255 normalizes them to [0, 1] ?

import tensorflow as tf

# Create a sample image tensor with values 0-255
sample_image = tf.constant([[[100, 150, 200], [50, 75, 25]]], dtype=tf.float32)
print("Original pixel values:")
print(sample_image)

# Apply rescaling
rescaling_layer = tf.keras.layers.Rescaling(1./255)
normalized_image = rescaling_layer(sample_image)

print("\nAfter normalization (scaled to 0-1):")
print(normalized_image)
Original pixel values:
tf.Tensor(
[[[100. 150. 200.]
  [ 50.  75.  25.]]], shape=(1, 2, 3), dtype=float32)

After normalization (scaled to 0-1):
tf.Tensor(
[[[0.39215687 0.58823532 0.78431374]
  [0.19607843 0.29411766 0.09803922]]], shape=(1, 2, 3), dtype=float32)

Key Points

  • TensorFlow Hub models expect float inputs in the range [0, 1] for optimal performance

  • The Rescaling layer performs element-wise multiplication by the specified factor

  • Normalization should be applied consistently to both training and validation datasets

  • This preprocessing step can be integrated directly into the model pipeline

Conclusion

The tf.keras.layers.Rescaling layer provides an efficient way to normalize image pixel values from [0, 255] to [0, 1]. This normalization is crucial for transfer learning and ensures optimal performance with pre-trained models from TensorFlow Hub.

Updated on: 2026-03-25T16:40:15+05:30

297 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements