Counterfactual Stress Testing for Image Classification Models

Moritz Stammel, Fabio De Sousa Ribeiro, Raghav Mehta, Mélanie Roschewitz, Ben GlockerarXiv 2026

TL;DR

Medical image classification models often fail when deployed in new clinical environments due to hidden "underspecification," where models with identical validation scores exhibit wildly different real-world failure modes. This paper introduces a counterfactual stress testing framework that uses causal generative models to simulate realistic clinical shifts—such as changing scanner hardware or patient demographics—while preserving underlying anatomy. The resulting synthetic images serve as a highly accurate proxy for real out-of-distribution (OOD) performance, far outperforming traditional image-corruption baselines.

What Problem Does This Paper Address?

When deep learning models for medical imaging move from the lab to the clinic, they frequently stumble. A model trained on chest X-rays from one hospital might fail at another simply because the new site uses a different scanner manufacturer or serves a different demographic mix.

A core culprit is underspecification: multiple models can achieve identical performance on internal validation data but rely on different predictive shortcuts, causing them to fail in completely unique ways when encountering out-of-distribution (OOD) data.

To catch these flaws before deployment, engineers use "stress testing." However, current approaches typically rely on simple, arbitrary image perturbations (like altering brightness, adding noise, or applying Gaussian blur). These simplistic corruptions don't mirror real clinical variations and often overestimate a model's robustness. Ideally, we would test models on real OOD data from the target site, but collecting large, matched, and labeled cohorts for every potential deployment scenario is practically impossible.

Methods

The authors propose a Counterfactual Stress Testing framework. Instead of applying blind pixel-level distortions, they use Deep Structural Causal Models (DSCMs) to generate realistic "what-if" clinical images.

Fig. 1: Proposed counterfactual stress testing framework. Unlike classical stress testing, which is limited to simple perturbations (e.g., contrast adjustment, rotation), counterfactual stress testing uses a causal generative model to simulate targeted attribute-level changes (e.g., scanner-induced appearance shifts) while largely preserving anatomical identity of the original patient.

The process works as follows:

  1. Causal Modeling: The framework assumes a causal graph where parent attributes (like scanner type or recorded sex) influence the final image appearance.
  2. Generative Mechanism: The DSCM is implemented via a Hierarchical $\beta$-Variational Autoencoder (HVAE).
  3. Counterfactual Generation: To test a model, the DSCM takes a real image, infers its image-specific latent variables (which capture patient-specific anatomy), intervenes on a target attribute (e.g., changing the scanner from Philips to IDC via a $do$-intervention), and decodes it.

This process creates an anatomically consistent "counterfactual twin" of the original image, isolating the exact environmental or demographic shift under evaluation. The authors evaluate this framework against standard classifiers (ResNet-50, DenseNet-121, and Vision Transformers).

Datasets

The framework was validated across two distinct imaging modalities:

  • PadChest: A large public dataset of over 160,000 chest X-rays. The downstream task was binary pneumonia classification, focusing on shifts in scanner manufacturers (Philips vs. IDC) and recorded sex (Male vs. Female).
  • EMBED: The Emory Breast Imaging Dataset, containing over 290,000 2D mammography images. The task was predicting breast density across four BI-RADS categories, testing shifts across four different scanner models.

Key Insights and Ideas

  • Capturing the "Sign" of the Shift: Traditional stress tests (like blurring) always degrade image quality, meaning they can only predict a performance drop. However, in the real world, moving to a newer, higher-quality scanner might actually improve model performance. Counterfactual stress testing successfully captured these positive performance shifts, whereas classical methods failed entirely.
  • Shifting the Data Burden: This method doesn't eliminate the need for data, but it changes what data is required. Instead of needing hard-to-get task labels (e.g., expert pneumonia annotations) from every new hospital, the causal generative model can be trained on broad, unlabeled imaging data that simply includes basic metadata like scanner type.
  • Anatomical Preservation: By reusing inferred latent variables, the generative model alters the style and domain characteristics of the image while leaving the underlying pathology intact, allowing for semantically meaningful evaluation.
Fig. 2: Examples of original images and generated counterfactuals for PadChest (left) and EMBED (right) using our DSCM.

Key Results

The authors compared how well synthetic stress tests correlated with performance on actual, matched real-world OOD cohorts:

  • Chest X-Ray Scanner Shifts: Counterfactual stress testing closely tracked real OOD degradation, achieving a remarkable Pearson correlation of $r = 0.93$ and recovering the correct direction of the performance shift across all model combinations. The strongest classical baseline (Sharpness Change) managed a correlation of only $r = 0.30$.
  • Composite Shifts: When jointly intervening on both scanner type and recorded sex simultaneously, the counterfactual tests achieved a Pearson correlation of $r = 0.95$ with real OOD trends.
  • Mammography Generalization: On the EMBED dataset, the counterfactual framework correctly predicted the direction of the real OOD shift in 96% of the evaluated model/scanner combinations, achieving a Pearson correlation of $r = 0.69$.

Limitations and Future Work

While promising, the authors note several limitations:

  • Causal Graph Assumptions: The framework relies on a predefined causal graph and assumes that counterfactuals are identifiable from the observed data, which may not always hold true in complex clinical settings.
  • Pragmatic Attribute Interpretation: Interventions on attributes like "recorded sex" probe general image variations (including potential imaging shortcuts or anatomical correlations) rather than a pure biological-sex effect.
  • Calibration: The synthetic tests are highly effective at predicting the direction and rank order of model failures, but they are not yet perfectly calibrated to predict the exact absolute magnitude of a real-world performance drop.

Future directions include exploring data-driven causal structure learning (such as DECI) to relax graph assumptions, establishing quantitative metrics for counterfactual realism, and extending the framework to 3D imaging modalities like CT or MRI.

Summary updated

BibTeX
@misc{stammel2026counterfactual,
  author = {Moritz Stammel and Fabio De Sousa Ribeiro and Raghav Mehta and Mélanie Roschewitz and Ben Glocker},
  title = {{Counterfactual Stress Testing for Image Classification Models}},
  year = {2026},
  eprint = {2605.10894},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2605.10894},
}
Figure