Views
No views yet
1import requests
2from PIL import Image
3
4import torch
5from transformers import CLIPProcessor
6
7# Check hf_models code here: https://github.com/naver-ai/pcmepp/tree/main/hf_models
8from hf_models import HfPCMEPPModel, tokenize
9
10
11processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
12# IN-top1: 34.64%
13model = HfPCMEPPModel.from_pretrained("SanghyukChun/PCMEPP-ViT-B-16-CC3M-12M-RedCaps")
14# IN-top1: 41.81%
15# model = HfPCMEPPModel.from_pretrained("SanghyukChun/PCMEPP-ViT-B-16-CC3M-12M-RedCaps-256M")
16
17
18url = "http://images.cocodataset.org/val2017/000000039769.jpg"
19image = Image.open(requests.get(url, stream=True).raw)
20inputs = processor(images=image, return_tensors="pt", padding=True)
21texts = ["a photo of a cat", "a photo of a dog"]
22texts = tokenize(texts)
23
24outputs = model(images=inputs["pixel_values"], texts=texts)
25print("Logits:", outputs["image_features"] @ outputs["text_features"].T)
26print("Image uncertainty: ", torch.exp(outputs["image_stds"]).mean(dim=-1))
27print("Text uncertainty: ", torch.exp(outputs["text_stds"]).mean(dim=-1))@inproceedings{
chun2024pcmepp,
title={Improved Probabilistic Image-Text Representations},
author={Sanghyuk Chun},
booktitle={The Twelfth International Conference on Learning Representations},
year={2024},
url={https://openreview.net/forum?id=ft1mr3WlGM}
}