TommyNgx commited on
Commit
60b4256
·
verified ·
1 Parent(s): 772b929

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. .gitattributes +1 -0
  2. README.md +203 -0
  3. config.json +37 -0
  4. pytorch_model.bin +3 -0
  5. requesting_access.png +0 -0
  6. uni.jpg +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* 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
 
 
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
36
+ uni.jpg filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: cc-by-nc-nd-4.0
3
+ language:
4
+ - en
5
+ tags:
6
+ - histology
7
+ - pathology
8
+ - vision
9
+ - pytorch
10
+ - self-supervised
11
+ - vit
12
+ extra_gated_prompt: >-
13
+ This model and associated code are released under the CC-BY-NC-ND 4.0 license and may only be used for non-commercial, academic research purposes with proper attribution.
14
+ Any commercial use, sale, or other monetization of the UNI model and its derivatives, which include models trained on outputs from the UNI model or datasets created from the UNI model, is prohibited and requires prior approval.
15
+ Please note that the primary email used to sign up for your Hugging Face account must match your institutional email to receive approval. By downloading the model, you attest that all information (affiliation, research use) is correct and up-to-date.
16
+ Downloading the model requires prior registration on Hugging Face and agreeing to the terms of use.
17
+ By downloading this model, you agree not to distribute, publish or reproduce a copy of the model.
18
+ If another user within your organization wishes to use the UNI model, they must register as an individual user and agree to comply with the terms of use.
19
+ Users may not attempt to re-identify the deidentified data used to develop the underlying model.
20
+ If you are a commercial entity, please contact the corresponding author.
21
+ extra_gated_fields:
22
+ Full name (first and last): text
23
+ Current affiliation (no abbreviations): text
24
+ Type of Affiliation:
25
+ type: select
26
+ options:
27
+ - Academia
28
+ - Industry
29
+ - label: Other
30
+ value: other
31
+ Current and official institutional email (**this must match your primary email in your Hugging Face account, @gmail/@hotmail/@qq email domains will be denied**): text
32
+ Please explain your intended research use: text
33
+ I agree to all terms outlined above: checkbox
34
+ I agree to use this model for non-commercial, academic purposes only: checkbox
35
+ I agree not to distribute the model, if another user within your organization wishes to use the UNI model, they must register as an individual user: checkbox
36
+ metrics:
37
+ - accuracy
38
+ pipeline_tag: image-feature-extraction
39
+ library_name: timm
40
+ ---
41
+ # Updates:
42
+ UNI2, a successor to UNI, trained on over 200 million images from over 350k diverse H&E and IHC slides has been released! Model weights and instructions are available at: \[[Huggingface Repo](https://huggingface.co/MahmoodLab/UNI2-h)\]
43
+
44
+
45
+
46
+ # Model Card for UNI
47
+ \[[Journal Link](https://www.nature.com/articles/s41591-024-02857-3)\] | \[[Open Access Read Link](https://rdcu.be/dBMgh)\] | \[[Github Repo](https://github.com/mahmoodlab/uni)\] | \[[Cite](#bibtex)\]
48
+
49
+
50
+ ## What is UNI?
51
+
52
+ UNI is the largest pretrained vision encoder for histopathology (100M images, 100K WSIs) _**developed on internal neoplastic, infectious, inflamatory and normal tissue and also made publicly available**_. We show state-of-the-art performance across 34 clinical tasks, with strong performance gains on rare and underrepresented cancer types.
53
+ - _**Why use UNI?**_: UNI does not use open datasets and large public histology slide collections (TCGA, CPTAC, PAIP, CAMELYON, PANDA, and others in TCIA) for pretraining, which are routinely used in benchmark development in computational pathology. We make UNI available for the research community in building and evaluating pathology AI models without risk of data contamination on public benchmarks or private histopathology slide collections.
54
+
55
+ ![](https://huggingface.co/MahmoodLab/UNI/resolve/main/uni.jpg)
56
+
57
+ ## Requesting Access
58
+
59
+ As mentioned in the gated prompt, you must agree to the outlined terms of use, _**with the primary email for your HuggingFace account matching your institutional email**_. If your primary email is a personal email (@gmail/@hotmail/@qq) **your request will be denied**. To fix this, you can: (1) add your official institutional email to your HF account, and confirm your email address to verify, and (2) set your institutional email as your primary email in your HF account. Other reasons for your request access being denied include other mistakes in the form submitted, for example: full name includes abbreviations, affiliation is not spelled out, the described research use is not sufficient, or email domain address not recognized.
60
+
61
+ ![](https://huggingface.co/MahmoodLab/UNI/resolve/main/requesting_access.png)
62
+
63
+ ## Model Description
64
+
65
+ - **Developed by:** Mahmood Lab AI for Pathology @ Harvard/BWH
66
+ - **Model type:** Pretrained vision backbone (ViT-L/16 via DINOv2) for multi-purpose evaluation on histopathology images
67
+ - **Pretraining dataset:** Mass-100K, sourced from private histology collections (BWH / MGH), in addition to slides from the public GTEx consortium.
68
+ - **Repository:** https://github.com/mahmoodlab/UNI
69
+ - **Paper:** https://www.nature.com/articles/s41591-024-02857-3
70
+ - **License:** CC-BY-NC-ND-4.0
71
+
72
+ ### How To Use (Feature Extraction)
73
+
74
+ Following authentication (using ```huggingface_hub```), the ViT-L/16 model architecture with pretrained weights and image transforms for UNI can be directly loaded using the [timm](https://huggingface.co/docs/hub/en/timm) library. This method automatically downloads the model weights to the [huggingface_hub cache](https://huggingface.co/docs/huggingface_hub/en/guides/manage-cache) in your home directory (```~/.cache/huggingface/hub/models--MahmoodLab--UNI```), which ```timm``` will automatically find when using the commands below:
75
+
76
+ ```python
77
+ import timm
78
+ from timm.data import resolve_data_config
79
+ from timm.data.transforms_factory import create_transform
80
+ from huggingface_hub import login
81
+
82
+ login() # login with your User Access Token, found at https://huggingface.co/settings/tokens
83
+
84
+ # pretrained=True needed to load UNI weights (and download weights for the first time)
85
+ # init_values need to be passed in to successfully load LayerScale parameters (e.g. - block.0.ls1.gamma)
86
+ model = timm.create_model("hf-hub:MahmoodLab/uni", pretrained=True, init_values=1e-5, dynamic_img_size=True)
87
+ transform = create_transform(**resolve_data_config(model.pretrained_cfg, model=model))
88
+ model.eval()
89
+ ```
90
+
91
+ You can also download the model weights to a specified checkpoint location in your local directory. The ```timm``` library is still used for defining the ViT-L/16 model architecture. Pretrained weights and image transforms for UNI need to be manually loaded and defined.
92
+ ```python
93
+ import os
94
+ import torch
95
+ from torchvision import transforms
96
+ import timm
97
+ from huggingface_hub import login, hf_hub_download
98
+
99
+ login() # login with your User Access Token, found at https://huggingface.co/settings/tokens
100
+
101
+ local_dir = "../assets/ckpts/vit_large_patch16_224.dinov2.uni_mass100k/"
102
+ os.makedirs(local_dir, exist_ok=True) # create directory if it does not exist
103
+ hf_hub_download("MahmoodLab/UNI", filename="pytorch_model.bin", local_dir=local_dir, force_download=True)
104
+ model = timm.create_model(
105
+ "vit_large_patch16_224", img_size=224, patch_size=16, init_values=1e-5, num_classes=0, dynamic_img_size=True
106
+ )
107
+ model.load_state_dict(torch.load(os.path.join(local_dir, "pytorch_model.bin"), map_location="cpu"), strict=True)
108
+ transform = transforms.Compose(
109
+ [
110
+ transforms.Resize(224),
111
+ transforms.ToTensor(),
112
+ transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
113
+ ]
114
+ )
115
+ model.eval()
116
+ ```
117
+
118
+ You can use the UNI pretrained encoder to extract features from histopathology ROIs, as follows:
119
+
120
+ ```python
121
+ from PIL import Image
122
+ image = Image.open("uni.jpg")
123
+ image = transform(image).unsqueeze(dim=0) # Image (torch.Tensor) with shape [1, 3, 224, 224] following image resizing and normalization (ImageNet parameters)
124
+ with torch.inference_mode():
125
+ feature_emb = model(image) # Extracted features (torch.Tensor) with shape [1,1024]
126
+ ```
127
+
128
+ These pre-extracted features can then be used ROI classification (via linear probing), slide classification (via multiple instance learning), and other machine learning settings.
129
+
130
+
131
+
132
+
133
+ ### Direct Use (with Pre-Extracted and Frozen Features)
134
+
135
+ The models can be used without fine-tuning to obtain competitive results on:
136
+ - ROI classification, with logistic regression classifiers applied on the class token.
137
+ - ROI classification, with k-nearest neighbors (k-NN) classifiers applied on the class token.
138
+ - ROI classification, with nearest centroid classifiers (SimpleShot) applied on the class token.
139
+ - ROI retrieval, using nearest neighbors classifiers
140
+ - slide classification, with multiple instance learning (MIL) classifiers applied on a bag of class tokens extracted from the WSI
141
+
142
+ ### Downstream Use (Finetuning)
143
+
144
+ It is also possible to perform fine-tuning on the models, and recommended for competitive performance on segmentation tasks. We recommend finetuning using frameworks specialized for adapting ViTs for dense prediction tasks, such as ViTDet or ViT-Adapter (which depends on Mask2Former).
145
+
146
+ ## Training Details
147
+
148
+ - **Training data:** Mass-100K, a pretraining dataset (sourced from MGH, BWH, and GTEx) composed of 75,832,905 [256×256] and 24,297,995 [512×512] histology images at 20× resolution, sampled from 100,402 H&E WSIs (100,130,900 images in total).
149
+ - **Training regime:** fp16 using PyTorch-FSDP mixed-precision.
150
+ - **Training objective:** DINOv2 SSL recipe with the following losses:
151
+ - DINO self-distillation loss with multi-crop
152
+ - iBOT masked-image modeling loss
153
+ - KoLeo regularization on [CLS] tokens
154
+ - **Training length:** 125,000 iterations with a batch size of 3072
155
+ - **Model architecture:** ViT-Large (0.3B params): Patch size 16, embedding dimension 1024, 16 heads, MLP FFN
156
+ - **Hardware used:** 4x8 Nvidia A100 80GB
157
+ - **Hours trained:** Approx 1024 GPU hours (32 hours total)
158
+ - **Cloud provider:** MGB ERIS Research Computing Core
159
+
160
+ ## Software Dependencies
161
+
162
+ **Python Packages**
163
+ - torch>=2.0: https://pytorch.org
164
+ - xformers>=0.0.18: https://github.com/facebookresearch/xformers
165
+ - timm>=0.9.8: https://github.com/huggingface/pytorch-image-models
166
+
167
+ **Repositories**
168
+ - DINOv2 (self-supervised learning): https://github.com/facebookresearch/dinov2
169
+ - CLAM (slide classification): https://github.com/mahmoodlab/CLAM
170
+ - Mask2Former (cell and tissue segmentation): https://github.com/facebookresearch/Mask2Former
171
+ - ViT-Adapter (cell and tissue segmentation): https://github.com/czczup/ViT-Adapter
172
+ - LGSSL (Linear Probe & Few-Shot Eval): https://github.com/mbanani/lgssl
173
+
174
+ ## License and Terms of Use
175
+ This model and associated code are released under the CC-BY-NC-ND 4.0 license and may only be used for non-commercial, academic research purposes with proper attribution. Any commercial use, sale, or other monetization of the UNI model and its derivatives, which include models trained on outputs from the UNI model or datasets created from the UNI model, is prohibited and requires prior approval. Downloading the model requires prior registration on Hugging Face and agreeing to the terms of use. By downloading this model, you agree not to distribute, publish or reproduce a copy of the model. If another user within your organization wishes to use the UNI model, they must register as an individual user and agree to comply with the terms of use. Users may not attempt to re-identify the deidentified data used to develop the underlying model. If you are a commercial entity, please contact the corresponding author.
176
+
177
+
178
+ ## Contact
179
+ For any additional questions or comments, contact Faisal Mahmood (`[email protected]`),
180
+ Richard J. Chen (`[email protected]`),
181
+ Tong Ding (`[email protected]`),
182
+ or Ming Y. Lu (`[email protected]`).
183
+
184
+
185
+ ## Acknowledgements
186
+ The project was built on top of amazing repositories such as [ViT](https://github.com/google-research/big_vision), [DINOv2](https://github.com/facebookresearch/dinov2), [LGSSL](https://github.com/mbanani/lgssl), and [Timm](https://github.com/huggingface/pytorch-image-models/) (ViT model implementation). We thank the authors and developers for their contribution.
187
+
188
+
189
+ ## BibTeX
190
+ If you found our work useful in your research, please consider citing our work at:
191
+
192
+ Chen, R.J., Ding, T., Lu, M.Y., Williamson, D.F.K., et al. Towards a general-purpose foundation model for computational pathology. Nat Med (2024). https://doi.org/10.1038/s41591-024-02857-3
193
+
194
+ ```
195
+ @article{chen2024uni,
196
+ title={Towards a General-Purpose Foundation Model for Computational Pathology},
197
+ author={Chen, Richard J and Ding, Tong and Lu, Ming Y and Williamson, Drew FK and Jaume, Guillaume and Chen, Bowen and Zhang, Andrew and Shao, Daniel and Song, Andrew H and Shaban, Muhammad and others},
198
+ journal={Nature Medicine},
199
+ publisher={Nature Publishing Group},
200
+ year={2024}
201
+ }
202
+ ```
203
+ Works that use UNI should also attribute ViT and DINOv2.
config.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "vit_large_patch16_224",
3
+ "patch_size": 16,
4
+ "img_size": 224,
5
+ "init_values": 1.0,
6
+ "num_classes": 0,
7
+ "num_features": 1024,
8
+ "global_pool": "token",
9
+ "dynamic_img_size": true,
10
+ "pretrained_cfg": {
11
+ "tag": "uni_mass100k",
12
+ "custom_load": true,
13
+ "crop_pct": 1,
14
+ "input_size": [
15
+ 3,
16
+ 224,
17
+ 224
18
+ ],
19
+ "fixed_input_size": false,
20
+ "interpolation": "bilinear",
21
+ "mean": [
22
+ 0.485,
23
+ 0.456,
24
+ 0.406
25
+ ],
26
+ "std": [
27
+ 0.229,
28
+ 0.224,
29
+ 0.225
30
+ ],
31
+ "num_classes": 0,
32
+ "pool_size": null,
33
+ "first_conv": "patch_embed.proj",
34
+ "classifier": "head",
35
+ "license": "CC BY-NC-ND 4.0"
36
+ }
37
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:56ef09b44a25dc5c7eedc55551b3d47bcd17659a7a33837cf9abc9ec4e2ffb40
3
+ size 1213527781
requesting_access.png ADDED
uni.jpg ADDED

Git LFS Details

  • SHA256: 4ae14e4e52ff42768bfb22145c66690754fdc8b43d67dabd6b70580542105481
  • Pointer size: 132 Bytes
  • Size of remote file: 1.21 MB