Views
No views yet
open_clip to use this model:pip install open_clip_torch1import torch
2from PIL import Image
3import open_clip
4
5CLIP_TEXT_TEMPLATE = "an icon of {}"
6ICON_CLASSES = ["add", "close", "play", ...] # Modify your class names here
7
8model_checkpoint = "<path_to_your_local_model>"
9model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained=model_checkpoint)
10model.eval()
11tokenizer = open_clip.get_tokenizer('ViT-B-32')
12
13image = preprocess(Image.open("icon.png")).unsqueeze(0)
14text = tokenizer([CLIP_TEXT_TEMPLATE.format(cls) for cls in ICON_CLASSES])
15
16with torch.no_grad(), torch.autocast("cuda"):
17 image_features = model.encode_image(image)
18 text_features = model.encode_text(text)
19 image_features /= image_features.norm(dim=-1, keepdim=True)
20 text_features /= text_features.norm(dim=-1, keepdim=True)
21
22 text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
23
24print("Label probs:", text_probs) # prints something like: [[1., 0., 0., ...]]