Pneumonia refers to the inflammation of lungs caused by bacterial or viral infection affecting primarily the small air sacs known as alveoli. Symptoms typically include some combination of productive or dry cough, chest pain, fever and difficulty breathing. pre-existing conditions like cystic fibrosis, chronic obstructive pulmonary disease (COPD), asthma, diabetes, heart failure, a history of smoking, stroke and weak immune system increase the risk factor for contracting pneumonia.

Pneumonia is usually diagnosed using chest x-ray, blood tests detecting complete blood count (CBC), culture of the sputum and pulse oximetry measuring oxygen level. Along with the diagnosis, detecting the source of the disease as to where it was acquired such as community-based or hospital-acquired or healthcare-associated plays a vital role in controlling the spread.

Does AI help in detecting pneumonia?

Pneumonia affects 700,000 children and 7% of global population every year. Even for trained professional, it becomes tedious to detect pneumonia with high accuracy rate. This is where Artificial intelligence (AI) has the potential to revolutionize both diagnosis and management of such disease. Using AI, the tedious job of rapidly reviewing immense amounts of images and classifying diseases like pneumonia becomes much simpler thereby facilitating earlier treatment resulting in improved clinical outcomes.

How we employed AI to detect Pneumonia?

Deep learning model learns the patterns from the chest x-rays and identifies if the image of the chest x-ray is either normal or is having pneumonia.

Data Source:

Our data source consisted of 5,863 X-Ray images (JPEG) and 2 categories (Pneumonia/Normal). The dataset was organized into 3 folders (train, test, val) and subfolders for each image category (Pneumonia/Normal).

Chest X-ray images (anterior-posterior) were selected from retrospective cohorts of pediatric patients of one to five years old from Guangzhou Women and Children’s Medical Center, Guangzhou.

Data Source Quality Check:

All chest X-ray imaging was performed as part of patients’ routine clinical care. For the analysis of chest x-ray images, all chest radiographs were initially screened for quality control by removing all low quality or unreadable scans. The diagnoses for the images were then graded by two expert physicians before being cleared for training the AI system. In order to account for any grading errors, the evaluation set was also checked by a third expert.

Deep Learning Approaches:

Initially we tried out transfer learning algorithms which are trained on ImageNet as the base models. We built convolutional neural network (cnn) layers on top of them, using their learned weights. We then trained the cnn layers with the training data and built the final trained models. The transfer learning algorithms we tried out were Vgg16 and ResNet.

The data preprocessing steps included steps like

– grayscale conversion,

– data normalization,

– resizing images,

– data augmentation to randomly rotate images in the range (degrees, 0 to 180),

– random zooming of images,

– shift images horizontally (some fraction of total width),

– shift images vertically (some fraction of total height).

Final Approach:

We finally implemented a cnn model with data preprocessing steps like – converting to grayscale, resizing images, converting images to numpy arrays, data normalization, resizing to required array dimensions, and data augmentation. But we did not make use of transfer learning as we achieved better accuracy without using it.

For the data augmentation we did the following:

– Randomly rotate some training images by 30 degrees

– Randomly Zoom by 20% some training images

– Randomly shift images horizontally by 10% of the width

– Randomly shift images vertically by 10% of the height

– Randomly flip images horizontally. 

The data augmentation was done on both training and validation datasets and the model was trained with 12 epochs.

How did we improve the performance of the model detecting pneumonia?

Hyper Parameter is defined as the parameters that directly controls the performance of the models. We tune these parameters to get the best performance.

Why use Keras Tuner for Hyperparameter Tuning

Keras Tuner is an easy-to-use, distributable hyperparameter optimization framework that solves the pain points of performing a hyperparameter search.

Keras Tuner comes with Bayesian Optimization, Hyperband, and Random Search algorithms built-in, and is also designed to be easy for researchers to extend in order to experiment with new search algorithms.

Keras Tuner makes it easy to define a search space and leverage included algorithms to find the best hyperparameter values.

In machine learning, we have techniques like GridSearchCV and RandomizedSearchCV for doing hyper-parameter tuning. We just need to define the range of the parameters and then automatically the algorithm computes the different combinations. Keras tuner was used similarly. We have to define the range of neurons we want to compute and similarly kernel size.

How to build a model using Keras Tuner:

1] Install and import the Keras Tuner

2] Prepare the dataset

3] Define the model

The model you set up for hypertuning is called a hypermodel. You can define a hypermodel through two approaches:

1. By using a model builder function

2. By subclassing the HyperModel class of the Keras Tuner API

4] Instantiate the tuner and perform hypertuning:

The Keras Tuner has four tuners available: 

– RandomSearch

– Hyperband

– BayesianOptimization

– Sklearn

5] Retrain the model with the optimal hyperparameters from the search

6] Analyze Performance Output

pneumonia Predera AIQ

Fig. Above are the snapshots of training of 5 different trials that outputs the trial summary and the best hyperparameters.

Now we check the best model from these 5 trials and use it for training the models.

Summary of the best model:

We used below code to find the best model:


Model: “sequential”

pneumonia 5 Predera AIQ

Total params: 3,197,601

Trainable params: 3,194,849

Non-trainable params: 2,752

Model Performance:

Train Metrics: (5216 images)

Loss of the model is –  0.37348188760557727

Accuracy of the model is –  89.9923324584961 %

Test Metrics: (624 images)

Loss of the model is –  0.4661979952779336

Accuracy of the model is –  91.02563858032227 %

Classification Report:

pneumonia 3 Predera AIQ

Confusion Matrix:

pneumonia 1 Predera AIQ

After retraining the model with the optimal hyperparameters from the search, we achieved 91% accuracy on the validation data. As per our analysis, Keras tuner did take little bit more time to compute the best hyperparameters but provided the best accuracy in detecting pneumonia.