Zero-Shot Image Classification
Transformers
Safetensors
siglip
vision

text feature extract

#10
by browallia - opened

I use siglip2 to extract text feature but it seems like not work.

from PIL import Image
import requests
from transformers import AutoProcessor, AutoModel
import torch

model = AutoModel.from_pretrained("google/siglip2-so400m-patch14-384")
processor = AutoProcessor.from_pretrained("google/siglip2-so400m-patch14-384")

url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)

texts = ["a photo of 2 cats", "a photo of 2 dogs"]
# important: we pass `padding=max_length` since the model was trained with this
inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt")

with torch.no_grad():
    outputs = model(**inputs)

logits_per_image = outputs.logits_per_image
probs = torch.sigmoid(logits_per_image) # these are the probabilities
print(f"{probs[0][0]:.1%} that image 0 is '{texts[0]}'")

the output is 「0.0% that image 0 is 'a photo of 2 cats」

browallia changed discussion status to closed

Sign up or log in to comment