Views
No views yet
1import requests
2from PIL import Image
3from torch.nn.functional import cosine_similarity
4
5from transformers import AutoModel, AutoProcessor
6
7url_1 = "http://images.cocodataset.org/val2017/000000039769.jpg"
8url_2 = "http://images.cocodataset.org/val2017/000000219578.jpg"
9image_1 = Image.open(requests.get(url_1, stream=True).raw)
10image_2 = Image.open(requests.get(url_2, stream=True).raw)
11
12model_id = "jmtzt/ijepa_vith14_22k"
13processor = AutoProcessor.from_pretrained(model_id)
14model = AutoModel.from_pretrained(model_id)
15
16
17def infer(image):
18 inputs = processor(image, return_tensors="pt")
19 outputs = model(**inputs)
20 return outputs.last_hidden_state.mean(dim=1)
21
22
23embed_1 = infer(image_1)
24embed_2 = infer(image_2)
25
26similarity = cosine_similarity(embed_1, embed_2)
27print(similarity)@article{assran2023self,
title={Self-Supervised Learning from Images with a Joint-Embedding Predictive Architecture},
author={Assran, Mahmoud and Duval, Quentin and Misra, Ishan and Bojanowski, Piotr and Vincent, Pascal and Rabbat, Michael and LeCun, Yann and Ballas, Nicolas},
journal={arXiv preprint arXiv:2301.08243},
year={2023}
}