Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The result is random #862

Open
qfmy opened this issue Apr 17, 2024 · 2 comments
Open

The result is random #862

qfmy opened this issue Apr 17, 2024 · 2 comments

Comments

@qfmy
Copy link

qfmy commented Apr 17, 2024

Everytime I run the demo below,the result is random. Like:

first time:
Label probs: tensor([[0.1304, 0.1254, 0.7442]])
second time:
Label probs: tensor([[0.0421, 0.4105, 0.5474]])
third time:
Label probs: tensor([[0.0628, 0.8441, 0.0931]])

Is this normal?

import torch
from PIL import Image
import open_clip

model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained='laion2b_s34b_b79k')
tokenizer = open_clip.get_tokenizer('ViT-B-32')

image = preprocess(Image.open("docs/CLIP.png")).unsqueeze(0)
text = tokenizer(["a diagram", "a dog", "a cat"])

with torch.no_grad(), torch.cuda.amp.autocast():
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) # prints: [[1., 0., 0.]]

@rwightman
Copy link
Collaborator

@qfmy it's not normal and it does not occur for me. Are you sure there isn't an issue on your setup loading the pretrained weights? corrupt file download?

@rwightman
Copy link
Collaborator

If you happened to have enabled stochastic depth or dropouts, you need model.eval() after creation, just updated the README

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants