Try it: live demo (the six held-out examples are pre-computed; uploading your own photo uses Hugging Face's free GPU allowance, larger if you are signed in to Hugging Face) · code and every number · video. Runs locally on a CPU with make app.

Inspiration

Diabetic retinopathy steals sight quietly. In Malaysia, 15.6% of adults have diabetes (NHMS 2023), and in one Kuala Lumpur study 29.2% of people with type 2 diabetes already had retinopathy at their first eye examination. Screening works, but every photo needs a trained grader, and graders are scarce.

AI grading papers often report 0.95+ AUROC. We wanted to know what happens when you check that number properly, and to build a tool that is honest about where it fails.

The problem and who has it

Who: the nurse or medical assistant running a fundus camera at a primary-care clinic, deciding which photos need an ophthalmologist. And the person with diabetes waiting for that decision.

Problem: too many photos for too few graders, and AI models whose published accuracy does not survive a new hospital, camera or country.

What it does

RetinaGate takes one colour fundus photo and gives one of four answers in under a second on a CPU:

  • Refer to an eye specialist
  • No referable retinopathy found
  • Retake the photo (too poor to grade)
  • Uncertain: send to a human grader

Each answer shows how often that answer was right on a test set the model never saw. The app also shows an exact score map: the score is a mean of per-region scores, so the map is a decomposition of what the model computed, not a post-hoc estimate.

Method

  1. Preprocess: crop to the retinal disc, square, 448 px, JPEG q93. The same function runs on every dataset and every upload.
  2. Frozen features: ConvNeXt-Tiny pretrained on ImageNet only, so no retinal image leaked into pretraining. It was chosen on validation over EfficientNet-B0, DINOv2-small and a 45-feature handcrafted baseline.
  3. Two logistic heads: a gate for "ungradable" and a disease head for referable DR (ICDR grade 2+), Platt-calibrated.
  4. Thresholds come from targets written down before any data was seen: clear below 97% sensitivity, refer above 95% specificity, retake above 95% gradable retention. Everything between goes to a human.
  5. Freezing: every model and threshold is frozen and committed to git before the test set it is judged on is scored. The commit history is the audit trail.

Evaluation

Metric: referable-DR AUROC, plus sensitivity, specificity, and missed referable eyes and unnecessary referrals per 1,000 photos screened, for a plain threshold and for the full four-way policy. Also expected calibration error. All with 95% bootstrap CIs (2,000 resamples).

How it was measured, in three rounds:

Round 1 (v1). Trained on DDR (China). Tuned and frozen on DDR validation, where AUROC was 0.973. Then scored once on held-out DDR test (0.892), APTOS 2019 India (0.971) and IDRiD India (0.945). The ranking travelled, but the frozen thresholds did not. On DDR test the model missed 85 referable eyes per 1,000; on the Indian sets specificity fell to about 50%.

Round 2 (diagnosis). Why did validation say 0.973 and test 0.892? In DDR's train and validation splits, every timestamp-named photo is grade 0 and all disease sits in the other file series. A linear probe recognises the image source with 88.7% accuracy. The model could pass validation by recognising the series; within one series, validation AUROC was only 0.833. On DDR test, where that series does contain disease, sensitivity on it fell to 41%. Near-duplicates were ruled out (0.3%).

Round 3 (v2, untouched test). I wrote a protocol and committed it, then trained and froze v2, before downloading a fourth dataset. v2 removes the single-class series and trains on DDR, APTOS and IDRiD. The test set is 2,999 EyePACS photos from the USA, one eye per patient, drawn by patient ID with seed 0.

v2 on untouched EyePACS Result
AUROC 0.797 (0.776–0.819)
Plain threshold: sensitivity / specificity 70.2% / 75.1%
Plain threshold: missed referable per 1,000 56
Four-way policy: missed referable per 1,000 21
Four-way: retake · human grader · auto-refer · auto-clear (per 1,000) 75 · 401 · 121 · 402
Severe or proliferative eyes auto-cleared 4 of 126
Recalibrated on 200 local photos: sensitivity / specificity (median of 200 draws) 89.5% / 44.7%

Exploratory robustness: re-saving a photo as JPEG at quality 70 changed 33.5% of decisions, almost all to or from "human grader" (0.2% straight between refer and clear).

What it means: abstaining cuts missed referable eyes from 56 to 21 per 1,000, at the cost of sending 40% of photos to a human. But a frozen-feature model trained on Asian photos is not accurate enough on US photos to replace graders. Local recalibration does not fix it, because the model's ranking is the bottleneck, not the thresholds.

Who benefits, and who could be harmed

Benefits: graders, who could see the uncertain and referable photos first; and patients in clinics without an on-site grader, if a future version clears a proper local validation.

Could be harmed:

  • People with sight-threatening disease who are falsely cleared. That is the worst error, so misses are reported per 1,000 and by grade.
  • Eye clinics flooded by false referrals.
  • Populations and cameras never tested: Malaysian patients, handheld or smartphone cameras, children.
  • Anyone whose photo was compressed, for example sent over a messaging app.
  • People with other eye disease, because "no referable retinopathy" says nothing about glaucoma or anything else.

Limitations

  • Research prototype; not a medical device; no prospective validation.
  • Single image-level labels from each dataset's own graders, with known noise.
  • DDR's split is by image, not patient, and is confounded by image series.
  • The gate can only be measured on DDR, the only dataset with "ungradable" labels.
  • Frozen features plus linear heads trade accuracy for speed, CPU reproducibility and an exact explanation.

What I'd do with another month

  1. Fine-tune the backbone with heavy compression and colour augmentation, with leave-one-country-out validation.
  2. Test on handheld-camera photos (mBRSET, Brazil; it needs PhysioNet credentialing).
  3. Partner with a Malaysian primary-care clinic to grade around 500 local photos for a real local validation.
  4. Add macular oedema detection, and track per-camera calibration drift.

Built with

Python, PyTorch, timm, Hugging Face Transformers and Hub, scikit-learn, OpenCV, Gradio, matplotlib. Reproducible on a 2-core CPU with make all.

Attribution

  • DDR / OIA-DDR: Li et al., Information Sciences 501 (2019), CC BY-NC-SA 4.0
  • APTOS 2019 Blindness Detection: Kaggle / Asia Pacific Tele-Ophthalmology Society, non-commercial research use
  • IDRiD: Porwal et al., Data 3(3):25 (2018), CC BY 4.0
  • EyePACS: Kaggle Diabetic Retinopathy Detection (2015), non-commercial research use; test only, not redistributed
  • Backbones: ConvNeXt-Tiny and EfficientNet-B0 (timm, Apache 2.0); DINOv2-small (Meta, Apache 2.0)

Full credits are in the README.

AI tools disclosure: Claude (Anthropic) helped write code, documentation and the video script. I reviewed and ran all code and can explain every function; every number comes from scripts in the repo.

Built With

Share this project:

Updates

Submission history