Transformer-based out-of-distribution detection for clinically safe segmentation

Mark S Graham, Petru-Daniel Tudosiu, Paul Wright, Walter Hugo Lopez Pinaya, U Jean-Marie, Yee Mah, James Teo, Rolf H Jäger, David Werring, Parashkev Nachev, Sebastien Ourselin, M Jorge Cardoso

TL;DR

Standard uncertainty methods in medical AI often fail when they encounter "out-of-distribution" (OOD) data, leading to dangerously confident but incorrect clinical predictions. This paper proposes a novel 3D VQ-GAN and Transformer-based pipeline that explicitly estimates data likelihood to filter out anomalous scans before they reach the segmentation model, ensuring safer clinical deployment.

What Problem Does This Paper Address?

Deep learning models for medical image segmentation—such as identifying brain hemorrhages in CT scans—are typically trained on "clean," curated datasets. However, in a real-world clinic, these models face a "wild" range of inputs: scans with motion artifacts, incorrect intensity scaling, or even images of the wrong organ entirely.

The industry standard for safety is to use uncertainty estimation (like Monte Carlo Dropout or Deep Ensembles). The hope is that the model will say "I don't know" when it sees something weird. The authors demonstrate a critical flaw: these methods often fail to distinguish between difficult in-distribution cases and completely anomalous OOD data. In many cases, the models make "confidently wrong" predictions on OOD data, which could lead to catastrophic clinical errors.

Methods

The authors propose a two-stage generative modeling approach to detect OOD samples:

  1. Compression (VQ-GAN): 3D medical volumes are high-dimensional and computationally expensive. The authors use a Vector Quantized Generative Adversarial Network (VQ-GAN) to compress 3D CT volumes into a discrete, low-dimensional latent space. This turns the image into a sequence of "codebook" indices.
  2. Density Estimation (Transformer): They then train a Transformer (specifically a Performer architecture to handle long sequences efficiently) to learn the probability distribution of these latent codes. By calculating the log-likelihood of a new image's latent representation, the system can quantify how "similar" that image is to the training data.

This is the first time such a transformer-based approach has been applied to OOD detection in 3D medical imaging.

Datasets

  • CROMIS: 687 head CT scans containing Intracerebral Hemorrhages (ICH), used for training the generative models and segmentation networks.
  • KCH: 47 clinical head CTs with ICH, used as the "in-distribution" test set.
  • Corrupted KCH (Near-OOD): The authors applied 15 types of synthetic corruptions (e.g., Gaussian noise, skull-stripping, flipping, intensity scaling) to the KCH scans to simulate common clinical data errors.
  • Medical Decathlon (Far-OOD): A collection of medical images covering different organs (liver, lung, heart) and modalities (MR, CT), representing data that should never be processed by a head CT segmentation model.

Key Insights and Ideas

  • Uncertainty is not a proxy for OOD detection: Standard methods like Deep Ensembles and Dropout show significant overlap in confidence scores between valid head CTs and completely unrelated images (like a liver scan). This means you cannot rely on the segmentation model's own "confidence" to catch errors.
The distribution of confidence scores for far-OOD data and true detections overlap significantly, making them indistinguishable using uncertainty alone.
  • Likelihood as a Robust Filter: Unlike uncertainty scores, the log-likelihood generated by the Transformer provides a clear separation. It can perfectly distinguish far-OOD data (AUC = 1.00) and effectively flags severe near-OOD corruptions.
  • Synergy between Likelihood and Uncertainty: The authors found that the Transformer catches "highly OOD" images (like skull-stripped scans), while standard uncertainty methods perform better on "slightly OOD" images (like low-level noise). Using them in tandem creates a multi-layered safety net.

Key Results

  • Far-OOD Detection: The VQ-GAN + Transformer achieved a perfect AUC of 1.00 for distinguishing head CTs from all 10 non-head CT classes in the Medical Decathlon dataset.
  • Near-OOD Detection: The model achieved an AUC of 1.00 for detecting critical corruptions like background changes, skull-stripping, and global intensity scaling.
Log-likelihood distributions show clear separation between in-distribution head CTs and far-OOD non-head CT data.
  • Segmentation Quality Correlation: There is a strong relationship between the likelihood assigned by the transformer and the number of false positives produced by the segmentation model. Setting a likelihood threshold (e.g., -7000) effectively filters out scans where the segmentation model is likely to fail.

Limitations and Future Work

While the results are promising, the authors acknowledge that most of the "near-OOD" testing was performed on synthetic corruptions. Real-world clinical artifacts can be more subtle and complex.

Future work will focus on validating this pipeline on larger, multi-center clinical datasets to see how well it handles the natural variability of hospital data. Additionally, the authors suggest that the "spatial likelihood maps" produced by the transformer could be used to provide explainable AI, highlighting exactly which part of a scan is anomalous to the clinician.

Summary updated

Abstract

In a clinical setting it is essential that deployed image processing systems are robust to the full range of inputs they might encounter and, in particular, do not make confidently wrong predictions. The most popular approach to safe processing is to train networks that can provide a measure of their uncertainty, but these tend to fail for inputs that are far outside the training data distribution. Recently, generative modelling approaches have been proposed as an alternative; these can quantify the likelihood of a data sample explicitly, filtering out any out-of-distribution (OOD) samples before further processing is performed. In this work, we focus on image segmentation and evaluate several approaches to network uncertainty in the far-OOD and near-OOD cases for the task of segmenting haemorrhages in head CTs. We find all of these approaches are unsuitable for safe segmentation as they provide confidently wrong predictions when operating OOD. We propose performing full 3D OOD detection using a VQ-GAN to provide a compressed latent representation of the image and a transformer to estimate the data likelihood. Our approach successfully identifies images in both the far- and near-OOD cases. We find a strong relationship between image likelihood and the quality of a model's segmentation, making this approach viable for filtering images unsuitable for segmentation. To our knowledge, this is the first time transformers have been applied to perform OOD detection on 3D image data. Code is available at github.com/marksgraham/transformer-ood.
BibTeX
@inproceedings{graham2022transformer,
  author = {Mark S Graham and Petru-Daniel Tudosiu and Paul Wright and Walter Hugo Lopez Pinaya and U Jean-Marie and Yee Mah and James Teo and Rolf H Jäger and David Werring and Parashkev Nachev and Sebastien Ourselin and M Jorge Cardoso},
  title = {{Transformer-based out-of-distribution detection for clinically safe segmentation}},
  booktitle = {MIDL},
  year = {2022},
  url = {https://openreview.net/forum?id=5QDK5LpyxO},
}
Figure