Transfer Learning Using Keras Explained: How to train your first image classifier using Transfer…

Original article can be found here (source): Artificial Intelligence on Medium

#Initialize the hyper parametersImage_width, Image_height = 299, 299 
Training_Epochs = 2
Batch_Size = 32
Number_FC_Neurons = 1024
# Load the Inception V3 model and load it with it's pre-trained weights. But exclude the final fully Connected layerInceptionV3_base_model = InceptionV3(weights='imagenet', include_top=False) #include_top=False excludes final FC layer# Define the layers in the new classification prediction
x = InceptionV3_base_model.output
x = GlobalAveragePooling2D()(x)
# new FC layer, random init
x = Dense(Number_FC_Neurons, activation='relu')(x)
# new softmax layer
predictions = Dense(num_classes, activation='softmax')(x)
# freeze some top layers and only train remaing layers
Layers_To_Freeze = 172
for layer in model.layers[:Layers_To_Freeze]:
layer.trainable = False
for layer in model.layers[Layers_To_Freeze:]:
layer.trainable = True
#Let's compile the model with all perameters
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy'])
# Fit the Fine-tuning model to the data from the generators.
history_fine_tune = model.fit_generator(
steps_per_epoch = num_train_samples // batch_size,
validation_steps = num_validate_samples // batch_size,
# Save fine tuned model'inceptionv3-image-classification.model')

Please note that this above code is to give some better intuition in practical about how the transfer learning can be implemented. You can get full code in this repository:

The above code was inspired from the article from the keras documentation that can be found here;

This approach is effective because the images were trained on a large corpus of photographs and require the model to make predictions on a relatively large number of classes, in turn, requiring that the model efficiently learn to extract features from photographs in order to perform well on the problem.

Benefits of Transfer Learning

  • Improved baseline performance
  • Model-development time
  • Improved features
  • Helps in solve complex real-world problems with less training effort
  • Obtain good metrics with no or very limited labeled data

Thank you for your time and hope you learn little something. Appreciate any feedback that would improve quality and produce more relevant and better content in future.