On the use of Mahalanobis distance for out-of-distribution detection with neural networks for medical imaging

Harry Anthony, Konstantinos Kamnitsas · DOI

Introduction

Deep learning models in medical imaging are susceptible to "silent failures." These occur when a model, trained on a specific population or set of conditions, encounters out-of-distribution (OOD) data—such as a new medical implant, a different patient demographic, or unexpected image artifacts. Instead of flagging these inputs as unfamiliar, models often provide high-confidence predictions that are fundamentally incorrect. To mitigate this risk, researchers employ OOD detection methods to identify and flag suspicious inputs before they reach a clinician.

One of the most prominent methods for this task is the Mahalanobis distance ($D_M$), which measures the distance between an input's internal feature representations and the distribution of the training data. However, a critical question remains: which layer of the neural network provides the most effective features for this calculation? While many practitioners default to the last hidden layer (LHL), this paper demonstrates that such a choice is often sub-optimal and proposes a more robust alternative.

The Limitations of the Last Hidden Layer

The common practice of using only the final layer for OOD detection assumes that the most relevant information for identifying anomalies is concentrated at the highest level of abstraction. Through a granular analysis of ResNet18 and VGG16 architectures, the authors reveal that the "optimal" layer for detection depends entirely on the specific nature of the OOD pattern.

For example, low-level synthetic artifacts (like geometric shapes) are often caught more effectively in the early layers of a network. Conversely, subtle clinical shifts—such as identifying a patient from an unseen demographic—are better detected in deeper layers. By relying solely on the LHL, researchers may be discarding critical "OOD signals" that are present earlier in the processing pipeline. In several experiments, the LHL actually provided some of the weakest detection performance.

Methodology: Multi-branch Mahalanobis (MBM)

To address the variability of OOD patterns, the authors propose the Multi-branch Mahalanobis (MBM) approach. This method moves away from selecting a single layer and instead creates a comprehensive "safety net" across the network depth.

1. Branching Strategy

The network is divided into "branches" based on segments between downsampling operations. The authors calculate normalized $D_M$ scores for every module within these branches and aggregate them. This allows the system to operate simultaneously at different resolutions and levels of abstraction.

2. ReLU Optimization

A key technical insight from the study is the role of activation functions. The authors found that calculating distances specifically after ReLU (Rectified Linear Unit) activations significantly improves performance compared to calculating them after Batch Normalization or Convolutional layers. The non-linearity of the ReLU appears to "push" OOD features further away from the in-distribution data in the latent space, making them easier to distinguish.

3. Feature Perturbation

To further enhance the separation between distributions, the authors incorporate the Fast Gradient Sign Method (FGSM). By applying a small, calculated perturbation to the input images, the distance between in-distribution and OOD features is amplified, leading to higher detection accuracy.

Experimental Results

The MBM approach was validated using the CheXpert dataset (chest X-rays) against several OOD configurations, including synthetic artifacts and real-world clinical shifts.

Clinical OOD: Pacemakers and Demographic Shifts

The authors manually labeled 50% of the CheXpert dataset to identify scans containing support devices (like pacemakers), creating a rigorous benchmark for foreign object detection. They also tested demographic shifts by training models exclusively on male patients and testing them on female patients.

  • Pacemaker Detection: On a ResNet18 architecture, the standard LHL Mahalanobis score achieved an AUROC (Area Under the ROC Curve) of only 57.1%. The MBM approach, combined with ReLU optimization and FGSM, increased this to 76.8%.
  • Demographic Shift (Sex): Detection performance improved from 55.6% (LHL) to 72.1% using the multi-branch strategy.

In both cases, MBM consistently outperformed standard baselines such as Maximum Softmax Probability (MCP) and ODIN, which often struggle with the subtle, high-dimensional shifts characteristic of medical imaging.

Conclusion and Future Work

The findings suggest that robust OOD detection in medical AI requires a multi-layered approach. By aggregating signals from different depths of the network, the MBM method acts as a "jack of all trades," capable of identifying both coarse textural anomalies and fine structural shifts.

However, the multi-branch approach introduces the challenge of thresholding. Determining the optimal cutoff for multiple detectors at different depths currently requires a grid search, which may be difficult to implement in clinical settings where OOD patterns are unknown. Future research will focus on automated configuration methods to make these multi-detector systems easier to deploy across diverse hospital environments, ensuring that medical AI remains a reliable tool for clinicians.

Summary updated

BibTeX
@inproceedings{anthony2023use,
  author = {Harry Anthony and Konstantinos Kamnitsas},
  title = {{On the use of Mahalanobis distance for out-of-distribution detection with neural networks for medical imaging}},
  booktitle = {UNSURE @ MICCAI},
  year = {2023},
  url = {https://openreview.net/forum?id=08vRe8TJJr},
}
Figure