codingutf8 2021 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license processor class for layoutlmv2 attributes imageprocessor tokenizer imageprocessorclass layoutlmv2imageprocessor tokenizerclass layoutlmv2tokenizer layoutlmv2tokenizerfast def initself imageprocessornone tokenizernone kwargs featureextractor none if featureextractor in kwargs warnings warn the featureextractor argument is deprecated and will be removed in v5 use imageprocessor instead futurewarning featureextractor kwargs popfeatureextractor imageprocessor imageprocessor if imageprocessor is not none else featureextractor if imageprocessor is none raise valueerroryou need to specify an imageprocessor if tokenizer is none raise valueerroryou need to specify a tokenizer super initimageprocessor tokenizer def call self images text uniontextinput pretokenizedinput listtextinput listpretokenizedinput none textpair optionalunionpretokenizedinput listpretokenizedinput none boxes unionlistlistint listlistlistint none wordlabels optionalunionlistint listlistint none addspecialtokens bool true padding unionbool str paddingstrategy false truncation unionbool str truncationstrategy false maxlength optionalint none stride int 0 padtomultipleof optionalint none returntokentypeids optionalbool none returnattentionmask optionalbool none returnoverflowingtokens bool false returnspecialtokensmask bool false returnoffsetsmapping bool false returnlength bool false verbose bool true returntensors optionalunionstr tensortype none kwargs batchencoding verify input if self imageprocessor applyocr and boxes is not none raise valueerror you cannot provide bounding boxes if you initialized the image processor with applyocr set to true if self imageprocessor applyocr and wordlabels is not none raise valueerror you cannot provide word labels if you initialized the image processor with applyocr set to true if returnoverflowingtokens is true and returnoffsetsmapping is false raise valueerroryou cannot return overflowing tokens without returning the offsets mapping first apply the image processor features self imageprocessorimagesimages returntensorsreturntensors second apply the tokenizer if text is not none and self imageprocessor applyocr and textpair is none if isinstancetext str text text add batch dimension as the image processor always adds a batch dimension textpair featureswords encodedinputs self tokenizer texttext if text is not none else featureswords textpairtextpair if textpair is not none else none boxesboxes if boxes is not none else featuresboxes wordlabelswordlabels addspecialtokensaddspecialtokens paddingpadding truncationtruncation maxlengthmaxlength stridestride padtomultipleofpadtomultipleof returntokentypeidsreturntokentypeids returnattentionmaskreturnattentionmask returnoverflowingtokensreturnoverflowingtokens returnspecialtokensmaskreturnspecialtokensmask returnoffsetsmappingreturnoffsetsmapping returnlengthreturnlength verboseverbose returntensorsreturntensors kwargs add pixel values images features poppixelvalues if returnoverflowingtokens is true images self getoverflowingimagesimages encodedinputsoverflowtosamplemapping encodedinputsimage images return encodedinputs def getoverflowingimagesself images overflowtosamplemapping in case there s an overflow ensure each inputids sample is mapped to its corresponding image imageswithoverflow for sampleidx in overflowtosamplemapping imageswithoverflow appendimagessampleidx if lenimageswithoverflow lenoverflowtosamplemapping raise valueerror expected length of images to be the same as the length of overflowtosamplemapping but got f lenimageswithoverflow and lenoverflowtosamplemapping return imageswithoverflow def batchdecodeself args kwargs return self tokenizer batchdecodeargs kwargs def decodeself args kwargs return self tokenizer decodeargs kwargs property def modelinputnamesself return inputids bbox tokentypeids attentionmask image property def featureextractorclassself warnings warn featureextractorclass is deprecated and will be removed in v5 use imageprocessorclass instead futurewarning return self imageprocessorclass property def featureextractorself warnings warn featureextractor is deprecated and will be removed in v5 use imageprocessor instead futurewarning return self imageprocessor coding utf 8 2021 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license processor class for layoutlmv2 constructs a layoutlmv2 processor which combines a layoutlmv2 image processor and a layoutlmv2 tokenizer into a single processor layoutlmv2processor offers all the functionalities you need to prepare data for the model it first uses layoutlmv2imageprocessor to resize document images to a fixed size and optionally applies ocr to get words and normalized bounding boxes these are then provided to layoutlmv2tokenizer or layoutlmv2tokenizerfast which turns the words and bounding boxes into token level input_ids attention_mask token_type_ids bbox optionally one can provide integer word_labels which are turned into token level labels for token classification tasks such as funsd cord args image_processor layoutlmv2imageprocessor optional an instance of layoutlmv2imageprocessor the image processor is a required input tokenizer layoutlmv2tokenizer or layoutlmv2tokenizerfast optional an instance of layoutlmv2tokenizer or layoutlmv2tokenizerfast the tokenizer is a required input this method first forwards the images argument to layoutlmv2imageprocessor __call__ in case layoutlmv2imageprocessor was initialized with apply_ocr set to true it passes the obtained words and bounding boxes along with the additional arguments to layoutlmv2tokenizer __call__ and returns the output together with resized images in case layoutlmv2imageprocessor was initialized with apply_ocr set to false it passes the words text text_pair and boxes specified by the user along with the additional arguments to layoutlmv2tokenizer __call__ and returns the output together with resized images please refer to the docstring of the above two methods for more information verify input first apply the image processor second apply the tokenizer add batch dimension as the image processor always adds a batch dimension add pixel values in case there s an overflow ensure each input_ids sample is mapped to its corresponding image this method forwards all its arguments to pretrainedtokenizer s pretrainedtokenizer batch_decode please refer to the docstring of this method for more information this method forwards all its arguments to pretrainedtokenizer s pretrainedtokenizer decode please refer to the docstring of this method for more information
import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType


class LayoutLMv2Processor(ProcessorMixin):
    r

    attributes = ["image_processor", "tokenizer"]
    image_processor_class = "LayoutLMv2ImageProcessor"
    tokenizer_class = ("LayoutLMv2Tokenizer", "LayoutLMv2TokenizerFast")

    def __init__(self, image_processor=None, tokenizer=None, **kwargs):
        feature_extractor = None
        if "feature_extractor" in kwargs:
            warnings.warn(
                "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                " instead.",
                FutureWarning,
            )
            feature_extractor = kwargs.pop("feature_extractor")

        image_processor = image_processor if image_processor is not None else feature_extractor
        if image_processor is None:
            raise ValueError("You need to specify an `image_processor`.")
        if tokenizer is None:
            raise ValueError("You need to specify a `tokenizer`.")

        super().__init__(image_processor, tokenizer)

    def __call__(
        self,
        images,
        text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,
        text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None,
        boxes: Union[List[List[int]], List[List[List[int]]]] = None,
        word_labels: Optional[Union[List[int], List[List[int]]]] = None,
        add_special_tokens: bool = True,
        padding: Union[bool, str, PaddingStrategy] = False,
        truncation: Union[bool, str, TruncationStrategy] = False,
        max_length: Optional[int] = None,
        stride: int = 0,
        pad_to_multiple_of: Optional[int] = None,
        return_token_type_ids: Optional[bool] = None,
        return_attention_mask: Optional[bool] = None,
        return_overflowing_tokens: bool = False,
        return_special_tokens_mask: bool = False,
        return_offsets_mapping: bool = False,
        return_length: bool = False,
        verbose: bool = True,
        return_tensors: Optional[Union[str, TensorType]] = None,
        **kwargs,
    ) -> BatchEncoding:
        
        
        if self.image_processor.apply_ocr and (boxes is not None):
            raise ValueError(
                "You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True."
            )

        if self.image_processor.apply_ocr and (word_labels is not None):
            raise ValueError(
                "You cannot provide word labels if you initialized the image processor with apply_ocr set to True."
            )

        if return_overflowing_tokens is True and return_offsets_mapping is False:
            raise ValueError("You cannot return overflowing tokens without returning the offsets mapping.")

        
        features = self.image_processor(images=images, return_tensors=return_tensors)

        
        if text is not None and self.image_processor.apply_ocr and text_pair is None:
            if isinstance(text, str):
                text = [text]  
            text_pair = features["words"]

        encoded_inputs = self.tokenizer(
            text=text if text is not None else features["words"],
            text_pair=text_pair if text_pair is not None else None,
            boxes=boxes if boxes is not None else features["boxes"],
            word_labels=word_labels,
            add_special_tokens=add_special_tokens,
            padding=padding,
            truncation=truncation,
            max_length=max_length,
            stride=stride,
            pad_to_multiple_of=pad_to_multiple_of,
            return_token_type_ids=return_token_type_ids,
            return_attention_mask=return_attention_mask,
            return_overflowing_tokens=return_overflowing_tokens,
            return_special_tokens_mask=return_special_tokens_mask,
            return_offsets_mapping=return_offsets_mapping,
            return_length=return_length,
            verbose=verbose,
            return_tensors=return_tensors,
            **kwargs,
        )

        
        images = features.pop("pixel_values")
        if return_overflowing_tokens is True:
            images = self.get_overflowing_images(images, encoded_inputs["overflow_to_sample_mapping"])
        encoded_inputs["image"] = images

        return encoded_inputs

    def get_overflowing_images(self, images, overflow_to_sample_mapping):
        
        images_with_overflow = []
        for sample_idx in overflow_to_sample_mapping:
            images_with_overflow.append(images[sample_idx])

        if len(images_with_overflow) != len(overflow_to_sample_mapping):
            raise ValueError(
                "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
                f" {len(images_with_overflow)} and {len(overflow_to_sample_mapping)}"
            )

        return images_with_overflow

    def batch_decode(self, *args, **kwargs):
        
        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):
        
        return self.tokenizer.decode(*args, **kwargs)

    @property
    def model_input_names(self):
        return ["input_ids", "bbox", "token_type_ids", "attention_mask", "image"]

    @property
    def feature_extractor_class(self):
        warnings.warn(
            "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.",
            FutureWarning,
        )
        return self.image_processor_class

    @property
    def feature_extractor(self):
        warnings.warn(
            "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.",
            FutureWarning,
        )
        return self.image_processor