Covid19 Classification from X-Ray Images
By: Plaban Nayak
Tags: Keras, Transfer Learning, Grad Cam

X-rays of patients under scan for Covid19

Table of Contents

Background Criteria for selecting models Code Transfer Learning Code Model Interpretability using Gradient Weighted Class Activation Mapping Code Resources

Background

Transfer learning generally refers to a process where a model trained on one problem is used in some way on a second related problem.

Convolutional Neural Network

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:

Source : DPHI sprint15 hackathon

Data Preprocessing

Train Data:

Test Data:

Train Test Split

Keras Selector Processing

Output:

Output

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

Transfer Learning

Define custom metrics to derive F1 Score and Callbacks

Build Model

Train the Model

Evaluation on validation Data

F1 Score:

Confusion Matrix

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