Pneumonia Diagnosis Using Deep Learning Techniques
X-RAY IMAGE CLASSIFICATION
By Khulood Nasher
Introduction
Define the Problem
Disease diagnosis with radiology is a common practice in medicine but requires doctors to interpret the results from the x-ray images. Due to the increase in the number of patients and the low availability of doctors, there was a need for a new method to diagnose. Fortunately, machine learning has introduced the solution to this problem. In this project, we are going to introduce deep learning models and techniques in diagnosing pneumonia. Through this project, we applied deep learning CNN techniques in image classification and we followed the data science methodology as follows:
Obtain Data:
First: Preprocessing Images:
To get my data which is images here ready for classification, I performed the following preprocessing:
- All the images were reshaped to size 150 by 150
- Loaded image data from the hierarchical file ‘chest_xray’ using an image data generator.
- Defined testing, training, validation.
- Decoded the JPEG content to RGB grids of pixels.
- Reshaped all images to the same size of 150 x 150 pixels
- Converted pixels into floating-point tensors
- Chuck data by “batch_size=32'
- Rescaled the pixel values( between o and 255) to the [0,1] interval.
Explore Data:
About the X-ray images data:
An input of total x-ray images of 4,704 were downloaded from Kaggle, https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia.
Chest X-ray images were selected from kids of one to five years old from
Guangzhou Women and Children’sMedical Center, Guangzhou, China. Source:https://data.mendeley.com/datasets/rscbjbr9sj/3
The data images in Kaggle have not split appropriately, so we split the images manually with percentages of:60% train, 20% val, 20% test. Our image data were as follows:
Our image data were as follows:
Train data:
2480 images belong to 2 classes: Normal and pneumonia. The Normal images in the training set are 878 images, while the pneumonia images are 1604 images which is almost double the number.
Validation Data:
There are 1102 images belonging to 2 classes: Normal and pneumonia. The Normal images in the training set are 322 images, while the pneumonia images are 802 images which is almost double the number.
Test data:
There are 1102 images belonging to 2 classes: Normal and pneumonia. The Normal images in the training set are 321 images, while the pneumonia images are 781 images which is almost double the number.
We then visualized the distribution of the normal images and pneumonia images in each dataset as following
We also visualized the x-ray images of the normal and pneumonia cases in the train and validation dataset.
Methodology:
In my project, I tried to answer the following important question:
Question: How is an image classification model typically designed?
We can divide this process broadly into 4 steps. Each step requires a certain amount of time to execute: https://www.analyticsvidhya.com/blog/2019/01/
Build-image-classification-model
Loading and pre-processing Data — 30% time
Defining Model architecture — 10% time
Training the model — 50% time
Estimation of performance — 10% time
Training the model For training the model, we require:
Training images and their corresponding true labels
Validation images and their corresponding true labels (we use these labels only
to validate the model and not during the training phase).
Training the model
For training the model, we require:
Training images and their corresponding true labels. We used CNN deep learning
and adjust weights(parameters) through hyper tuning parameters such as Padding,
Dropout, Elastic Net, Augmentation techniques, and Transfer Learning Techniques, VGG16.
Validation images and their corresponding true labels (we use these labels only to validate the model and not during the training phase)
We also define the number of epochs. we ran all the models for 30 epochs
Baseline Model
In my base model, I used 4 different conv layers with max-pooling and after that, I applied a dense layer with 512 neurons and finally the output layer with one neuron.
- Important Note: Number of filters which is the same as the number of features has been increasing in my conv layers from 32 to 64 to 128. With each conv layers, we doubled the number of filters that I used. Another thing with max-pooling 2 by 2, we reduced the size of our images, so the image of 150 by 150, after first max pooling is 75 by 75. Then the image size after second max pooling is 37 by 37. Then after the third max-pooling image size is 18 by 18. Then the last image size is 9 by 9. As we went along in our neural network number of features (filters) is increasing while the weight and length of our images are decreasing. So image size should decrease with each conv layer and the feature map height length increases as we go along the neural network.
Construct Architecture of the Base Model:
CNN
Layers used to build ConvNets. As we described above, a simple ConvNet is a sequence of layers, and every layer of a ConvNet transforms one volume of activations to another through a differentiable function. We use three main types of layers to build ConvNet architectures: Convolutional Layer, Pooling Layer, and Fully-Connected Layer (exactly as seen in regular Neural Networks). We will stack these layers to form a full ConvNet architecture.
We can see that the base model has 4 conv layers with 4 different polling layers, dense layer, and finally output layer. The total number of parameters we are training in our base model is around three million parameters. Now the next step is to compile the model. We are going to use RMSprop which has a little advantage over sgd while performing image processing, and we are using a learning rate of 0.0001, by default the learning rate is 0.01.
Compiling the Neural Network
The accuracy of the base model was great with a recall of 99%, and testing accuracy of 87%
Visualizing The Accuracy and Loss in Training and Validation of the base model
I assessed further my base model through confusion matrix,f1 score, precision, and recall.
Interpreting the Confusion Matrix on testing data:
In medical centers, False Negative is more important than False Positive. A false positive means here to predict pneumonia when the person is healthy is less critical than a false negative which is predicting healthy while the patient is pneumonia. A big number of False Negatives in medical centers might cause a loss of life because of the wrong diagnosis and that happens when we tell the person you are healthy while the person is sick. The number of false negatives obtained with our base model here is extremely low.
The base model misclassified only 10 images out of a total of 1,148 images. This represents the number of 10/1148*100%=0.87%. Meaning that my base model misclassifies pneumonia patients as normal with an error of less than 1%, and with an accuracy greater than 99% which suggests that our base model is reliable for pneumonia diagnosis.
Interpreting the Classification Report on testing data:
The Base Model is doing a great job with all metrics including precision 87%, Recall 99% and this is the most important metrics to recall pneumonia patient, and in addition to the great number of f1-score with an accuracy of 92%
Conclusion
Finally, We can say that the weight of our base model is great because the Recall was 99% in the base model and the testing accuracy is 88%.
The number of trainable parameters in the base model is 3.5 million. The good weight of parameters from the base model is ideal and that’s what increases the Recall metric. In conclusion, I recommend using my base model as a reliable tool to be deployed in the medical centers for diagnosing pneumonia.