For my college project, I built a Convolutional Neural Network that detects pneumonia from chest X-ray images. It sounds complex (and the math behind it is), but the core idea is surprisingly intuitive. Here's what I learned building it from scratch.
The Problem
Pneumonia kills over 2.5 million people annually. Early diagnosis significantly improves outcomes, but reading X-rays requires trained radiologists, and there aren't enough of them, especially in developing countries. An AI model that can flag potential pneumonia cases could help doctors prioritize urgent cases and catch what the human eye might miss.
What Are Convolutional Neural Networks?
Think of how you recognize a face. You don't look at individual pixels. You recognize features: edges, shapes, textures, and patterns. Your brain processes these features hierarchically, from simple edges to complex structures like eyes and noses, to the full face.
CNNs work the same way:
Convolutional Layers act as feature detectors. Early layers detect simple patterns like edges and gradients. Deeper layers combine these into complex features like lung boundaries, tissue density variations, and fluid patterns.
Pooling Layers reduce the spatial dimensions while keeping the important features. Think of it as zooming out: you lose some detail but gain a broader view.
Fully Connected Layers at the end take all the extracted features and make a classification decision: pneumonia or healthy.
Building the Model
Here's the practical journey:
Dataset
I used the Chest X-Ray Images dataset from Kaggle, containing over 5,800 X-ray images split into "NORMAL" and "PNEUMONIA" categories. The dataset was already split into training, validation, and test sets.
Preprocessing
Raw X-ray images come in different sizes and orientations. I standardized everything:
- Resized all images to 224x224 pixels
- Normalized pixel values to 0-1 range
- Applied data augmentation (rotation, flipping, brightness adjustment) to prevent overfitting
Architecture
After experimenting with several architectures, I settled on a model inspired by VGG16 but simplified:
- 4 convolutional blocks, each with two Conv2D layers + BatchNorm + MaxPooling
- Dropout layers (0.25 after conv blocks, 0.5 before the final layer) to prevent overfitting
- Final dense layer with sigmoid activation for binary classification
Training
Training was done using TensorFlow and PyTorch (I experimented with both). Key decisions:
Optimizer: Adam with learning rate 0.0001
Loss function: Binary cross-entropy
Batch size: 32
Epochs: 30 with early stopping
The model converged around epoch 20, reaching 92% accuracy on the validation set.
Results and What They Mean
On the test set:
Accuracy: 91.3%
Sensitivity (Recall): 95.2% for pneumonia cases
Specificity: 86.1% for normal cases
The high sensitivity is crucial. In medical applications, it's better to have false positives (flagging a healthy X-ray as potentially pneumonia) than false negatives (missing actual pneumonia). A false positive means an additional review by a doctor. A false negative could mean a missed diagnosis.
What I Learned
1. Data quality matters more than model complexity. My fancy deep architecture performed worse than a simpler model trained on well-preprocessed data. Garbage in, garbage out applies to deep learning just as much as traditional programming.
2. Transfer learning is incredibly powerful. I also tried fine-tuning a pre-trained ResNet50, and it outperformed my custom model with much less training time. Standing on the shoulders of giants isn't cheating; it's smart engineering.
3. Medical AI has unique constraints. You can't just optimize for accuracy. You need to consider sensitivity vs. specificity tradeoffs, explainability (doctors need to understand why the model flagged something), and real-world deployment conditions (different X-ray machines produce different image qualities).
4. The gap between "model works on my laptop" and "model is deployed in a hospital" is enormous. Regulatory requirements, integration with existing medical systems, edge cases, and reliability requirements make deployment a much harder problem than model building.
The Bigger Picture
This project gave me a deep appreciation for how AI is transforming healthcare. We're not replacing doctors; we're building tools that help them work faster and more accurately. A CNN that can scan 1,000 X-rays in the time it takes a radiologist to review 10 isn't competition. It's a force multiplier.
The tools are accessible. TensorFlow, PyTorch, and OpenCV are free. Datasets are publicly available. Cloud GPUs are affordable. If you're a student interested in AI for healthcare, there's never been a better time to start building.
Tools Used
For anyone who wants to replicate or build on this work:
Python as the primary language
TensorFlow and PyTorch for model building and training
OpenCV and Pillow for image preprocessing
NumPy and Pandas for data manipulation
Matplotlib for visualization and results analysis
The full code is available on my GitHub. Feel free to fork, improve, and build upon it.