Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import torch
- from PIL import Image
- import open_clip
- model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained='laion2b_s34b_b79k')
- model.eval()
- tokenizer = open_clip.get_tokenizer('ViT-B-32')
- image = preprocess(Image.open("source.webp")).unsqueeze(0)
- classes = [
- "a bird",
- "a plane",
- "a helicopter",
- "superman"
- ]
- text = tokenizer(classes)
- with torch.no_grad(), torch.autocast("cuda"):
- image_features = model.encode_image(image)
- text_features = model.encode_text(text)
- image_features /= image_features.norm(dim=-1, keepdim=True)
- text_features /= text_features.norm(dim=-1, keepdim=True)
- text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)[0]
- best_guess = torch.argmax(text_probs)
- print(classes[best_guess] if text_probs[best_guess] > 0.1 else "unknown")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement