Yue Yang
commited on
Commit
•
a47f4ef
1
Parent(s):
ea0bff7
update README
Browse files- X-ray-results.png +0 -0
- test.py +20 -0
- test_xray.jpg +0 -0
X-ray-results.png
ADDED
test.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
import open_clip
|
4 |
+
|
5 |
+
model, _, preprocess = open_clip.create_model_and_transforms("hf-hub:yyupenn/whyxrayclip")
|
6 |
+
model.eval()
|
7 |
+
tokenizer = open_clip.get_tokenizer("ViT-L-14")
|
8 |
+
|
9 |
+
image = preprocess(Image.open("test_xray.jpg")).unsqueeze(0)
|
10 |
+
text = tokenizer(["enlarged heart", "pleural effusion"])
|
11 |
+
|
12 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
13 |
+
image_features = model.encode_image(image)
|
14 |
+
text_features = model.encode_text(text)
|
15 |
+
image_features /= image_features.norm(dim=-1, keepdim=True)
|
16 |
+
text_features /= text_features.norm(dim=-1, keepdim=True)
|
17 |
+
|
18 |
+
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
|
19 |
+
|
20 |
+
print("Label probs:", text_probs)
|
test_xray.jpg
ADDED