Created
May 11, 2022 08:42
-
-
Save ovuruska/8b8df3a584a32a49b00af4b318d50105 to your computer and use it in GitHub Desktop.
CLIP kodu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import clip | |
from PIL import Image | |
import gradio as gr | |
if __name__ == "__main__": | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
model, preprocess = clip.load("ViT-B/32", device=device) | |
items = ["image of fire", "forest image", "image of smoke","image of sea","image of clouds"] | |
def infer(image): | |
out_labels = {} | |
with torch.no_grad(): | |
image = preprocess(Image.fromarray(image)).unsqueeze(0).to(device) | |
text = clip.tokenize(items).to(device) | |
logits_per_image, logits_per_text = model(image, text) | |
probs = logits_per_image.softmax(dim=-1).cpu().numpy() | |
for item,prob in zip(items,probs[0].squeeze()): | |
out_labels[item] = float(prob) | |
return out_labels | |
iface = gr.Interface(fn=infer, inputs="image", outputs="label") | |
iface.launch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment