View file src/colab/clip_vit_large_patch14.py - Download
# -*- coding: utf-8 -*-
"""clip-vit-large-patch14.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1gFWEcNz7xNqLSysHt6KSP5qgCJvfPt-D
https://huggingface.co/openai/clip-vit-large-patch14
"""
from PIL import Image
import requests
from transformers import CLIPProcessor, CLIPModel
model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
display(image)
inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
print(probs)