Views
No views yet
1from PIL import Image
2import requests
3from transformers import CLIPProcessor, CLIPModel
4
5model = CLIPModel.from_pretrained("jrheiner/thesis-clip-geoloc-continent")
6processor = CLIPProcessor.from_pretrained("jrheiner/thesis-clip-geoloc-continent")
7
8url = "https://huggingface.co/spaces/jrheiner/thesis-demo/resolve/main/kerger-test-images/Oceania_Australia_-32.947127313081_151.47903359833_kerger.jpg"
9image = Image.open(requests.get(url, stream=True).raw)
10choices = ["North America", "Africa", "Asia", "Oceania", "South America", "Europe"]
11inputs = processor(text=choices, images=image, return_tensors="pt", padding=True)
12outputs = model(**inputs)
13logits_per_image = outputs.logits_per_image # this is the image-text similarity score
14probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities