When a disease-grading model moves to a new hospital, its accuracy drops, and so does its knowledge of when it is wrong. Diabetic retinopathy is graded on five ordered stages, and the costly mistakes cross one boundary in particular: the line between "watch" and "refer to a specialist." A model calls a moderate case mild with high confidence because the new camera renders lesions differently. We propose replacing the usual softmax classifier on top of a domain-generalized image encoder with a small normalizing flow. A normalizing flow is an invertible neural network that maps features to a simple reference distribution and therefore gives an exact probability density for every input. We make that reference distribution ordinal: each grade is a Gaussian placed in order along a one-dimensional severity axis, and the remaining feature dimensions are absorbed as nuisance variation.
This one head lets us do three things at an unseen hospital, using only that hospital's unlabeled images. We can re-estimate how common each grade is there. We can adapt the feature-to-severity mapping by maximum likelihood without letting grades collapse onto each other. And we can refer cases to clinicians either when they fall outside the density the model has seen or when their probability mass straddles the referable boundary. The claim is narrow and testable. Beyond the gains a strong encoder already provides, an ordered generative head improves rare-grade recall and severity-weighted referral safety under domain shift, and these gains depend on the head being both ordered and more flexible than a Gaussian.
Where this sits
Three strands of prior work meet here, and none of them does this. The first strand motivates the proposal. Multi-center retinopathy grading has been attacked with heuristic artifact augmentation and re-balancing (GDRNet), frequency-channel style swapping (CauDR), diffusion-synthesized rare cases plus a loss that pulls same-grade features together across hospitals (DG-ADR), and a state-space encoder whose features are clustered into severity bases by expectation-maximization over a Gaussian mixture (Samba). All four are discriminative at deployment. None gives a density, adapts to the target hospital's grade prevalence, or abstains. Samba is the closest relative because it already uses severity-indexed mixture components. Its authors report, however, that these components degrade for rare severe grades, and the components are unordered.
The second strand supplies the method. MapFlow uses an invertible flow to transport target features onto a source latent. It argues that forcing exact invariance between domains erases the fine distinctions classes need. Gradual Domain Adaptation via Normalizing Flows anchors each class to a Gaussian-mixture component and pulls unlabeled domains toward it by likelihood. Flow-MIL places per-class mixture prototypes inside a flow over pathology patch embeddings. We reuse the flow-on-features design and the likelihood-based transport. We differ in three ways. Our base distribution is ordered rather than a set of unrelated class modes. We model label shift explicitly, whereas MapFlow assumes balanced classes and pure covariate shift. And we evaluate on clinical grading with referral rather than on digits, office objects, or in-domain slides. The second strand also constrains the design. Flows on raw images are memory-bound because they cannot downsample (cINN domain transfer), and continuous-time flows scale badly with dimension. So the flow operates only on a projected feature vector.
The third strand defines the safety target. Avoiding Catastrophic Referral Failures shows that standard and Bayesian networks become confidently wrong under shift: out-of-domain negatives grow a long logit tail into the positive region. To keep the analysis tractable, that paper binarized retinopathy, and its best fixes need a held-out out-of-domain validation set. We keep all five grades and set every threshold on source data only. INPUTADAPTER argues for adapting the incoming data while keeping the regulated predictor frozen. Our target-side adapter follows that philosophy.
Core Claim
Let $h(x)\in\mathbb{R}^d$ be the features of a fixed, domain-generalized encoder. We use the GDRNet-trained backbone, so the head is the only thing that changes. Let $f_\theta$ be an invertible flow mapping $h(x)$ to $z=(s,u)$, where $s$ is a scalar severity coordinate and $u\in\mathbb{R}^{d-1}$ captures nuisance variation. The grade-conditional density is
$$ p(x\mid g) = \mathcal{N}(s;\mu_g,\sigma_g^2)\,\mathcal{N}(u;0,I)\,\left|\det \tfrac{\partial f_\theta}{\partial h}\right|,\qquad \mu_0<\mu_1<\dots<\mu_4, $$
with the grade means constrained to be increasing. The grade posterior is $p(g\mid x)\propto \pi_g\, p(x\mid g)$, where $\pi_g$ is the prevalence of grade $g$ at a given site.
The flow can bend and split each grade's region in feature space. This is needed because within-grade lesion presentations are heterogeneous, which is exactly the "high within-level discrepancy" Samba documents. Adjacency between grades, however, is fixed by the base distribution.
At an unseen site we take its unlabeled features $\{h(x_j)\}$ and run an alternating procedure. One step updates $\pi^T$, the target-site prevalences, by expectation-maximization. The other step fits a small adapter flow $a_\phi$, initialized to the identity and composed in front of the frozen source flow $f_\theta$:
$$ \max_{\phi,\ \pi^T}\ \sum_j \log \sum_{g} \pi^T_g\, p\big(a_\phi(h(x_j))\mid g\big)\;-\;\lambda\,\|\phi\|^2 . $$
The source flow and the ordered base stay frozen. The adapter can therefore move target features toward the source density, but it cannot reorder or merge grades.
Referral follows two rules. A case is referred as out of support when $\log p(x)$ falls below a quantile calibrated on held-out source data. A case is referred as boundary-ambiguous when the posterior probability on the opposite side of the referable line (grade 2 or higher versus grade 1 or lower) exceeds a threshold $\tau$, also set on source data.
Research Program
The work has four parts. First, we build the head and verify on source data that it matches softmax accuracy. If it does not, everything downstream is confounded. The head uses roughly six to eight affine-coupling layers on a 64-dimensional projection of the backbone features.
Second, we test whether ordering helps, with no adaptation at all. We compare the ordinal base against a categorical base, meaning five unordered Gaussian modes in the style of MapFlow and gradual-domain-adaptation anchors. The comparison measures severe-grade recall and how far off misclassifications land, that is, whether errors stay at adjacent grades.
Third, we study target-side adaptation. We measure prevalence-estimation error against the true grade counts of each held-out site, and how much prevalence correction and the adapter each contribute.
Fourth, we evaluate referral using a severity-weighted risk–coverage curve. Errors that cross the referable boundary are weighted more heavily than adjacent-grade errors on the same side. We also report the rate of confident referable misses: grade-2-or-higher cases that are accepted, not referred, and predicted at grade 1 or lower.
A secondary check repeats the experiment on Samba's two-hospital fatigue-fracture X-ray grading. This tests whether the effect generalizes beyond fundus cameras.
Baselines
The naive reference point is Empirical Risk Minimization (ERM): a ResNet50 with a softmax head, trained on pooled source domains, using maximum softmax probability for referral. GDRNet reports this baseline at 75.9% AUC on the GDRBench leave-one-domain-out test. We rerun it under our splits rather than copying that figure.
The strongest existing grading method is GDRNet itself. It reports 82.6% average AUC and 43.5% F1 on the GDRBench DG test, and 73.4% AUC in the single-source extreme test. It is the right competitor because it already targets style shift and grade imbalance across eight datasets. Our head sits on its frozen backbone, so any gain is attributable to the head alone. Beating it means a statistically reliable F1 gain across held-out domains, since AUC is less sensitive to rare-grade errors. It also means equal or better AUC and lower severity-weighted risk at matched coverage.
On the 4DR benchmark, the comparison point is CauDR's reported 61.82% average accuracy. DG-ADR reports 56.4% accuracy, 81.9% AUC, and 43.2% F1 on its seven-dataset protocol. We cite both as context but only claim wins under protocols we reproduce.
The strongest referral competitor is Split Platt Scaling and Shifted Split Referral from the referral-failures paper. We deliberately give these methods their advantage: access to a labeled out-of-domain validation split. Our method gets none. On EyePACS→APTOS the file has no single scalar result. There is no reported result found on Lacuna for multi-grade referral under shift, so we generate that baseline ourselves on identical splits.
Two ablations isolate the key ingredients, each run under identical features, adaptation budget, and hyperparameter search. The first is a zero-layer flow, which reduces the head to ordered Gaussians with a learned linear map; target adaptation then becomes affine. The second is the full flow with a categorical rather than ordinal base. If the zero-layer version matches the full flow, the claim that a flexible density is needed fails.
Why not just…?
Why not just fit class-conditional Gaussians on the features and use Mahalanobis distance for both classification and referral? This is the most serious threat, and it is built in as the zero-layer ablation. Our reason to expect a gap is that grades are internally heterogeneous. A single Gaussian per grade must be broad enough to cover all presentations of that grade. That makes the out-of-support test lax and the boundary posteriors smeared. Samba's own report that its Gaussian-mixture severity bases fail on rare severe grades points the same way. If the ablation matches the flow on held-out sites, we will report that the ordered generative head, not the flow, carries the benefit.
Why not just recalibrate the softmax post hoc, as the referral-failures paper does? Those methods need labeled out-of-domain data to fit their shift parameters, which a new hospital rarely has. They also cannot re-estimate grade prevalence, and a pure confidence rescaling cannot tell apart "unfamiliar image" from "between two grades." The comparison above grants them the labeled out-of-domain split, so a tie would still favor the unsupervised method in practice.
Why not just adapt the classifier at test time by entropy minimization or pseudo-labeling? Under label shift these objectives push predictions toward the majority grades. The rare severe stages are the ones whose under-prediction matters, and the gradual-adaptation paper reports that discrete pseudo-labeling collapses from accumulated error across large gaps. We include an entropy-minimization adapter under the same budget. The decisive comparison is severe-grade recall after adaptation.
Why not just synthesize more rare cases with diffusion, as DG-ADR does? This is complementary rather than competing. It improves the encoder, while our method changes the head and deployment-time behavior. DG-ADR's authors also note that too much synthetic data makes the classifier overfit to generator artifacts. We do not claim to replace it, and if compute allows, we will stack the head on a DG-ADR backbone.
Decisive Checks
Three results would confirm the proposal. The ordinal flow head beats both the zero-layer and categorical ablations on held-out-domain F1 and severe-grade recall. Its unsupervised prevalence estimates track the true site prevalences more closely than the source prior. And its confident referable-miss rate at 80% coverage is lower than that of Split Platt Scaling with oracle out-of-domain validation.
Three results would refute or narrow it. If the zero-layer ablation ties the full flow, the claim narrows to "ordered generative heads," not flows. If adaptation lowers severe-grade recall, the likelihood objective is collapsing grades despite the frozen base. If out-of-support referral catches no more boundary-crossing errors than softmax entropy, density is not the right signal at this feature level.
References
- Towards Generalizable Diabetic Retinopathy Grading in Unseen Domains
- CauDR: A causality-inspired domain generalization framework for fundus-based diabetic retinopathy grading
- Divergent Domains, Convergent Grading: Enhancing Generalization in Diabetic Retinopathy Grading
- Samba: Severity-aware Recurrent Modeling for Cross-domain Medical Image Grading
- MapFlow: latent transition via normalizing flow for unsupervised domain adaptation
- Gradual Domain Adaptation via Normalizing Flows
- Flow-MIL: Constructing Highly-expressive Latent Feature Space For Whole Slide Image Classification Using Normalizing Flow
- Unsupervised Domain Transfer with Conditional Invertible Neural Networks
- Avoiding Catastrophic Referral Failures In Medical Images Under Domain Shift
- Adapt the Data, Not the Model: Input-Space Adaptation for Frozen Time-Series Predictors