codingutf8 microsoft research and 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 layoutlmv2 model configuration from configurationutils import pretrainedconfig from utils import isdetectron2available logging logger logging getloggername layoutlmv2pretrainedconfigarchivemap layoutlmv2baseuncased https huggingface comicrosoftlayoutlmv2baseuncasedresolvemainconfig json layoutlmv2largeuncased https huggingface comicrosoftlayoutlmv2largeuncasedresolvemainconfig json see all layoutlmv2 models at https huggingface comodels filterlayoutlmv2 soft dependency if isdetectron2available import detectron2 class layoutlmv2configpretrainedconfig r this is the configuration class to store the configuration of a layoutlmv2model it is used to instantiate an layoutlmv2 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 layoutlmv2 microsoftlayoutlmv2baseuncasedhttps huggingface comicrosoftlayoutlmv2baseuncased architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args vocabsize int optional defaults to 30522 vocabulary size of the layoutlmv2 model defines the number of different tokens that can be represented by the inputsids passed when calling layoutlmv2model or tflayoutlmv2model hiddensize int optional defaults to 768 dimension of the encoder layers and the pooler layer numhiddenlayers int optional defaults to 12 number of hidden layers in the transformer encoder numattentionheads int optional defaults to 12 number of attention heads for each attention layer in the transformer encoder intermediatesize int optional defaults to 3072 dimension of the intermediate i e feedforward layer in the transformer encoder hiddenact str or function optional defaults to gelu the nonlinear activation function function or string in the encoder and pooler if string gelu relu selu and gelunew are supported hiddendropoutprob float optional defaults to 0 1 the dropout probabilitiy for all fully connected layers in the embeddings encoder and pooler attentionprobsdropoutprob float optional defaults to 0 1 the dropout ratio for the attention probabilities maxpositionembeddings int optional defaults to 512 the maximum sequence length that this model might ever be used with typically set this to something large just in case e g 512 or 1024 or 2048 typevocabsize int optional defaults to 2 the vocabulary size of the tokentypeids passed when calling layoutlmv2model or tflayoutlmv2model initializerrange float optional defaults to 0 02 the standard deviation of the truncatednormalinitializer for initializing all weight matrices layernormeps float optional defaults to 1e12 the epsilon used by the layer normalization layers max2dpositionembeddings int optional defaults to 1024 the maximum value that the 2d position embedding might ever be used with typically set this to something large just in case e g 1024 maxrelpos int optional defaults to 128 the maximum number of relative positions to be used in the selfattention mechanism relposbins int optional defaults to 32 the number of relative position bins to be used in the selfattention mechanism fastqkv bool optional defaults to true whether or not to use a single matrix for the queries keys values in the selfattention layers maxrel2dpos int optional defaults to 256 the maximum number of relative 2d positions in the selfattention mechanism rel2dposbins int optional defaults to 64 the number of 2d relative position bins in the selfattention mechanism imagefeaturepoolshape listint optional defaults to 7 7 256 the shape of the averagepooled feature map coordinatesize int optional defaults to 128 dimension of the coordinate embeddings shapesize int optional defaults to 128 dimension of the width and height embeddings hasrelativeattentionbias bool optional defaults to true whether or not to use a relative attention bias in the selfattention mechanism hasspatialattentionbias bool optional defaults to true whether or not to use a spatial attention bias in the selfattention mechanism hasvisualsegmentembedding bool optional defaults to false whether or not to add visual segment embeddings detectron2configargs dict optional dictionary containing the configuration arguments of the detectron2 visual backbone refer to this filehttps github commicrosoftunilmblobmasterlayoutlmftlayoutlmftmodelslayoutlmv2detectron2config py for details regarding default values example python from transformers import layoutlmv2config layoutlmv2model initializing a layoutlmv2 microsoftlayoutlmv2baseuncased style configuration configuration layoutlmv2config initializing a model with random weights from the microsoftlayoutlmv2baseuncased style configuration model layoutlmv2modelconfiguration accessing the model configuration configuration model config modeltype layoutlmv2 def init self vocabsize30522 hiddensize768 numhiddenlayers12 numattentionheads12 intermediatesize3072 hiddenactgelu hiddendropoutprob0 1 attentionprobsdropoutprob0 1 maxpositionembeddings512 typevocabsize2 initializerrange0 02 layernormeps1e12 padtokenid0 max2dpositionembeddings1024 maxrelpos128 relposbins32 fastqkvtrue maxrel2dpos256 rel2dposbins64 convertsyncbatchnormtrue imagefeaturepoolshape7 7 256 coordinatesize128 shapesize128 hasrelativeattentionbiastrue hasspatialattentionbiastrue hasvisualsegmentembeddingfalse detectron2configargsnone kwargs super init vocabsizevocabsize hiddensizehiddensize numhiddenlayersnumhiddenlayers numattentionheadsnumattentionheads intermediatesizeintermediatesize hiddenacthiddenact hiddendropoutprobhiddendropoutprob attentionprobsdropoutprobattentionprobsdropoutprob maxpositionembeddingsmaxpositionembeddings typevocabsizetypevocabsize initializerrangeinitializerrange layernormepslayernormeps padtokenidpadtokenid kwargs self max2dpositionembeddings max2dpositionembeddings self maxrelpos maxrelpos self relposbins relposbins self fastqkv fastqkv self maxrel2dpos maxrel2dpos self rel2dposbins rel2dposbins self convertsyncbatchnorm convertsyncbatchnorm self imagefeaturepoolshape imagefeaturepoolshape self coordinatesize coordinatesize self shapesize shapesize self hasrelativeattentionbias hasrelativeattentionbias self hasspatialattentionbias hasspatialattentionbias self hasvisualsegmentembedding hasvisualsegmentembedding self detectron2configargs detectron2configargs if detectron2configargs is not none else self getdefaultdetectron2config classmethod def getdefaultdetectron2configself return model maskon true model pixelstd 57 375 57 120 58 395 model backbone name buildresnetfpnbackbone model fpn infeatures res2 res3 res4 res5 model anchorgenerator sizes 32 64 128 256 512 model rpn infeatures p2 p3 p4 p5 p6 model rpn prenmstopktrain 2000 model rpn prenmstopktest 1000 model rpn postnmstopktrain 1000 model postnmstopktest 1000 model roiheads name standardroiheads model roiheads numclasses 5 model roiheads infeatures p2 p3 p4 p5 model roiboxhead name fastrcnnconvfchead model roiboxhead numfc 2 model roiboxhead poolerresolution 14 model roimaskhead name maskrcnnconvupsamplehead model roimaskhead numconv 4 model roimaskhead poolerresolution 7 model resnets depth 101 model resnets sizes 32 64 128 256 512 model resnets aspectratios 0 5 1 0 2 0 model resnets outfeatures res2 res3 res4 res5 model resnets numgroups 32 model resnets widthpergroup 8 model resnets stridein1x1 false def getdetectron2configself detectron2config detectron2 config getcfg for k v in self detectron2configargs items attributes k split toset detectron2config for attribute in attributes 1 toset getattrtoset attribute setattrtoset attributes1 v return detectron2config coding utf 8 microsoft research and 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 layoutlmv2 model configuration see all layoutlmv2 models at https huggingface co models filter layoutlmv2 soft dependency this is the configuration class to store the configuration of a layoutlmv2model it is used to instantiate an layoutlmv2 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 layoutlmv2 microsoft layoutlmv2 base uncased https huggingface co microsoft layoutlmv2 base uncased architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args vocab_size int optional defaults to 30522 vocabulary size of the layoutlmv2 model defines the number of different tokens that can be represented by the inputs_ids passed when calling layoutlmv2model or tflayoutlmv2model hidden_size int optional defaults to 768 dimension of the encoder layers and the pooler layer num_hidden_layers int optional defaults to 12 number of hidden layers in the transformer encoder num_attention_heads int optional defaults to 12 number of attention heads for each attention layer in the transformer encoder intermediate_size int optional defaults to 3072 dimension of the intermediate i e feed forward layer in the transformer encoder hidden_act str or function optional defaults to gelu the non linear activation function function or string in the encoder and pooler if string gelu relu selu and gelu_new are supported hidden_dropout_prob float optional defaults to 0 1 the dropout probabilitiy for all fully connected layers in the embeddings encoder and pooler attention_probs_dropout_prob float optional defaults to 0 1 the dropout ratio for the attention probabilities max_position_embeddings int optional defaults to 512 the maximum sequence length that this model might ever be used with typically set this to something large just in case e g 512 or 1024 or 2048 type_vocab_size int optional defaults to 2 the vocabulary size of the token_type_ids passed when calling layoutlmv2model or tflayoutlmv2model initializer_range float optional defaults to 0 02 the standard deviation of the truncated_normal_initializer for initializing all weight matrices layer_norm_eps float optional defaults to 1e 12 the epsilon used by the layer normalization layers max_2d_position_embeddings int optional defaults to 1024 the maximum value that the 2d position embedding might ever be used with typically set this to something large just in case e g 1024 max_rel_pos int optional defaults to 128 the maximum number of relative positions to be used in the self attention mechanism rel_pos_bins int optional defaults to 32 the number of relative position bins to be used in the self attention mechanism fast_qkv bool optional defaults to true whether or not to use a single matrix for the queries keys values in the self attention layers max_rel_2d_pos int optional defaults to 256 the maximum number of relative 2d positions in the self attention mechanism rel_2d_pos_bins int optional defaults to 64 the number of 2d relative position bins in the self attention mechanism image_feature_pool_shape list int optional defaults to 7 7 256 the shape of the average pooled feature map coordinate_size int optional defaults to 128 dimension of the coordinate embeddings shape_size int optional defaults to 128 dimension of the width and height embeddings has_relative_attention_bias bool optional defaults to true whether or not to use a relative attention bias in the self attention mechanism has_spatial_attention_bias bool optional defaults to true whether or not to use a spatial attention bias in the self attention mechanism has_visual_segment_embedding bool optional defaults to false whether or not to add visual segment embeddings detectron2_config_args dict optional dictionary containing the configuration arguments of the detectron2 visual backbone refer to this file https github com microsoft unilm blob master layoutlmft layoutlmft models layoutlmv2 detectron2_config py for details regarding default values example 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
from ...configuration_utils import PretrainedConfig
from ...utils import is_detectron2_available, logging


logger = logging.get_logger(__name__)

LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "layoutlmv2-base-uncased": "https://huggingface.co/microsoft/layoutlmv2-base-uncased/resolve/main/config.json",
    "layoutlmv2-large-uncased": "https://huggingface.co/microsoft/layoutlmv2-large-uncased/resolve/main/config.json",
    
}


if is_detectron2_available():
    import detectron2


class LayoutLMv2Config(PretrainedConfig):
    r

    model_type = "layoutlmv2"

    def __init__(
        self,
        vocab_size=30522,
        hidden_size=768,
        num_hidden_layers=12,
        num_attention_heads=12,
        intermediate_size=3072,
        hidden_act="gelu",
        hidden_dropout_prob=0.1,
        attention_probs_dropout_prob=0.1,
        max_position_embeddings=512,
        type_vocab_size=2,
        initializer_range=0.02,
        layer_norm_eps=1e-12,
        pad_token_id=0,
        max_2d_position_embeddings=1024,
        max_rel_pos=128,
        rel_pos_bins=32,
        fast_qkv=True,
        max_rel_2d_pos=256,
        rel_2d_pos_bins=64,
        convert_sync_batchnorm=True,
        image_feature_pool_shape=[7, 7, 256],
        coordinate_size=128,
        shape_size=128,
        has_relative_attention_bias=True,
        has_spatial_attention_bias=True,
        has_visual_segment_embedding=False,
        detectron2_config_args=None,
        **kwargs,
    ):
        super().__init__(
            vocab_size=vocab_size,
            hidden_size=hidden_size,
            num_hidden_layers=num_hidden_layers,
            num_attention_heads=num_attention_heads,
            intermediate_size=intermediate_size,
            hidden_act=hidden_act,
            hidden_dropout_prob=hidden_dropout_prob,
            attention_probs_dropout_prob=attention_probs_dropout_prob,
            max_position_embeddings=max_position_embeddings,
            type_vocab_size=type_vocab_size,
            initializer_range=initializer_range,
            layer_norm_eps=layer_norm_eps,
            pad_token_id=pad_token_id,
            **kwargs,
        )
        self.max_2d_position_embeddings = max_2d_position_embeddings
        self.max_rel_pos = max_rel_pos
        self.rel_pos_bins = rel_pos_bins
        self.fast_qkv = fast_qkv
        self.max_rel_2d_pos = max_rel_2d_pos
        self.rel_2d_pos_bins = rel_2d_pos_bins
        self.convert_sync_batchnorm = convert_sync_batchnorm
        self.image_feature_pool_shape = image_feature_pool_shape
        self.coordinate_size = coordinate_size
        self.shape_size = shape_size
        self.has_relative_attention_bias = has_relative_attention_bias
        self.has_spatial_attention_bias = has_spatial_attention_bias
        self.has_visual_segment_embedding = has_visual_segment_embedding
        self.detectron2_config_args = (
            detectron2_config_args if detectron2_config_args is not None else self.get_default_detectron2_config()
        )

    @classmethod
    def get_default_detectron2_config(self):
        return {
            "MODEL.MASK_ON": True,
            "MODEL.PIXEL_STD": [57.375, 57.120, 58.395],
            "MODEL.BACKBONE.NAME": "build_resnet_fpn_backbone",
            "MODEL.FPN.IN_FEATURES": ["res2", "res3", "res4", "res5"],
            "MODEL.ANCHOR_GENERATOR.SIZES": [[32], [64], [128], [256], [512]],
            "MODEL.RPN.IN_FEATURES": ["p2", "p3", "p4", "p5", "p6"],
            "MODEL.RPN.PRE_NMS_TOPK_TRAIN": 2000,
            "MODEL.RPN.PRE_NMS_TOPK_TEST": 1000,
            "MODEL.RPN.POST_NMS_TOPK_TRAIN": 1000,
            "MODEL.POST_NMS_TOPK_TEST": 1000,
            "MODEL.ROI_HEADS.NAME": "StandardROIHeads",
            "MODEL.ROI_HEADS.NUM_CLASSES": 5,
            "MODEL.ROI_HEADS.IN_FEATURES": ["p2", "p3", "p4", "p5"],
            "MODEL.ROI_BOX_HEAD.NAME": "FastRCNNConvFCHead",
            "MODEL.ROI_BOX_HEAD.NUM_FC": 2,
            "MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION": 14,
            "MODEL.ROI_MASK_HEAD.NAME": "MaskRCNNConvUpsampleHead",
            "MODEL.ROI_MASK_HEAD.NUM_CONV": 4,
            "MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION": 7,
            "MODEL.RESNETS.DEPTH": 101,
            "MODEL.RESNETS.SIZES": [[32], [64], [128], [256], [512]],
            "MODEL.RESNETS.ASPECT_RATIOS": [[0.5, 1.0, 2.0]],
            "MODEL.RESNETS.OUT_FEATURES": ["res2", "res3", "res4", "res5"],
            "MODEL.RESNETS.NUM_GROUPS": 32,
            "MODEL.RESNETS.WIDTH_PER_GROUP": 8,
            "MODEL.RESNETS.STRIDE_IN_1X1": False,
        }

    def get_detectron2_config(self):
        detectron2_config = detectron2.config.get_cfg()
        for k, v in self.detectron2_config_args.items():
            attributes = k.split(".")
            to_set = detectron2_config
            for attribute in attributes[:-1]:
                to_set = getattr(to_set, attribute)
            setattr(to_set, attributes[-1], v)

        return detectron2_config