Skip to content

Latest commit

Β 

History

History
156 lines (128 loc) Β· 5.4 KB

File metadata and controls

156 lines (128 loc) Β· 5.4 KB

DINOv3-MRI

DINOv3 self-supervised pretraining for 3D brain MRI, adapted from SPECTRE (CVPR 2026).

SPECTRE is a CT foundation model using DINO + SigLIP. This repo strips it down to the DINO self-supervised stage and adapts it for isotropic brain MRI (IXI T1 dataset as a starting point).

Quick start

# 1. Clone
git clone https://github.com/yourname/dinov3-mri.git
cd dinov3-mri

# 2. Install
pip install -e .
# or manually:
pip install torch timm monai nibabel omegaconf accelerate

# 3. Download IXI T1 data
bash scripts/download_ixi.sh
# β†’ places ~600 NIfTI files in data/IXI-T1/

# 4. Train
python train.py --output_dir outputs/dino_ixi

# 5. Multi-GPU
NUM_GPUS=4 bash scripts/pretrain.sh

Project structure

dinov3-mri/
β”‚
β”œβ”€β”€ configs/
β”‚   └── pretrain.yaml          # Default hyperparameters
β”‚
β”œβ”€β”€ data/                      # ← YOUR DATA GOES HERE (git-ignored)
β”‚   β”œβ”€β”€ .gitkeep
β”‚   └── IXI-T1/                # Raw NIfTI files after download
β”‚       β”œβ”€β”€ IXI002-Guys-0828-T1.nii.gz
β”‚       └── ...
β”‚
β”œβ”€β”€ outputs/                   # ← TRAINING OUTPUTS (git-ignored)
β”‚   β”œβ”€β”€ .gitkeep
β”‚   └── dino_ixi/              # Checkpoints, logs, saved configs
β”‚
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ download_ixi.sh        # Download IXI T1 dataset
β”‚   └── pretrain.sh            # Launch training (single/multi-GPU)
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ vision_transformer.py   # 3D ViT with isotropic MRI patches
β”‚   β”‚   β”œβ”€β”€ dino.py                 # DINO + DINOv2 teacher-student
β”‚   β”‚   β”œβ”€β”€ dino_head.py            # Projection head
β”‚   β”‚   β”œβ”€β”€ losses.py               # DINOLoss, iBOT, KoLeo, Center
β”‚   β”‚   └── layers/                 # Attention, PatchEmbed, 3D RoPE
β”‚   β”‚
β”‚   β”œβ”€β”€ data/
β”‚   β”‚   β”œβ”€β”€ mri_dataset.py          # IXI dataset (auto file discovery)
β”‚   β”‚   β”œβ”€β”€ transforms.py           # DINO multi-crop for MRI
β”‚   β”‚   β”œβ”€β”€ collate.py              # DataLoader collate function
β”‚   β”‚   └── preprocessing.py        # Offline preprocessing + stats
β”‚   β”‚
β”‚   β”œβ”€β”€ engine/
β”‚   β”‚   β”œβ”€β”€ trainer.py              # Training loop (Accelerate)
β”‚   β”‚   └── evaluator.py            # kNN + linear probe evaluation
β”‚   β”‚
β”‚   └── utils/
β”‚       β”œβ”€β”€ config.py               # OmegaConf config management
β”‚       β”œβ”€β”€ checkpoint.py           # Save/load with distributed RNG
β”‚       β”œβ”€β”€ scheduler.py            # Cosine warmup schedules
β”‚       β”œβ”€β”€ param_groups.py         # LLRD parameter groups
β”‚       β”œβ”€β”€ modeling.py             # EMA, position embed resampling
β”‚       └── misc.py                 # Seeds, Format enum, tuple helpers
β”‚
β”œβ”€β”€ train.py                   # Pretraining entry point
β”œβ”€β”€ evaluate.py                # Evaluation entry point
β”œβ”€β”€ pyproject.toml             # Dependencies & project metadata
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .env.example               # Environment variable template
└── README.md

Key differences from SPECTRE (CT β†’ MRI)

Aspect SPECTRE (CT) This repo (MRI)
Patch size (16, 16, 8) anisotropic (16, 16, 16) isotropic
Input size (128, 128, 64) (96, 96, 96)
Intensity normalization HU window [-1000, 1000] Percentile clip (0.5th–99.5th)
Voxel spacing (0.5, 0.5, 1.0) mm (1.0, 1.0, 1.0) mm
Augmentations RandScaleIntensityRange RandScaleIntensity + RandBiasField
Datasets 8 CT datasets (~100K vols) IXI T1 (~600 vols)
VLA stage SigLIP with radiology reports Not included (DINO only)

Configuration

All hyperparameters live in configs/pretrain.yaml. Override via CLI:

python train.py --output_dir outputs/debug \
    train.batch_size_per_gpu=4 \
    optim.epochs=10 \
    model.architecture=vit_base_patch16_96

Available architectures: vit_small_patch16_96 (default, 22M params), vit_base_patch16_96 (86M), vit_base_rope_patch16_96 (86M, RoPE).

Evaluation

After training, evaluate feature quality:

python evaluate.py \
    --checkpoint outputs/dino_ixi/checkpoint.pt \
    --data_dir data/IXI-T1 \
    --architecture vit_small_patch16_96

Data organization

Place your data under the data/ directory (git-ignored):

data/
β”œβ”€β”€ IXI-T1/                # ← bash scripts/download_ixi.sh
β”‚   β”œβ”€β”€ IXI002-Guys-0828-T1.nii.gz
β”‚   └── ...
β”œβ”€β”€ IXI-T1-preprocessed/   # ← optional: python -m src.data.preprocessing ...
β”‚   └── ...
└── your-other-dataset/    # ← add more datasets as needed

To use a different data location, either:

  • Symlink: ln -s /your/data/path data/IXI-T1
  • Override via CLI: train.data_dir=/abs/path/to/data
  • Set env var: DATA_DIR=/abs/path bash scripts/pretrain.sh

License

Code: MIT (same as SPECTRE). Pretrained weights may carry additional dataset license restrictions.

Citation

If you use this code, please cite SPECTRE:

@misc{claessens_scaling_2025,
  title={Scaling Self-Supervised and Cross-Modal Pretraining for Volumetric CT Transformers},
  author={Claessens, Cris and Viviers, Christiaan and D'Amicantonio, Giacomo and Bondarev, Egor and Sommen, Fons van der},
  year={2025},
  url={http://arxiv.org/abs/2511.17209},
}