How can Tensorflow be used to standardize the data using Python?

PythonServer Side ProgrammingProgrammingTensorflow

We will be using the flowers dataset, which contains images of several thousands of flowers. It contains 5 sub-directories, and there is one sub-directory for every class. Once the flower dataset has been downloaded using the ‘get_file’ method, it will be loaded into the environment to work with it.

The flower data can be standardized by introducing a normalization layer in the model. This layer is called the ‘Rescaling’ layer, which is applied to the entire dataset using the ‘map’ method.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

print("Normalization layer is created ")
normalization_layer = layers.experimental.preprocessing.Rescaling(1./255)
print("This layer is applied to dataset using map function ")
normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
print(np.min(first_image), np.max(first_image))

Code credit: https://www.tensorflow.org/tutorials/images/classification

Output

Normalization layer is created
This layer is applied to dataset using map function
0.0 1.0

Explanation

  • The RGB channel values are in the range [0, 255].
  • This is not considered ideal for a neural network.
  • As a thumb rule, ensure that the input values small.
  • Hence, we can standardize values to fall in between the range [0, 1].
  • This is done by using a Rescaling layer.
  • It can be done by applying the layer on the dataset by calling the map function.
  • Another way to do it is to include the layer within the model definition.
  • This would simplify the deployment process.
raja
Published on 11-Feb-2021 09:30:32
Advertisements