microsoft / Cream

This is a collection of our NAS and Vision Transformer work.
MIT License
1.69k stars 230 forks source link

Can I use tinyclip as if I were using clip? #195

Closed wudizuixiaosa closed 1 year ago

wudizuixiaosa commented 1 year ago

import 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]] like this,I only need to add a small amount of code to apply it to my code and provide assistance

wkcn commented 1 year ago

Thanks for your attention to our work! We will adapt our code to the latest clip and open_clip.

wkcn commented 1 year ago

Hi @wudizuixiaosa , I have uploaded an example code for inference: https://github.com/microsoft/Cream/blob/main/TinyCLIP/inference.py, whose usage is similar to CLIP.

wudizuixiaosa commented 1 year ago

Hi @wudizuixiaosa , I have uploaded an example code for inference: https://github.com/microsoft/Cream/blob/main/TinyCLIP/inference.py, whose usage is similar to CLIP.

Thank you!sir