Update README.md
Browse files
README.md
CHANGED
|
@@ -18,7 +18,7 @@ It was introduced in the paper [ColPali: Efficient Document Retrieval with Visio
|
|
| 18 |
|
| 19 |
## Version specificity
|
| 20 |
|
| 21 |
-
This version is trained with `colpali-engine==0.2.0`.
|
| 22 |
|
| 23 |
Compared to `colpali`, this version is trained with right padding for queries to fix unwanted tokens in the query encoding.
|
| 24 |
It also stems from the fixed `vidore/colpaligemma-3b-pt-448-base` to guarantee deterministic projection layer initialization.
|
|
@@ -52,89 +52,53 @@ We train on an 8 GPU setup with data parallelism, a learning rate of 5e-5 with l
|
|
| 52 |
|
| 53 |
## Usage
|
| 54 |
|
|
|
|
|
|
|
| 55 |
```bash
|
| 56 |
-
pip install colpali-engine
|
| 57 |
```
|
| 58 |
|
|
|
|
| 59 |
|
| 60 |
```python
|
|
|
|
|
|
|
| 61 |
import torch
|
| 62 |
-
import typer
|
| 63 |
-
from torch.utils.data import DataLoader
|
| 64 |
-
from tqdm import tqdm
|
| 65 |
-
from transformers import AutoProcessor
|
| 66 |
from PIL import Image
|
| 67 |
|
| 68 |
-
from colpali_engine.models
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# run inference - queries
|
| 103 |
-
dataloader = DataLoader(
|
| 104 |
-
queries,
|
| 105 |
-
batch_size=4,
|
| 106 |
-
shuffle=False,
|
| 107 |
-
collate_fn=lambda x: process_queries(processor, x, Image.new("RGB", (448, 448), (255, 255, 255))),
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
qs = []
|
| 111 |
-
for batch_query in dataloader:
|
| 112 |
-
with torch.no_grad():
|
| 113 |
-
batch_query = {k: v.to(model.device) for k, v in batch_query.items()}
|
| 114 |
-
embeddings_query = model(**batch_query)
|
| 115 |
-
qs.extend(list(torch.unbind(embeddings_query.to("cpu"))))
|
| 116 |
-
|
| 117 |
-
# run evaluation
|
| 118 |
-
retriever_evaluator = CustomEvaluator(is_multi_vector=True)
|
| 119 |
-
scores = retriever_evaluator.evaluate(qs, ds)
|
| 120 |
-
print(scores.argmax(axis=1))
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
if __name__ == "__main__":
|
| 124 |
-
typer.run(main)
|
| 125 |
-
|
| 126 |
-
```
|
| 127 |
-
|
| 128 |
-
**Note:** If you need to further train ColPali from this adapter, you should run:
|
| 129 |
-
|
| 130 |
-
```python
|
| 131 |
-
lora_config = LoraConfig.from_pretrained("vidore/colpali-v1.1")
|
| 132 |
-
lora_config.inference_mode = False # force training mode for fine-tuning
|
| 133 |
-
|
| 134 |
-
model = get_peft_model(model, lora_config)
|
| 135 |
-
|
| 136 |
-
print("after")
|
| 137 |
-
model.print_trainable_parameters()
|
| 138 |
```
|
| 139 |
|
| 140 |
## Limitations
|
|
|
|
| 18 |
|
| 19 |
## Version specificity
|
| 20 |
|
| 21 |
+
This version is trained with `colpali-engine==0.2.0` but can be loaded for any version `>=0.2.0`.
|
| 22 |
|
| 23 |
Compared to `colpali`, this version is trained with right padding for queries to fix unwanted tokens in the query encoding.
|
| 24 |
It also stems from the fixed `vidore/colpaligemma-3b-pt-448-base` to guarantee deterministic projection layer initialization.
|
|
|
|
| 52 |
|
| 53 |
## Usage
|
| 54 |
|
| 55 |
+
Install [`colpali-engine`](https://github.com/illuin-tech/colpali):
|
| 56 |
+
|
| 57 |
```bash
|
| 58 |
+
pip install colpali-engine>=0.3.0,<0.4.0
|
| 59 |
```
|
| 60 |
|
| 61 |
+
Then run the following code:
|
| 62 |
|
| 63 |
```python
|
| 64 |
+
from typing import cast
|
| 65 |
+
|
| 66 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
from PIL import Image
|
| 68 |
|
| 69 |
+
from colpali_engine.models import ColPali, ColPaliProcessor
|
| 70 |
+
|
| 71 |
+
model = cast(
|
| 72 |
+
ColPali,
|
| 73 |
+
ColPali.from_pretrained(
|
| 74 |
+
"vidore/colpali-v1.2",
|
| 75 |
+
torch_dtype=torch.bfloat16,
|
| 76 |
+
device_map="cuda:0", # or "mps" if on Apple Silicon
|
| 77 |
+
),
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
processor = cast(ColPaliProcessor, ColPaliProcessor.from_pretrained("google/paligemma-3b-mix-448"))
|
| 81 |
+
|
| 82 |
+
# Your inputs
|
| 83 |
+
images = [
|
| 84 |
+
Image.new("RGB", (32, 32), color="white"),
|
| 85 |
+
Image.new("RGB", (16, 16), color="black"),
|
| 86 |
+
]
|
| 87 |
+
queries = [
|
| 88 |
+
"Is attention really all you need?",
|
| 89 |
+
"Are Benjamin, Antoine, Merve, and Jo best friends?",
|
| 90 |
+
]
|
| 91 |
+
|
| 92 |
+
# Process the inputs
|
| 93 |
+
batch_images = processor.process_images(images).to(model.device)
|
| 94 |
+
batch_queries = processor.process_queries(queries).to(model.device)
|
| 95 |
+
|
| 96 |
+
# Forward pass
|
| 97 |
+
with torch.no_grad():
|
| 98 |
+
image_embeddings = model(**batch_images)
|
| 99 |
+
querry_embeddings = model(**batch_queries)
|
| 100 |
+
|
| 101 |
+
scores = processor.score_multi_vector(querry_embeddings, image_embeddings)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
```
|
| 103 |
|
| 104 |
## Limitations
|