[GH-ISSUE #380] Question on the example: #327

Closed
opened 2026-10-31 21:13:54 +00:00 by bsakaguchi · 1 comment
Owner

Originally created by @shersoni610 on GitHub (Jul 22, 2023).
Original GitHub issue: https://github.com/openai/CLIP/issues/380

Hello,

I see the following example on the page:

Qs: (1) we are calculate image and text features, but they are not being used in the code.
(2) Are the arguments to the model (image,text) or (image_feature, text_features)

Thanks

mport torch
import clip
from PIL import Image

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

image = preprocess(Image.open("CLIP.png")).unsqueeze(0).to(device)
text = clip.tokenize(["a diagram", "a dog", "a cat"]).to(device)

with torch.no_grad():
image_features = model.encode_image(image)
text_features = model.encode_text(text)

logits_per_image, logits_per_text = model(image, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()

print("Label probs:", probs) # prints: 0.9927937 0.00421068 0.00299572

Originally created by @shersoni610 on GitHub (Jul 22, 2023). Original GitHub issue: https://github.com/openai/CLIP/issues/380 Hello, I see the following example on the page: Qs: (1) we are calculate image and text features, but they are not being used in the code. (2) Are the arguments to the model (image,text) or (image_feature, text_features) Thanks mport torch import clip from PIL import Image device = "cuda" if torch.cuda.is_available() else "cpu" model, preprocess = clip.load("ViT-B/32", device=device) image = preprocess(Image.open("CLIP.png")).unsqueeze(0).to(device) text = clip.tokenize(["a diagram", "a dog", "a cat"]).to(device) with torch.no_grad(): image_features = model.encode_image(image) text_features = model.encode_text(text) logits_per_image, logits_per_text = model(image, text) probs = logits_per_image.softmax(dim=-1).cpu().numpy() print("Label probs:", probs) # prints: [[0.9927937 0.00421068 0.00299572]]
Author
Owner

@jongwook commented on GitHub (Jul 26, 2023):

Those are intentional. Please see:

<!-- gh-comment-id:1650810392 --> @jongwook commented on GitHub (Jul 26, 2023): Those are intentional. Please see: * https://github.com/openai/CLIP/pull/368#issuecomment-1626973523
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openai/CLIP#327
No description provided.