How can Tensorflow and pre-trained model be used to continue training the model using Python?


Tensorflow and the pre-trained model can be used to continue training the model by using the ‘fit’ method and specifying the number of training steps. The validation data is used to fit the model.

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. 

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("The model training continues")
fine_tune_epochs = 10
total_epochs = initial_epochs + fine_tune_epochs
print("The model is being fit to the data")
history_fine = model.fit(train_dataset,
   epochs=total_epochs,
   initial_epoch=history.epoch[-1],
   validation_data=validation_dataset)

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

Output

The model training continues
The model is being fit to the data
Epoch 10/20
63/63 [==============================] - 85s 1s/step - loss: 0.1568 - accuracy: 0.9244 - val_loss: 0.0506 - val_accuracy: 0.9864
Epoch 11/20
63/63 [==============================] - 73s 1s/step - loss: 0.1433 - accuracy: 0.9419 - val_loss: 0.0429 - val_accuracy: 0.9851
Epoch 12/20
63/63 [==============================] - 72s 1s/step - loss: 0.0984 - accuracy: 0.9609 - val_loss: 0.0450 - val_accuracy: 0.9827
Epoch 13/20
63/63 [==============================] - 72s 1s/step - loss: 0.1130 - accuracy: 0.9567 - val_loss: 0.0377 - val_accuracy: 0.9876
Epoch 14/20
63/63 [==============================] - 72s 1s/step - loss: 0.0783 - accuracy: 0.9685 - val_loss: 0.0406 - val_accuracy: 0.9889
Epoch 15/20
63/63 [==============================] - 72s 1s/step - loss: 0.0740 - accuracy: 0.9697 - val_loss: 0.0365 - val_accuracy: 0.9839
Epoch 16/20
63/63 [==============================] - 72s 1s/step - loss: 0.0794 - accuracy: 0.9647 - val_loss: 0.0376 - val_accuracy: 0.9839
Epoch 17/20
63/63 [==============================] - 71s 1s/step - loss: 0.0744 - accuracy: 0.9710 - val_loss: 0.0318 - val_accuracy: 0.9913
Epoch 18/20
63/63 [==============================] - 72s 1s/step - loss: 0.0725 - accuracy: 0.9719 - val_loss: 0.0410 - val_accuracy: 0.9876
Epoch 19/20
63/63 [==============================] - 72s 1s/step - loss: 0.0761 - accuracy: 0.9684 - val_loss: 0.0331 - val_accuracy: 0.9889
Epoch 20/20
63/63 [==============================] - 71s 1s/step - loss: 0.0632 - accuracy: 0.9742 - val_loss: 0.0405 - val_accuracy: 0.9814

Explanation

  • The model is fit to the data.
  • This is done using the ‘fit’ method.
  • The number of epochs used is initially 10.

Updated on: 25-Feb-2021

580 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements