Counterfactual contrastive learning: robust representations via causal image synthesis

Mélanie Roschewitz, Fabio De Sousa Ribeiro, Tian Xia, Galvin Khara, Ben Glocker · OpenReview

Abstract

Contrastive pretraining is well-known to improve downstream task performance and model generalisation, especially in limited label settings. However, it is sensitive to the choice of augmentation pipeline. Positive pairs should preserve semantic information while destroying domain-specific information. Standard augmentation pipelines emulate domain-specific changes with pre-defined photometric transformations, but what if we could simulate realistic domain changes instead? In this work, we show how to utilise recent progress in counterfactual image generation to this effect. We propose CF-SimCLR, a counterfactual contrastive learning approach which leverages approximate counterfactual inference for positive pair creation. Comprehensive evaluation across five datasets, on chest radiography and mammography, demonstrates that CF-SimCLR substantially improves robustness to acquisition shift with higher downstream performance on both in- and out-of-distribution data, particularly for domains which are under-represented during training.

Introduction

A major hurdle in deploying medical AI is "acquisition shift." A model trained on X-rays from one hospital using a specific scanner manufacturer often performs poorly at another hospital using different hardware. This occurs because models inadvertently learn to rely on scanner-specific image characteristics rather than underlying clinical features.

Standard self-supervised techniques like Contrastive Learning (CL) attempt to mitigate this by teaching models that different "views" of the same image should have similar representations. However, standard views are typically created using generic augmentations like cropping or color jittering, which fail to capture the complex, realistic variations caused by different medical hardware. CF-SimCLR addresses this by using causal generative models to create "counterfactual" positive pairs, simulating how a specific patient’s scan would look if taken on a different manufacturer's scanner.

Methodology

The CF-SimCLR Framework

The authors introduce CF-SimCLR, a framework that integrates counterfactual image synthesis into the popular SimCLR pipeline. Unlike standard SimCLR, which uses two random augmentations of the same original image, CF-SimCLR creates a positive pair consisting of one augmentation of the original image and one augmentation of a generated counterfactual image.

Comparison of standard SimCLR, the SimCLR+ baseline, and the proposed CF-SimCLR framework.
  1. Counterfactual Generation: A Deep Structural Causal Model (DSCM) is trained using Hierarchical Variational Autoencoders (HVAE). This allows for an intervention—changing the "scanner" variable while keeping the patient's anatomy (exogenous factors) constant.
  2. Contrastive Objective: The model is trained to maximize the similarity between the original and counterfactual views. This forces the encoder to become invariant to the "style" of the scanner, focusing instead on semantic clinical information.

To validate the importance of explicit pairing, the authors also compare against SimCLR+, where counterfactual images are added to the training set as independent samples without being explicitly paired.

Causal Image Synthesis

The success of the method relies on the ability to generate faithful counterfactuals that realistically simulate different acquisition domains while preserving anatomy.

Visual comparison of real images and generated counterfactuals across the EMBED (mammography) and PadChest (radiography) datasets.

Experimental Setup and Datasets

The study evaluates CF-SimCLR across two modalities and five datasets:

  • Chest Radiography: Pretrained on PadChest (multi-scanner). Evaluated on RSNA Pneumonia Detection and CheXpert to test out-of-distribution (OOD) transfer.
  • Mammography: Pretrained on EMBED (six different scanners). Evaluated on VinDR-Mammo (scanners from Vietnam) to test OOD robustness.

Key Results and Insights

  • Superior Robustness: CF-SimCLR consistently outperformed both standard SimCLR and SimCLR+. On OOD datasets like CheXpert and VinDR-Mammo, the performance gap was particularly noticeable, demonstrating superior generalization to unseen hardware.
  • Explicit Pairing is Crucial: CF-SimCLR consistently beat SimCLR+. This suggests that simply increasing data diversity is insufficient; the model requires the explicit signal that a scan from Scanner A is semantically identical to its counterfactual from Scanner B.
  • Domain Alignment: t-SNE projections revealed that while standard SimCLR embeddings cluster by scanner, CF-SimCLR embeddings are much more mixed, indicating the model successfully learned to ignore acquisition sources.
  • Efficiency in Low-Label Settings: Performance gains were most dramatic when labeled data was scarce. By learning robust representations during pretraining, the model requires fewer expensive medical annotations to achieve high accuracy.

Limitations and Future Work

While effective, the method requires a predefined causal graph and metadata (like scanner IDs) to train the generative model. Future work could extend this approach to other types of domain shifts, such as enforcing subgroup invariance to improve model fairness across different patient demographics. Additionally, the method's success is tied to the generative quality of the HVAE; any errors in the counterfactual synthesis could potentially introduce unwanted biases.

Summary updated

BibTeX
@inproceedings{roschewitz2024counterfactual,
  author = {Mélanie Roschewitz and Fabio De Sousa Ribeiro and Tian Xia and Galvin Khara and Ben Glocker},
  title = {{Counterfactual contrastive learning: robust representations via causal image synthesis}},
  booktitle = {DEMI @ MICCAI},
  year = {2024},
  doi = {10.1007/978-3-031-73748-0\_3},
}
Figure