Update README.md
Browse files
README.md
CHANGED
@@ -36,9 +36,76 @@ with `alpha=32` and `r=32` on the transformer layers from the language model,
|
|
36 |
as well as the final randomly initialized projection layer, and use a `paged_adamw_8bit` optimizer.
|
37 |
We train on an 8 GPU setup with data parallelism, a learning rate of 5e-5 with linear decay with 2.5% warmup steps, and a batch size of 32.
|
38 |
|
39 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
42 |
|
43 |
## Limitations
|
44 |
|
|
|
36 |
as well as the final randomly initialized projection layer, and use a `paged_adamw_8bit` optimizer.
|
37 |
We train on an 8 GPU setup with data parallelism, a learning rate of 5e-5 with linear decay with 2.5% warmup steps, and a batch size of 32.
|
38 |
|
39 |
+
## Usage
|
40 |
+
|
41 |
+
An example usa
|
42 |
+
|
43 |
+
```python
|
44 |
+
import torch
|
45 |
+
import typer
|
46 |
+
from torch.utils.data import DataLoader
|
47 |
+
from tqdm import tqdm
|
48 |
+
from transformers import AutoProcessor
|
49 |
+
from PIL import Image
|
50 |
+
|
51 |
+
from colpali_engine.models.paligemma_colbert_architecture import ColPali
|
52 |
+
from colpali_engine.trainer.retrieval_evaluator import CustomEvaluator
|
53 |
+
from colpali_engine.utils.colpali_processing_utils import process_images, process_queries
|
54 |
+
from colpali_engine.utils.image_from_page_utils import load_from_dataset
|
55 |
+
|
56 |
+
|
57 |
+
def main() -> None:
|
58 |
+
"""Example script to run inference with ColPali"""
|
59 |
+
|
60 |
+
# Load model
|
61 |
+
model_name = "vidore/colpali"
|
62 |
+
model = ColPali.from_pretrained("google/paligemma-3b-mix-448", torch_dtype=torch.bfloat16, device_map="cuda").eval()
|
63 |
+
model.load_adapter(model_name)
|
64 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
65 |
+
|
66 |
+
# select images -> load_from_pdf(<pdf_path>), load_from_image_urls(["<url_1>"]), load_from_dataset(<path>)
|
67 |
+
images = load_from_dataset("vidore/docvqa_test_subsampled")
|
68 |
+
queries = ["From which university does James V. Fiorca come ?", "Who is the japanese prime minister?"]
|
69 |
+
|
70 |
+
# run inference - docs
|
71 |
+
dataloader = DataLoader(
|
72 |
+
images,
|
73 |
+
batch_size=4,
|
74 |
+
shuffle=False,
|
75 |
+
collate_fn=lambda x: process_images(processor, x),
|
76 |
+
)
|
77 |
+
ds = []
|
78 |
+
for batch_doc in tqdm(dataloader):
|
79 |
+
with torch.no_grad():
|
80 |
+
batch_doc = {k: v.to(model.device) for k, v in batch_doc.items()}
|
81 |
+
embeddings_doc = model(**batch_doc)
|
82 |
+
ds.extend(list(torch.unbind(embeddings_doc.to("cpu"))))
|
83 |
+
|
84 |
+
# run inference - queries
|
85 |
+
dataloader = DataLoader(
|
86 |
+
queries,
|
87 |
+
batch_size=4,
|
88 |
+
shuffle=False,
|
89 |
+
collate_fn=lambda x: process_queries(processor, x, Image.new("RGB", (448, 448), (255, 255, 255))),
|
90 |
+
)
|
91 |
+
|
92 |
+
qs = []
|
93 |
+
for batch_query in dataloader:
|
94 |
+
with torch.no_grad():
|
95 |
+
batch_query = {k: v.to(model.device) for k, v in batch_query.items()}
|
96 |
+
embeddings_query = model(**batch_query)
|
97 |
+
qs.extend(list(torch.unbind(embeddings_query.to("cpu"))))
|
98 |
+
|
99 |
+
# run evaluation
|
100 |
+
retriever_evaluator = CustomEvaluator(is_multi_vector=True)
|
101 |
+
scores = retriever_evaluator.evaluate(qs, ds)
|
102 |
+
print(scores.argmax(axis=1))
|
103 |
+
|
104 |
+
|
105 |
+
if __name__ == "__main__":
|
106 |
+
typer.run(main)
|
107 |
|
108 |
+
```
|
109 |
|
110 |
## Limitations
|
111 |
|