Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
| 1 |
---
|
| 2 |
license: cc-by-nc-4.0
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: cc-by-nc-4.0
|
| 3 |
---
|
| 4 |
+
This is the official Hugging Face repo for PathCLIP
|
| 5 |
+
|
| 6 |
+
# Usage
|
| 7 |
+
|
| 8 |
+
```python
|
| 9 |
+
import torch
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
import open_clip
|
| 13 |
+
|
| 14 |
+
##load the model
|
| 15 |
+
model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-16', pretrained='/mnt/Xsky/syx/model/clip/exp/ViT-B-16/23_1227_openpath/epoch_10_ori.pt',
|
| 16 |
+
cache_dir='/mnt/Xsky/syx/model/open_clip', force_quick_gelu=True)
|
| 17 |
+
tokenizer = open_clip.get_tokenizer('ViT-B-16')
|
| 18 |
+
model = model.cuda()
|
| 19 |
+
|
| 20 |
+
##load the image and prepare the text prompt
|
| 21 |
+
img_path = 'your_img_path'
|
| 22 |
+
label_description_list = ['label description1', 'label description3', 'label description3'] # specify the label descriptions
|
| 23 |
+
text_label_list = ['An image of {}'.format(i) for i in label_description_list]
|
| 24 |
+
image = Image.open(img_path)
|
| 25 |
+
image = preprocess(image).unsqueeze(0).cuda()
|
| 26 |
+
text = tokenizer(text_label_list).cuda()
|
| 27 |
+
|
| 28 |
+
##extract the img and text feature and predict the label
|
| 29 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
| 30 |
+
image_features = model.encode_image(image)
|
| 31 |
+
text_features = model.encode_text(text)
|
| 32 |
+
image_features /= image_features.norm(dim=-1, keepdim=True)
|
| 33 |
+
text_features /= text_features.norm(dim=-1, keepdim=True)
|
| 34 |
+
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
|
| 35 |
+
predict_label = torch.argmax(text_probs).item()
|
| 36 |
+
```
|
| 37 |
+
|