Andrey Khlopotnukh commited on
Commit
298d1f1
·
verified ·
1 Parent(s): bf70a76

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +22 -3
README.md CHANGED
@@ -1,3 +1,22 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pip install open_clip_torch
2
+ import torch
3
+ from PIL import Image
4
+ import open_clip
5
+
6
+ model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained='laion2b_s34b_b79k')
7
+ model.eval() # model in train mode by default, impacts some models with BatchNorm or stochastic depth active
8
+ tokenizer = open_clip.get_tokenizer('ViT-B-32')
9
+
10
+ image = preprocess(Image.open("docs/CLIP.png")).unsqueeze(0)
11
+ text = tokenizer(["a diagram", "a dog", "a cat"])
12
+
13
+ with torch.no_grad(), torch.cuda.amp.autocast():
14
+ image_features = model.encode_image(image)
15
+ text_features = model.encode_text(text)
16
+ image_features /= image_features.norm(dim=-1, keepdim=True)
17
+ text_features /= text_features.norm(dim=-1, keepdim=True)
18
+
19
+ text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
20
+
21
+ print("Label probs:", text_probs) # prints: [[1., 0., 0.]]
22
+