Skip to content
All work

Case studyIndependent project

Diabetic retinopathy screening with Grad-CAM

A ResNet-18 transfer-learning model that screens retinal fundus images for diabetic retinopathy, served through a Flask app with a confidence score and a Grad-CAM heatmap, and audited honestly afterwards.

VALIDATION ACCURACY · 20 EPOCHS93%95%97%besttestedTEST · n = 231103101117DRNO DR95.2% ACCURACY
Plotted from the notebook’s saved outputs: validation accuracy per epoch and the test-set confusion matrix.
My role
Sole developer: data pipeline, training, evaluation, Grad-CAM and Flask app
Period
2025
Type
Independent project
Reading depth

View repository (opens in a new tab)

The 30-second version

Problem
Diabetic retinopathy screening depends on specialists reading fundus photographs; flagging likely cases can help prioritise review, but only if the model is transparent.
My contribution
Independent project: I built the training notebook, the evaluation, the Grad-CAM implementation and the Flask app, and later audited my own results.
Approach
Frozen ImageNet ResNet-18 with a new two-class head, trained on a public Kaggle dataset with its own train/valid/test split; Grad-CAM via forward and backward hooks.
Results
95.2% accuracy (220/231) on the untouched test set, 91.2% sensitivity, 99.2% specificity, for the final-epoch model. The audit found a checkpoint bug and edge-focused heatmaps.
Stack
  • PyTorch
  • torchvision
  • ResNet-18
  • Grad-CAM
  • OpenCV
  • Flask

Problem

Diabetic retinopathy damages the blood vessels of the retina and is a leading cause of preventable vision loss. Screening means looking at retinal fundus photographs, and the bottleneck is specialist time. A model that flags likely cases can help prioritise which images a clinician reviews first, but only if it is transparent about why it flagged an image.

Objective

Build a complete, explainable screening prototype: a binary classifier (DR / No DR), an honest evaluation on held-out data, and a web application that returns a prediction, a confidence score and a visual explanation.

Dataset and preprocessing

The model uses the public Diagnosis of Diabetic Retinopathy dataset from Kaggle, which ships with separate training, validation and test folders. Keeping the provided test split untouched until the end is what makes the final number meaningful.

  • Images resized to 224 × 224 pixels and normalised with ImageNet statistics, matching the pretrained backbone.
  • Random horizontal flips on the training set only; validation and test images are never augmented.
  • The held-out test set contains 231 images: 113 DR and 118 No DR, so it is close to balanced.

Architecture

Screening pipeline · select a stage

Stage 1 of 6 · Input

Fundus image upload

A retinal photograph is uploaded through the web app by drag-and-drop or the file picker.

  • Animated loading state while the model runs
  • Error handling with flash messages

Stage 2 of 6 · Preprocess

Resize and normalise

The image is resized to 224 × 224 and normalised with ImageNet mean and standard deviation, exactly as during training.

  • Same transform as validation
  • No augmentation at inference

Stage 3 of 6 · Backbone

Frozen ResNet-18

An ImageNet-pretrained ResNet-18 extracts visual features. Its weights are frozen, so only the new head is trained.

  • Small enough for CPU inference
  • Pretrained features reduce the data needed

Stage 4 of 6 · Head

Classification head

A small fully connected head maps the 512 backbone features to the two classes.

  • Linear 512 → 256, ReLU, Dropout 0.3
  • Linear 256 → 2 (DR / No DR)

Stage 5 of 6 · Explain

Grad-CAM heatmap

Forward and backward hooks on the last convolution of ResNet-18 produce a class-activation map, overlaid on the input image.

  • Shows which regions drove the prediction
  • Returned alongside the label

Stage 6 of 6 · Output

Prediction and confidence

The app shows the predicted class, a softmax confidence bar and the Grad-CAM overlay.

  • Single request–response cycle in Flask
  • Runs on CPU-only machines

Model selection and engineering decisions

  • Transfer learning over training from scratch. The dataset is modest in size. A pretrained backbone brings general visual features and makes good accuracy reachable with far less data and compute.
  • ResNet-18 rather than a larger network. The goal included a responsive web app on ordinary hardware. ResNet-18 keeps CPU inference fast, and its final convolutional block is a natural Grad-CAM target.
  • Freeze the backbone, train only the head. With limited data this lowers the risk of overfitting and makes training quick and stable. The trade-off is that the features are never adapted to retinal images, which caps achievable accuracy.

Training and evaluation

Training used cross-entropy loss and Adam (learning rate 0.001) on the head parameters, batch size 32, for 20 epochs, tracking validation accuracy every epoch.

TrainingValidation

Accuracy per epoch

88%92%96%100%15101520epochbest val.evaluated

Loss per epoch

0.00.10.20.315101520epochbest val.evaluated
View data table
EpochTrain lossTrain accVal lossVal acc
10.275289.16%0.147494.35%
20.186793.45%0.155693.60%
30.152794.99%0.122595.48%
40.145495.47%0.118695.10%
50.148594.89%0.153394.16%
60.194193.11%0.118095.67%
70.134995.66%0.110596.23%
80.139295.47%0.119195.86%
90.123895.86%0.102396.23%
100.124595.91%0.177993.79%
110.126995.52%0.106496.05%
120.122796.00%0.182393.03%
130.124595.81%0.108196.05%
140.143094.75%0.113595.67%
150.126095.76%0.112196.05%
160.111396.05%0.103996.61%
170.092797.16%0.095696.99%
180.097797.11%0.116196.42%
190.096696.68%0.116396.23%
200.110396.00%0.179593.79%
Training and validation curves over 20 epochs. Validation peaked at 96.99% at epoch 17; the model that was tested is the epoch-20 model (93.79% validation).Source: Repository notebook, per-epoch training log

The checkpoint bug, and what the 95.2% really represents

The loop was meant to remember the best-validation weights and restore them before testing. This is the code, verbatim:

if phase == 'valid' and epoch_acc > best_acc:
    best_acc = epoch_acc
    best_model_wts = model.state_dict()
...
model.load_state_dict(best_model_wts)

state_dict() returns references to the model’s live tensors, not copies. Every later optimiser step (and every BatchNorm statistics update in training mode) changes the “snapshot” too, so load_state_dict(best_model_wts) restores the weights the model already has. The model evaluated on the test set is the final, epoch-20 model, whose validation accuracy was 93.79%, not the epoch-17 model at 96.99%. The 95.2% test accuracy is a genuine measurement of that final model; the epoch-17 model was never tested, so I do not claim anything about it.

The fix is one line, best_model_wts = copy.deepcopy(model.state_dict()), or saving a checkpoint file at the best epoch. It has not yet been applied and re-run; the full story is in The checkpoint bug that changed my reported model.

Results

Test accuracy
95.2%
220 of 231
Sensitivity (DR recall)
91.2%
103 of 113
Specificity
99.2%
117 of 118
Test images
231
held out
Pred. DRPred. No DRActual DR
103Correct
10Missed
Actual No DR
1False alarm
117Correct
Accuracy
95.2%
220 / 231
Sensitivity (DR recall)
91.2%
103 / 113
Specificity
99.2%
117 / 118
Precision (DR)
99.0%
103 / 104

What each cell means

True positive · 103
103 images with DR that the model flagged as DR.
False negative · 10
10 images with DR that the model labelled No DR: missed cases, the costly error in screening.
False positive · 1
1 image without DR that the model flagged as DR: a false alarm.
True negative · 117
117 images without DR that the model correctly labelled No DR.

If the same rates held at another prevalence

Illustrative arithmetic

This test set: 48.9% (113 of 231)

Precision (a positive flag is right)
92.3%
Negative predictive value
99.0%

Per 10,000 screened: about 912 correctly flagged, 88 missed, 76 false alarms.

Formulas and assumptions

Measured on this test set: sensitivity = 103 / 113 = 91.2%; specificity = 117 / 118 = 99.2%. With assumed prevalence p:

precision= sens·psens·p+(1−spec)·(1−p) NPV= spec·(1−p)spec·(1−p)+(1−sens)·p

This assumes the measured rates stay the same at other prevalences, which is an assumption, not a measurement. They come from one test set of 231 images, are not clinical performance or validated screening results, and say nothing about how the model would behave on other populations or cameras.

PrevalencePrecisionNPV
1.0%52.1%99.9%
5.0%85.0%99.5%
10.0%92.3%99.0%
20.0%96.4%97.8%
48.9% (test set)99.0%92.1%
Confusion matrix of the final-epoch model on the held-out test set. Rates are computed directly from these counts.Source: Repository notebook, confusion-matrix output

The model is conservative: it almost never raises a false alarm (1 of 118 healthy images), but it misses 10 of 113 DR cases. For screening, a missed case costs more than a false alarm, so the next version should trade some specificity for sensitivity, for example by tuning the decision threshold on the validation set.

Explainability

The Flask application after a prediction: the label 'Prediction: DR', a confidence bar at 66.48 and a Grad-CAM heatmap over the fundus image.
The Flask app after a prediction: label, confidence bar and Grad-CAM overlay.Source: Project README screenshot
A Grad-CAM overlay on a retinal fundus image. The retina appears blue because of a colour-channel bug; the red and yellow hotspot sits at the upper-left edge of the fundus.
A Grad-CAM output generated by the app (native 224 × 224).Source: Repository, static/gradcam.jpg

Before reading these images, it helps to know how they were drawn. The overlay code in model/model.py is:

heatmap = cv2.applyColorMap(cam, cv2.COLORMAP_JET)
superimposed = heatmap * 0.4 + orig

gradcam_path = "static/gradcam.jpg"
cv2.imwrite(gradcam_path, superimposed)

applyColorMap returns BGR and cv2.imwrite expects BGR, so the heatmap colours are correct (red and yellow mean high activation). But orig comes from PIL in RGB order, so the retina’s own colours are swapped: the orange fundus is rendered blue. The sum is also not clipped before saving, which washes out bright areas.

With that in mind, the reading is clear: in the saved output (the same image appears in both figures above), the hottest region sits at the edge of the fundus, not on lesions such as haemorrhages or exudates. A correct label with an explanation pointing at the border can mean the model has learned a shortcut, such as the edge of the camera’s field of view. One image is not a systematic audit, so I treat this as a warning sign rather than a conclusion. The broader discussion is in What Grad-CAM revealed about shortcut learning.

Application workflow

The Flask application wraps the trained model in a single request–response flow: upload, validate, preprocess, predict, compute Grad-CAM, render. The front end provides drag-and-drop upload, a loading state, an animated confidence bar and readable error messages. Inference runs on the CPU.

Challenges, trade-offs and lessons

  • Trusting a variable name. best_model_wts sounded right, so the restore step was never verified. A one-line assertion that the restored weights differ from the final weights would have caught it.
  • Accuracy hides the costly error. Publishing the confusion matrix makes the 10 missed DR cases visible instead of burying them in 95.2%.
  • Visualisation code is part of the model’s credibility. A channel-order slip changes what a reader sees. Explanations need the same review as metrics.
  • Speed versus adaptation. Freezing the backbone made training fast and stable but limits how well the model fits retinal images.

What I would do next

  1. Fix the checkpoint copy, re-run, and report the best-validation model alongside the final one.
  2. Fine-tune the last ResNet block, not only the head.
  3. Tune the decision threshold for sensitivity and report the ROC curve.
  4. Fix the overlay colour order and clipping, then audit Grad-CAM across the whole test set (for example, the share of activation inside the fundus versus at its edge).
  5. Validate on a second, independent fundus dataset.

Repository

The code, notebook and Flask app are on GitHub.

100%