← portfolio.os/ projects / brain_tumor_detection

project / brain_tumor_detection

Brain Tumor Detection Model

shipped
AI/MLData Analytics

Overview

A deep learning pipeline that classifies MRI slices as glioma vs. healthy, trained on the UCSF-PDGM collection from The Cancer Imaging Archive (TCIA). The project isn't just a classifier: it also produces Grad-CAM heatmaps so a prediction comes with a visual explanation of which region of the brain drove it, rather than a bare label.

Problem

Tumor screening from MRI is slow and reviewer-dependent, and a black-box classifier that just outputs "glioma" or "healthy" isn't trustworthy enough to reason about. There's no way to check why the model thinks what it thinks. The goal was a model that's both accurate and inspectable.

What I built

  • A CNN image classifier trained on central MRI slices extracted from DICOM/NIfTI volumes (data_prep.py, make_dataset.py)
  • A training pipeline with data augmentation for robustness and automatic best-checkpoint saving (train.py, model.py)
  • An evaluation pass reporting precision, recall, F1, and a precision-recall curve (evaluate.py)
  • Grad-CAM heatmap generation to visualize which brain regions influenced each prediction (grad_cam.py)

Architecture

Raw MRI volumes (DICOM/NIfTI) go through data_prep.py, which extracts central slices and converts them into NumPy arrays (X.npy/y.npy). train.py loads those arrays, does a stratified 70/15/15 train/val/test split, and fits a CNN:

  • 3 conv blocks (32 → 64 → 128 filters, 3×3, ReLU, same padding) each followed by 2×2 max-pooling, over a single-channel 224×224 input
  • Flatten → Dense(128, ReLU) → Dropout(0.5) → Dense(1, sigmoid) for binary glioma-vs-healthy output
  • Adam optimizer (lr 1e-4), binary crossentropy, trained up to 40 epochs with EarlyStopping (patience 5 on val_loss) and ModelCheckpoint saving the best weights to models/best_model.h5
  • Light augmentation on the training set only: ±10° rotation, 5% width/height shift, 10% zoom, horizontal flip; batch size 16

evaluate.py and grad_cam.py both load a saved checkpoint. One scores it against the held-out test split, and the other renders heatmaps for individual predictions.

architecture diagram
Architecture diagram of Brain Tumor Detection Model, generated by GitDiagram

generated by gitdiagram · click to explore interactively ↗ · full size

Decisions

  • Central-slice extraction over full 3D volumes: trades some spatial context for a much smaller, faster-to-train 2D CNN (single-channel 224×224 input) than a volumetric model would need, which mattered more for a first working version than squeezing out volumetric accuracy.
  • A deliberately simple 3-block CNN over a pretrained backbone: kept the model small and fast to iterate on for a first version, at the cost of whatever accuracy a transfer-learned backbone might have added.
  • Grad-CAM instead of a plain classifier: treats interpretability as a first-class requirement rather than an afterthought, since a medical-imaging prediction without a "why" is hard to trust or debug.

Results

A complete pipeline from raw MRI volumes to a trained classifier: evaluation reports precision, recall, F1-score and a precision-recall curve on the held-out 15% test split, and Grad-CAM renders a heatmap for any individual prediction.

What I learned

  • Interpretability tooling (Grad-CAM) is worth building alongside the model, not bolted on after. It changed how I debugged misclassifications during training.
  • Medical imaging formats (DICOM/NIfTI) need real preprocessing discipline before they're usable as flat training arrays: reading two volume formats, picking the central slice and producing consistent single-channel 224×224 inputs all happen before the model sees anything.
  • Start simple, then scale. A 3-block CNN on central 2D slices got a working end-to-end pipeline quickly, and it gives a baseline that 3D or pretrained models have to beat.
  • Precision and recall say more than accuracy in medical screening, where a missed tumor and a false alarm have very different costs.
  • A stratified split keeps the glioma-to-healthy ratio the same across train, validation and test, so the test metrics reflect the real class balance.

What's next

  • Multi-modal MRI input (T1, T1-CE, T2, FLAIR) instead of a single sequence
  • 3D CNN models trained on full MRI volumes instead of central slices
  • Clinical label prediction (e.g. IDH mutation status)