Delete .ipynb_checkpoints/README-checkpoint.md
Browse files
.ipynb_checkpoints/README-checkpoint.md
DELETED
@@ -1,213 +0,0 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
language:
|
4 |
-
- en
|
5 |
-
- it
|
6 |
-
- fr
|
7 |
-
- de
|
8 |
-
- es
|
9 |
-
base_model:
|
10 |
-
- MrLight/dse-qwen2-2b-mrl-v1
|
11 |
-
tags:
|
12 |
-
- transformers
|
13 |
-
- Qwen2-VL
|
14 |
-
---
|
15 |
-
|
16 |
-
# vdr-2b-multi-v1
|
17 |
-
|
18 |
-
![](cover.png)
|
19 |
-
|
20 |
-
vdr-2b-multi-v1 is a multilingual model designed for visual document retrieval across multiple languages and domains. This model is designed to encode document page screenshots into dense single-vector representations, this will effectively allow to search and query visually rich multilingual documents without the need for any OCR, data extraction pipelines, chunking...
|
21 |
-
|
22 |
-
|
23 |
-
- **Trained on 🇮🇹 Italian, 🇪🇸 Spanish, 🇬🇧 English, 🇫🇷 French and 🇩🇪 German:** together they form a new large, open-source, multilingual training dataset of 500k high-quality samples.
|
24 |
-
|
25 |
-
- **Low VRAM and Faster Inference**: english model achieves better results on synthetic vidore benchmarks with just 30% of the base model image resolution. This results in 3x faster inference and much lower VRAM usage.
|
26 |
-
|
27 |
-
- **Cross-lingual Retrieval**: substantially better on real-world scenarios. For example, this allows for searching german documents with italian queries.
|
28 |
-
|
29 |
-
- **Matryoshka Representation Learning**: You can reduce the vectors size 3x and still keep 98% of the embeddings quality.
|
30 |
-
|
31 |
-
# Usage
|
32 |
-
|
33 |
-
**Initialize model and processor**
|
34 |
-
|
35 |
-
```python
|
36 |
-
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
|
37 |
-
from PIL import Image
|
38 |
-
import torch
|
39 |
-
import math
|
40 |
-
|
41 |
-
# more pixels -> better embeddings -> more VRAM -> slower inference
|
42 |
-
# From my experience, 768 image patches is the right spot for compute efficient embeddings.
|
43 |
-
max_pixels = 768 * 28 * 28
|
44 |
-
min_pixels = 1 * 28 * 28
|
45 |
-
|
46 |
-
# Load the embedding model and processor
|
47 |
-
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
48 |
-
'llamaindex/vdr-2b-multi-v1',
|
49 |
-
attn_implementation="flash_attention_2",
|
50 |
-
torch_dtype=torch.bfloat16,
|
51 |
-
device_map="cuda:0"
|
52 |
-
).eval()
|
53 |
-
|
54 |
-
processor = AutoProcessor.from_pretrained(
|
55 |
-
'llamaindex/vdr-2b-multi-v1',
|
56 |
-
min_pixels=min_pixels,
|
57 |
-
max_pixels=max_pixels
|
58 |
-
)
|
59 |
-
|
60 |
-
model.padding_side = "left"
|
61 |
-
processor.tokenizer.padding_side = "left"
|
62 |
-
|
63 |
-
document_prompt = "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>What is shown in this image?<|im_end|>\n<|endoftext|>"
|
64 |
-
|
65 |
-
query_prompt = "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Query: %s<|im_end|>\n<|endoftext|>"
|
66 |
-
```
|
67 |
-
|
68 |
-
**Encode queries**
|
69 |
-
|
70 |
-
```python
|
71 |
-
def encode_queries(queries: list[str], dimension: int) -> torch.Tensor:
|
72 |
-
"""
|
73 |
-
Encode a list of queries into a tensor of embeddings.
|
74 |
-
|
75 |
-
Args:
|
76 |
-
queries: A list of strings, each representing a query.
|
77 |
-
dimension: The desired dimension of the output embeddings.
|
78 |
-
|
79 |
-
Returns:
|
80 |
-
A tensor of shape (num_queries, dimension) containing the encoded queries.
|
81 |
-
"""
|
82 |
-
|
83 |
-
dummy_image = Image.new('RGB', (56, 56))
|
84 |
-
inputs = processor(
|
85 |
-
text=[query_prompt % x for x in queries],
|
86 |
-
images=[dummy_image for _ in queries],
|
87 |
-
videos=None,
|
88 |
-
padding='longest',
|
89 |
-
return_tensors='pt'
|
90 |
-
).to('cuda:0')
|
91 |
-
|
92 |
-
cache_position = torch.arange(0, len(queries))
|
93 |
-
inputs = model.prepare_inputs_for_generation(
|
94 |
-
**inputs, cache_position=cache_position, use_cache=False)
|
95 |
-
|
96 |
-
with torch.no_grad():
|
97 |
-
output = self.model(
|
98 |
-
**inputs,
|
99 |
-
return_dict=True,
|
100 |
-
output_hidden_states=True
|
101 |
-
)
|
102 |
-
|
103 |
-
embeddings = output.hidden_states[-1][:, -1]
|
104 |
-
return torch.nn.functional.normalize(embeddings[:, :dimension], p=2, dim=-1)
|
105 |
-
```
|
106 |
-
|
107 |
-
**Encode documents**
|
108 |
-
```python
|
109 |
-
def round_by_factor(number: float, factor: int) -> int:
|
110 |
-
return round(number / factor) * factor
|
111 |
-
|
112 |
-
def ceil_by_factor(number: float, factor: int) -> int:
|
113 |
-
return math.ceil(number / factor) * factor
|
114 |
-
|
115 |
-
def floor_by_factor(number: float, factor: int) -> int:
|
116 |
-
return math.floor(number / factor) * factor
|
117 |
-
|
118 |
-
def smart_resize(height: int, width: int) -> tuple[int, int]:
|
119 |
-
h_bar = max(28, round_by_factor(height, 28))
|
120 |
-
w_bar = max(28, round_by_factor(width, 28))
|
121 |
-
if h_bar * w_bar > max_pixels:
|
122 |
-
beta = math.sqrt((height * width) / max_pixels)
|
123 |
-
h_bar = floor_by_factor(height / beta, 28)
|
124 |
-
w_bar = floor_by_factor(width / beta, 28)
|
125 |
-
elif h_bar * w_bar < min_pixels:
|
126 |
-
beta = math.sqrt(min_pixels / (height * width))
|
127 |
-
h_bar = ceil_by_factor(height * beta, 28)
|
128 |
-
w_bar = ceil_by_factor(width * beta, 28)
|
129 |
-
return w_bar, h_bar
|
130 |
-
|
131 |
-
def resize(image: Image.Image):
|
132 |
-
new_size = smart_resize(image.height, image.width)
|
133 |
-
return image.resize(new_size)
|
134 |
-
|
135 |
-
def encode_documents(documents: list[Image.Image], dimension: int):
|
136 |
-
"""
|
137 |
-
Encode a list of images into a tensor of embeddings.
|
138 |
-
|
139 |
-
Args:
|
140 |
-
documents: A list of PIL Image objects.
|
141 |
-
dimension: The desired dimension of the output embeddings.
|
142 |
-
|
143 |
-
Returns:
|
144 |
-
A tensor of shape (num_documents, dimension) containing the encoded images.
|
145 |
-
"""
|
146 |
-
|
147 |
-
inputs = processor(
|
148 |
-
text=[document_prompt] * len(documents),
|
149 |
-
images=[resize(x) for x in documents],
|
150 |
-
videos=None,
|
151 |
-
padding='longest',
|
152 |
-
return_tensors='pt'
|
153 |
-
).to('cuda:0')
|
154 |
-
|
155 |
-
cache_position = torch.arange(0, len(queries))
|
156 |
-
inputs = model.prepare_inputs_for_generation(
|
157 |
-
**inputs, cache_position=cache_position, use_cache=False)
|
158 |
-
|
159 |
-
with torch.no_grad():
|
160 |
-
output = self.model(
|
161 |
-
**inputs,
|
162 |
-
return_dict=True,
|
163 |
-
output_hidden_states=True
|
164 |
-
)
|
165 |
-
|
166 |
-
embeddings = output.hidden_states[-1][:, -1]
|
167 |
-
return torch.nn.functional.normalize(embeddings[:, :dimension], p=2, dim=-1)
|
168 |
-
```
|
169 |
-
|
170 |
-
# Training
|
171 |
-
|
172 |
-
The model is based on [MrLight/dse-qwen2-2b-mrl-v1](https://huggingface.co/MrLight/dse-qwen2-2b-mrl-v1) and it was trained on the new [vdr-multilingual-train](https://huggingface.co/datasets/llamaindex/vdr-multilingual-train) dataset that consinsists of 500k high quality, multilingual query image pairs. It was trained for 1 epoch using the [DSE approach](https://arxiv.org/abs/2406.11251), with a batch size of 128 and hard-mined negatives.
|
173 |
-
|
174 |
-
# Results
|
175 |
-
|
176 |
-
The model has been evaluated on the Vidore benchmark and on custom-built evaluation sets that allow testing its multilingual capabilities on text-only, visual-only and mixed page screenshots. The evaluation dataset is publicly available [here on HuggingFace](https://huggingface.co/datasets/llamaindex/vdr-multilingual-test).
|
177 |
-
|
178 |
-
All evaluations are performed by calculating **NDCG@5** scores using **1536 dimensions** vectors and an image resolution that can be represented with **maximum 768 tokens**.
|
179 |
-
|
180 |
-
| | Avg | Italian (text) | Italian (visual) | Italian (mix) |
|
181 |
-
|---------------------|----------|----------------|------------------|---------------|
|
182 |
-
| dse-qwen2-2b-mrl-v1 | 95.1 | 95.1 | 94 | 96.2 |
|
183 |
-
| vdr-2b-multi-v1 | **97.0** | **96.4** | **96.3** | **98.4** |
|
184 |
-
| | **+2%** | | | |
|
185 |
-
|
186 |
-
| | Avg | French (text) | French (visual) | French (mix) |
|
187 |
-
|---------------------|-----------|---------------|-----------------|--------------|
|
188 |
-
| dse-qwen2-2b-mrl-v1 | 93.5 | 94.7 | 90.8 | 95.1 |
|
189 |
-
| vdr-2b-multi-v1 | **95.6** | **95.6** | **93.3** | **97.9** |
|
190 |
-
| | **+2.2%** | | | |
|
191 |
-
|
192 |
-
| | Avg | Spanish (text) | Spanish (visual) | Spanish (mix) |
|
193 |
-
|---------------------|-----------|----------------|------------------|---------------|
|
194 |
-
| dse-qwen2-2b-mrl-v1 | 96.7 | 97.2 | 94.7 | 98.2 |
|
195 |
-
| vdr-2b-multi-v1 | **98.1** | **98.3** | **96.9** | **99.1** |
|
196 |
-
| | **+1.4%** | | | |
|
197 |
-
|
198 |
-
| | Avg | German (text) | German (visual) | German (mix) |
|
199 |
-
|---------------------|-----------|---------------|-----------------|--------------|
|
200 |
-
| dse-qwen2-2b-mrl-v1 | 93.0 | 93.4 | 90 | 95.5 |
|
201 |
-
| vdr-2b-multi-v1 | **96.2** | **94.8** | **95.7** | **98.1** |
|
202 |
-
| | **+3.4%** | | | |
|
203 |
-
|
204 |
-
| | Avg | English (text) | English (visual) | English (mix) |
|
205 |
-
|---------------------|-----------|----------------|------------------|---------------|
|
206 |
-
| dse-qwen2-2b-mrl-v1 | 98.0 | **98.3** | 98.5 | 97.1 |
|
207 |
-
| vdr-2b-multi-v1 | **98.1** | 97.9 | **99.1** | **97.3** |
|
208 |
-
| | **+0.1%** | | | |
|
209 |
-
|
210 |
-
| | **Avg** | **shiftproject** | **government** | **healthcare** | **energy** | **ai** | **docvqa** | **arxivqa** | **tatdqa** | **infovqa** | **tabfquad** |
|
211 |
-
|--------------------:|---------:|-----------------:|---------------:|---------------:|-----------:|-----------:|-----------:|------------:|-----------:|------------:|-------------:|
|
212 |
-
| dse-qwen2-2b-mrl-v1 | 83.6 | 79.8 | **95.7** | **96.9** | **92** | 98.2 | 56.3 | **85.2** | **53.9** | **87.5** | 90.3 |
|
213 |
-
| vdr-2b-multi-v1 | **84.0** | **82.4** | 95.5 | 96.5 | 91.2 | **98.5** | **58.5** | 84.7 | 53.6 | 87.1 | **92.2** |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|