Good-Lab commited on
Commit
2310750
·
verified ·
1 Parent(s): 574879c

Correct README: 512-d shared space, fix package import + GitHub URL, fill in verified eval numbers

Browse files

Fixes from end-to-end reproducer validation (zinagoodlab/spatialwhisperer PR #1):
- shared embedding dim: 2048 -> 512 (2048 was the pooled BioBERT feature dim, not the projected shared space)
- code example: from spatialwhisperer import load_spatialwhisperer_model (was cellwhisperer.utils.model_io.load_cellwhisperer_model, pre-rename)
- GitHub URL: zinagoodlab/spatialwhisperer (was Good-Lab, which is the HF org only)
- removed reference to non-existent examples/zero_shot_celltype.py
- dropped "private repo access" framing + pass api_keys/huggingface_write shell example
- evaluation table: filled in Lizard (0.764) and PanNuke (0.689) AUROCs with verified-reproduced numbers; dropped the suspect +XX% column (baseline comparisons live in the paper)
- ckpt size: ~530 MB (was ~505 MB)

Files changed (1) hide show
  1. README.md +72 -51
README.md CHANGED
@@ -1,94 +1,119 @@
1
  ---
2
  license: cc-by-nc-4.0
3
  tags:
4
- - histopathology
5
- - spatial-transcriptomics
6
- - multimodal
7
- - vision-language
8
- - clip
9
- - cell-type-annotation
 
 
10
  library_name: pytorch
11
- pipeline_tag: zero-shot-image-classification
12
  ---
13
 
14
  # SpatialWhisperer
15
 
16
- SpatialWhisperer is a trimodal embedding model that aligns hematoxylin & eosin (H&E) image patches, gene-expression profiles, and free-text descriptions into a shared 2048-dimensional space. It enables zero-shot cell-type annotation of H&E patches and natural-language querying over histopathology and spatial transcriptomics data.
17
 
18
- This repository hosts the main checkpoint (seed=0) from the ICML 2026 paper *[Transitive Representation Learning Enhances Histopathology Annotation](https://openreview.net/forum?id=Ze7U293Zw4)* (Schaefer et al., PMLR vol. 306).
19
 
20
- ## Model architecture
 
21
 
22
- Three encoders project into a shared embedding space:
23
 
24
- | Modality | Encoder | Freezing |
25
- |----------------|------------------|----------|
26
- | Image (H&E) | UNI2 | locked |
27
- | Transcriptome | Geneformer (12L) | locked |
28
- | Text | BioBERT v1.1 | unfrozen |
29
 
30
- Following LiT convention, the freezing pattern is **LUL** (image locked, text unlocked, transcriptome locked). Only the text tower and the three projection heads are trained. Projection dimension is 2048.
31
 
32
  ## Training data
33
 
34
- Three paired datasets cover the three modality pairs:
35
 
36
  - **HEST-1K** — H&E ↔ spatial gene expression (Visium-style spots)
37
- - **cellxgene_census** — gene expression ↔ free-text cell/sample metadata
38
  - **ARCHS4/GEO** — gene expression ↔ free-text sample descriptions
39
 
40
- Training was 4 epochs with AdamW at learning rate 1e-5 and cosine schedule (warmup 3%), batch size 512, on a single H100 GPU. This checkpoint reflects epoch 3, global step 14624.
41
 
42
- ## Evaluation
 
 
43
 
44
- Reported AUROC on cell-type benchmarks (mean across cell types):
45
 
46
- | Benchmark | SpatialWhisperer | Best published baseline | Δ rel. |
47
- |-----------|------------------|-------------------------|---------|
48
- | PathoCell | **0.630** | 0.554 | +13.7% |
49
- | Lizard | (see paper) | — | +15.9% |
50
- | PanNuke | (see paper) | — | +13.7% |
51
 
52
- Modality-pair benchmarks (Tabula Sapiens, HEST-1K, Skin Conditions) confirm the trimodal model retains per-pair performance under low-n subsampling. See the paper for full numbers.
53
 
54
- ## How to use
 
 
 
 
 
55
 
56
- The checkpoint is a stripped Lightning state-dict (~505 MB, 236 tensors covering the trained BioBERT text tower and the three 2048-d projection heads) plus its `hyper_parameters` block. **Foundation model weights are NOT included** — the locked UNI2 image encoder and locked Geneformer transcriptome encoder are re-instantiated at load time from their original providers (and remain under their respective licenses). The ckpt's `hyper_parameters.model_config.use_cache = True` flag triggers the `FrozenCachedModel` wrapping that excludes the locked towers from `state_dict` during load.
57
 
58
- Loading requires the cellwhisperer code at <https://github.com/Good-Lab/spatialwhisperer> (model code) and the foundation models (UNI2, Geneformer, BioBERT v1.1), which are downloaded by the cellwhisperer setup scripts.
59
 
60
  ```python
61
- from cellwhisperer.utils.model_io import load_cellwhisperer_model
62
 
63
- model, tokenizer, transcriptome_proc, image_proc = load_cellwhisperer_model(
64
- model_path="hf://Good-Lab/spatialwhisperer"
65
- )
66
- # model is a TranscriptomeTextDualEncoderLightning in eval mode
 
 
67
  ```
68
 
69
- While the repo is private, export a token first:
70
 
71
- ```bash
72
- export HUGGINGFACE_TOKEN=$(pass api_keys/huggingface_write) # or any read token with access
73
- ```
 
 
 
 
 
 
74
 
75
- To compute image–text similarities for zero-shot cell-type annotation, encode patches and class-name strings, then take cosine similarity in the shared 2048-d space. See `examples/zero_shot_celltype.py` in the model code repository.
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  ## Intended use & limitations
78
 
79
- **Intended.** Research on multimodal histopathology, cell-type annotation, spatial transcriptomics analysis, and natural-language querying over H&E and gene-expression data.
80
 
81
- **Not intended.** Clinical diagnosis or treatment decisions. The model was trained on academic datasets and is not validated for clinical use.
82
 
83
- **Known limitations.**
84
  - Trained on Visium-scale spots (~55 μm); finer-grained image–expression alignment is not guaranteed.
85
  - BioBERT vocabulary constrains the text tower; rare technical terms may be out-of-distribution.
86
  - The image tower (UNI2) is locked; performance on tissue types poorly represented in UNI2's pretraining will be lower.
87
 
88
- ## File contents
89
 
90
- - `spatialwhisperer.ckpt` — Lightning checkpoint (state_dict + hyper_parameters; optimizer/scheduler state stripped).
91
- - `README.md` — this card.
92
 
93
  ## Citation
94
 
@@ -106,7 +131,3 @@ To compute image–text similarities for zero-shot cell-type annotation, encode
106
  url = {https://openreview.net/forum?id=Ze7U293Zw4}
107
  }
108
  ```
109
-
110
- ## License
111
-
112
- CC BY-NC 4.0 (research use). Foundation model weights (UNI2, Geneformer, BioBERT) carry their own licenses; please consult upstream repositories.
 
1
  ---
2
  license: cc-by-nc-4.0
3
  tags:
4
+ - biology
5
+ - histopathology
6
+ - spatial-transcriptomics
7
+ - multimodal
8
+ - pathology
9
+ - gene-expression
10
+ - biobert
11
+ - vision-language
12
  library_name: pytorch
 
13
  ---
14
 
15
  # SpatialWhisperer
16
 
17
+ A trimodal embedding model that aligns hematoxylin & eosin (H&E) image patches, gene-expression profiles, and free-text descriptions into a shared 512-dimensional space. Enables zero-shot cell-type annotation of H&E patches and natural-language querying over histopathology and spatial transcriptomics data.
18
 
19
+ This checkpoint (seed 0) is from the ICML 2026 paper *[Transitive Representation Learning Enhances Histopathology Annotation](https://openreview.net/forum?id=Ze7U293Zw4)* (Schaefer et al., PMLR vol. 306). The paper refers to this configuration as the **Trimodal** model: three encoders trained on two paired-modality datasets (transcriptome↔text and image↔transcriptome) that together span three modalities.
20
 
21
+ - **Code & reproduction pipeline:** <https://github.com/zinagoodlab/spatialwhisperer>
22
+ - **Paper:** <https://openreview.net/forum?id=Ze7U293Zw4>
23
 
24
+ ## Architecture
25
 
26
+ | Modality | Encoder | Status |
27
+ |----------|---------|--------|
28
+ | Image (H&E) | UNI2 (`MahmoodLab/UNI2-h`) | locked |
29
+ | Transcriptome | Geneformer-12L-30M | locked |
30
+ | Text | BioBERT v1.1 | trained |
31
 
32
+ Three projection heads map each encoder's pooled features into a shared 512-dimensional space. Only the text tower and the three projection heads are trained.
33
 
34
  ## Training data
35
 
36
+ Three paired-modality datasets:
37
 
38
  - **HEST-1K** — H&E ↔ spatial gene expression (Visium-style spots)
39
+ - **CellxGene Census** — gene expression ↔ free-text cell/sample metadata
40
  - **ARCHS4/GEO** — gene expression ↔ free-text sample descriptions
41
 
42
+ Training: 4 epochs, AdamW (lr 1e-5), cosine schedule (3% warmup), batch size 512, single H100. This checkpoint is from epoch 3, global step 14624.
43
 
44
+ ## What this checkpoint contains
45
+
46
+ - `spatialwhisperer.ckpt` — Lightning state-dict (~530 MB, 236 tensors: trained BioBERT text tower + three projection heads) plus the `hyper_parameters` block. Optimizer/scheduler state is stripped.
47
 
48
+ **The locked foundation-model weights are NOT included.** UNI2 and Geneformer are re-instantiated at load time from their original providers. The `load_spatialwhisperer_model()` convenience wrapper fetches both on first call.
49
 
50
+ ## Usage
 
 
 
 
51
 
52
+ Install the [code repository](https://github.com/zinagoodlab/spatialwhisperer) (pixi env), then:
53
 
54
+ ```python
55
+ from spatialwhisperer import load_spatialwhisperer_model
56
+
57
+ model, tokenizer, transcriptome_processor, image_processor = load_spatialwhisperer_model()
58
+ # model: TranscriptomeTextDualEncoderLightning (frozen, eval mode, on CUDA if available)
59
+ ```
60
 
61
+ First call downloads the SpatialWhisperer checkpoint plus UNI2 and Geneformer weights; subsequent calls load from cache.
62
 
63
+ Each `get_<modality>_features` call returns `(pooled_features, projected_embed_in_shared_space)`. The second element is the 512-D shared-space embedding to compare across modalities.
64
 
65
  ```python
66
+ import torch
67
 
68
+ prompts = ["cytotoxic T cells", "plasma cells"]
69
+ text_inputs = tokenizer(prompts, return_tensors="pt", padding=True).to(model.device)
70
+
71
+ with torch.no_grad():
72
+ _, text_emb = model.model.get_text_features(normalize_embeds=True, **text_inputs)
73
+ print(text_emb.shape) # (2, 512)
74
  ```
75
 
76
+ Image and transcriptome embeddings follow the same pattern; see the [GitHub README](https://github.com/zinagoodlab/spatialwhisperer#use-the-model) for complete examples covering all three modalities.
77
 
78
+ ## Foundation-model setup
79
+
80
+ UNI2 (`MahmoodLab/UNI2-h`) is a gated HuggingFace model. Before first use:
81
+
82
+ 1. Accept the terms at <https://huggingface.co/MahmoodLab/UNI2-h>.
83
+ 2. Make a read token visible to your environment — the loader checks `HF_TOKEN` / `HUGGINGFACE_TOKEN`, otherwise falls back to `huggingface_hub`'s on-disk cache. The simplest setup is:
84
+ ```bash
85
+ huggingface-cli login # paste your read token
86
+ ```
87
 
88
+ Geneformer downloads without gating.
89
+
90
+ ## Evaluation
91
+
92
+ The accompanying [code repository](https://github.com/zinagoodlab/spatialwhisperer) reproduces every paper benchmark with one command: `pixi run snakemake -j N paper_all`. Verified end-to-end on a fresh ILC ampere4 clone (2026-05-31):
93
+
94
+ | Benchmark | Macro AUROC (this ckpt, seed 0) |
95
+ |-----------|-----------------|
96
+ | PathoCell CRC (13-class) | 0.630 |
97
+ | Lizard (3-class reduced) | 0.764 |
98
+ | PanNuke (4-class reduced) | 0.689 |
99
+ | Kriegsmann Skin Conditions (16-class, clinical labels) | 0.698 |
100
+
101
+ These match the paper's reported Trimodal seed-0 numbers exactly. Comparisons to CONCH, PLIP, OmiCLIP, and other baselines are in the paper.
102
 
103
  ## Intended use & limitations
104
 
105
+ **Intended:** research on multimodal histopathology, zero-shot cell-type annotation, spatial transcriptomics analysis, and natural-language querying over H&E and gene-expression data.
106
 
107
+ **Not intended:** clinical diagnosis or treatment decisions. The model was trained on academic datasets and is not validated for clinical use.
108
 
109
+ **Limitations:**
110
  - Trained on Visium-scale spots (~55 μm); finer-grained image–expression alignment is not guaranteed.
111
  - BioBERT vocabulary constrains the text tower; rare technical terms may be out-of-distribution.
112
  - The image tower (UNI2) is locked; performance on tissue types poorly represented in UNI2's pretraining will be lower.
113
 
114
+ ## License
115
 
116
+ **CC BY-NC 4.0** (research use). Foundation-model weights (UNI2, Geneformer, BioBERT) carry their own licenses; consult the upstream repositories.
 
117
 
118
  ## Citation
119
 
 
131
  url = {https://openreview.net/forum?id=Ze7U293Zw4}
132
  }
133
  ```