DEV Community

Sheikh Sadi Asif
Sheikh Sadi Asif

Posted on

I Built an AI That Detects Pneumonia From Chest X-Rays Here's Exactly How I Did It

A few weeks ago, I shipped PneumoScan AI a deep learning model that analyzes chest X-ray images and detects pneumonia in seconds, with 90%+ accuracy. It's live, it's free, and anyone can use it right now.

πŸ”— pneumonia-scan-ai.netlify.app

This is the story of how I built it β€” and everything I learned along the way.


Why Pneumonia?

Pneumonia kills over 2 million people annually. A huge portion of those deaths happen in low-resource areas where radiologists are scarce and diagnosis is slow.

I'm not claiming to solve that problem. But I wanted to build something that mattered not just another MNIST classifier or iris flower predictor. Medical imaging felt real.


The Dataset

I used the Chest X-Ray Images (Pneumonia) dataset from Kaggle.

  • 5,800+ clinical chest X-ray images
  • Two classes: NORMAL and PNEUMONIA (Viral & Bacterial)
  • Real hospital data from Guangzhou Women and Children's Medical Center

One thing I learned immediately: the dataset is imbalanced. There are significantly more pneumonia images than normal ones. This is something you have to think about carefully in medical AI because a model that just predicts "pneumonia" on everything could still hit decent accuracy numbers while being completely useless.


The Architecture β€” MobileNetV2 + Custom Head

I chose MobileNetV2 as my base model for two reasons:

  1. It's lightweight (14MB) perfect for deployment on free-tier infrastructure
  2. It was pre-trained on ImageNet, so it already knows how to extract visual features

The key idea is transfer learning instead of training a CNN from scratch on 5,800 images (which isn't enough), I used MobileNetV2 as a frozen feature extractor and added my own classification head on top.

Here's the full pipeline:

Input (any size RGB image)
    ↓
Resizing Layer      β†’ 224 Γ— 224 (built into model)
Rescaling Layer     β†’ pixel values Γ· 255
    ↓
MobileNetV2         β†’ FROZEN (trainable = False)
16 inverted residual blocks
Final feature map: 7 Γ— 7 Γ— 1280
    ↓
GlobalAveragePooling2D  β†’ collapses to 1280 values
    ↓
Dense(128, ReLU)        β†’ learns pneumonia-specific patterns
    ↓
Dropout(0.3)            β†’ prevents overfitting
    ↓
Dense(1, Sigmoid)       β†’ outputs probability (0 = Normal, 1 = Pneumonia)
Enter fullscreen mode Exit fullscreen mode

Why freeze MobileNetV2?

Because it already knows how to see. The ImageNet weights encode knowledge about edges, textures, and shapes that transfer surprisingly well to X-rays. Fine-tuning all those layers on a small dataset would just cause overfitting.

Why add Dense(128) before the output?

The raw GlobalAveragePooling output is 1280 features most of which are irrelevant to pneumonia. The Dense(128) layer acts as a bottleneck, forcing the model to compress what it learned into the most useful 128 features for this specific task.

Training config:

  • Optimizer: Adam (lr=0.001)
  • Loss: Binary Crossentropy
  • Final accuracy: 90%+

The Problem I Didn't Expect The Dog Test

After I deployed the first version, I decided to test it.

I uploaded a photo of a dog sitting in a bathroom smoking a cigarette.

The result?

{
  "Result": "PNEUMONIA DETECTED",
  "Confidence Score": "100.00%"
}
Enter fullscreen mode Exit fullscreen mode

100% confident. That dog had pneumonia.

This is a classic failure mode of CNNs the model has no concept of "this isn't even an X-ray." It was trained only on X-rays, so it forced every single image into one of two buckets regardless of what it actually was.

I needed an input validation layer.


The Fix Saturation Gate

The solution I came up with is what I call a Saturation Gate.

The logic is simple: chest X-rays are grayscale. Any real X-ray will have near-zero color saturation. A photo of a dog, a selfie, a meme these all have high saturation.

So before the image ever reaches the model, I convert it to HSV color space and measure the mean saturation value. If it exceeds a threshold.

The dog photo gets rejected now. The model only sees what it was trained to see.


The Deployment Stack

I wanted this to be completely free to host β€” no cloud bills, no server management.

Here's what I ended up with:

Layer Technology
Model Inference Hugging Face Spaces (Gradio + TensorFlow-CPU)
Frontend Netlify (custom Tailwind CSS portal)
Bridge iframe

The Gradio app on Hugging Face handles all the heavy lifting loading the .h5 model, running inference, returning JSON results. The Netlify frontend is just a clean portal that embeds the Gradio Space via iframe.

Total hosting cost: $0.


What I Learned

1. Accuracy alone is a lie in medical AI.
A model can hit 90% accuracy while still missing dangerous cases. Always look at your confusion matrix. False negatives missed pneumonia cases β€” are the ones that matter most.

2. Input validation is not optional.
Any model you deploy in the real world needs to handle unexpected inputs gracefully. The Saturation Gate wasn't in any tutorial I followed. I had to think of it myself after seeing the model fail in a funny but revealing way.

3. Transfer learning is magic for small datasets.
If you're working with fewer than 50,000 images, you almost certainly shouldn't be training a CNN from scratch. Use pretrained weights. Freeze the base. Train only the head.

4. Ship first, improve later.
The first version was broken in obvious ways. But shipping it is what revealed those problems. The dog test only happened because I deployed it.


What's Next

  • Grad-CAM heatmap overlays showing which part of the X-ray triggered the detection. This is the standard in medical AI and would make the tool genuinely useful for educational purposes. Precision/Recall analysis properly evaluating false negative rate on the test set
  • More pathologies tuberculosis, pleural effusion, cardiomegaly

The Repo

The trained model is open source. You can download it, use it, build on it.

github.com/GradienNinja/PneumoScan-AI


Final Thought

I fix cars for a living. I don't have a degree. I didn't take a course that handed me this project.

I just built it.

If you're self-taught and reading this wondering whether you're "ready" to build something real you're not going to get ready by waiting. You get ready by shipping.


Built by Sheikh Sadi Asif β€” @GradienNinja | AstroLabSoft AI Lab

  • Disclaimer: This is a research and educational project. Not a certified medical device. Always consult a qualified healthcare professional for medical decisions.*

Top comments (0)