Model Development
We will use pre-trained weight for an Inception network which is trained on imagenet dataset. This dataset contains millions of images for around 1000 classes of images.
Python3
from tensorflow.keras.applications.inception_v3 import InceptionV3 pre_trained_model = InceptionV3( input_shape = ( 128 , 128 , 3 ), weights = 'imagenet' , include_top = False ) |
Output:
87916544/87910968 [==============================] - 1s 0us/step 87924736/87910968 [==============================] - 1s 0us/step
Let’s check how deep or the number of layers are there in this pre-trained model.
Python3
len (pre_trained_model.layers) |
Output:
311
This is how deep this model is this also justifies why this model is highly effective in extracting useful features from images which helps us to build classifiers. The parameters of a model we import are already trained on millions of images and for weeks so, we do not need to train them again.
Python3
for layer in pre_trained_model.layers: layer.trainable = False last_layer = pre_trained_model.get_layer( 'mixed7' ) print ( 'last layer output shape: ' , last_layer.output_shape) last_output = last_layer.output |
Output:
last layer output shape: (None, 6, 6, 768)
Model Architecture
We will implement a model using the Functional API of Keras which will contain the following parts:
- The base model is the Inception model in this case.
- The Flatten layer flattens the output of the base model’s output.
- Then we will have two fully connected layers followed by the output of the flattened layer.
- We have included some BatchNormalization layers to enable stable and fast training and a Dropout layer before the final layer to avoid any possibility of overfitting.
- The final layer is the output layer which outputs soft probabilities for the three classes.
Python3
# Model Architecture x = layers.Flatten()(last_output) x = layers.Dense( 256 , activation = 'relu' )(x) x = layers.BatchNormalization()(x) x = layers.Dense( 256 , activation = 'relu' )(x) x = layers.Dropout( 0.3 )(x) x = layers.BatchNormalization()(x) output = layers.Dense( 120 , activation = 'softmax' )(x) model = keras.Model(pre_trained_model. input , output) # Model Compilation model. compile ( optimizer = 'adam' , loss = keras.losses.CategoricalCrossentropy(from_logits = True ), metrics = [ 'AUC' ] ) |
Dog Breed Classification using Transfer Learning
In this article, we will learn how to build a classifier using the Transfer Learning technique which can classify among different breeds of dogs. This project has been developed using collab and the dataset has been taken from Kaggle whose link has been provided as well.