Visual Document Retrieval
Transformers
Safetensors
ret2

Add sample usage section to model card

#1
by nielsr HF Staff - opened
Files changed (1) hide show
  1. README.md +61 -7
README.md CHANGED
@@ -1,11 +1,11 @@
1
  ---
2
- library_name: transformers
3
- license: apache-2.0
4
- datasets:
5
- - aimagelab/ReT-M2KR
6
  base_model:
7
  - google/siglip2-large-patch16-256
8
  - colbert-ir/colbertv2.0
 
 
 
 
9
  pipeline_tag: visual-document-retrieval
10
  ---
11
 
@@ -13,8 +13,8 @@ pipeline_tag: visual-document-retrieval
13
 
14
  Official implementation of ReT-2: Recurrence Meets Transformers for Universal Multimodal Retrieval.
15
 
16
- This model features a visual backbone based on [google/siglip2-large-patch16-256](https://huggingface.co/google/siglip2-large-patch16-256) and a textual backbone based on [colbert-ir/colbertv2.0](https://huggingface.co/colbert-ir/colbertv2.0).
17
- <br>The backbones have been fine-tuned on the M2KR dataset.
18
 
19
 
20
  ### Model Sources
@@ -28,11 +28,65 @@ This model features a visual backbone based on [google/siglip2-large-patch16-256
28
  ### Training Data
29
  [aimagelab/ReT-M2KR](https://huggingface.co/datasets/aimagelab/ReT-M2KR)
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  ## Citation
33
  ```
34
  @article{caffagni2025recurrencemeetstransformers,
35
- title={{Recurrence Meets Transformers for Universal Multimodal Retrieval}},
36
  author={Davide Caffagni and Sara Sarto and Marcella Cornia and Lorenzo Baraldi and Rita Cucchiara},
37
  journal={arXiv preprint arXiv:2509.08897},
38
  year={2025}
 
1
  ---
 
 
 
 
2
  base_model:
3
  - google/siglip2-large-patch16-256
4
  - colbert-ir/colbertv2.0
5
+ datasets:
6
+ - aimagelab/ReT-M2KR
7
+ library_name: transformers
8
+ license: apache-2.0
9
  pipeline_tag: visual-document-retrieval
10
  ---
11
 
 
13
 
14
  Official implementation of ReT-2: Recurrence Meets Transformers for Universal Multimodal Retrieval.
15
 
16
+ This model features a visual backbone based on [google/siglip2-large-patch16-256](https://huggingface.co/google/siglip2-large-patch16-256) and a textual backbone based on [colbert-ir/colbertv2.0](https://huggingface.co/colbert-ir/colbertv2.0).
17
+ <br>The backbones have been fine-tuned on the M2KR dataset.
18
 
19
 
20
  ### Model Sources
 
28
  ### Training Data
29
  [aimagelab/ReT-M2KR](https://huggingface.co/datasets/aimagelab/ReT-M2KR)
30
 
31
+ ## Use with 🤗's Transformers
32
+
33
+ ```python
34
+ from src.models import Ret2Model
35
+ import requests
36
+ from PIL import Image
37
+ from io import BytesIO
38
+ import torch
39
+ import torch.nn.functional as F
40
+
41
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
42
+
43
+ headers = {
44
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
45
+ }
46
+
47
+ query_img_url = 'https://upload.wikimedia.org/wikipedia/commons/8/84/Ghirlandina_%28Modena%29.jpg'
48
+ response = requests.get(query_img_url, headers=headers)
49
+ query_image = Image.open(BytesIO(response.content)).convert('RGB')
50
+ query_text = 'Where is this building located?'
51
+
52
+ passage_img_url = 'https://upload.wikimedia.org/wikipedia/commons/0/09/Absidi_e_Ghirlandina.jpg'
53
+ response = requests.get(query_img_url, headers=headers)
54
+ passage_image = Image.open(BytesIO(response.content)).convert('RGB')
55
+ passage_text = (
56
+ "The Ghirlandina is the bell tower of the Cathedral of Modena, in Modena, Italy. "
57
+ "It is 86.12 metres (282.7 ft) high and is the symbol of the city. "
58
+ "It was built in Romanesque style in the 12th century and is part of a UNESCO World Heritage Site."
59
+ )
60
+
61
+ model = Ret2Model.from_pretrained('aimagelab/ReT2-M2KR-ColBERT-SigLIP2-ViT-L', device_map=device)
62
+
63
+ query_txt_inputs = model.tokenizer([query_text], return_tensors='pt').to(device)
64
+ query_img_inputs = model.image_processor([query_image], return_tensors='pt').to(device)
65
+ passage_txt_inputs = model.tokenizer([passage_text], return_tensors='pt').to(device)
66
+ passage_img_inputs = model.image_processor([passage_image], return_tensors='pt').to(device)
67
+
68
+ with torch.inference_mode():
69
+ query_feats = model.get_ret_features(
70
+ input_ids=query_txt_inputs.input_ids,
71
+ attention_mask=query_txt_inputs.attention_mask,
72
+ pixel_values=query_img_inputs.pixel_values
73
+ )
74
+
75
+ passage_feats = model.get_ret_features(
76
+ input_ids=passage_txt_inputs.input_ids,
77
+ attention_mask=passage_txt_inputs.attention_mask,
78
+ pixel_values=passage_img_inputs.pixel_values
79
+ )
80
+
81
+ sim = F.normalize(query_feats, p=2, dim=-1) @ F.normalize(passage_feats, p=2, dim=-1).T
82
+
83
+ print(f"query-passage similarity: {sim.item():.3f}")
84
+ ```
85
 
86
  ## Citation
87
  ```
88
  @article{caffagni2025recurrencemeetstransformers,
89
+ title={{Recurrence Meets Transformers for Universal Multimodal Retrieval}},
90
  author={Davide Caffagni and Sara Sarto and Marcella Cornia and Lorenzo Baraldi and Rita Cucchiara},
91
  journal={arXiv preprint arXiv:2509.08897},
92
  year={2025}