Diagram of the model: a 128 by 128 chest X-ray with an opaque patch passes through three rounds of convolution and pooling, then a dense layer, ending in a prediction of pneumonia at 0.99 and normal at 0.01.

Pneumonia is one of the leading causes of death in children under five worldwide. A chest radiograph is the standard way to diagnose it, but reading one takes a trained radiologist. Using the RSNA Pneumonia Detection Challenge dataset on Kaggle, I built a binary classifier that predicts whether a chest X-ray shows pneumonia.

I also wanted to see how much better deep learning does than classical models on this problem, so I built both and evaluated them the same way.

The notebooks

The project is split into four notebooks:

  1. Exploratory data analysis of the DICOM images and labels.
  2. Feature extraction for the classical models.
  3. Classical models: logistic regression, kNN, Gaussian naive Bayes, random forest, SVM and gradient boosting.
  4. Deep learning models: a fully connected network, a custom CNN, and MobileNet with transfer learning.

Features for the classical models

Classical models can't take raw pixels, so each image went through a traditional image-processing pipeline first. Histogram equalisation evened out the contrast, sharpening brought out structure, edge detection traced the lung outlines, and thresholding picked out opaque regions.

  1. DICOM image
  2. Histogram equalisation
  3. Sharpening
  4. Edge detection (perimeter)
  5. Thresholding (area)

From the processed images I extracted hand-built features, including:

  • the area of opacity
  • the perimeter of the visible lung regions
  • Hu moments
  • the mean and standard deviation of the raw DICOM pixels
  • contour statistics

These were written to a CSV so the modelling notebook could load them without reprocessing every image.

The deep learning input was much simpler: read each DICOM with pydicom and reshape it to a 128 × 128 × 3 array.

Building the CNN

I grew the network step by step. Each extra convolution made it deeper, and max pooling kept variance and compute down. As depth went up the network began to overfit, so I added dropout, with the highest rates on the fully connected layers at the end, since they hold most of the parameters.

I followed the usual rule of keeping the feature space wide and shallow early in the network and narrow and deep near the end. I used no padding on the input layers. The edges of a chest X-ray carry little signal, since what matters is the lungs in the centre.

Tuning

For the classical models I used scikit-learn's randomised search, which covers a large search space for a fraction of the cost of a full grid search.

For the neural networks most of the tuning was done by hand, since a grid search would have taken far too long. Three Keras callbacks did a lot of the work:

  • Early stopping ends training once validation performance stops improving, which caps overfitting without guessing an epoch count.
  • Learning rate scheduling applies exponential decay over training.
  • Model checkpointing keeps a snapshot of the best weights in case a run fails.

The best settings each search found:

ModelBest settings
Logistic regressionC = 1, unweighted classes
kNN150 neighbours, weighted by distance
Random forest700 trees, max depth 9, min split 4
SVMC = 1.5, unweighted classes
Gradient boosting700 trees, max depth 9, min split 4, learning rate 0.005
Fully connected NNBatch size 32, unweighted classes
CNNBatch size 128, exponential LR decay, balanced classes, heavier dropout near the end
MobileNetBatch size 64, exponential LR decay, balanced classes

Picking a model

All nine models were compared using 5-fold cross validation, averaging accuracy, precision, recall and AUC across the folds. I cared most about recall: a missed case of pneumonia is worse than a false alarm.

As expected, the neural networks came out on top. MobileNet with transfer learning beat everything else despite having fewer parameters than some of the other models, and the custom CNN came second. Pretrained features transferred well even from natural images to radiographs.

The gap was widest on recall. All three networks caught more than 95% of pneumonia cases, while the best classical model, kNN, caught 70% and logistic regression only 17%.

Mean across 5 folds, in %. Best model first.
ModelAccuracyPrecisionRecallAUC
MobileNet98.698.299.199.6
CNN96.297.497.598.9
Fully connected NN94.494.695.197.1
kNN83.772.370.480.1
Gradient boosting77.868.455.471.8
Random forest74.564.642.665.9
SVM73.162.038.263.7
Naive Bayes69.152.919.255.7
Logistic regression68.952.217.255.0

Results

I retrained MobileNet on about 13,000 images, roughly half the training set. Early stopping ended training after around 13 epochs, as the validation metrics flattened out while the training metrics kept climbing. On 3,000 held-out test images it scored:

  • Accuracy: 98.9%
  • Precision: 98.89%
  • Recall: 99.07%
  • AUC: 99.67%

Out of 3,000 patients, 13 with pneumonia were missed and 20 healthy patients were flagged incorrectly.

The code is on GitHub.