File size: 12,183 Bytes
b39aef7
 
 
 
 
 
 
 
 
 
 
 
 
7cc1b7c
 
b39aef7
80df7e4
 
60e3fe9
89dc002
8b41845
 
 
 
 
 
 
 
 
 
 
 
 
60e3fe9
 
8b41845
 
 
 
 
 
 
 
 
 
 
 
 
80df7e4
60e3fe9
8b41845
 
b39aef7
 
 
 
 
8b41845
b39aef7
80df7e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b39aef7
 
80df7e4
 
 
 
 
 
b39aef7
 
 
80df7e4
 
 
 
 
 
 
 
 
 
 
 
 
8b41845
80df7e4
 
 
8b41845
80df7e4
 
 
 
8b41845
80df7e4
8b41845
 
80df7e4
8b41845
 
80df7e4
 
8b41845
 
 
 
80df7e4
 
 
 
8b41845
 
 
80df7e4
8b41845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80df7e4
 
 
 
 
 
8b41845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80df7e4
8b41845
80df7e4
8b41845
 
 
 
 
 
 
80df7e4
8b41845
80df7e4
8b41845
 
 
80df7e4
8b41845
 
 
 
 
80df7e4
8b41845
 
 
 
 
 
80df7e4
8b41845
80df7e4
8b41845
 
 
 
 
 
 
 
 
 
 
80df7e4
8b41845
 
80df7e4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
---

license: cc-by-nc-sa-4.0
language:
- en
tags:
- medical-imaging
- ct-scan
- 3d
- vision-transformer
- self-supervised-learning
- foundation-model
- radiology
library_name: transformers
pipeline_tag: feature-extraction
---


πŸ“’ [2026-07-17] SPECTRE now ships a command-line tool: point `spectre embed` at a `.nii`/`.nii.gz` file or a folder of them to get embeddings without writing any Python. Check below for details and usage examples.

πŸ“’ [2026-05-20] The pretrained SPECTRE model can now be loaded directly through the `transformers` library, no separate SPECTRE package installation required. Check below for details and usage examples.

πŸ“’ [2026-04-10] SPECTRE is now an official baseline for the [**CVPR 2026 Workshop Competition: Foundation Models for General CT Image Diagnosis**](https://www.codabench.org/competitions/12650/)! See `experiments/cvpr26_fm_for_ct_diag_task_1` for scripts and additional details.  

πŸ“’ [2026-02-21] SPECTRE has been accepted for presentation at **CVPR 2026** (Denver, Colorado, USA)!  

πŸ“’ [2026-01-20] [Semantic segmentation](https://github.com/cviviers/nnUNet) code and configurations using the nnUNet framework are now released!  

# SPECTRE πŸ‘»πŸ‘»πŸ‘»

<p align="center">
  <a href="https://pypi.org/project/spectre-fm/"><img alt="PyPI Version" src="https://img.shields.io/pypi/v/spectre-fm?style=flat-square&label=version&cacheSeconds=0" /></a>
  <a href="https://pypi.org/project/spectre-fm/"><img alt="Python Versions" src="https://img.shields.io/pypi/pyversions/spectre-fm?style=flat-square&cacheSeconds=0" /></a>
  <a href="https://pypi.org/project/spectre-fm/"><img alt="Downloads per Month" src="https://img.shields.io/pypi/dm/spectre-fm?style=flat-square&label=downloads&cacheSeconds=0" /></a>
  <a href="https://github.com/cclaess/SPECTRE/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/github/license/cclaess/SPECTRE?style=flat-square&cacheSeconds=0" /></a>
  <a href="https://huggingface.co/cclaess/SPECTRE-Large"><img alt="Model weights" src="https://img.shields.io/badge/model-Hugging%20Face-yellow?style=flat-square&cacheSeconds=0" /></a>
  <a href="https://arxiv.org/abs/2511.17209"><img alt="Preprint" src="https://img.shields.io/badge/preprint-arXiv-b31b1b?style=flat-square&cacheSeconds=0" /></a>
</p>

<p align="center">
   <img src="imgs/method_overview.jpg" alt="SPECTRE architecture and pretraining strategies" width="600"/>
</p>

SPECTRE (**S**elf-Supervised & Cross-Modal **P**r**e**training for **CT** **R**epresentation **E**xtraction) is a **Transformer-based foundation model for 3D Computed Tomography (CT) scans**, trained using **self-supervised learning** (SSL) and **cross-modal vision–language alignment** (VLA). It provides rich and generalizable representations from medical imaging data, which can be fine-tuned for downstream tasks such as segmentation, classification, and anomaly detection.  

SPECTRE has been trained on a large cohort of **open-source CT scans** of the **human abdomen and thorax**, as well as **paired radiology reports** and **Electronic Health Record data**, enabling it to capture representations that generalize across datasets and clinical settings.  

This repository provides pretrained SPECTRE models together with tools for fine-tuning and evaluation.

## 🧠 Pretrained Models

The pretrained SPECTRE model can easily be imported using the `transformers` library

```python

from transformers import AutoModel

model = AutoModel.from_pretrained('cclaess/SPECTRE-Large', trust_remote_code=True)

```

or by using the `spectre-fm` package as follows:

```python

from spectre import SpectreImageFeatureExtractor

model = SpectreImageFeatureExtractor.from_pretrained('spectre-large')

```

Run `spectre list-models` to see what is available. Pass `include_feature_combiner=False` for per-crop backbone features instead of one embedding per scan.

### πŸ–₯️ From the command line

If you just want embeddings out of a CT scan and would rather not write Python:

```bash

pip install "spectre-fm[inference]"



spectre embed scan.nii.gz -o embeddings/          # one scan

spectre embed /data/scans/ -o embeddings/         # a whole folder

```

This handles everything internally with the defaults SPECTRE was pretrained on: HU windowing to [-1000, 1000], RAS orientation, and 128Γ—128Γ—64 crops at the scan's native voxel spacing. Each scan produces `<name>.npz` containing `cls` (one vector for the scan) and `patch_tokens` (one vector per crop, shaped to the crop grid), plus a `manifest.csv`. Useful flags: `--backbone-only`, `--device cuda`, `--spacing 0.5 0.5 1.0` to resample, and `--max-crops-per-forward` if you run out of memory. See `spectre embed --help`.

### 🐍 From Python

Hand the model a scan in Hounsfield Units and it does the windowing for you:

```python

import torch



# One scan: (C, H, W, D) or (H, W, D), in raw HU.

scan = torch.randn(1, 384, 384, 256) * 500 - 500



with torch.no_grad():

    features = model(scan)



print("Features shape:", features.shape)   # (T', F') -> a CLS token plus one token per crop

```

Scans of different sizes can be embedded together by passing a list. All crops from all scans go through the backbone in a single pass, and only the feature combiner is split back out per scan:

```python

scans = [scan_a, scan_b, scan_c]           # any sizes, all in HU



with torch.no_grad():

    features = model.extract(scans)        # -> list of (T', F') tensors

```

If you have already windowed the scans yourself, pass the crops and their grid instead, and they are used untouched:

```python

from spectre import window_scan



crops, grid_size = window_scan(scan)       # (N, C, 128, 128, 64), (n_h, n_w, n_d)



with torch.no_grad():

    features = model(crops, grid_size=grid_size)

```

> **Reading files:** `spectre.load_ct` / `spectre.load_and_window` read `.nii`/`.nii.gz` and need the `[inference]` extra. Everything above works with just `pip install spectre-fm`.



Alternatively, you can download the weights of the separate components through HuggingFace using the following links:



| Architecture              | Input Modality     | Pretraining Objective   | Model Weights                                                                                                               |

|---------------------------|--------------------|-------------------------|-----------------------------------------------------------------------------------------------------------------------------|

| SPECTRE-ViT-Local         | CT crops           | SSL                     | [Download](https://huggingface.co/cclaess/SPECTRE/resolve/main/spectre_backbone_vit_large_patch16_128_no_vla.pt?download=true)  |

| SPECTRE-ViT-Local         | CT crops           | SSL + VLA               | [Download](https://huggingface.co/cclaess/SPECTRE/resolve/main/spectre_backbone_vit_large_patch16_128.pt?download=true)         |

| SPECTRE-ViT-Global        | Embedded CT crops  | VLA                     | [Download](https://huggingface.co/cclaess/SPECTRE/resolve/main/spectre_combiner_feature_vit_large.pt?download=true)             |

| Qwen3-Embedding-0.6B LoRA | Text (radiology)   | VLA                     | [Download](https://huggingface.co/cclaess/SPECTRE/resolve/main/spectre_qwen3_embedding_0.6B_lora.pt?download=true)              |



## 🩻 Segmentation (nnUNet)



If you're looking for a nnUNet-based segmentation pipeline that uses SPECTRE as the backbone, see this [GitHub](https://github.com/cviviers/nnUNet).



## πŸ“‚ Repository Contents



This repository is organized as follows:



- πŸš€ **`src/spectre/`** – Contains the core package, including:

  - Pretraining methods

  - Model architectures

  - Data handling and transformations



- πŸ› οΈ **`src/spectre/configs/`** – Stores configuration files for different training settings.



- πŸ”¬ **`experiments/`** – Includes Python scripts for running various pretraining and downstream experiments.



- 🐳 **`Dockerfile`** – Defines the environment for running a local version of SPECTRE inside a container.



## βš™οΈ Setting Up the Environment



To get up and running with SPECTRE, install the base package with pip:



```bash

pip install spectre-fm

```



This installs only the runtime dependencies needed to load and run the pretrained models.



To read CT scans from `.nii`/`.nii.gz` files or use the `spectre` command-line tool, add the inference extra:



```bash

pip install "spectre-fm[inference]"

```



If you want to fine-tune or pretrain SPECTRE, install the matching extra:



```bash

pip install "spectre-fm[training]"

```



If you only need the evaluation stack, install:



```bash

pip install "spectre-fm[eval]"

```



If training on GDS-enabled systems is required, install the CUDA 12 specific extra:



```bash

pip install "spectre-fm[gds-cuda12]"  # with training stack: "spectre-fm[training,gds-cuda12]"

```



**Note that** `gds-cuda12` is only compatible with CUDA 12.x environments.



To install everything at once, use:



```bash

pip install "spectre-fm[all]"

```



or install the latest updates directly from GitHub:



```bash

pip install git+https://github.com/cclaess/SPECTRE.git

```



## 🐳 Building and Using Docker



To facilitate deployment and reproducibility, SPECTRE can be run using **Docker**. This allows you to set up a fully functional environment without manually installing dependencies using your own local copy of spectre.



### **Building the Docker Image**



First, ensure you have **Docker** installed. Then, clone and navigate to the repository to build the image:



```bash

git clone https://github.com/cclaess/SPECTRE

cd SPECTRE

docker build -t spectre-fm .

```



### **Running Experiments Inside Docker**



Once the image is built, you can start a container and execute scripts inside it. For example, to run a DINO pretraining experiment:



```bash

docker run --gpus all --rm -v "$(pwd):/mnt" spectre-fm python3 experiments/pretraining/pretrain_dino.py --config_file spectre/configs/dino_default.yaml --output_dir /mnt/outputs/pretraining/dino/

```



- `--gpus all` enables GPU acceleration if available.

- `--rm` removes the container after execution.

- `-v $(pwd):/mnt` mounts the current directory inside the container.



## βš–οΈ License



- **Code: MIT** β€” see `LICENSE` (permissive; commercial use permitted).

- **Pretrained model weights: CC-BY-NC-SA** β€” non-commercial share-alike. The weights and any derivative models that include these weights are NOT cleared for commercial use. See `LICENSE_MODELS` for details and the precise license text.

> Note: the pretrained weights are subject to the original dataset licenses. Users intending to use SPECTRE in commercial settings should verify dataset and model licensing and obtain any required permissions.

## πŸ“œ Citation

If you use SPECTRE in your research or wish to cite it, please use the following BibTeX entry of our [preprint](https://arxiv.org/abs/2511.17209):

```

@misc{claessens_scaling_2025,

  title = {Scaling {Self}-{Supervised} and {Cross}-{Modal} {Pretraining} for {Volumetric} {CT} {Transformers}},

  url = {http://arxiv.org/abs/2511.17209},

  doi = {10.48550/arXiv.2511.17209},

  author = {Claessens, Cris and Viviers, Christiaan and D'Amicantonio, Giacomo and Bondarev, Egor and Sommen, Fons van der},

  year={2025},

}

```

## 🀝 Acknowledgements

This project builds upon prior work in self-supervised learning, medical imaging, and transformer-based representation learning. We especially acknowledge [**MONAI**](https://project-monai.github.io/) for their awesome framework and the [**timm**](https://timm.fast.ai/) & [**lightly**](https://docs.lightly.ai/self-supervised-learning/) Python libraries for providing 2D PyTorch models (timm) and object-oriented self-supervised learning methods (lightly), from which we adapted parts of the code for 3D.

[![Star History Chart](https://api.star-history.com/svg?repos=cclaess/SPECTRE&type=Date)](https://star-history.com/#cclaess/SPECTRE&Date)