How can Tensorflow be used with pre-trained model to rescale pixel values?


Tensorflow can be used with pre-trained model to rescale the pixel values using ‘Rescaling’ layer, and using ‘preprocess_input’ method which is present in ‘tf.keras.applications.mobilenet_v2’ package.

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

We will understand how to classify images of cats and dogs with the help of transfer learning from a pre-trained network.

The intuition behind transfer learning for image classification is, if a model is trained on a large and general dataset, this model can be used to effectively serve as a generic model for the visual world. It would have learned the feature maps, which means the user won’t have to start from scratch by training a large model on a large dataset.

Read More: How can a customized model be pre-trained?

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.

Example

print("Pixel values are being rescaled")
preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input
rescale = tf.keras.layers.experimental.preprocessing.Rescaling(1./127.5, offset= -1)

Code credit −https://www.tensorflow.org/tutorials/images/transfer_learning

Output

Pixel values are being rescaled

Explanation

  • The tf.keras.applications.MobileNetV2 will be used as the base model.
  • This model expects pixel vaues in the range [-1,1].
  • But pixel values in the images are in the range [0-255].
  • They can be rescaled and then preprocessed that is included in the model can be used.

Updated on: 25-Feb-2021

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements