Views
No views yet
1import torch
2from urllib.request import urlopen
3from PIL import Image
4from open_clip import create_model_from_pretrained, get_tokenizer
5
6repo_id = "hf-hub:timm/ViT-SO400M-16-SigLIP2-naflex"
7device = "cuda" if torch.cuda.is_available() else "cpu"
8
9model, preprocess = create_model_from_pretrained(repo_id, device=device)
10model = model.eval()
11tokenizer = get_tokenizer(repo_id)
12
13image = Image.open(urlopen(
14 "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png"
15)).convert("RGB")
16image = preprocess(image).unsqueeze(0).to(device)
17
18labels_list = ["a dog", "a cat", "a donut", "a beignet"]
19text = tokenizer(labels_list, context_length=model.context_length).to(device)
20
21with torch.no_grad():
22 image_features = model.encode_image(image, normalize=True)
23 text_features = model.encode_text(text, normalize=True)
24 logits = image_features @ text_features.T * model.logit_scale.exp()
25 if model.logit_bias is not None:
26 logits = logits + model.logit_bias
27 text_probs = torch.sigmoid(logits)
28
29print("Label probabilities:", list(zip(labels_list, [round(p.item(), 3) for p in text_probs[0]])))1import torch
2from urllib.request import urlopen
3from PIL import Image
4from open_clip import create_model_and_transforms, get_tokenizer
5
6repo_id = "hf-hub:timm/ViT-SO400M-16-SigLIP2-naflex"
7device = "cuda" if torch.cuda.is_available() else "cpu"
8
9model, _, preprocess_factory = create_model_and_transforms(
10 repo_id,
11 device=device,
12 aug_cfg={"naflex": True, "use_timm": True},
13)
14model = model.eval()
15tokenizer = get_tokenizer(repo_id)
16
17# 576 tokens = 24 x 24 patch grid at patch size 16.
18preprocess = preprocess_factory(max_seq_len=576, patch_size=16)
19
20image = Image.open(urlopen(
21 "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png"
22)).convert("RGB")
23image = preprocess(image)
24
25# Add batch dimension for a single image.
26image = {
27 k: v.unsqueeze(0).to(device) if torch.is_tensor(v) else v
28 for k, v in image.items()
29}
30image["seq_len"] = image["patches"].shape[1]
31
32labels_list = ["a dog", "a cat", "a donut", "a beignet"]
33text = tokenizer(labels_list, context_length=model.context_length).to(device)
34
35with torch.no_grad():
36 image_features = model.encode_image(image, normalize=True)
37 text_features = model.encode_text(text, normalize=True)
38 logits = image_features @ text_features.T * model.logit_scale.exp()
39 if model.logit_bias is not None:
40 logits = logits + model.logit_bias
41 text_probs = torch.sigmoid(logits)
42
43print("Label probabilities:", list(zip(labels_list, [round(p.item(), 3) for p in text_probs[0]])))@article{tschannen2025siglip,
title={SigLIP 2: Multilingual Vision-Language Encoders with Improved Semantic Understanding, Localization, and Dense Features},
author={Tschannen, Michael and Gritsenko, Alexey and Wang, Xiao and Naeem, Muhammad Ferjad and Alabdulmohsin, Ibrahim and Parthasarathy, Nikhil and Evans, Talfan
and Beyer, Lucas and Xia, Ye and Mustafa, Basil and H'enaff, Olivier and Harmsen, Jeremiah and Steiner, Andreas and Zhai, Xiaohua},
year={2025},
journal={arXiv preprint arXiv:2502.14786}
}
@article{zhai2023sigmoid,
title={Sigmoid loss for language image pre-training},
author={Zhai, Xiaohua and Mustafa, Basil and Kolesnikov, Alexander and Beyer, Lucas},
journal={arXiv preprint arXiv:2303.15343},
year={2023}
}
@misc{big_vision,
author = {Beyer, Lucas and Zhai, Xiaohua and Kolesnikov, Alexander},
title = {Big Vision},
year = {2022},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/google-research/big_vision}}
}