Table of Contents
- Criteria for selecting models
- Transfer Learning
- Model Interpretability using Gradient Weighted Class Activation Mapping
Transfer learning generally refers to a process where a model trained on one problem is used in some way on a second related problem.
Transfer learning is a technique in machine learning where we can take a model developed on one task and use it as a starting point in some other similar but different tasks. Transfer learning is extremely popular in Deep Learning since the “transfer” of knowledge from one “parent” model to a “child” model means that the “child” model can be trained to high accuracies with a much smaller dataset compared to the “parent” model.
- Transfer learning involves using models trained on one problem as a starting point on a related problem.
- Transfer learning is flexible, allowing the use of pre-trained models directly, as feature extraction preprocessing, and integrated into entirely new models.
- Keras provides convenient access to many top performing models on the ImageNet image recognition tasks such as VGG, Inception, and ResNet.
Criteria for selecting models
Criteria for selecting pretrained models
- Accuracy of the model: Higher the better
- Speed of Model Training and Predictions: Faster the better
Steps to follow :
- Import necessary packages
- list all available pretrained models using tf.keras.applications
- Preprocess input images
- Loop over each model calculating the accuracy and model size
- Visualize the results
Note : here I have excluded NasNetLarge owing to space constraints
Download Input Images:
Train Test Split
Keras Selector Processing
From the above we chose DenseNet201 based on the accuracy and also the number of parameters. We did try with all models having an accuracy above 90% but the scores were better with DenseNet201
Define custom metrics to derive F1 Score and Callbacks
Train the Model
Evaluation on validation Data
Visualize class activation maps using Gradient Weighted Class Activation Maps
Grad-CAM is an algorithm that can be used to visualize the class activation maps of a convolutional neural network thereby verifying if the network is looking and activating at correct locations.
It helps to visualize the CNN layer heatmap around the region where the activation maps have converged.
It works as follows:
- Finding the Convolutional layer in the network
- Examining gradient information flowing into that layer
Output of Grad-CAM is a heatmap visualization for a given class label either to predicted label or an arbitrary label we select for debugging.
Define GRAD-CAM Class
Function to display images
Extracting Covid and Non Covid Images
Loop through Covid19 Input Images picked randomly and generate corresponding heatmaps
Gradient Weighted Class Activation Maps for random Covid19 Samples
- By training on a set of pretrained models we can have a fair idea of what pretrianed networks to zero in for a specific image classification problem.
- After training our own CNNs we can apply Grad-CAM and visually verify that your model is learning the patterns that you think it learning and not some other pattern that occurs by happenstance in your dataset.
- Grad-CAM help : https://chitra.aniketmaurya.ml/image-classification-example/
- Transfer learning using DenseNet architecture :https://medium.com/@carlosz22/transfer-learning-using-keras-with-densenet-169-91679300f94a