### Install LayoutLMV2 dependencies Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Install the required external libraries for LayoutLMV2, including detectron2, torchvision, and tesseract. ```bash python -m pip install 'git+https://github.com/facebookresearch/detectron2.git' python -m pip install torchvision tesseract ``` -------------------------------- ### Load and Use LayoutLMv2Model Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Demonstrates loading a processor and the LayoutLMv2Model, processing an image, and obtaining the last hidden states. Ensure the 'transformers', 'Pillow', 'torch', and 'datasets' libraries are installed. ```python >>> from transformers import AutoProcessor, LayoutLMv2Model, set_seed >>> from PIL import Image >>> import torch >>> from datasets import load_dataset >>> set_seed(0) >>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased") >>> model = LayoutLMv2Model.from_pretrained("microsoft/layoutlmv2-base-uncased") >>> dataset = load_dataset("hf-internal-testing/fixtures_docvqa") >>> image = dataset["test"][0]["image"] >>> encoding = processor(image, return_tensors="pt") >>> outputs = model(**encoding) >>> last_hidden_states = outputs.last_hidden_state >>> last_hidden_states.shape torch.Size([1, 342, 768]) ``` -------------------------------- ### Load Document Image with PIL Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Example of loading an image using the PIL library to retrieve dimensions for bounding box normalization. ```python from PIL import Image image = Image.open( "name_of_your_document - can be a png, jpg, etc. of your documents (PDFs must be converted to images)." ) width, height = image.size ``` -------------------------------- ### Question Answering with LayoutLMv2 and Target Spans Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 This snippet demonstrates how to provide target start and end positions to the LayoutLMv2 model for question answering, allowing for loss calculation during training or evaluation. It uses the same setup as the previous example. ```python target_start_index = torch.tensor([7]) target_end_index = torch.tensor([14]) outputs = model(**encoding, start_positions=target_start_index, end_positions=target_end_index) predicted_answer_span_start = outputs.start_logits.argmax(-1).item() predicted_answer_span_end = outputs.end_logits.argmax(-1).item() predicted_answer_span_start, predicted_answer_span_end ``` -------------------------------- ### LayoutLMv2ForQuestionAnswering.forward Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 The forward method for the LayoutLMv2ForQuestionAnswering model, which computes span start and end logits for question-answering tasks. ```APIDOC ## forward ### Description Computes span start and end logits for extractive question-answering tasks using the LayoutLMv2 architecture. ### Parameters #### Request Body - **input_ids** (torch.LongTensor) - Optional - Indices of input sequence tokens in the vocabulary. - **bbox** (torch.LongTensor) - Optional - Bounding boxes of each input sequence token in (x0, y0, x1, y1) format. - **image** (torch.FloatTensor) - Optional - Batch of document images. - **attention_mask** (torch.FloatTensor) - Optional - Mask to avoid performing attention on padding token indices. - **token_type_ids** (torch.LongTensor) - Optional - Segment token indices to indicate first and second portions of the inputs. - **position_ids** (torch.LongTensor) - Optional - Indices of positions of each input sequence token. - **inputs_embeds** (torch.FloatTensor) - Optional - Directly passed embedded representation instead of input_ids. - **start_positions** (torch.LongTensor) - Optional - Labels for the start of the labelled span for loss computation. - **end_positions** (torch.LongTensor) - Optional - Labels for the end of the labelled span for loss computation. ``` -------------------------------- ### Question Answering with LayoutLMv2 Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 This snippet shows how to load the processor and model, process an image and question, and get predicted answer spans. It requires the 'transformers', 'torch', 'PIL', and 'datasets' libraries. ```python from transformers import AutoProcessor, LayoutLMv2ForQuestionAnswering, set_seed import torch from PIL import Image from datasets import load_dataset set_seed(0) processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased") model = LayoutLMv2ForQuestionAnswering.from_pretrained("microsoft/layoutlmv2-base-uncased") dataset = load_dataset("hf-internal-testing/fixtures_docvqa") image = dataset["test"][0]["image"] question = "When is coffee break?" encoding = processor(image, question, return_tensors="pt") outputs = model(**encoding) predicted_start_idx = outputs.start_logits.argmax(-1).item() predicted_end_idx = outputs.end_logits.argmax(-1).item() predicted_start_idx, predicted_end_idx predicted_answer_tokens = encoding.input_ids.squeeze()[predicted_start_idx : predicted_end_idx + 1] predicted_answer = processor.tokenizer.decode(predicted_answer_tokens) predicted_answer # results are not good without further fine-tuning ``` -------------------------------- ### LayoutLMv2Model Forward Pass Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 This section describes the forward pass of the LayoutLMv2Model, detailing its output attributes and providing a Python code example. ```APIDOC ## POST /models/layoutlmv2 ### Description Processes input data through the LayoutLMv2 model to obtain hidden states and other outputs. ### Method POST ### Endpoint /models/layoutlmv2 ### Parameters #### Request Body - **input_ids** (torch.FloatTensor) - Required - Input token IDs. - **attention_mask** (torch.FloatTensor) - Optional - Attention mask. - **token_type_ids** (torch.FloatTensor) - Optional - Token type IDs. - **position_ids** (torch.FloatTensor) - Optional - Position IDs. - **output_attentions** (bool) - Optional - Whether to return attentions weights. - **output_hidden_states** (bool) - Optional - Whether to return all hidden states. - **return_dict** (bool) - Optional - Whether to return a BaseModelOutputWithPooling object. ### Request Example ```json { "input_ids": "[101, 19204, 2053, 1037, 2179, 102]", "attention_mask": "[1, 1, 1, 1, 1, 1]", "return_dict": true } ``` ### Response #### Success Response (200) - **last_hidden_state** (torch.FloatTensor) - Sequence of hidden-states at the output of the last layer of the model. - **pooler_output** (torch.FloatTensor) - Last layer hidden-state of the first token of the sequence (classification token) after further processing. - **hidden_states** (tuple(torch.FloatTensor), optional) - Tuple of hidden-states from all layers. - **attentions** (tuple(torch.FloatTensor), optional) - Tuple of attention weights from all layers. #### Response Example ```json { "last_hidden_state": "[[...]]", "pooler_output": "[[...]]", "hidden_states": "[[...]]", "attentions": "[[...]]" } ``` ``` -------------------------------- ### LayoutLMv2ForQuestionAnswering Forward Pass Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Details the forward method for the LayoutLMv2ForQuestionAnswering model, which computes span extraction loss and returns logits for start and end positions. ```APIDOC ## LayoutLMv2ForQuestionAnswering Forward Pass ### Description The forward method for LayoutLMv2ForQuestionAnswering computes span extraction loss and returns start and end logits for question answering tasks. It overrides the __call__ method and should be invoked via the model instance. ### Parameters #### Request Body - **start_positions** (torch.LongTensor) - Optional - Labels for position (index) of the start of the labelled span for computing the token classification loss. - **end_positions** (torch.LongTensor) - Optional - Labels for position (index) of the end of the labelled span for computing the token classification loss. ### Response #### Success Response (200) - **loss** (torch.FloatTensor) - Total span extraction loss (sum of Cross-Entropy for start and end positions). - **start_logits** (torch.FloatTensor) - Span-start scores (before SoftMax). - **end_logits** (torch.FloatTensor) - Span-end scores (before SoftMax). - **hidden_states** (tuple(torch.FloatTensor)) - Hidden-states of the model at the output of each layer. - **attentions** (tuple(torch.FloatTensor)) - Attentions weights after the attention softmax. ``` -------------------------------- ### Initialize LayoutLMv2Processor Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Instantiate the processor by combining a LayoutLMv2ImageProcessor and a LayoutLMv2TokenizerFast. ```python from transformers import LayoutLMv2ImageProcessor, LayoutLMv2TokenizerFast, LayoutLMv2Processor image_processor = LayoutLMv2ImageProcessor() # apply_ocr is set to True by default tokenizer = LayoutLMv2TokenizerFast.from_pretrained("microsoft/layoutlmv2-base-uncased") processor = LayoutLMv2Processor(image_processor, tokenizer) ``` -------------------------------- ### Initialize LayoutLMv2Config and Model Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Demonstrates how to initialize a LayoutLMv2 configuration and then use it to create a model with random weights. Accessing model.config retrieves the configuration object. ```python >>> from transformers import LayoutLMv2Config, LayoutLMv2Model >>> # Initializing a LayoutLMv2 microsoft/layoutlmv2-base-uncased style configuration >>> configuration = LayoutLMv2Config() >>> # Initializing a model (with random weights) from the microsoft/layoutlmv2-base-uncased style configuration >>> model = LayoutLMv2Model(configuration) >>> # Accessing the model configuration >>> configuration = model.config ``` -------------------------------- ### LayoutLMv2 Model Initialization Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Details the required components for initializing the LayoutLMv2 model and the structure of the returned encoding data. ```APIDOC ## LayoutLMv2 Model Initialization ### Description Initializes the LayoutLMv2 model with the required image processor and tokenizer, returning a BatchEncoding object. ### Parameters - **image_processor** (LayoutLMv2ImageProcessor) - Required - The image processor instance. - **tokenizer** (LayoutLMv2Tokenizer) - Required - The tokenizer instance. ### Returns - **BatchEncoding** (tokenization_utils_base.BatchEncoding) - The processed data containing input_ids, attention_mask, and other relevant encoding information. ### Response Fields - **data** (dict) - Optional - Dictionary of lists/arrays/tensors returned by the encoding methods. - **encoding** (tokenizers.Encoding or Sequence) - Optional - Additional information like mapping from word/character space to token space. - **tensor_type** (Union[None, str, TensorType]) - Optional - Used to convert lists of integers to PyTorch/Numpy Tensors. - **prepend_batch_axis** (bool) - Optional - Whether to add a batch axis when converting to tensors (defaults to False). - **n_sequences** (int) - Optional - Used for tensor conversion initialization. ``` -------------------------------- ### Load Dataset, Processor, and Model for LayoutLMv2 Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Loads a dataset, processor, and the LayoutLMv2ForSequenceClassification model. Ensure the number of labels matches the dataset's requirements. The model weights are loaded using `from_pretrained`. ```python from transformers import AutoProcessor, LayoutLMv2ForSequenceClassification, set_seed from PIL import Image import torch from datasets import load_dataset set_seed(0) dataset = load_dataset("aharley/rvl_cdip", split="train", streaming=True) data = next(iter(dataset)) image = data["image"].convert("RGB") processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased") model = LayoutLMv2ForSequenceClassification.from_pretrained( "microsoft/layoutlmv2-base-uncased", num_labels=dataset.info.features["label"].num_classes ) ``` -------------------------------- ### Tokenizer Initialization Settings Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Settings for initializing a tokenizer, including vocabulary files and special token definitions for document-level tasks. ```APIDOC ## Tokenizer Initialization ### Parameters - **vocab_file** (str) - Required - File containing the vocabulary. - **do_lower_case** (bool) - Optional - Whether to lowercase the input. - **unk_token** (str) - Optional - The unknown token. - **sep_token** (str) - Optional - The separator token. - **pad_token** (str) - Optional - The padding token. - **cls_token** (str) - Optional - The classifier token. - **mask_token** (str) - Optional - The masking token. - **cls_token_box** (List[int]) - Optional - Bounding box for the [CLS] token. - **sep_token_box** (List[int]) - Optional - Bounding box for the [SEP] token. - **pad_token_box** (List[int]) - Optional - Bounding box for the [PAD] token. - **pad_token_label** (int) - Optional - Label to use for padding tokens. ``` -------------------------------- ### LayoutLMv2Tokenizer Configuration Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Configuration parameters for the LayoutLMv2Tokenizer initialization. ```APIDOC ### Parameters - **only_label_first_subword** (bool) - Optional - Whether or not to only label the first subword, in case word labels are provided. Defaults to True. - **tokenize_chinese_chars** (bool) - Optional - Whether or not to tokenize Chinese characters. Defaults to True. - **strip_accents** (bool) - Optional - Whether or not to strip all accents. If not specified, determined by the value for lowercase. ``` -------------------------------- ### Tokenizer Configuration and Parameters Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Details the various parameters available for configuring the tokenizer and its behavior during the tokenization process. ```APIDOC ## Tokenizer Parameters This section describes the parameters used for tokenizing input sequences. ### Parameters - **max_length** (`int`, *optional*) -- Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. - **stride** (`int`, *optional*, defaults to 0) -- If set to a number along with `max_length`, the overflowing tokens returned when `return_overflowing_tokens=True` will contain some tokens from the end of the truncated sequence returned to provide some overlap between truncated and overflowing sequences. The value of this argument defines the number of overlapping tokens. - **pad_to_multiple_of** (`int`, *optional*) -- If set will pad the sequence to a multiple of the provided value. This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability `>= 7.5` (Volta). - **return_token_type_ids** (`bool`, *optional*) -- Whether to return token type IDs. If left to the default, will return the token type IDs according to the specific tokenizer's default, defined by the `return_outputs` attribute. - **return_attention_mask** (`bool`, *optional*) -- Whether to return the attention mask. If left to the default, will return the attention mask according to the specific tokenizer's default, defined by the `return_outputs` attribute. - **return_overflowing_tokens** (`bool`, *optional*, defaults to `False`) -- Whether or not to return overflowing token sequences. If a pair of sequences of input ids (or a batch of pairs) is provided with `truncation_strategy = longest_first` or `True`, an error is raised instead of returning overflowing tokens. - **return_special_tokens_mask** (`bool`, *optional*, defaults to `False`) -- Whether or not to return special tokens mask information. - **return_offsets_mapping** (`bool`, *optional*, defaults to `False`) -- Whether or not to return `(char_start, char_end)` for each token. This is only available on fast tokenizers inheriting from [PreTrainedTokenizerFast](/docs/transformers/v5.5.0/en/main_classes/tokenizer#transformers.TokenizersBackend), if using Python's tokenizer, this method will raise `NotImplementedError`. - **return_length** (`bool`, *optional*, defaults to `False`) -- Whether or not to return the lengths of the encoded inputs. - **verbose** (`bool`, *optional*, defaults to `True`) -- Whether or not to print more information and warnings. - ****kwargs** -- passed to the `self.tokenize()` method ### Tokenizer Initialization Parameters - **vocab_file** (`str`) : File containing the vocabulary. - **do_lower_case** (`bool`, *optional*, defaults to `True`) : Whether or not to lowercase the input when tokenizing. - **unk_token** (`str`, *optional*, defaults to `"[UNK]"`) : The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead. - **sep_token** (`str`, *optional*, defaults to `"[SEP]"`) : The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for sequence classification or for a text and a question for question answering. It is also used as the last token of a sequence built with special tokens. - **pad_token** (`str`, *optional*, defaults to `"[PAD]"`) : The token used for padding, for example when batching sequences of different lengths. - **cls_token** (`str`, *optional*, defaults to `"[CLS]"`) : The classifier token which is used when doing sequence classification (classification of the whole sequence instead of per-token classification). It is the first token of the sequence when built with special tokens. - **mask_token** (`str`, *optional*, defaults to `"[MASK]"`) : The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - **cls_token_box** (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`) : The bounding box to use for the special [CLS] token. - **sep_token_box** (`List[int]`, *optional*, defaults to `[1000, 1000, 1000, 1000]`) : The bounding box to use for the special [SEP] token. - **pad_token_box** (`List[int]`, *optional`, defaults to `[0, 0, 0, 0]`) : The bounding box to use for the special [PAD] token. - **pad_token_label** (`int`, *optional*, defaults to -100) : The label to use for padding tokens. Defaults to -100, which is the `ignore_index` of PyTorch's CrossEntropyLoss. ``` -------------------------------- ### LayoutLMv2ForQuestionAnswering Configuration Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Details regarding the initialization and configuration parameters for the LayoutLMv2ForQuestionAnswering model. ```APIDOC ## LayoutLMv2ForQuestionAnswering Configuration ### Description Model configuration class for LayoutLMv2ForQuestionAnswering. Initializing with a config file does not load model weights. ### Parameters #### Request Body - **config** (LayoutLMv2ForQuestionAnswering) - Required - Model configuration class with all parameters. - **has_visual_segment_embedding** (bool) - Optional - Whether or not to add visual segment embeddings. Defaults to True. ### Response #### Success Response (200) - **Returns** (QuestionAnsweringModelOutput or tuple(torch.FloatTensor)) - Returns a QuestionAnsweringModelOutput or a tuple of torch.FloatTensor depending on the return_dict configuration. ``` -------------------------------- ### Perform Token Classification with LayoutLMv2 Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Demonstrates loading a pre-trained LayoutLMv2 model, processing input data including images and bounding boxes, and extracting predicted token classes. ```python >>> from transformers import AutoProcessor, LayoutLMv2ForTokenClassification, set_seed >>> from PIL import Image >>> from datasets import load_dataset >>> set_seed(0) >>> datasets = load_dataset("nielsr/funsd", split="test") >>> labels = datasets.features["ner_tags"].feature.names >>> id2label = {v: k for v, k in enumerate(labels)} >>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased", revision="no_ocr") >>> model = LayoutLMv2ForTokenClassification.from_pretrained( ... "microsoft/layoutlmv2-base-uncased", num_labels=len(labels) ... ) >>> data = datasets[0] >>> image = Image.open(data["image_path"]).convert("RGB") >>> words = data["words"] >>> boxes = data["bboxes"] # make sure to normalize your bounding boxes >>> word_labels = data["ner_tags"] >>> encoding = processor( ... image, ... words, ... boxes=boxes, ... word_labels=word_labels, ... padding="max_length", ... truncation=True, ... return_tensors="pt", ... ) >>> outputs = model(**encoding) >>> logits, loss = outputs.logits, outputs.loss >>> predicted_token_class_ids = logits.argmax(-1) >>> predicted_tokens_classes = [id2label[t.item()] for t in predicted_token_class_ids[0]] >>> predicted_tokens_classes[:5] # results are not good without further fine-tuning ['I-HEADER', 'I-HEADER', 'I-QUESTION', 'I-HEADER', 'I-QUESTION'] ``` -------------------------------- ### LayoutLMv2Config Class Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Configuration class for initializing a LayoutLMv2 model architecture. ```APIDOC ## LayoutLMv2Config ### Description This is the configuration class to store the configuration of a LayoutLMv2Model. It is used to instantiate a LayoutLMv2 model according to the specified arguments, defining the model architecture. ### Parameters - **vocab_size** (int) - Optional - Vocabulary size of the model. Defaults to 30522. - **hidden_size** (int) - Optional - Dimension of the hidden representations. Defaults to 768. - **num_hidden_layers** (int) - Optional - Number of hidden layers in the Transformer decoder. Defaults to 12. - **num_attention_heads** (int) - Optional - Number of attention heads for each attention layer. Defaults to 12. - **intermediate_size** (int) - Optional - Dimension of the MLP representations. Defaults to 3072. - **hidden_act** (str) - Optional - The non-linear activation function. Defaults to "gelu". - **hidden_dropout_prob** (float/int) - Optional - Dropout probability for fully connected layers. Defaults to 0.1. - **attention_probs_dropout_prob** (float/int) - Optional - Dropout ratio for attention probabilities. Defaults to 0.1. - **max_position_embeddings** (int) - Optional - Maximum sequence length. Defaults to 512. - **type_vocab_size** (int) - Optional - Vocabulary size of the token_type_ids. Defaults to 2. - **initializer_range** (float) - Optional - Standard deviation of the truncated_normal_initializer. Defaults to 0.02. - **layer_norm_eps** (float) - Optional - Epsilon used by layer normalization layers. Defaults to 1e-12. - **pad_token_id** (int) - Optional - Token id used for padding. Defaults to 0. - **max_2d_position_embeddings** (int) - Optional - Maximum value for 2D position embedding. Defaults to 1024. - **max_rel_pos** (int) - Optional - Maximum number of relative positions. Defaults to 128. - **rel_pos_bins** (int) - Optional - Number of relative position bins. Defaults to 32. - **fast_qkv** (bool) - Optional - Whether to use a single matrix for queries, keys, values. Defaults to True. - **max_rel_2d_pos** (int) - Optional - Maximum number of relative 2D positions. Defaults to 256. - **rel_2d_pos_bins** (int) - Optional - Number of 2D relative position bins. Defaults to 64. - **convert_sync_batchnorm** (bool) - Optional - Whether to convert batch normalization to synchronized batch normalization. Defaults to True. - **image_feature_pool_shape** (list[int]) - Optional - Shape of the average-pooled feature map. Defaults to [7, 7, 256]. - **coordinate_size** (int) - Optional - Dimension of the coordinate embeddings. Defaults to 128. - **shape_size** (int) - Optional - Dimension of the width and height embeddings. Defaults to 128. - **has_relative_attention_bias** (bool) - Optional - Whether to use a relative attention bias. Defaults to True. - **has_spatial_attention_bias** (bool) - Optional - Whether to use a spatial attention bias. Defaults to True. - **has_visual_segment_embedding** (bool) - Optional - Whether to add visual segment embeddings. Defaults to False. ``` -------------------------------- ### Process Document with Manual OCR Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Provide custom words and normalized bounding boxes when the processor is initialized with apply_ocr=False. ```python from transformers import LayoutLMv2Processor from PIL import Image processor = LayoutLMv2Processor.from_pretrained("microsoft/layoutlmv2-base-uncased", revision="no_ocr") image = Image.open( "name_of_your_document - can be a png, jpg, etc. of your documents (PDFs must be converted to images)." ).convert("RGB") words = ["hello", "world"] boxes = [[1, 2, 3, 4], [5, 6, 7, 8]] # make sure to normalize your bounding boxes encoding = processor(image, words, boxes=boxes, return_tensors="pt") print(encoding.keys()) ``` -------------------------------- ### LayoutLMv2Processor Configuration Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Configuration parameters for the LayoutLMv2Processor class. ```APIDOC ## LayoutLMv2Processor ### Description Configuration parameters for the LayoutLMv2Processor class used in the Transformers library. ### Parameters - **only_label_first_subword** (bool) - Optional - Whether or not to only label the first subword, in case word labels are provided. Defaults to True. - **tokenize_chinese_chars** (bool) - Optional - Whether or not to tokenize Chinese characters. Defaults to True. - **strip_accents** (bool) - Optional - Whether or not to strip all accents. If not specified, determined by the value for lowercase. ``` -------------------------------- ### Tokenizer Configuration Parameters Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Configuration parameters used to control sequence processing, truncation, padding, and output formatting during tokenization. ```APIDOC ## Tokenizer Configuration Parameters ### Parameters - **max_length** (int) - Optional - Controls the maximum length for truncation/padding. - **stride** (int) - Optional - Number of overlapping tokens when returning overflowing tokens. - **pad_to_multiple_of** (int) - Optional - Pads sequence to a multiple of the provided value. - **return_token_type_ids** (bool) - Optional - Whether to return token type IDs. - **return_attention_mask** (bool) - Optional - Whether to return the attention mask. - **return_overflowing_tokens** (bool) - Optional - Whether to return overflowing token sequences. - **return_special_tokens_mask** (bool) - Optional - Whether to return special tokens mask information. - **return_offsets_mapping** (bool) - Optional - Whether to return (char_start, char_end) for each token. - **return_length** (bool) - Optional - Whether to return the lengths of the encoded inputs. - **verbose** (bool) - Optional - Whether to print information and warnings. ``` -------------------------------- ### LayoutLMv2 Tokenizer Initialization and Usage Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 The LayoutLMv2 tokenizer is a fast tokenizer built on HuggingFace's *tokenizers* library. It inherits from PreTrainedTokenizerFast and is primarily used via its __call__ method for encoding text, text pairs, and associated bounding boxes and labels. ```APIDOC ## transformers.LayoutLMv2Tokenizer ### Description Construct a "fast" LayoutLMv2 tokenizer (backed by HuggingFace's *tokenizers* library). Based on WordPiece. This tokenizer inherits from [PreTrainedTokenizerFast](/docs/transformers/v5.5.0/en/main_classes/tokenizer#transformers.TokenizersBackend) which contains most of the main methods. Users should refer to this superclass for more information regarding those methods. ### Method __call__ ### Endpoint N/A (This is a class method, not an API endpoint) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **text** (str | List[str] | List[List[str]]) - Required - The sequence or batch of sequences to be encoded. Each sequence can be a string, a list of strings (words of a single example or questions of a batch of examples) or a list of list of strings (batch of words). - **text_pair** (List[str] | List[List[str]] | None) - Optional - The sequence or batch of sequences to be encoded. Each sequence should be a list of strings (pretokenized string). - **boxes** (List[List[int]] | List[List[List[int]]] | None) - Optional - Word-level bounding boxes. Each bounding box should be normalized to be on a 0-1000 scale. - **word_labels** (List[int] | List[List[int]] | None) - Optional - Word-level integer labels (for token classification tasks such as FUNSD, CORD). - **add_special_tokens** (bool) - Optional - Defaults to True. Whether or not to encode the sequences with the special tokens relative to their model. - **padding** (bool | str | PaddingStrategy) - Optional - Defaults to False. Activates and controls padding. Accepts 'True' or 'longest', 'max_length', 'False' or 'do_not_pad'. - **truncation** (bool | str | TruncationStrategy) - Optional - Defaults to False. Activates and controls truncation. Accepts 'True' or 'longest_first', 'only_first', 'only_second', 'False' or 'do_not_truncate'. - **max_length** (int | None) - Optional - Maximum length for padding or truncation. - **stride** (int) - Optional - Defaults to 0. Stride for truncation. - **pad_to_multiple_of** (int | None) - Optional - Pad to a multiple of this value. - **padding_side** (str | None) - Optional - The side to pad on ('left' or 'right'). - **return_tensors** (str | TensorType | None) - Optional - If specified, returns PyTorch, TensorFlow or NumPy arrays. - **return_token_type_ids** (bool | None) - Optional - Whether to return token type IDs. - **return_attention_mask** (bool | None) - Optional - Whether to return the attention mask. - **return_overflowing_tokens** (bool) - Optional - Defaults to False. Whether to return overflowing tokens. - **return_special_tokens_mask** (bool) - Optional - Defaults to False. Whether to return the special tokens mask. - **return_offsets_mapping** (bool) - Optional - Defaults to False. Whether to return the offsets mapping. - **return_length** (bool) - Optional - Defaults to False. Whether to return the length of the encoded tokens. - **verbose** (bool) - Optional - Defaults to True. Whether to be verbose. - **&&kwargs** - Optional - Additional keyword arguments. ### Request Example ```json { "text": "Example text for tokenization.", "boxes": [[10, 20, 30, 40]], "word_labels": [0] } ``` ### Response #### Success Response (200) - **input_ids** (List[int]) - The token IDs of the encoded text. - **token_type_ids** (List[int]) - The token type IDs. - **attention_mask** (List[int]) - The attention mask. - **offset_mapping** (List[Tuple[int, int]]) - The offset mapping for each token. #### Response Example ```json { "input_ids": [101, 2023, 4572, 2005, 19204, 102], "token_type_ids": [0, 0, 0, 0, 0, 0], "attention_mask": [1, 1, 1, 1, 1, 1], "offset_mapping": [(0, 0), (0, 7), (8, 12), (13, 17), (18, 28), (0, 0)] } ``` ``` -------------------------------- ### LayoutLMv2ImageProcessorPil.preprocess Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Processes images for LayoutLMv2. It can optionally apply OCR to extract text and bounding boxes. ```APIDOC ## POST /transformers/LayoutLMv2ImageProcessorPil/preprocess ### Description Processes images for LayoutLMv2, with optional OCR capabilities. ### Method POST ### Endpoint /transformers/LayoutLMv2ImageProcessorPil/preprocess ### Parameters #### Request Body - **images** (Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]]) - Required - 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`. - **apply_ocr** (bool) - Optional - Whether to apply the Tesseract OCR engine to get words + normalized bounding boxes. Can be overridden by the `apply_ocr` parameter in the `preprocess` method. - **ocr_lang** (str) - Optional - The language, specified by its ISO code, to be used by the Tesseract OCR engine. By default, English is used. Can be overridden by the `ocr_lang` parameter in the `preprocess` method. - **tesseract_config** (str) - Optional - Any additional custom configuration flags that are forwarded to the `config` parameter when calling Tesseract. For example: '--psm 6'. Can be overridden by the `tesseract_config` parameter in the `preprocess` method. - **return_tensors** (str or TensorType) - Optional - Returns stacked tensors if set to 'pt', otherwise returns a list of tensors. - **kwargs** (ImagesKwargs) - Optional - Additional image preprocessing options. Model-specific kwargs are listed above; see the TypedDict class for the complete list of supported arguments. ### Request Example { "images": "...", "apply_ocr": true, "ocr_lang": "eng", "return_tensors": "pt" } ### Response #### Success Response (200) - **data** (dict) - Dictionary of lists/arrays/tensors returned by the __call__ method ('pixel_values', etc.). - **tensor_type** (Union[None, str, TensorType]) - You can give a tensor_type here to convert the lists of integers in PyTorch/Numpy Tensors at initialization. #### Response Example { "data": { "pixel_values": [ [...] ], "input_ids": [ [...] ], "attention_mask": [ [...] ] }, "tensor_type": "pt" } ``` -------------------------------- ### Tokenization Parameters Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 This section describes the parameters used for controlling tokenization behavior, such as truncation strategies, padding, and output formatting. ```APIDOC ## Tokenization Parameters This section describes the parameters used for controlling tokenization behavior, such as truncation strategies, padding, and output formatting. ### Truncation Strategy - **truncation** (`bool` or `str`, *optional*) Controls the truncation strategy. Possible values are: - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). ### Maximum Length - **max_length** (`int`, *optional*) Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. ### Stride - **stride** (`int`, *optional*, defaults to `0`) If set to a number along with `max_length`, the overflowing tokens returned when `return_overflowing_tokens=True` will contain some tokens from the end of the truncated sequence returned to provide some overlap between truncated and overflowing sequences. The value of this argument defines the number of overlapping tokens. ### Pad to Multiple Of - **pad_to_multiple_of** (`int`, *optional*) If set will pad the sequence to a multiple of the provided value. Requires `padding` to be activated. This is especially useful to enable using Tensor Cores on NVIDIA hardware with compute capability `>= 7.5` (Volta). ### Return Options - **return_token_type_ids** (`bool`, *optional*) Whether to return token type IDs. If left to the default, will return the token type IDs according to the specific tokenizer's default, defined by the `return_outputs` attribute. - **return_attention_mask** (`bool`, *optional*) Whether to return the attention mask. If left to the default, will return the attention mask according to the specific tokenizer's default, defined by the `return_outputs` attribute. - **return_overflowing_tokens** (`bool`, *optional*, defaults to `False`) Whether or not to return overflowing token sequences. If a pair of sequences of input ids (or a batch of pairs) is provided with `truncation_strategy = longest_first` or `True`, an error is raised instead of returning overflowing tokens. - **return_special_tokens_mask** (`bool`, *optional*, defaults to `False`) Whether or not to return special tokens mask information. - **return_offsets_mapping** (`bool`, *optional*, defaults to `False`) Whether or not to return `(char_start, char_end)` for each token. This is only available on fast tokenizers inheriting from [PreTrainedTokenizerFast](/docs/transformers/v5.5.0/en/main_classes/tokenizer#transformers.TokenizersBackend), if using Python's tokenizer, this method will raise `NotImplementedError`. - **return_length** (`bool`, *optional*, defaults to `False`) Whether or not to return the lengths of the encoded inputs. ### Verbose - **verbose** (`bool`, *optional*, defaults to `True`) Whether or not to print more information and warnings. ### Return Tensors - **return_tensors** (`Union[str, ~utils.generic.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. ### Output Structure - **data** (`dict`, *optional*) Dictionary of lists/arrays/tensors returned by the `__call__`/`encode_plus`/`batch_encode_plus` methods ('input_ids', 'attention_mask', etc.). - **encoding** (`tokenizers.Encoding` or `Sequence[tokenizers.Encoding]`, *optional*) If the tokenizer is a fast tokenizer which outputs additional information like mapping from word/character space to token space the `tokenizers.Encoding` instance or list of instance (for batches) hold this information. - **tensor_type** (`Union[None, str, TensorType]`, *optional*) You can give a tensor_type here to convert the lists of integers in PyTorch/Numpy Tensors at initialization. - **prepend_batch_axis** (`bool`, *optional*, defaults to `False`) Whether or not to add a batch axis when converting to tensors (see `tensor_type` above). Note that this parameter has an effect if the parameter `tensor_type` is set, *otherwise has no effect*. ``` -------------------------------- ### POST save_vocabulary Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Saves the vocabulary of the LayoutLMv2Tokenizer to a file. ```APIDOC ## save_vocabulary ### Description Saves the vocabulary of the LayoutLMv2Tokenizer. ### Method POST ### Endpoint transformers.LayoutLMv2Tokenizer.save_vocabulary ``` -------------------------------- ### LayoutLMv2ImageProcessor.preprocess Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Preprocesses images for the LayoutLMv2 model, including optional OCR processing. ```APIDOC ## LayoutLMv2ImageProcessor.preprocess ### Description Preprocesses a single image or a batch of images. It handles pixel value scaling and optionally applies Tesseract OCR to extract words and normalized bounding boxes. ### Parameters #### Request Body - **images** (Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list]) - Required - Image to preprocess. Expects pixel values ranging from 0 to 255. - **apply_ocr** (bool) - Optional - Whether to apply the Tesseract OCR engine. Defaults to self.apply_ocr. - **ocr_lang** (str) - Optional - ISO code for the language used by Tesseract. Defaults to English. - **tesseract_config** (str) - Optional - Additional custom configuration flags for Tesseract. - **return_tensors** (str or TensorType) - Optional - Returns stacked tensors if set to 'pt'. ### Response #### Success Response (200) - **data** (dict) - Dictionary containing 'pixel_values' and other processed features. - **tensor_type** (Union[None, str, TensorType]) - Optional - Type used to convert lists to PyTorch/Numpy tensors. ``` -------------------------------- ### Visual Question Answering Inference without OCR using LayoutLMv2Processor Source: https://huggingface.co/docs/transformers/model_doc/layoutlmv2 Use this snippet for visual question answering when you want to perform OCR yourself and provide custom words and bounding boxes. The processor will then use your provided information. ```python from transformers import LayoutLMv2Processor from PIL import Image processor = LayoutLMv2Processor.from_pretrained("microsoft/layoutlmv2-base-uncased", revision="no_ocr") image = Image.open( "name_of_your_document - can be a png, jpg, etc. of your documents (PDFs must be converted to images)." ).convert("RGB") question = "What's his name?" words = ["hello", "world"] boxes = [[1, 2, 3, 4], [5, 6, 7, 8]] # make sure to normalize your bounding boxes encoding = processor(image, question, words, boxes=boxes, return_tensors="pt") print(encoding.keys()) # dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'bbox', 'image']) ```