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 H. Mah, James T. Teo, Hans Rolf Jäger, David Werring, Parashkev Nachev, Sébastien Ourselin, M. Jorge Cardoso

Introduction

Deep learning models have shown remarkable success in medical image segmentation, particularly for tasks like identifying brain hemorrhages in CT scans. However, a significant barrier to clinical deployment remains: the tendency of these models to produce "confidently wrong" predictions when presented with data outside their training distribution. In a clinical setting, a model might encounter motion artifacts, incorrect preprocessing, or even images of the wrong anatomy entirely. Standard uncertainty estimation methods often fail to flag these anomalies, potentially leading to dangerous clinical errors. This paper proposes a robust safety filter using a VQ-GAN and a Transformer to estimate the likelihood of 3D medical images, acting as a gatekeeper for safer AI-assisted diagnosis.

The Challenge of Out-of-Distribution Data

Most medical AI models are trained on curated, "clean" datasets. Clinical reality, however, is heterogeneous. Out-of-Distribution (OOD) data can range from "far-OOD" (e.g., a lung scan being fed into a brain-segmentation model) to "near-OOD" (e.g., a brain scan with unusual noise or different intensity scaling).

The core danger is that standard models—and even those utilizing uncertainty estimation techniques like Deep Ensembles or Monte Carlo Dropout—often assign high confidence to these anomalous inputs. For instance, a model trained on head CTs might look at a lung scan and confidently claim to see a brain bleed. There is a critical need for a system that can identify these anomalies before they reach the segmentation model.

Methodology: Generative Modeling for Likelihood Estimation

The authors introduce a two-stage generative approach to estimate the log-likelihood of 3D medical volumes.

1. VQ-GAN Compression

Processing 3D medical volumes directly with Transformers is computationally prohibitive due to high dimensionality. To address this, the authors employ a Vector Quantised Generative Adversarial Network (VQ-GAN). The VQ-GAN encoder compresses the 3D volume into a discrete latent space, effectively representing the image as a grid of "visual words" from a learned codebook. The decoder is trained to reconstruct the original image from these codes, ensuring the latent representation preserves essential structural information.

2. Sequence Modeling with the Performer

Once the 3D image is converted into a sequence of discrete codes, a Transformer is used to learn the probability distribution of these sequences. Specifically, the authors utilize the Performer architecture. The Performer uses a linear attention mechanism, which is essential for handling the long sequences generated by 3D data while remaining within memory constraints.

By calculating the log-likelihood of a new scan's code sequence, the system determines how closely the image matches the training distribution. If the likelihood is low, the image is flagged as OOD.

Experimental Evaluation

The models were trained using the CROMIS dataset, which contains 687 head CT scans with intracerebral hemorrhages (ICH). Testing was conducted across several scenarios:

  • In-Distribution: 47 clinical head CT scans from the KCH dataset.
  • Far-OOD: Images from the Medical Decathlon, featuring 10 different organs and modalities (e.g., heart MRI, lung CT).
  • Near-OOD: The Corrupted KCH dataset, where 15 types of corruptions (e.g., Gaussian noise, axis flipping, skull-stripping, and global intensity scaling) were applied to the KCH scans.

Key Results and Insights

The proposed method demonstrated superior performance in identifying anomalous data compared to traditional uncertainty metrics.

Superior Detection Performance

The VQ-GAN + Transformer approach achieved a perfect AUC of 1.0 when distinguishing head CTs from far-OOD images (non-head scans). More impressively, it also achieved an AUC of 1.0 for several challenging near-OOD cases, such as skull-stripped images and global intensity scaling. In these same near-OOD scenarios, standard uncertainty methods performed poorly, with AUCs as low as 0.36.

Correlation with Segmentation Quality

A key finding was the strong correlation between the log-likelihood assigned by the Transformer and the actual performance of the segmentation model. Scans with low likelihood scores consistently resulted in a higher frequency of false-positive detections, confirming that likelihood is a reliable proxy for model reliability.

Spatial Interpretability

Beyond a global score, the method allows for the generation of spatial likelihood maps. By upsampling the conditional probabilities from the Transformer, the system can visualize exactly which regions of an image are considered anomalous. For example, if a scan is missing a portion of the skull, the spatial map highlights that specific area as low-likelihood, providing clinicians with interpretable evidence for why a scan was flagged.

Conclusion

This work represents the first successful application of Transformers for OOD detection in 3D medical imaging. By acting as a "gatekeeper," the VQ-GAN and Performer pipeline can effectively filter out scans that would otherwise lead to "confidently wrong" predictions. While future work is needed to validate the system against real-world clinical artifacts and to integrate it into live clinical workflows, this approach provides a significant step toward safer, more reliable medical AI.

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.
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 H. Mah and James T. Teo and Hans Rolf Jäger and David Werring and Parashkev Nachev and Sébastien 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