
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Standardize Data Using TensorFlow in Python
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.