Pneumonia classification in X-rays using Deep learning

Arjun Subramaniyan K S
6 min readMar 18, 2021

Before getting into the solution, sparing a minute to understand the problem..

Pneumonia is a life-threatening infectious disease affecting one or both lungs in humans as the air sacs in the lungs known as the alveoli get inflated due to the presence of infection.

The cause for pneumonia could either be bacterial or viral or fungal pathogens. These pathogens induce filling up of the air sacs with fluid or pus and this leads to difficulty in breathing. Other associated symptoms could be cough with phlegm, fever and chills.

Image Reference: https://my.clevelandclinic.org/health/diseases/4471-pneumonia

Since Pneumonia can be more harmful based on the causal organism it could be contagious, hence there is a need for efficient and immediate detection of Pneumonia among suspected patients and provide suitable treatment. According to the Cleveland Clinic in the USA, approximately 1 million adults in the United States are hospitalized each year for pneumonia and 50,000 die from the disease. It is the second most common reason for being admitted to the hospital — childbirth is number one. Pneumonia is the most common reason children are admitted to the hospital in the United States. Seniors who are hospitalized for pneumonia face a higher risk of death compared to any of the top 10 other reasons for hospitalization.

Existing methods for Pneumonia diagnosis

- Pulse Oximetry: A drop in the level of saturation of oxygen(Percentage of Oxygen presence in arterial blood) could indicate presence of fluid/pus in the lungs, hence this can be used as a screening method

- Blood culture reports: By taking a blood sample and sending it for culturing to check for presence of pneumonia causing pathogens could prove pneumonia, however this method would take a long time for obtaining results

- Chest X Ray: By looking into a chest X ray a radiologist would be able to find out signs of infection and fluid filling in the lungs.

One of the fast and effective way of diagnosing pneumonia was by taking chest X rays, However, it can only be interpreted well by experts. Hence to automate this process, Convolution neural networks are trained and could be used for detecting pneumonia. The same approach has also been adopted even for COVID-19 detection from Chest X-ray and CT images.

The Dataset used- Description as on Kaggle

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. All chest X-ray imaging was performed as part of patients’ routine clinical care. The dataset is readily organized into 3 folders (train, test, val) and contains subfolders for each image category (Pneumonia/Normal). There are a total of 5,863 X-Ray images (JPEG) and 2 categories (Pneumonia/Normal).

However, just like several healthcare datasets, there is a data imbalance in classes which needs to be accounted for.

Sample Normal and Pneumonia X-ray image from the dataset

Point to emphasize
The method used to balance the dataset is by using Data- augmentation where multiple copies of the input data is obtained by rotations/flips as defined in the Tensorflow imagedatagenerator. One should avoid using horizontal flip/shift and vertical flip/shift since it would invert the image.

Why not to shift/flip: Because a horizontal flip/shift would result in the heart appearing on the right side of the image which is an abnormal condition known as Dextrocardia, secondly since Pneumonia checks for the presence of fluid/pus filling, we ourselves would be biasing the model by providing a whitened space on the right side of the chest and labeling it as normal since the heart has been inverted, we may end up missing detection of fluid fill on the right side of the chest,thus leading to False negatives.

Image 1: Images generated using ImageDataGenerator, Image 2: X-Ray image of a patients with Dextrocardia

Hence to account for the imbalance, only shear range and zoom range were used for building the final model, Even brightness range was tried but did not give good results. After several trials, an input standard size of 200 X 200 was chosen. Thus all of the input images were resized to 200 X 200 images.

Image Data generator to account for imbalance

Now began the building of the convolution neural network, I had 3 convolution 2D layers, each followed by a MaxPooling layer for getting the max value from each (2,2) patch as shown below.

Final deep learning model compiled

At the end of the convolution layers by flattening the output obtained from the convolution layer, where the entire output becomes a single row array element. The output array(tensor) is fed to the Dense layer and a sigmoid activation is used to determine whether the image belongs to a diseased class or normal class, thus the ouput of the network would be a single number that indicates the probability of the input image belonging to the diseased class(1).

Also, in order to prevent over-fitting a callback function called ReduceLRonplateau has been used, where the learning rate would be reduced by monitoring the parameter we choose and the patience(no of epochs to be monitored)

With 15 epochs and a batch size as shown in the previous image the Convolution neural network model was trained giving:

Accuracy and loss in 15 epochs

This was obtained after several different trials of image augmentation and different layers in the CNN. After this final model, I saved this model as a .h5 file and used this model to predict on the test set, which was also an imbalanced set containing more pneumonia images than normal images. As mentioned earlier, since the sigmoid activation curve has been used for getting an output which would be the probability ranging between 0 and 1, hence a Data-frame is created to easily visualize the results and the conventional threshold of 0.5 is chosen.

From the above image we notice that the test set has an accuracy of 88%, however since it is imbalanced, it is better to look into other parameter and the confusion matrix.

Figure 1: Confusion matrix of the test set Figure 2: AUC ROC Curve

From the above confusion matrix we can see that the no of healthy images are 234 while the number of Pneumonia images are 390 in number and we can also understand that there are a lot of False positives(Predicting healthy images as diseased), however since this is a health detection model, my focus was more on reducing the number of false negatives(Predicting diseased images as healthy) since the disease could be contagious. Looking at the no of False negatives, this model has been doing it’s job really well. The model gave an Area under curve value of 95.3 indicating that the model has learnt something and is not random along with an F1 score(A combination of both precision and sensitivity )of 91.38 which indicates a robust performance even with an imbalanced dataset like this one.

--

--

Arjun Subramaniyan K S

Health technology enthusiast|Trying my hands in Data science|Engineering freak|Master’s student