How can Tensorflow be used to attach a classification head using Python?

TensorflowPythonServer Side ProgrammingProgramming

TensorFlow can be used to attach a classification head using a sequential model that has a Dense layer, using a feature extractor model, which is previously defined.

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

A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. 

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.

TensorFlow Hub is a repository that contains pre-trained TensorFlow models. TensorFlow can be used to fine-tune learning models.

We will understand how to use models from TensorFlow Hub with tf.keras, use an image classification model from TensorFlow Hub.  Once this is done, transfer learning can be performed to fine-tune a model for customized image classes. This is done by using a pretrained classifier model to take an image and predict what it is. This can be done without needing any training.  

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("Attaching a classification head")
num_classes = len(class_names)
model = tf.keras.Sequential([
   feature_extractor_layer,
   tf.keras.layers.Dense(num_classes)
])
print("The base architecture of the model")
model.summary()
print("The predictions are made")
predictions = model(image_batch)
print("The dimensions of the predictions")
predictions.shape

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

Output

Attaching a classification head
The base architecture of the model
Model: "sequential_3"
_________________________________________________________________
Layer (type)                Output Shape        Param #
=================================================================
keras_layer_1 (KerasLayer) (None, 1280)        2257984
_________________________________________________________________
dense_3 (Dense)           (None, 5)            6405
=================================================================
Total params: 2,264,389
Trainable params: 6,405
Non-trainable params: 2,257,984
_________________________________________________________________
The predictions are made
The dimensions of the predictions
TensorShape([32, 5])

Explanation

  • The classification head is attached to the model.
  • Once this is done, the base architecture of the model is determined.
  • This is done with the help of ‘summary’ method.
  • The dimensions of the data is determined.
  • This information is displayed on the console.
raja
Published on 13-Feb-2021 08:35:16
Advertisements