CXFormer is a self-supervised foundation model family tailored for Chest X-ray (CXR) analysis. Built on top of DINOv2 and adapted with domain-specific optimizations, CXFormer delivers SOTA performance on multiple medical imaging tasks while being compute-efficient.
2
stars
19
commits
Python
primary language
May 11, 2025
updated
CXformer is a vision transformer tailored for chest X-ray analysis, adapted from DINOv2 with clinically motivated training modifications. This repository provides code for pretraining CXformer using our optimized pipeline, as well as scripts for finetuning on downstream tasks like classification, segmentation, and report generation.
Key highlights:
📄 Paper: Empirical Analysis of Scaling Vision Foundation Models for Chest X-rays (MIDL 2025)
👨⚕️ Authors: Ahmed Al-Mahrooqi, Prateek Munjal, Ronnie Rajan, Marco AF Pimentel, Praveenkumar Kanithi
📍 Affiliation: M42, Abu Dhabi
📦 Models: CXformer(S), CXformer(B)
🧠 Base Architecture: Vision Transformers (ViT-S, ViT-B)
📊 Tasks: Image Classification, Semantic Segmentation, Report Generation
| Model | Params | Pretrain Compute (FLOPs) | Mean AUROC | HuggingFace Model Card |
|---|---|---|---|---|
| CXformer(S) | 22M | 3.63 ExaFLOPs | 86.05% | Huggingface Link |
| CXformer(B) | 87M | 14.42 ExaFLOPs | 87.93% | Huggingface Link |
| RAD-DINO | 87M | 26.71 ExaFLOPs | 87.32 | Huggingface Link |
📌 Note: Both models are trained solely on image data—no text supervision.
| Task | Dataset(s) |
|---|---|
| Image Classification | CheXpert, NIH-CXR8, RSNA, VinDr |
| Segmentation | CheXmask |
| Report Generation | MIMIC-CXR, IU-Xray |
git clone https://github.com/m42-health/CXformer.git
cd CXformer
pip install -r requirements.txt
Download the DINOv2 pretrained weights from https://github.com/facebookresearch/dinov2 and update the path in your config file.
For example, if you're using the following script which uses "dinov2/configs/pretrain/cxformer_small.yaml" as config file:
Make sure to set the pretrained_weights path under both the student and teacher sections like this:
student:
pretrained_weights: /path/to/dinov2_checkpoint.pth
teacher:
pretrained_weights: /path/to/dinov2_checkpoint.pth
sh scripts/pretrain/cxformer_slurm_submit.sh
export PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:256 # to reduce fragmentation
n_nodes=1
cfg_file=dinov2/configs/pretrain/cxformer_small.yaml
PYTHONPATH=. python dinov2/run/train/train.py \
--nodes $n_nodes \
--nodelist "worker-13" \
--config-file $cfg_file \
--output-dir output_ablations_new/pretrain/cxformer_small_slurm/
You can fine-tune CXformer on downstream image classification tasks such as CheXpert using the provided shell script.
sh scripts/finetuning/image_classification/ft_cxformer_chexpert.sh
export CUDA_VISIBLE_DEVICES=0
n_epochs=100
pretrained_wt="m42-health/CXformer-small"
PYTHONPATH=. deepspeed dinov2/train/cxr_finetune.py \
--config-file dinov2/configs/downstream/classification/cxformer_chexpert_small.yaml \
--output-dir output_ablations_new/finetune/cxformer_chexpert \
--exp-name ft_cxformer \
--pretrained-weights $pretrained_wt \
--model-type dinov2 \
--num-epochs $n_epochs \
--batch-size 10 \
--num_workers 1 \
--seed 7479 \
--cls-n-layers 4 \
--apply-avgpool \
--clf_lr 5e-5 \
--backbone_lr 5e-7
📁 Output The fine-tuned model, logs, and metrics will be saved in:
output_ablations_new/finetune/cxformer_chexpert/
sh scripts/finetuning/segmentation/cxformer_chexmask.sh
We refer interested readers to llava repo as we straightaway used it in our work.
| Model | CheXpert | RSNA | NIH-CXR8 | VinDr | Avg. |
|---|---|---|---|---|---|
| CXformer(S) | 83.34 | 91.13 | 83.68 | 46.03 (AUPRC) | 86.05 |
| CXformer(B) | 86.80 | 91.71 | 85.28 | 48.02 (AUPRC) | 87.93 |
| Model | Lungs | Heart | Avg. |
|---|---|---|---|
| CXformer(S) | 91.69 | 89.35 | 90.52 |
| CXformer(B) | 91.94 | 89.94 | 90.94 |
| Model | ROUGE-L | BLEU-4 | RGER | F1-14 | Avg. |
|---|---|---|---|---|---|
| CXformer(S) | 25.25 | 9.11 | 23.06 | 33.85 | 27.51 |
| CXformer(B) | 24.93 | 9.03 | 22.94 | 33.45 | 27.16 |
CXformer/
├── configs/ # YAML configuration files
├── models/ # Vision backbone encoders and heads
├── dinov2/
│ ├── data/ # Data loading utilities
│ ├── cxr_data/ # Custom dataset classes and preprocessing logic for chest X-rays
│ └── train/
│ ├── cxr_pretrain.py # Pretraining script
│ ├── cxr_finetune.py # Finetuning script for image classification
│ └── cxr_segmentation.py # Finetuning script for image segmentation
└── README.md # Project overview
This project is licensed under CC BY-NC-4.0 - see the LICENSE.md file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
@inproceedings{al2025empirical,
title={Empirical Analysis of Scaling Vision Foundation Models for Chest X-rays},
author={Al Mahrooqi, Ahmed and Munjal, Prateek and Rajan, Ronnie and Pimentel, Marco AF and Kanithi, Praveenkumar},
booktitle={Medical Imaging with Deep Learning},
year={2025}
}
14 commits
5 commits
Python
99.8%
CXFormer is a self-supervised foundation model family tailored for Chest X-ray (CXR) analysis. Built on top of DINOv2 and adapted with domain-specific optimizations, CXFormer delivers SOTA performance on multiple medical imaging tasks while being compute-efficient.
2
stars
19
commits
Python
primary language
May 11, 2025
updated
CXformer is a vision transformer tailored for chest X-ray analysis, adapted from DINOv2 with clinically motivated training modifications. This repository provides code for pretraining CXformer using our optimized pipeline, as well as scripts for finetuning on downstream tasks like classification, segmentation, and report generation.
Key highlights:
📄 Paper: Empirical Analysis of Scaling Vision Foundation Models for Chest X-rays (MIDL 2025)
👨⚕️ Authors: Ahmed Al-Mahrooqi, Prateek Munjal, Ronnie Rajan, Marco AF Pimentel, Praveenkumar Kanithi
📍 Affiliation: M42, Abu Dhabi
📦 Models: CXformer(S), CXformer(B)
🧠 Base Architecture: Vision Transformers (ViT-S, ViT-B)
📊 Tasks: Image Classification, Semantic Segmentation, Report Generation
| Model | Params | Pretrain Compute (FLOPs) | Mean AUROC | HuggingFace Model Card |
|---|---|---|---|---|
| CXformer(S) | 22M | 3.63 ExaFLOPs | 86.05% | Huggingface Link |
| CXformer(B) | 87M | 14.42 ExaFLOPs | 87.93% | Huggingface Link |
| RAD-DINO | 87M | 26.71 ExaFLOPs | 87.32 | Huggingface Link |
📌 Note: Both models are trained solely on image data—no text supervision.
| Task | Dataset(s) |
|---|---|
| Image Classification | CheXpert, NIH-CXR8, RSNA, VinDr |
| Segmentation | CheXmask |
| Report Generation | MIMIC-CXR, IU-Xray |
git clone https://github.com/m42-health/CXformer.git
cd CXformer
pip install -r requirements.txt
Download the DINOv2 pretrained weights from https://github.com/facebookresearch/dinov2 and update the path in your config file.
For example, if you're using the following script which uses "dinov2/configs/pretrain/cxformer_small.yaml" as config file:
Make sure to set the pretrained_weights path under both the student and teacher sections like this:
student:
pretrained_weights: /path/to/dinov2_checkpoint.pth
teacher:
pretrained_weights: /path/to/dinov2_checkpoint.pth
sh scripts/pretrain/cxformer_slurm_submit.sh
export PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:256 # to reduce fragmentation
n_nodes=1
cfg_file=dinov2/configs/pretrain/cxformer_small.yaml
PYTHONPATH=. python dinov2/run/train/train.py \
--nodes $n_nodes \
--nodelist "worker-13" \
--config-file $cfg_file \
--output-dir output_ablations_new/pretrain/cxformer_small_slurm/
You can fine-tune CXformer on downstream image classification tasks such as CheXpert using the provided shell script.
sh scripts/finetuning/image_classification/ft_cxformer_chexpert.sh
export CUDA_VISIBLE_DEVICES=0
n_epochs=100
pretrained_wt="m42-health/CXformer-small"
PYTHONPATH=. deepspeed dinov2/train/cxr_finetune.py \
--config-file dinov2/configs/downstream/classification/cxformer_chexpert_small.yaml \
--output-dir output_ablations_new/finetune/cxformer_chexpert \
--exp-name ft_cxformer \
--pretrained-weights $pretrained_wt \
--model-type dinov2 \
--num-epochs $n_epochs \
--batch-size 10 \
--num_workers 1 \
--seed 7479 \
--cls-n-layers 4 \
--apply-avgpool \
--clf_lr 5e-5 \
--backbone_lr 5e-7
📁 Output The fine-tuned model, logs, and metrics will be saved in:
output_ablations_new/finetune/cxformer_chexpert/
sh scripts/finetuning/segmentation/cxformer_chexmask.sh
We refer interested readers to llava repo as we straightaway used it in our work.
| Model | CheXpert | RSNA | NIH-CXR8 | VinDr | Avg. |
|---|---|---|---|---|---|
| CXformer(S) | 83.34 | 91.13 | 83.68 | 46.03 (AUPRC) | 86.05 |
| CXformer(B) | 86.80 | 91.71 | 85.28 | 48.02 (AUPRC) | 87.93 |
| Model | Lungs | Heart | Avg. |
|---|---|---|---|
| CXformer(S) | 91.69 | 89.35 | 90.52 |
| CXformer(B) | 91.94 | 89.94 | 90.94 |
| Model | ROUGE-L | BLEU-4 | RGER | F1-14 | Avg. |
|---|---|---|---|---|---|
| CXformer(S) | 25.25 | 9.11 | 23.06 | 33.85 | 27.51 |
| CXformer(B) | 24.93 | 9.03 | 22.94 | 33.45 | 27.16 |
CXformer/
├── configs/ # YAML configuration files
├── models/ # Vision backbone encoders and heads
├── dinov2/
│ ├── data/ # Data loading utilities
│ ├── cxr_data/ # Custom dataset classes and preprocessing logic for chest X-rays
│ └── train/
│ ├── cxr_pretrain.py # Pretraining script
│ ├── cxr_finetune.py # Finetuning script for image classification
│ └── cxr_segmentation.py # Finetuning script for image segmentation
└── README.md # Project overview
This project is licensed under CC BY-NC-4.0 - see the LICENSE.md file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
@inproceedings{al2025empirical,
title={Empirical Analysis of Scaling Vision Foundation Models for Chest X-rays},
author={Al Mahrooqi, Ahmed and Munjal, Prateek and Rajan, Ronnie and Pimentel, Marco AF and Kanithi, Praveenkumar},
booktitle={Medical Imaging with Deep Learning},
year={2025}
}
14 commits
5 commits
Python
99.8%