Spaces:
Paused
Paused
Download src/clip_test.py from Vittav/ImageSearcherVitor: direct link, hf CLI and curl.
- Browser
- Download file 767 Bytes
-
https://huggingface.co/spaces/Vittav/ImageSearcherVitor/resolve/main/src/clip_test.py
- Command line
-
hf download hf://spaces/Vittav/ImageSearcherVitor/src/clip_test.py
-
curl -L -o clip_test.py https://huggingface.co/spaces/Vittav/ImageSearcherVitor/resolve/main/src/clip_test.py
767 Bytes
| 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("data/Flicker8k_Dataset/1000268201_693b08cb0e.jpg")).unsqueeze(0) | |
| text = tokenizer(["a little girl climbing into a wooden playhouse", "a dog on the beach", "a cat"]) | |
| with torch.no_grad(): | |
| 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) | |
| print("Label probs:", text_probs) |