Create openclip/basic.py
Browse files- openclip/basic.py +24 -0
openclip/basic.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
#basic openclip usage
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import open_clip
|
| 6 |
+
|
| 7 |
+
mtype='ViT-B-32'
|
| 8 |
+
mname='laion2b_s34b_b79k'
|
| 9 |
+
|
| 10 |
+
print("Loading",mtype,mname)
|
| 11 |
+
|
| 12 |
+
model, _, preprocess = open_clip.create_model_and_transforms(mtype,
|
| 13 |
+
pretrained=mname)
|
| 14 |
+
tokenizer = open_clip.get_tokenizer(mtype)
|
| 15 |
+
|
| 16 |
+
#image = preprocess(Image.open("CLIP.png")).unsqueeze(0)
|
| 17 |
+
text = tokenizer(["a diagram", "a dog", "a cat"])
|
| 18 |
+
text = tokenizer("cat")
|
| 19 |
+
|
| 20 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
| 21 |
+
# image_features = model.encode_image(image)
|
| 22 |
+
text_features = model.encode_text(text)
|
| 23 |
+
embedding=text_features[0]
|
| 24 |
+
|