How it works
Pipeline
One request, one process, one pass. Every stage is timed and the timings are returned with the result — the latency you see in the interface is measured, not quoted.
| Stage | What it does | Can it fail alone? |
|---|---|---|
| Quality gate | Focus, illumination, field of view | A failure stops everything — an ungradeable image is never graded |
| Preprocess | Retina crop, Ben-Graham, CLAHE on green | Identical code at train and inference time |
| Grade | EfficientNetV2-S + CORAL ordinal head | Yes — lesions and rules still return without it |
| Grad-CAM | Attention for the referral decision | Yes — the grade still returns |
| Lesion CV | Microaneurysms, haemorrhages, exudates by quadrant | Yes — the grade still returns |
| ICDR rules | Independent grade from the clinical criteria | Needs lesion counts only, not the network |
| Report | One-page PDF for sign-off | Yes — the result is still valid |
Nothing in this chain can turn a partial result into no result. If the model is absent the interface says so and still returns the lesion evidence and the rule-based grade.
Design decisions
An ordinal head, not a 5-way softmax
The ICDR grades are ordered. Ordinary classification treats "predicted 0, actually 4" and "predicted 3, actually 4" as equally wrong — clinically the first could blind someone and the second is a rounding error. We use a CORAL ordinal head that answers four cumulative questions instead: is it worse than 0, than 1, than 2, than 3? The grade is how many are yes.
Why it matters: The second question is exactly the referral decision. P(grade ≥ 2) is a direct model output with its own threshold, so hitting a sensitivity target is a dial we turn, not a hope.
Sharpness measured relative to the image's own contrast
The textbook focus measure is variance of the Laplacian. It conflates blurry with smooth: a healthy retina has few lesions and therefore few edges, and scored as blurrier than a diseased out-of-focus one.
Why it matters: Left alone, the gate would have quietly rejected healthy eyes and biased every prevalence number downstream. We normalise gradient energy by the retina's own contrast, and a test pins the ordering.
Vessel suppression only on elongated structures
Vessels and dark lesions are indistinguishable to an intensity threshold, so the lesion detector suppresses vessels first. But at small scales a microaneurysm — a two-pixel dark dot — is itself a perfectly good ridge, and naive suppression deleted every one of them.
Why it matters: We raised the filter's minimum scale and restricted suppression to components that are genuinely long and thin. A compact blob of vessel-like response is far more likely to be the lesion we are counting.
The model runs inside our own server
Grad-CAM needs the gradient of an output with respect to an intermediate activation. A managed prediction endpoint returns the answer tensor and nothing else.
Why it matters: The explainability requirement makes the fashionable architecture impossible. We load the model in-process instead — which also happens to be cheaper.
Confidence is calibrated, and we show the correction
A raw network score is not a probability; modern networks are systematically overconfident. Temperature scaling fits a single scalar on validation data to correct it.
Why it matters: One parameter, so it cannot overfit, and it changes no ranking — accuracy and thresholds are mathematically untouched. It only makes the number honest. We report calibration error before and after.
Two independent opinions, and disagreements are surfaced
Separately from the network, classical computer vision counts lesions per quadrant and applies the published ICDR criteria — including the 4-2-1 rule, which is why we locate the optic disc and fovea at all.
Why it matters: When the two disagree we escalate to clinician review rather than averaging them. A false alarm costs one appointment; a missed grade 3 costs sight.
Where MATLAB stays
The problem statement asks for a MATLAB-based pipeline and we built a hybrid, so this is stated up front rather than waited on.
- The Simulink workflow model. A discrete-event resource study is genuinely what SimEvents is for, and it needs no GPU.
- The classical CV baseline. Top-hat exudates, extended-minima microaneurysms, matched-filter vessels, SVM grading — on identical splits.
That second one turns a constraint into a scoring advantage: the problem statement asks for proof the integrated pipeline beats any single technique, and the classical pipeline is that single technique. It is the control arm, not an omission.
- No GPU in the room. Two hours on a rented T4 against over a day on a laptop CPU.
- Toolbox risk. Six toolboxes are named; a licence gap found at hour 20 is fatal.
- Deployability. "Deployment in primary healthcare centres" is not answered by a model on a developer's laptop.
- Reproducibility. Versioned, re-runnable jobs are what clinical validation rigour means in practice.