ColFlor: Towards BERT-Size Vision-Language Document Retrieval Models
In June 2024,
ColPali
was introduced as an OCR-free document retrieval model, built over
PaliGemma
, shifting the paradigm of PDF document retrieval by directly processing images instead of using error-prone and resource-heavy OCR pipelines. However, with three billion parameters, ColPali might be computationally expensive, especially for large document databases. In contrast, text retrieval models like
ColBERT
are more efficient with just a few hundred million parameters, but they require error-prone and expensive OCR pipelines to. To bridge this gap, we introduce ColFlor, an OCR-free visual document retrieval model with only 174 million parameters. ColFlor is 17 times smaller than ColPali, 9.8 times faster in encoding queries and 5.25 faster in encoding images, with only a 1.8% drop in performance on text-rich English documents.
First, you need to clone the github repo and install the dependencies as follows
git clone https://github.com/AhmedMasryKU/colflor
cd colflor
pip install . -e
Then, you can run the following inference code:
import pprint
from typing importList, cast
import torch
from datasets import Dataset, load_dataset
from torch.utils.data import DataLoader
from tqdm import tqdm
from colpali_engine.models import ColFlor
from colpali_engine.models import ColFlorProcessor
from colpali_engine.utils.processing_utils import BaseVisualRetrieverProcessor
from colpali_engine.utils.torch_utils import ListDataset, get_torch_device
defmain():
""" Example script to run inference with ColFlor. """
device = get_torch_device("auto")
print(f"Device used: {device}")
# Model name
model_name = "ahmed-masry/ColFlor"# Load model
model = ColFlor.from_pretrained(
model_name,
#torch_dtype=torch.bfloat16,
device_map=device,
).eval()
# Load processor
processor = cast(ColFlorProcessor, ColFlorProcessor.from_pretrained(model_name))
ifnotisinstance(processor, BaseVisualRetrieverProcessor):
raise ValueError("Processor should be a BaseVisualRetrieverProcessor")
# NOTE: Only the first 16 images are used for demonstration purposes
dataset = cast(Dataset, load_dataset("vidore/docvqa_test_subsampled", split="test[:16]"))
images = dataset["image"]
# Select a few queries for demonstration purposes
query_indices = [12, 15]
queries = [dataset[idx]["query"] for idx in query_indices]
print("Selected queries:")
pprint.pprint(dict(zip(query_indices, queries)))
# Run inference - docs
dataloader = DataLoader(
dataset=ListDataset[str](images),
batch_size=4,
shuffle=False,
collate_fn=lambda x: processor.process_images(x),
)
ds: List[torch.Tensor] = []
for batch_doc in tqdm(dataloader):
with torch.no_grad():
batch_doc = {k: v.to(model.device) for k, v in batch_doc.items()}
embeddings_doc = model(**batch_doc)
ds.extend(list(torch.unbind(embeddings_doc.to("cpu"))))
# Run inference - queries
dataloader = DataLoader(
dataset=ListDataset[str](queries),
batch_size=4,
shuffle=False,
collate_fn=lambda x: processor.process_queries(x),
)
qs: List[torch.Tensor] = []
for batch_query in dataloader:
with torch.no_grad():
batch_query = {k: v.to(model.device) for k, v in batch_query.items()}
embeddings_query = model(**batch_query)
qs.extend(list(torch.unbind(embeddings_query.to("cpu"))))
# Run scoring
scores = processor.score(qs, ds).cpu().numpy()
idx_top_1 = scores.argmax(axis=1)
print("Indices of the top-1 retrieved documents for each query:", idx_top_1)
# Sanity checkif idx_top_1.tolist() == query_indices:
print("The top-1 retrieved documents are correct.")
else:
print("The top-1 retrieved documents are incorrect.")
returnif __name__ == "__main__":
typer.run(main)
Limitations
Figures
: While ColFlor exhibits reasonable performance on figures, there's a relatively large gap in performance between it and larger models such as ColPali.
Multilinguality
: The current version of the model only supports the Engligh language and performs poorly on other languages.
This work was carried out at the Intelligent Visualization Lab at York University in Canada. It was supported by the Natural Sciences Engineering Research Council (NSERC) of Canada and Canada Foundation for Innovation (CFI). Additionally, it received support through a GCP credits award from Google's PaliGemma Academic Program.
We appreciate the well-documented training and evaluation GitHub repositories provided by the ColPali team, which were essential in our model development.
This model card is adapted from
ColPali Model Card
Citation
If you plan to use ColFlor in your research, please consider citing us as follows:
ColFlor huggingface.co is an AI model on huggingface.co that provides ColFlor's model effect (), which can be used instantly with this ahmed-masry ColFlor model. huggingface.co supports a free trial of the ColFlor model, and also provides paid use of the ColFlor. Support call ColFlor model through api, including Node.js, Python, http.
ColFlor huggingface.co is an online trial and call api platform, which integrates ColFlor's modeling effects, including api services, and provides a free online trial of ColFlor, you can try ColFlor online for free by clicking the link below.
ahmed-masry ColFlor online free url in huggingface.co:
ColFlor is an open source model from GitHub that offers a free installation service, and any user can find ColFlor on GitHub to install. At the same time, huggingface.co provides the effect of ColFlor install, users can directly use ColFlor installed effect in huggingface.co for debugging and trial. It also supports api for free installation.