EricPolaris Astaxanthin commited on
Commit
971142f
·
0 Parent(s):

Duplicate from Astaxanthin/KEEP

Browse files

Co-authored-by: Xiao Zhou <Astaxanthin@users.noreply.huggingface.co>

Files changed (9) hide show
  1. .gitattributes +35 -0
  2. README.md +130 -0
  3. config.json +43 -0
  4. example.tif +0 -0
  5. model.safetensors +3 -0
  6. modeling_keep.py +88 -0
  7. pytorch_model.bin +3 -0
  8. tokenizer_config.json +4 -0
  9. vocab.txt +0 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ ---
4
+
5
+ # Model Card for KEEP
6
+
7
+ <!-- Provide a quick summary of what the model is/does. -->
8
+
9
+ [Paper](https://www.cell.com/cancer-cell/fulltext/S1535-6108(26)00058-9) | [Github](https://github.com/MAGIC-AI4Med/KEEP) | [Webpage](https://loiesun.github.io/keep/) | [Cite](#citation)
10
+
11
+ **KEEP** (**K**nowledg**E**-**E**nhanced **P**athology) is a foundation model designed for cancer diagnosis that integrates disease knowledge into vision-language pre-training. It utilizes a comprehensive disease knowledge graph (KG) containing 11,454 human diseases and 139,143 disease attributes, such as synonyms, definitions, and hierarchical relationships. KEEP reorganizes millions of publicly available noisy pathology image-text pairs into 143K well-structured semantic groups based on the hierarchical relations of the disease KG. By incorporating disease knowledge into the alignment process, KEEP achieves more nuanced image and text representations. The model is validated on 18 diverse benchmarks with over 14,000 whole-slide images (WSIs), demonstrating state-of-the-art performance in zero-shot cancer diagnosis, including an average sensitivity of 89.8% for cancer detection across 7 cancer types. KEEP also excels in subtyping rare cancers, achieving strong generalizability in diagnosing rare tumor subtypes.
12
+
13
+
14
+ ## Model Details
15
+
16
+ ### Model Description
17
+
18
+ <!-- Provide a longer summary of what this model is. -->
19
+
20
+
21
+ - **Developed by:** MAGIC-AI4Med team from Shanghai Jiao Tong University and Shanghai AI Lab.
22
+ - **Model type:** Vision-language models (vision encoder: ViT-L/16; text encoder: Bert)
23
+ - **Pretrain datasets:** 143K pathology semantic groups, each with a single caption and multiple images.
24
+ - **License:** MIT
25
+
26
+ ### Model Sources [optional]
27
+
28
+ <!-- Provide the basic links for the model. -->
29
+
30
+ - **Repository:** https://github.com/MAGIC-AI4Med/KEEP
31
+ - **Paper [optional]:** https://www.cell.com/cancer-cell/fulltext/S1535-6108(26)00058-9
32
+ - **Demo [optional]:** [More Information Needed]
33
+
34
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
35
+
36
+ ## Direct Use
37
+
38
+ <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
39
+
40
+ ```python
41
+ from transformers import AutoModel, AutoTokenizer
42
+ from torchvision import transforms
43
+ from PIL import Image
44
+
45
+ model = AutoModel.from_pretrained("Astaxanthin/KEEP", trust_remote_code=True)
46
+ tokenizer = AutoTokenizer.from_pretrained("Astaxanthin/KEEP", trust_remote_code=True)
47
+ model.eval()
48
+ transform = transforms.Compose([
49
+ transforms.Resize(size=224, interpolation=transforms.InterpolationMode.BICUBIC),
50
+ transforms.CenterCrop(size=(224, 224)),
51
+ transforms.ToTensor(),
52
+ transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
53
+ ])
54
+
55
+ example_image_path = './example.tif'
56
+ example_text = ['an H&E image of breast invasive carcinoma.', 'an H&E image of normal tissue.', 'an H&E image of lung adenocarcinoma.']
57
+
58
+ img_input = transform(Image.open(example_image_path).convert('RGB')).unsqueeze(0)
59
+ token_input = tokenizer(example_text,max_length=256,padding='max_length',truncation=True, return_tensors='pt')
60
+
61
+ img_feature = model.encode_image(img_input)
62
+ text_feature = model.encode_text(token_input)
63
+
64
+ ```
65
+
66
+ ## Evaluation
67
+
68
+ <!-- This section describes the evaluation protocols and provides the results. -->
69
+
70
+ ### Testing Data
71
+
72
+ <!-- This should link to a Dataset Card if possible. -->
73
+
74
+ We present benchmark results for a range of representative tasks. A complete set of benchmarks can be found in the [paper](https://arxiv.org/abs/2412.18***). These results will be updated with each new iteration of KEEP.
75
+
76
+
77
+ ### Results
78
+
79
+
80
+ #### Zero-shot Cancer Region Segmentation (DICE)
81
+ | Models | PLIP[[1]](https://www.nature.com/articles/s41591-023-02504-3) | QuiltNet [[2]](https://proceedings.neurips.cc/paper_files/paper/2023/hash/775ec578876fa6812c062644964b9870-Abstract-Datasets_and_Benchmarks.html) | MI-Zero (Pub) [[3]](https://openaccess.thecvf.com/content/CVPR2023/html/Lu_Visual_Language_Pretrained_Multiple_Instance_Zero-Shot_Transfer_for_Histopathology_Images_CVPR_2023_paper.html) | CONCH [[4]](https://www.nature.com/articles/s41591-024-02856-4) | **KEEP(Ours)** |
82
+ |:---------------|--------------:|---------------------:|-------------------------:|-----------------:|------------------:|
83
+ | CAMELYON16 | 0.253 | 0.157 | 0.186 | 0.292 | **0.361** |
84
+ | PANDA | 0.295 | 0.309 | 0.276 | 0.315 | **0.334** |
85
+ | AGGC22 | 0.284 | 0.282 | 0.324 | 0.449 | **0.530** |
86
+
87
+ #### Zero-shot Cancer Detection (AUROC)
88
+ | Models | CHIEF[[1]](https://www.nature.com/articles/s41586-024-07894-z) | PLIP [[2]](https://www.nature.com/articles/s41591-023-02504-3) | QuiltNet [[3]](https://proceedings.neurips.cc/paper_files/paper/2023/hash/775ec578876fa6812c062644964b9870-Abstract-Datasets_and_Benchmarks.html) | MI-Zero (Pub) [[4]](https://openaccess.thecvf.com/content/CVPR2023/html/Lu_Visual_Language_Pretrained_Multiple_Instance_Zero-Shot_Transfer_for_Histopathology_Images_CVPR_2023_paper.html) | CONCH [[5]](https://www.nature.com/articles/s41591-024-02856-4) | KEEP(Ours) |
89
+ |:---------------|--------------:|--------------------:|-----------------:|-----------------:|------------------:| -----------------:|
90
+ | CPTAC-CM | 0.915 | 0.970 | 0.972 | 0.985 | **0.994** | **0.994** |
91
+ | CPTAC-CCRCC | 0.723 | 0.330 | 0.755 | 0.886 | 0.871 | **0.999** |
92
+ | CPTAC-PDA | 0.825 | 0.391 | 0.464 | 0.796 | 0.920 | **0.929** |
93
+ | CPTAC-UCEC | 0.955 | 0.945 | 0.973 | 0.979 | 0.996 | **0.998** |
94
+ | CPTAC-LSCC | 0.901 | 0.965 | 0.966 | 0.910 | **0.987** | 0.983 |
95
+ | CPTAC-HNSCC | 0.946 | 0.898 | 0.874 | 0.918 | **0.982** | 0.976 |
96
+ | CPTAC-LUAD | 0.891 | 0.988 | 0.991 | 0.981 | 0.999 | **1.000** |
97
+
98
+ #### Zero-shot Cancer Subtyping (BACC)
99
+ | Models | PLIP [[1]](https://www.nature.com/articles/s41591-023-02504-3) | QuiltNet [[2]](https://proceedings.neurips.cc/paper_files/paper/2023/hash/775ec578876fa6812c062644964b9870-Abstract-Datasets_and_Benchmarks.html) | MI-Zero (Pub) [[3]](https://openaccess.thecvf.com/content/CVPR2023/html/Lu_Visual_Language_Pretrained_Multiple_Instance_Zero-Shot_Transfer_for_Histopathology_Images_CVPR_2023_paper.html) | CONCH [[4]](https://www.nature.com/articles/s41591-024-02856-4) | **KEEP(Ours)** |
100
+ |:---------------|--------------:|---------------------------:|-------------------------:|-----------------:|------------------:|
101
+ | TCGA-BRCA | 0.519 | 0.500 | 0.633 | 0.727 | **0.774** |
102
+ | TCGA-NSCLC | 0.699 | 0.667 | 0.753 | 0.901 | **0.902** |
103
+ | TCGA-RCC | 0.735 | 0.755 | 0.908 | 0.921 | **0.926** |
104
+ | TCGA-ESCA | 0.614 | 0.746 | 0.954 | 0.923 | **0.977** |
105
+ | TCGA-BRAIN | 0.361 | 0.346 | 0.361 | 0.453 | **0.604** |
106
+ | UBC-OCEAN | 0.343 | 0.469 | 0.652 | **0.674** | 0.661 |
107
+ | CPTAC-NSCLC | 0.647 | 0.607 | 0.643 | 0.836 | **0.863** |
108
+ | EBRAINS | 0.096 | 0.093 | 0.325 | 0.371 | **0.456** |
109
+
110
+ ### Summary
111
+
112
+ Validated on 18 diverse benchmarks with more than 14,000 whole slide images (WSIs), KEEP achieves state-of-the-art performance in zero-shot cancer diagnostic tasks. Notably, for cancer detection, KEEP demonstrates an average sensitivity of 89.8% at a specificity of 95.0% across 7 cancer types, significantly outperforming vision-only foundation models and highlighting its promising potential for clinical application. For cancer subtyping, KEEP achieves a median balanced accuracy of 0.456 in subtyping 30 rare brain cancers, indicating strong generalizability for diagnosing rare tumors.
113
+
114
+
115
+ ## Citation
116
+
117
+ <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
118
+
119
+ ```
120
+ @article{ZHOU2026,
121
+ title = {Knowledge-enhanced pretraining for vision-language pathology foundation model on cancer diagnosis},
122
+ journal = {Cancer Cell},
123
+ year = {2026},
124
+ issn = {1535-6108},
125
+ doi = {https://doi.org/10.1016/j.ccell.2026.01.019},
126
+ url = {https://www.sciencedirect.com/science/article/pii/S1535610826000589},
127
+ author = {Xiao Zhou and Luoyi Sun and Dexuan He and Wenbin Guan and Ge Wang and Ruifen Wang and Lifeng Wang and Xiaojun Yuan and Xin Sun and Ya Zhang and Kun Sun and Yanfeng Wang and Weidi Xie},
128
+ keywords = {pathology foundation model, cancer diagnosis, vision-language, knowledge enhancement},
129
+ }
130
+ ```
config.json ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "KEEPModel"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "modeling_keep.KEEPConfig",
7
+ "AutoModel": "modeling_keep.KEEPModel"
8
+ },
9
+ "model_type": "keep",
10
+ "projection_dim": 768,
11
+ "text_config": {
12
+ "attention_probs_dropout_prob": 0.1,
13
+ "hidden_act": "gelu",
14
+ "hidden_dropout_prob": 0.1,
15
+ "hidden_size": 768,
16
+ "initializer_range": 0.02,
17
+ "intermediate_size": 3072,
18
+ "layer_norm_eps": 1e-12,
19
+ "max_position_embeddings": 512,
20
+ "model_type": "bert",
21
+ "num_attention_heads": 12,
22
+ "num_hidden_layers": 12,
23
+ "type_vocab_size": 2,
24
+ "vocab_size": 30522
25
+ },
26
+ "torch_dtype": "float32",
27
+ "transformers_version": "4.46.3",
28
+ "vision_config": {
29
+ "attn_drop_rate": 0.0,
30
+ "drop_rate": 0.0,
31
+ "dynamic_img_size": true,
32
+ "hidden_size": 1024,
33
+ "img_size": 224,
34
+ "init_values": 1e-05,
35
+ "mlp_ratio": 4.0,
36
+ "model_type": "vit",
37
+ "num_classes": 0,
38
+ "num_heads": 16,
39
+ "num_layers": 24,
40
+ "patch_size": 16,
41
+ "qkv_bias": true
42
+ }
43
+ }
example.tif ADDED
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:82f610d5359aca67b5fd5d841009f26db430ae78d0693743589c0a727b0a146d
3
+ size 1656902084
modeling_keep.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig, PreTrainedModel, BertModel, BertConfig
2
+ import timm
3
+ import torch.nn as nn
4
+ import torch
5
+ import numpy
6
+ from torchvision import transforms
7
+ from PIL import Image
8
+
9
+ class RenameLayerScale(nn.Module):
10
+ def __init__(
11
+ self,
12
+ dim: int,
13
+ init_values: float = 1e-5,
14
+ inplace: bool = False,
15
+ ) -> None:
16
+ super().__init__()
17
+ self.inplace = inplace
18
+ self.weight = nn.Parameter(init_values * torch.ones(dim))
19
+
20
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
21
+ return x.mul_(self.weight) if self.inplace else x * self.weight
22
+
23
+ timm.models.vision_transformer.LayerScale = RenameLayerScale
24
+
25
+ class KEEPConfig(PretrainedConfig):
26
+ model_type = "keep" #
27
+
28
+ def __init__(
29
+ self,
30
+ vision_config=None, # Vision Encoder
31
+ text_config=None, # Text Encoder
32
+ projection_dim=768,
33
+ **kwargs,
34
+ ):
35
+ super().__init__(**kwargs)
36
+ self.vision_config = vision_config
37
+ self.text_config = text_config
38
+ self.projection_dim = projection_dim
39
+
40
+ class KEEPModel(PreTrainedModel):
41
+ config_class = KEEPConfig #
42
+
43
+ def __init__(self, config):
44
+ super().__init__(config)
45
+
46
+ # Vision Encoder
47
+ vision_config = config.vision_config
48
+ self.visual = timm.create_model(
49
+ "vit_large_patch16_224",
50
+ pretrained=False,
51
+ img_size=vision_config["img_size"],
52
+ patch_size=vision_config["patch_size"],
53
+ init_values=vision_config["init_values"],
54
+ num_classes=vision_config["num_classes"],
55
+ )
56
+
57
+ self.visual_head = nn.Sequential(
58
+ nn.Linear(self.visual.num_features, config.projection_dim),
59
+ nn.GELU(),
60
+ nn.Linear(config.projection_dim, config.projection_dim)
61
+ )
62
+
63
+ # Text Encoder
64
+ text_config = BertConfig(**config.text_config)
65
+ self.text = BertModel(text_config)
66
+
67
+ self.logit_scale = nn.Parameter(torch.ones([]) * numpy.log(1 / 0.04))
68
+
69
+ def encode_image(self, image_inputs):
70
+ vision_features = self.visual(image_inputs) # [batch_size, vision_dim]
71
+ vision_features = torch.nn.functional.normalize(self.visual_head(vision_features), dim=-1) # [batch_size, projection_dim]
72
+
73
+ return vision_features
74
+
75
+ def encode_text(self, text_inputs):
76
+ text_features = torch.nn.functional.normalize(self.text(**text_inputs).pooler_output, dim=-1) # [batch_size, text_dim]
77
+ return text_features
78
+
79
+
80
+ def forward(self, image_inputs, text_inputs):
81
+ vision_features = self.encode_image(image_inputs)
82
+
83
+ text_features = self.encode_text(text_inputs)
84
+
85
+ return {
86
+ "vision_features": vision_features,
87
+ "text_features": text_features
88
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:526a677bf714388d2485a45f5c372505a9874d56a86645b154e2d46ab60d87ca
3
+ size 1657016149
tokenizer_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "do_lower_case": true,
3
+ "tokenizer_class": "BertTokenizer"
4
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff