Skip to content

Commit 1f5b464

Browse files
Add GenBio-PathFM patch encoder (ViT-Giant, 4608-dim, JEDI training) (#9)
1 parent d310436 commit 1f5b464

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ An API and command line interface (CLI) are provided to allow users to download
6262
* Virchow2: https://huggingface.co/paige-ai/Virchow2
6363
* H-optimus-0: https://huggingface.co/bioptimus/H-optimus-0
6464
* H-optimus-1: https://huggingface.co/bioptimus/H-optimus-1
65+
* GenBio-PathFM: https://huggingface.co/genbio-ai/genbio-pathfm
6566
* CONCH: https://huggingface.co/MahmoodLab/CONCH
6667
* TITAN/CONCHv1.5: https://huggingface.co/MahmoodLab/TITAN
6768
* Phikon: https://huggingface.co/owkin/phikon

src/thunder/benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def benchmark(
2020
2121
where options are:
2222
- dataset: *bach*, *bracs*, *break_his*, *ccrcc*, *crc*, *esca*, *mhist*, *ocelot*, *pannuke*, *patch_camelyon*, *segpath_epithelial*, *segpath_lymphocytes*, *tcga_crc_msi*, *tcga_tils*, *tcga_uniform*, *wilds*
23-
- model: *hiboub*, *hiboul*, *hoptimus0*, *hoptimus1*, *midnight*, *phikon*, *phikon2*, *uni*, *uni2h*, *virchow*, *virchow2*, *conch*, *titan*, *keep*, *musk*, *plip*, *quiltnetb32*, *dinov2base*, *dinov2large*, *vitbasepatch16224in21k*, *vitlargepatch16224in21k*, *clipvitbasepatch32*, *clipvitlargepatch14*
23+
- model: *hiboub*, *hiboul*, *hoptimus0*, *hoptimus1*, *genbio-pathfm*, *midnight*, *phikon*, *phikon2*, *uni*, *uni2h*, *virchow*, *virchow2*, *conch*, *titan*, *keep*, *musk*, *plip*, *quiltnetb32*, *dinov2base*, *dinov2large*, *vitbasepatch16224in21k*, *vitlargepatch16224in21k*, *clipvitbasepatch32*, *clipvitlargepatch14*
2424
- task: *adversarial_attack*, *alignment_scoring*, *image_retrieval*, *knn*, *linear_probing*, *pre_computing_embeddings*, *segmentation*, *simple_shot*, *transformation_invariance*, *zero_shot_vlm*
2525
- loading_mode: *online_loading*, *image_pre_loading*, *embedding_pre_loading*
2626
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
model_name: genbio-pathfm
2+
type: safetensors
3+
vlm: false
4+
emb_dim: 4608
5+
ckpt_path: ${oc.env:THUNDER_BASE_DATA_FOLDER}/pretrained_ckpts/genbiopathfm/model.pth
6+
hf_tag: hf-hub:genbio-ai/genbio-pathfm

src/thunder/models/download.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
"bioptimus/H-optimus-1",
3939
"pytorch_model.bin",
4040
), # H-optimus 1 (https://huggingface.co/bioptimus/H-optimus-1)
41+
"genbio-pathfm": (
42+
"genbio-ai/genbio-pathfm",
43+
"model.pth",
44+
), # GenBio-PathFM (https://huggingface.co/genbio-ai/genbio-pathfm)
4145
"provgigapath": (
4246
"prov-gigapath/prov-gigapath",
4347
"pytorch_model.bin",
@@ -156,6 +160,7 @@ def download_models(models: Union[List[str], str]) -> None:
156160
* hoptimus0
157161
* h0mini
158162
* hoptimus1
163+
* genbio-pathfm
159164
* provgigapath
160165
* conch
161166
* titan

src/thunder/models/pretrained_models.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ def get_model(model_cfg: dict, device: str):
126126
model, transform = get_titan(model_cfg.ckpt_path)
127127
elif model_cfg.model_name == "midnight":
128128
model, transform = get_midnight(model_cfg.ckpt_path)
129+
elif model_cfg.model_name == "genbio-pathfm":
130+
model, transform = get_genbio_pathfm(model_cfg.ckpt_path)
129131
else:
130132
model, transform, tokenizer = get_from_safetensors(
131133
model_cfg.ckpt_path, use_fast="dinov3" in model_cfg.model_name
@@ -366,6 +368,8 @@ def extract_embedding(src, pretrained_model, task_type="linear_probing"):
366368
emb = pretrained_model.trunk(src, return_all_tokens=True)[:, 1:]
367369
elif model_cfg.model_name == "openmidnight":
368370
emb = pretrained_model.get_intermediate_layers(src)[0]
371+
elif model_cfg.model_name == "genbio-pathfm":
372+
emb = pretrained_model.forward_with_patches(src)[1]
369373
else:
370374
emb = pretrained_model.forward_features(src)[:, 1:]
371375

@@ -394,6 +398,7 @@ def get_model_from_name(model_name: str, device: str):
394398
* hoptimus0
395399
* h0mini
396400
* hoptimus1
401+
* genbio-pathfm
397402
* provgigapath
398403
* conch
399404
* titan
@@ -756,3 +761,36 @@ def get_titan(ckpt_path: str):
756761
model, transform = titan.return_conch()
757762

758763
return model, transform
764+
765+
766+
def get_genbio_pathfm(ckpt_path: str):
767+
"""
768+
Adapted from:
769+
- https://github.com/genbio-ai/genbio-pathfm
770+
771+
:param ckpt_path: path to the stored checkpoint.
772+
"""
773+
try:
774+
from genbio_pathfm.model import GenBio_PathFM_Inference
775+
except ImportError:
776+
raise ImportError(
777+
"In order to use GenBio-PathFM, please run the following: 'pip install git+https://github.com/genbio-ai/genbio-pathfm.git'"
778+
)
779+
780+
from torchvision import transforms
781+
782+
# Model
783+
model = GenBio_PathFM_Inference(ckpt_path, device="cpu")
784+
785+
# Transform
786+
transform = transforms.Compose([
787+
transforms.Resize((224,224)),
788+
transforms.ToTensor(),
789+
transforms.Normalize(
790+
mean=(0.697, 0.575, 0.728),
791+
std=(0.188, 0.240, 0.187)
792+
),
793+
])
794+
795+
return model, transform
796+

src/thunder/utils/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ModelConstants(Enum):
99
"hoptimus0",
1010
"h0mini",
1111
"hoptimus1",
12+
"genbio-pathfm",
1213
"kaiko_vits8",
1314
"kaiko_vits16",
1415
"kaiko_vitb8",

0 commit comments

Comments
 (0)