Skip to content
Merged

Dev #49

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,13 @@ wandb/
*.pth

# VS Code
.vscode/
.vscode/

# Huggingface
hf_export/spectre/
hf_export/imgs/
hf_export/model.safetensors
hf_export/config.json
hf_export/README.md
hf_export/LICENSE
hf_export/LICENSE_MODELS
53 changes: 47 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
📢 [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)!
Expand All @@ -12,8 +14,8 @@
<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"><img alt="Model weights" src="https://img.shields.io/badge/models-Hugging%20Face-yellow?style=flat-square&cacheSeconds=0" /></a>
<a href="https://arxiv.org/abs/2511.17209"><img alt="Paper" src="https://img.shields.io/badge/paper-arXiv-b31b1b?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">
Expand All @@ -27,14 +29,25 @@ SPECTRE has been trained on a large cohort of **open-source CT scans** of the **
This repository provides pretrained SPECTRE models together with tools for fine-tuning and evaluation.

## 🧠 Pretrained Models
The pretrained SPECTRE model can easily be imported as follows:
The pretrained SPECTRE model can easily be imported using the `transformers` library

```python
from spectre import SpectreImageFeatureExtractor, MODEL_CONFIGS
import torch
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_CONFIGS
config = MODEL_CONFIGS['spectre-large-pretrained']
model = SpectreImageFeatureExtractor.from_config(config)
```

A simple forward pass would look like:
```python
import torch

model.eval()

# Dummy input: (batch, crops, channels, height, width, depth)
Expand Down Expand Up @@ -94,12 +107,40 @@ This repository is organized as follows:

## ⚙️ Setting Up the Environment

To get up and running with SPECTRE, simply install our package using pip:
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.

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
Expand Down
32 changes: 32 additions & 0 deletions hf_export/configuration_spectre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from transformers import PretrainedConfig


class SpectreConfig(PretrainedConfig):
model_type = "spectre"

def __init__(
self,
backbone_name="vit_large_patch16_128",
backbone_kwargs={
"num_classes": 0,
"global_pool": '',
"pos_embed": "rope",
"rope_kwargs": {"base": 1000.0},
"init_values": 1.0,
},
Comment on lines +7 to +16
feature_combiner_name="feat_vit_large",
feature_combiner_kwargs={
"num_classes": 0,
"global_pool": "",
"pos_embed": "rope",
"rope_kwargs": {"base": 100.0},
"init_values": 1.0,
},
**kwargs,
):
super().__init__(**kwargs)

self.backbone_name = backbone_name
self.backbone_kwargs = backbone_kwargs or {}
self.feature_combiner_name = feature_combiner_name
self.feature_combiner_kwargs = feature_combiner_kwargs or {}
Comment on lines +10 to +32
13 changes: 13 additions & 0 deletions hf_export/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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
41 changes: 41 additions & 0 deletions hf_export/modeling_spectre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import torch
from transformers import PreTrainedModel
from transformers.modeling_outputs import BaseModelOutput

from spectre.model import SpectreImageFeatureExtractor
try:
from .configuration_spectre import SpectreConfig
except ImportError:
from configuration_spectre import SpectreConfig


class SpectreModel(PreTrainedModel):
config_class = SpectreConfig
base_model_prefix = "spectre"
main_input_name = "pixel_values"

def __init__(self, config):
super().__init__(config)

self.model = SpectreImageFeatureExtractor(
backbone_name=config.backbone_name,
backbone_kwargs=config.backbone_kwargs,
feature_combiner_name=config.feature_combiner_name,
feature_combiner_kwargs=config.feature_combiner_kwargs,
)

self.post_init()

def forward(
self,
pixel_values: torch.Tensor,
grid_size=None,
return_dict=False,
**kwargs,
):
outputs = self.model(pixel_values, grid_size=grid_size)
Comment on lines +33 to +36

if not return_dict:
return outputs

return BaseModelOutput(last_hidden_state=outputs)
38 changes: 30 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,49 @@ keywords = ["medical", "ct", "self-supervised", "vision", "deep-learning"]

dependencies = [
"torch",
"wandb",
"numpy",
"monai[all]",
"timm",
"transformers",
"huggingface_hub",
"loralib",
]

[project.optional-dependencies]
training = [
"wandb",
"monai",
"nibabel",
"SimpleITK",
"timm",
"scipy",
"pandas",
"openpyxl",
"tqdm",
"accelerate",
"omegaconf",
"transformers",
"tokenizers",
"peft",
"loralib",
]

eval = [
"monai",
"nibabel",
"SimpleITK",
"pillow",
"scipy",
"scikit-learn",
"pandas",
"openpyxl",
"tqdm",
"matplotlib",
"seaborn",
"scikit-learn",
"umap-learn",
]

gds-cuda12 = [
"cupy-cuda12x",
"kvikio",
]

all = ["spectre-fm[training,eval,gds-cuda12]"]

[project.urls]
Homepage = "https://github.com/cclaess/SPECTRE"
Source = "https://github.com/cclaess/SPECTRE"
Expand Down
Loading