Buckets:
| # ColPali | |
| [ColPali](https://huggingface.co/papers/2407.01449) is a model designed to retrieve documents by analyzing their visual features. Unlike traditional systems that rely heavily on text extraction and OCR, ColPali treats each page as an image. It uses [Paligemma-3B](./paligemma) to capture not only text, but also the layout, tables, charts, and other visual elements to create detailed multi-vector embeddings that can be used for retrieval by computing pairwise late interaction similarity scores. This offers a more comprehensive understanding of documents and enables more efficient and accurate retrieval. | |
| This model was contributed by [@tonywu71](https://huggingface.co/tonywu71) (ILLUIN Technology) and [@yonigozlan](https://huggingface.co/yonigozlan) (HuggingFace). | |
| You can find all the original ColPali checkpoints under Vidore's [Hf-native ColVision Models](https://huggingface.co/collections/vidore/hf-native-colvision-models-6755d68fc60a8553acaa96f7) collection. | |
| > [!TIP] | |
| > Click on the ColPali models in the right sidebar for more examples of how to use ColPali for image retrieval. | |
| ```python | |
| import requests | |
| import torch | |
| from PIL import Image | |
| from transformers import ColPaliForRetrieval, ColPaliProcessor | |
| # Load the model and the processor | |
| model_name = "vidore/colpali-v1.3-hf" | |
| model = ColPaliForRetrieval.from_pretrained( | |
| model_name, | |
| device_map="auto", # "cpu", "cuda", "xpu", or "mps" for Apple Silicon | |
| ) | |
| processor = ColPaliProcessor.from_pretrained(model_name) | |
| # The document page screenshots from your corpus | |
| url1 = "https://upload.wikimedia.org/wikipedia/commons/8/89/US-original-Declaration-1776.jpg" | |
| url2 = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Romeoandjuliet1597.jpg/500px-Romeoandjuliet1597.jpg" | |
| images = [ | |
| Image.open(requests.get(url1, stream=True).raw), | |
| Image.open(requests.get(url2, stream=True).raw), | |
| ] | |
| # The queries you want to retrieve documents for | |
| queries = [ | |
| "When was the United States Declaration of Independence proclaimed?", | |
| "Who printed the edition of Romeo and Juliet?", | |
| ] | |
| # Process the inputs | |
| inputs_images = processor(images=images).to(model.device) | |
| inputs_text = processor(text=queries).to(model.device) | |
| # Forward pass | |
| with torch.no_grad(): | |
| image_embeddings = model(**inputs_images).embeddings | |
| query_embeddings = model(**inputs_text).embeddings | |
| # Score the queries against the images | |
| scores = processor.score_retrieval(query_embeddings, image_embeddings) | |
| print("Retrieval scores (query x image):") | |
| print(scores) | |
| ``` | |
| If you have issue with loading the images with PIL, you can use the following code to create dummy images: | |
| ```python | |
| images = [ | |
| Image.new("RGB", (128, 128), color="white"), | |
| Image.new("RGB", (64, 32), color="black"), | |
| ] | |
| ``` | |
| Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the [Quantization](../quantization/overview) overview for more available quantization backends. | |
| The example below uses [bitsandbytes](../quantization/bitsandbytes) to quantize the weights to int4. | |
| ```python | |
| import requests | |
| import torch | |
| from PIL import Image | |
| from transformers import BitsAndBytesConfig, ColPaliForRetrieval, ColPaliProcessor | |
| model_name = "vidore/colpali-v1.3-hf" | |
| # 4-bit quantization configuration | |
| bnb_config = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_use_double_quant=True, | |
| bnb_4bit_quant_type="nf4", | |
| bnb_4bit_compute_dtype=torch.float16, | |
| ) | |
| model = ColPaliForRetrieval.from_pretrained( | |
| model_name, | |
| quantization_config=bnb_config, | |
| device_map="auto", | |
| ) | |
| processor = ColPaliProcessor.from_pretrained(model_name) | |
| url1 = "https://upload.wikimedia.org/wikipedia/commons/8/89/US-original-Declaration-1776.jpg" | |
| url2 = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Romeoandjuliet1597.jpg/500px-Romeoandjuliet1597.jpg" | |
| images = [ | |
| Image.open(requests.get(url1, stream=True).raw), | |
| Image.open(requests.get(url2, stream=True).raw), | |
| ] | |
| queries = [ | |
| "When was the United States Declaration of Independence proclaimed?", | |
| "Who printed the edition of Romeo and Juliet?", | |
| ] | |
| # Process the inputs | |
| inputs_images = processor(images=images, return_tensors="pt").to(model.device) | |
| inputs_text = processor(text=queries, return_tensors="pt").to(model.device) | |
| # Forward pass | |
| with torch.no_grad(): | |
| image_embeddings = model(**inputs_images).embeddings | |
| query_embeddings = model(**inputs_text).embeddings | |
| # Score the queries against the images | |
| scores = processor.score_retrieval(query_embeddings, image_embeddings) | |
| print("Retrieval scores (query x image):") | |
| print(scores) | |
| ``` | |
| ## Notes | |
| - `score_retrieval()` returns a 2D tensor where the first dimension is the number of queries and the second dimension is the number of images. A higher score indicates more similarity between the query and image. | |
| ## ColPaliConfig[[transformers.ColPaliConfig]] | |
| #### transformers.ColPaliConfig[[transformers.ColPaliConfig]] | |
| ```python | |
| transformers.ColPaliConfig(transformers_version: str | None = None, architectures: list[str] | None = None, output_hidden_states: bool | None = False, return_dict: bool | None = True, dtype: typing.Union[str, ForwardRef('torch.dtype'), NoneType] = None, chunk_size_feed_forward: int = 0, is_encoder_decoder: bool = False, id2label: dict[int, str] | dict[str, str] | None = None, label2id: dict[str, int] | dict[str, str] | None = None, problem_type: typing.Optional[typing.Literal['regression', 'single_label_classification', 'multi_label_classification']] = None, vlm_config: dict | transformers.configuration_utils.PreTrainedConfig | None = None, text_config: dict | transformers.configuration_utils.PreTrainedConfig | None = None, embedding_dim: int = 128) | |
| ``` | |
| [Source](https://github.com/huggingface/transformers/blob/vr_48314/src/transformers/models/colpali/configuration_colpali.py#L28) | |
| **Parameters:** | |
| vlm_config (`Union[dict, ~configuration_utils.PreTrainedConfig]`, *optional*) : The config object or dictionary of the vision-language backbone. | |
| text_config (`Union[dict, ~configuration_utils.PreTrainedConfig]`, *optional*) : The config object or dictionary of the text backbone. | |
| embedding_dim (`int`, *optional*, defaults to `128`) : Dimensionality of the embeddings and hidden states. | |
| This is the configuration class to store the configuration of a ColpaliModel. It is used to instantiate a Colpali | |
| model according to the specified arguments, defining the model architecture. Instantiating a configuration with the | |
| defaults will yield a similar configuration to that of the [vidore/colpali-v1.2](https://huggingface.co/vidore/colpali-v1.2) | |
| Configuration objects inherit from [PreTrainedConfig](/docs/transformers/pr_48314/en/main_classes/configuration#transformers.PreTrainedConfig) and can be used to control the model outputs. Read the | |
| documentation from [PreTrainedConfig](/docs/transformers/pr_48314/en/main_classes/configuration#transformers.PreTrainedConfig) for more information. | |
| Example: | |
| ```python | |
| from transformers.models.colpali import ColPaliConfig, ColPaliForRetrieval | |
| config = ColPaliConfig() | |
| model = ColPaliForRetrieval(config) | |
| ``` | |
| ## ColPaliProcessor[[transformers.ColPaliProcessor]] | |
| #### transformers.ColPaliProcessor[[transformers.ColPaliProcessor]] | |
| ```python | |
| transformers.ColPaliProcessor(image_processor = None, tokenizer = None, chat_template = None, visual_prompt_prefix: str = 'Describe the image.', query_prefix: str = 'Question: ') | |
| ``` | |
| [Source](https://github.com/huggingface/transformers/blob/vr_48314/src/transformers/models/colpali/processing_colpali.py#L54) | |
| **Parameters:** | |
| image_processor (`SiglipImageProcessor`) : The image processor is a required input. | |
| tokenizer (`tokenizer_class`) : The tokenizer is a required input. | |
| chat_template (`str`) : A Jinja template to convert lists of messages in a chat into a tokenizable string. | |
| visual_prompt_prefix (`str`, *optional*, defaults to `"Describe the image."`) : A string that gets tokenized and prepended to the image tokens. | |
| query_prefix (`str`, *optional*, defaults to `"Question : "`): A prefix to be used for the query. | |
| Constructs a ColPaliProcessor which wraps a image processor and a tokenizer into a single processor. | |
| [ColPaliProcessor](/docs/transformers/pr_48314/en/model_doc/colpali#transformers.ColPaliProcessor) offers all the functionalities of [SiglipImageProcessor](/docs/transformers/pr_48314/en/model_doc/siglip#transformers.SiglipImageProcessor) and `tokenizer_class`. See the | |
| [~SiglipImageProcessor](/docs/transformers/pr_48314/en/model_doc/siglip#transformers.SiglipImageProcessor) and `~tokenizer_class` for more information. | |
| #### __call__[[transformers.ColPaliProcessor.__call__]] | |
| ```python | |
| __call__(images: typing.Union[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), list['PIL.Image.Image'], list[numpy.ndarray], list['torch.Tensor'], NoneType] = None, text: str | list[str] | list[list[str]] = None, **kwargs: Unpack) | |
| ``` | |
| [Source](https://github.com/huggingface/transformers/blob/vr_48314/src/transformers/models/colpali/processing_colpali.py#L94) | |
| **Parameters:** | |
| images (`Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]]`, *optional*) : Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. | |
| text (`Union[str, list[str], list[list[str]]]`, *optional*) : The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If you pass a pretokenized input, set `is_split_into_words=True` to avoid ambiguity with batched inputs. | |
| return_tensors (`str` or [TensorType](/docs/transformers/pr_48314/en/internal/file_utils#transformers.TensorType), *optional*) : If set, will return tensors of a particular framework. Acceptable values are: - `'pt'`: Return PyTorch `torch.Tensor` objects. - `'np'`: Return NumPy `np.ndarray` objects. | |
| - ****kwargs** ([ProcessingKwargs](/docs/transformers/pr_48314/en/main_classes/processors#transformers.ProcessingKwargs), *optional*) : Additional processing options for each modality (text, images, videos, audio). Model-specific parameters are listed above; see the TypedDict class for the complete list of supported arguments. | |
| **Returns:** [BatchFeature](/docs/transformers/pr_48314/en/main_classes/image_processor#transformers.BatchFeature) | |
| A [BatchFeature](/docs/transformers/pr_48314/en/main_classes/image_processor#transformers.BatchFeature) with the following fields: | |
| - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`. If `suffix` | |
| is provided, the `input_ids` will also contain the suffix input ids. | |
| - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when | |
| `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not | |
| `None`). | |
| - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`. | |
| - **labels** -- Labels compatible with training if `suffix` is not None | |
| ## ColPaliForRetrieval[[transformers.ColPaliForRetrieval]] | |
| #### transformers.ColPaliForRetrieval[[transformers.ColPaliForRetrieval]] | |
| ```python | |
| transformers.ColPaliForRetrieval(config: ColPaliConfig) | |
| ``` | |
| [Source](https://github.com/huggingface/transformers/blob/vr_48314/src/transformers/models/colpali/modeling_colpali.py#L89) | |
| **Parameters:** | |
| config ([ColPaliConfig](/docs/transformers/pr_48314/en/model_doc/colpali#transformers.ColPaliConfig)) : Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [from_pretrained()](/docs/transformers/pr_48314/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights. | |
| The ColPali architecture leverages VLMs to construct efficient multi-vector embeddings directly | |
| from document images (“screenshots”) for document retrieval. The model is trained to maximize the similarity | |
| between these document embeddings and the corresponding query embeddings, using the late interaction method | |
| introduced in ColBERT. | |
| Using ColPali removes the need for potentially complex and brittle layout recognition and OCR pipelines with a | |
| single model that can take into account both the textual and visual content (layout, charts, etc.) of a document. | |
| ColPali is part of the ColVision model family, which was first introduced in the following paper: | |
| [*ColPali: Efficient Document Retrieval with Vision Language Models*](https://huggingface.co/papers/2407.01449). | |
| This model inherits from [PreTrainedModel](/docs/transformers/pr_48314/en/main_classes/model#transformers.PreTrainedModel). Check the superclass documentation for the generic methods the | |
| library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads | |
| etc.) | |
| This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. | |
| Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage | |
| and behavior. | |
| #### forward[[transformers.ColPaliForRetrieval.forward]] | |
| ```python | |
| forward(input_ids: typing.Optional[torch.LongTensor] = None, pixel_values: typing.Optional[torch.FloatTensor] = None, attention_mask: typing.Optional[torch.Tensor] = None, **kwargs: Unpack) | |
| ``` | |
| [Source](https://github.com/huggingface/transformers/blob/vr_48314/src/transformers/models/colpali/modeling_colpali.py#L107) | |
| **Parameters:** | |
| input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*) : Indices of input sequence tokens in the vocabulary. Padding will be ignored by default. Indices can be obtained using [AutoTokenizer](/docs/transformers/pr_48314/en/model_doc/auto#transformers.AutoTokenizer). See [PreTrainedTokenizer.encode()](/docs/transformers/pr_48314/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.encode) and [PreTrainedTokenizer.__call__()](/docs/transformers/pr_48314/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.__call__) for details. [What are input IDs?](../glossary#input-ids) | |
| pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`, *optional*) : The tensors corresponding to the input images. Pixel values can be obtained using [SiglipImageProcessor](/docs/transformers/pr_48314/en/model_doc/siglip#transformers.SiglipImageProcessor). See `SiglipImageProcessor.__call__()` for details ([ColPaliProcessor](/docs/transformers/pr_48314/en/model_doc/colpali#transformers.ColPaliProcessor) uses [SiglipImageProcessor](/docs/transformers/pr_48314/en/model_doc/siglip#transformers.SiglipImageProcessor) for processing images). | |
| attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*) : Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. [What are attention masks?](../glossary#attention-mask) | |
| **Returns:** `ColPaliForRetrievalOutput` or `tuple(torch.FloatTensor)` | |
| A `ColPaliForRetrievalOutput` or a tuple of | |
| `torch.FloatTensor` (if `return_dict=False` is passed or when `config.return_dict=False`) comprising various | |
| elements depending on the configuration ([ColPaliConfig](/docs/transformers/pr_48314/en/model_doc/colpali#transformers.ColPaliConfig)) and inputs. | |
| The [ColPaliForRetrieval](/docs/transformers/pr_48314/en/model_doc/colpali#transformers.ColPaliForRetrieval) forward method, overrides the `__call__` special method. | |
| Although the recipe for forward pass needs to be defined within this function, one should call the `Module` | |
| instance afterwards instead of this since the former takes care of running the pre and post processing steps while | |
| the latter silently ignores them. | |
| - **loss** (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided) -- Language modeling loss (for next-token prediction). | |
| - **embeddings** (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`) -- The embeddings of the model. | |
| - **past_key_values** (`Cache`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`) -- It is a [Cache](/docs/transformers/pr_48314/en/internal/generation_utils#transformers.Cache) instance. For more details, see our [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache). | |
| Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see | |
| `past_key_values` input) to speed up sequential decoding. | |
| - **hidden_states** (`tuple[torch.FloatTensor]`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`) -- Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, + | |
| one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`. | |
| Hidden-states of the model at the output of each layer plus the optional initial embedding outputs. | |
| - **attentions** (`tuple[torch.FloatTensor]`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`) -- Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length, | |
| sequence_length)`. | |
| Attentions weights after the attention softmax, used to compute the weighted average in the self-attention | |
| heads. | |
| - **image_hidden_states** (`torch.FloatTensor`, *optional*) -- A `torch.FloatTensor` of size `(batch_size, num_images, sequence_length, hidden_size)`. | |
| image_hidden_states of the model produced by the vision encoder after projecting last hidden state. | |
| Example: | |
| ```python | |
| ``` | |
Xet Storage Details
- Size:
- 17.9 kB
- Xet hash:
- 860384f905b93da89756c570d6b04af72225570189bb5f35a967c4a3b637f2fd
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.