layoutlmv2
mindnlp.transformers.models.layoutlmv2.configuration_layoutlmv2
¶
LayoutLMv2 model configuration
mindnlp.transformers.models.layoutlmv2.configuration_layoutlmv2.LayoutLMv2Config
¶
Bases: PretrainedConfig
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 architecture.
Configuration objects inherit from [PretrainedConfig
] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig
] for more information.
PARAMETER | DESCRIPTION |
---|---|
vocab_size |
Vocabulary size of the LayoutLMv2 model. Defines the number of different tokens that can be represented by
the
TYPE:
|
hidden_size |
Dimension of the encoder layers and the pooler layer.
TYPE:
|
num_hidden_layers |
Number of hidden layers in the Transformer encoder.
TYPE:
|
num_attention_heads |
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
intermediate_size |
Dimension of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
TYPE:
|
hidden_act |
The non-linear activation function (function or string) in the encoder and pooler. If string,
TYPE:
|
hidden_dropout_prob |
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
attention_probs_dropout_prob |
The dropout ratio for the attention probabilities.
TYPE:
|
max_position_embeddings |
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:
|
type_vocab_size |
The vocabulary size of the
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
layer_norm_eps |
The epsilon used by the layer normalization layers.
TYPE:
|
max_2d_position_embeddings |
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).
TYPE:
|
max_rel_pos |
The maximum number of relative positions to be used in the self-attention mechanism.
TYPE:
|
rel_pos_bins |
The number of relative position bins to be used in the self-attention mechanism.
TYPE:
|
fast_qkv |
Whether or not to use a single matrix for the queries, keys, values in the self-attention layers.
TYPE:
|
max_rel_2d_pos |
The maximum number of relative 2D positions in the self-attention mechanism.
TYPE:
|
rel_2d_pos_bins |
The number of 2D relative position bins in the self-attention mechanism.
TYPE:
|
image_feature_pool_shape |
The shape of the average-pooled feature map.
TYPE:
|
coordinate_size |
Dimension of the coordinate embeddings.
TYPE:
|
shape_size |
Dimension of the width and height embeddings.
TYPE:
|
has_relative_attention_bias |
Whether or not to use a relative attention bias in the self-attention mechanism.
TYPE:
|
has_spatial_attention_bias |
Whether or not to use a spatial attention bias in the self-attention mechanism.
TYPE:
|
has_visual_segment_embedding |
Whether or not to add visual segment embeddings.
TYPE:
|
detectron2_config_args |
Dictionary containing the configuration arguments of the Detectron2 visual backbone. Refer to this file for details regarding default values.
TYPE:
|
Example
>>> 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
Source code in mindnlp/transformers/models/layoutlmv2/configuration_layoutlmv2.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
mindnlp.transformers.models.layoutlmv2.configuration_layoutlmv2.LayoutLMv2Config.__init__(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, 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, use_visual_backbone=True, detectron2_config_args=None, **kwargs)
¶
Initializes a LayoutLMv2Config object with the specified parameters.
PARAMETER | DESCRIPTION |
---|---|
vocab_size |
The size of the vocabulary.
TYPE:
|
hidden_size |
The hidden size for the model.
TYPE:
|
num_hidden_layers |
The number of hidden layers in the model.
TYPE:
|
num_attention_heads |
The number of attention heads in the model.
TYPE:
|
intermediate_size |
The size of the intermediate layer in the model.
TYPE:
|
hidden_act |
The activation function for the hidden layers.
TYPE:
|
hidden_dropout_prob |
The dropout probability for the hidden layers.
TYPE:
|
attention_probs_dropout_prob |
The dropout probability for the attention probabilities.
TYPE:
|
max_position_embeddings |
The maximum position embeddings allowed.
TYPE:
|
type_vocab_size |
The size of the type vocabulary.
TYPE:
|
initializer_range |
The range for parameter initialization.
TYPE:
|
layer_norm_eps |
The epsilon value for layer normalization.
TYPE:
|
pad_token_id |
The token ID for padding.
TYPE:
|
max_2d_position_embeddings |
The maximum 2D position embeddings allowed.
TYPE:
|
max_rel_pos |
The maximum relative position.
TYPE:
|
rel_pos_bins |
The number of relative position bins.
TYPE:
|
fast_qkv |
Flag to enable fast query, key, value computation.
TYPE:
|
max_rel_2d_pos |
The maximum relative 2D position.
TYPE:
|
rel_2d_pos_bins |
The number of relative 2D position bins.
TYPE:
|
image_feature_pool_shape |
The shape of the image feature pool.
TYPE:
|
coordinate_size |
The size of coordinates.
TYPE:
|
shape_size |
The size of shapes.
TYPE:
|
has_relative_attention_bias |
Flag indicating if relative attention bias is used.
TYPE:
|
has_spatial_attention_bias |
Flag indicating if spatial attention bias is used.
TYPE:
|
has_visual_segment_embedding |
Flag indicating if visual segment embedding is used.
TYPE:
|
use_visual_backbone |
Flag indicating if visual backbone is used.
TYPE:
|
detectron2_config_args |
Additional arguments for the Detectron2 configuration.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/layoutlmv2/configuration_layoutlmv2.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
mindnlp.transformers.models.layoutlmv2.configuration_layoutlmv2.LayoutLMv2Config.get_default_detectron2_config()
classmethod
¶
This method returns a dictionary containing the default configuration for the Detectron2 model. The configuration includes various settings related to the model's architecture, backbone, region of interest (ROI) heads, and other parameters.
PARAMETER | DESCRIPTION |
---|---|
cls |
The class object.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the default configuration for the Detectron2 model. |
Source code in mindnlp/transformers/models/layoutlmv2/configuration_layoutlmv2.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
mindnlp.transformers.models.layoutlmv2.configuration_layoutlmv2.LayoutLMv2Config.get_detectron2_config()
¶
This method generates a Detectron2 configuration for the LayoutLMv2 model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the LayoutLMv2Config class.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/layoutlmv2/configuration_layoutlmv2.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
mindnlp.transformers.models.layoutlmv2.image_processing_layoutlmv2
¶
Image processor class for LayoutLMv2.
mindnlp.transformers.models.layoutlmv2.image_processing_layoutlmv2.LayoutLMv2ImageProcessor
¶
Bases: BaseImageProcessor
Constructs a LayoutLMv2 image processor.
PARAMETER | DESCRIPTION |
---|---|
do_resize |
Whether to resize the image's (height, width) dimensions to
TYPE:
|
size |
224, "width": 224}
TYPE:
|
resample |
Resampling filter to use if resizing the image. Can be overridden by the
TYPE:
|
apply_ocr |
Whether to apply the Tesseract OCR engine to get words + normalized bounding boxes. Can be overridden by
TYPE:
|
ocr_lang |
The language, specified by its ISO code, to be used by the Tesseract OCR engine. By default, English is
used. Can be overridden by
TYPE:
|
tesseract_config |
Any additional custom configuration flags that are forwarded to the
TYPE:
|
Source code in mindnlp/transformers/models/layoutlmv2/image_processing_layoutlmv2.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
mindnlp.transformers.models.layoutlmv2.image_processing_layoutlmv2.LayoutLMv2ImageProcessor.__init__(do_resize=True, size=None, resample=PILImageResampling.BILINEAR, apply_ocr=True, ocr_lang=None, tesseract_config='', **kwargs)
¶
Initializes a LayoutLMv2ImageProcessor object.
PARAMETER | DESCRIPTION |
---|---|
self |
The LayoutLMv2ImageProcessor instance.
|
do_resize |
Indicates whether to perform image resizing. Defaults to True.
TYPE:
|
size |
A dictionary specifying the height and width for resizing the image. Defaults to {'height': 224, 'width': 224}.
TYPE:
|
resample |
The resampling filter to use when resizing the image. Defaults to PILImageResampling.BILINEAR.
TYPE:
|
apply_ocr |
Indicates whether optical character recognition (OCR) should be applied. Defaults to True.
TYPE:
|
ocr_lang |
The language for OCR. If None, the default language is used. Defaults to None.
TYPE:
|
tesseract_config |
Configuration options for the Tesseract OCR engine. Defaults to an empty string.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in mindnlp/transformers/models/layoutlmv2/image_processing_layoutlmv2.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
mindnlp.transformers.models.layoutlmv2.image_processing_layoutlmv2.LayoutLMv2ImageProcessor.preprocess(images, do_resize=None, size=None, resample=None, apply_ocr=None, ocr_lang=None, tesseract_config=None, return_tensors=None, data_format=ChannelDimension.FIRST, input_data_format=None, **kwargs)
¶
Preprocess an image or batch of images.
PARAMETER | DESCRIPTION |
---|---|
images |
Image to preprocess.
TYPE:
|
do_resize |
Whether to resize the image.
TYPE:
|
size |
Desired size of the output image after resizing.
TYPE:
|
resample |
Resampling filter to use if resizing the image. This can be one of the enum
TYPE:
|
apply_ocr |
Whether to apply the Tesseract OCR engine to get words + normalized bounding boxes.
TYPE:
|
ocr_lang |
The language, specified by its ISO code, to be used by the Tesseract OCR engine. By default, English is used.
TYPE:
|
tesseract_config |
Any additional custom configuration flags that are forwarded to the
TYPE:
|
return_tensors |
The type of tensors to return. Can be one of:
TYPE:
|
data_format |
The channel dimension format for the output image. Can be one of:
TYPE:
|
Source code in mindnlp/transformers/models/layoutlmv2/image_processing_layoutlmv2.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
mindnlp.transformers.models.layoutlmv2.image_processing_layoutlmv2.LayoutLMv2ImageProcessor.resize(image, size, resample=PILImageResampling.BILINEAR, data_format=None, input_data_format=None, **kwargs)
¶
Resize an image to (size["height"], size["width"])
.
PARAMETER | DESCRIPTION |
---|---|
image |
Image to resize.
TYPE:
|
size |
Dictionary in the format
TYPE:
|
resample |
TYPE:
|
data_format |
The channel dimension format for the output image. If unset, the channel dimension format of the input image is used. Can be one of:
TYPE:
|
input_data_format |
The channel dimension format for the input image. If unset, the channel dimension format is inferred from the input image. Can be one of:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in mindnlp/transformers/models/layoutlmv2/image_processing_layoutlmv2.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
mindnlp.transformers.models.layoutlmv2.image_processing_layoutlmv2.apply_tesseract(image, lang, tesseract_config=None, input_data_format=None)
¶
Applies Tesseract OCR on a document image, and returns recognized words + normalized bounding boxes.
Source code in mindnlp/transformers/models/layoutlmv2/image_processing_layoutlmv2.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
mindnlp.transformers.models.layoutlmv2.image_processing_layoutlmv2.normalize_box(box, width, height)
¶
PARAMETER | DESCRIPTION |
---|---|
box |
|
width |
|
height |
|
Source code in mindnlp/transformers/models/layoutlmv2/image_processing_layoutlmv2.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2
¶
Mindnlp LayoutLMv2 model.
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Attention
¶
Bases: Module
LayoutLMv2Attention is the attention layer for LayoutLMv2. It is based on the implementation of
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Attention.__init__(config)
¶
Initialize the LayoutLMv2Attention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the LayoutLMv2Attention class.
TYPE:
|
config |
Represents the configuration settings for the LayoutLMv2Attention instance.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Attention.forward(hidden_states, attention_mask=None, head_mask=None, output_attentions=False, rel_pos=None, rel_2d_pos=None)
¶
This method 'forward' is defined in the class 'LayoutLMv2Attention' and is responsible for forwarding the attention mechanism in the LayoutLMv2 model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the LayoutLMv2Attention class.
TYPE:
|
hidden_states |
The input hidden states to the attention mechanism.
TYPE:
|
attention_mask |
Mask to prevent attention to certain positions. Default is None.
TYPE:
|
head_mask |
Mask to hide certain heads of the attention mechanism. Default is None.
TYPE:
|
output_attentions |
Whether to output attentions weights. Default is False.
TYPE:
|
rel_pos |
Relative position encoding. Default is None.
TYPE:
|
rel_2d_pos |
2D relative position encoding. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple containing the attention output and additional outputs from the attention mechanism. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Embeddings
¶
Bases: Module
Construct the embeddings from word, position and token_type embeddings.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Embeddings.__init__(config)
¶
Initializes the LayoutLMv2Embeddings class with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the LayoutLMv2Embeddings class.
|
config |
An object containing configuration parameters for the embeddings.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Encoder
¶
Bases: Module
LayoutLMv2Encoder is a stack of LayoutLMv2Layer. It is based on the implementation of BertEncoder.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Encoder.__init__(config)
¶
Initializes a LayoutLMv2Encoder object.
PARAMETER | DESCRIPTION |
---|---|
config |
The configuration object containing the parameters for the LayoutLMv2Encoder. It is used to initialize various attributes of the LayoutLMv2Encoder.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Encoder.forward(hidden_states, attention_mask=None, head_mask=None, output_attentions=False, output_hidden_states=False, return_dict=True, bbox=None, position_ids=None)
¶
This method forwards the LayoutLMv2Encoder.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class LayoutLMv2Encoder.
|
hidden_states |
The input hidden states to the encoder.
TYPE:
|
attention_mask |
Mask to avoid performing attention on padding token indices.
TYPE:
|
head_mask |
Mask for attention heads. Defaults to None.
TYPE:
|
output_attentions |
Whether to output attentions. Defaults to False.
TYPE:
|
output_hidden_states |
Whether to output hidden states. Defaults to False.
TYPE:
|
return_dict |
Whether to return the output as a dictionary. Defaults to True.
TYPE:
|
bbox |
Bounding box coordinates for spatial attention bias. Defaults to None.
TYPE:
|
position_ids |
Position IDs for relative positional embeddings. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input parameters are not in the expected format. |
RuntimeError
|
If an error occurs during the execution of the method. |
IndexError
|
If there is an issue with accessing elements in the head_mask list. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForQuestionAnswering
¶
Bases: LayoutLMv2PreTrainedModel
LayoutLMv2ForQuestionAnswering is a LayoutLMv2 model with a question answering head. It is based on the implementation of LayoutLMv2ForQuestionAnswering.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForQuestionAnswering.__init__(config, has_visual_segment_embedding=True)
¶
Initialize the LayoutLMv2ForQuestionAnswering class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance of the LayoutLMv2ForQuestionAnswering class. |
config |
The configuration object for the LayoutLMv2 model.
TYPE:
|
has_visual_segment_embedding |
A boolean flag indicating whether visual segment embedding is enabled. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForQuestionAnswering.forward(input_ids=None, bbox=None, image=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, start_positions=None, end_positions=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
start_positions |
Labels for position (index) of the start of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (
TYPE:
|
end_positions |
Labels for position (index) of the end of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, QuestionAnsweringModelOutput]
|
Union[Tuple, QuestionAnsweringModelOutput] |
Example
In this example below, we give the LayoutLMv2 model an image (of texts) and ask it a question. It will give us a prediction of what it thinks the answer is (the span of the answer within the texts parsed from the image).
>>> from transformers import AutoProcessor, LayoutLMv2ForQuestionAnswering, set_seed
>>> import torch
>>> from PIL import Image
>>> from datasets import load_dataset
...
>>> set_seed(88)
>>> 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_path = dataset["test"][0]["file"]
>>> image = Image.open(image_path).convert("RGB")
>>> 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
(154, 287)
>>> 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 very good without further fine-tuning
'council mem - bers conducted by trrf treasurer philip g. kuehn to get answers which the public ...
>>> 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
(154, 287)
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForQuestionAnswering.get_input_embeddings()
¶
Method to retrieve the input embeddings from LayoutLMv2 model for question answering.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the LayoutLMv2ForQuestionAnswering class. This parameter represents the current instance of the LayoutLMv2ForQuestionAnswering class where the method is called. It is used to access the model's embeddings to retrieve the input embeddings. |
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value. It simply returns the word embeddings from the LayoutLMv2 model. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForSequenceClassification
¶
Bases: LayoutLMv2PreTrainedModel
LayoutLMv2ForSequenceClassification is a LayoutLMv2 model with a sequence classification head on top (a linear layer on top of the pooled output) It is based on the implementation of LayoutLMv2ForSequenceClassification.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForSequenceClassification.__init__(config)
¶
Initializes a new instance of the LayoutLMv2ForSequenceClassification class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
An instance of the LayoutLMv2Config class containing the configuration parameters for the model.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForSequenceClassification.forward(input_ids=None, bbox=None, image=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the sequence classification/regression loss. Indices should be in
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, SequenceClassifierOutput]
|
Union[Tuple, SequenceClassifierOutput] |
Example
>>> from transformers import AutoProcessor, LayoutLMv2ForSequenceClassification, set_seed
>>> from PIL import Image
>>> import torch
>>> from datasets import load_dataset
...
>>> set_seed(88)
...
>>> dataset = load_dataset("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
... )
...
>>> encoding = processor(image, return_tensors="pt")
>>> sequence_label = torch.tensor([data["label"]])
...
>>> outputs = model(**encoding, labels=sequence_label)
...
>>> loss, logits = outputs.loss, outputs.logits
>>> predicted_idx = logits.argmax(axis=-1).item()
>>> predicted_answer = dataset.info.features["label"].names[4]
>>> predicted_idx, predicted_answer
(4, 'advertisement')
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForSequenceClassification.get_input_embeddings()
¶
Method to retrieve the input embeddings from the LayoutLMv2 model for sequence classification.
PARAMETER | DESCRIPTION |
---|---|
self |
LayoutLMv2ForSequenceClassification object. Represents the instance of the LayoutLMv2ForSequenceClassification class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns None as it simply retrieves the input embeddings without any additional processing. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForTokenClassification
¶
Bases: LayoutLMv2PreTrainedModel
LayoutLMv2ForTokenClassification is a LayoutLMv2 model with a token classification head. It is based on the implementation of LayoutLMv2ForTokenClassification.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForTokenClassification.__init__(config)
¶
Initializes a LayoutLMv2ForTokenClassification instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the LayoutLMv2ForTokenClassification class. |
config |
An object containing the configuration settings for the LayoutLMv2 model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the 'config' parameter is not an instance of LayoutLMv2Config. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForTokenClassification.forward(input_ids=None, bbox=None, image=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the token classification loss. Indices should be in
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, TokenClassifierOutput]
|
Union[Tuple, TokenClassifierOutput] |
Example
>>> from transformers import AutoProcessor, LayoutLMv2ForTokenClassification, set_seed
>>> from PIL import Image
>>> from datasets import load_dataset
...
>>> set_seed(88)
...
>>> 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]
['B-ANSWER', 'B-HEADER', 'B-HEADER', 'B-HEADER', 'B-HEADER']
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForTokenClassification.get_input_embeddings()
¶
Returns the input embeddings for LayoutLMv2ForTokenClassification.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the LayoutLMv2ForTokenClassification class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
The method returns the input embeddings for the LayoutLMv2ForTokenClassification. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Intermediate
¶
Bases: Module
LayoutLMv2Intermediate is a simple feedforward network. It is based on the implementation of BertIntermediate.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Intermediate.__init__(config)
¶
Initialize the LayoutLMv2Intermediate class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the class.
TYPE:
|
config |
An object containing configuration parameters for the intermediate layer. It must have the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not provided. |
ValueError
|
If the config parameter does not contain the required attributes. |
KeyError
|
If the hidden activation function specified in the config parameter is not found in the ACT2FN dictionary. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Intermediate.forward(hidden_states)
¶
Method 'forward' in the class 'LayoutLMv2Intermediate'.
PARAMETER | DESCRIPTION |
---|---|
self |
LayoutLMv2Intermediate object. Represents the instance of the LayoutLMv2Intermediate class.
|
hidden_states |
mindspore.Tensor. Input tensor containing hidden states that need to be processed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor. Processed hidden states returned after passing through the dense layer and intermediate activation function. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Layer
¶
Bases: Module
LayoutLMv2Layer is made up of self-attention and feedforward network. It is based on the implementation of BertLayer.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Layer.__init__(config)
¶
Initialize a LayoutLMv2Layer.
PARAMETER | DESCRIPTION |
---|---|
self |
Instance of the LayoutLMv2Layer class.
|
config |
Configuration object containing parameters for the layer initialization.
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of the expected type. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Layer.feed_forward_chunk(attention_output)
¶
Performs a feed forward operation on the given attention output in the LayoutLMv2Layer.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the LayoutLMv2Layer class.
TYPE:
|
attention_output |
The attention output tensor to be processed. It should have shape (batch_size, sequence_length, hidden_size).
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method modifies the internal state of the LayoutLMv2Layer instance. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Layer.forward(hidden_states, attention_mask=None, head_mask=None, output_attentions=False, rel_pos=None, rel_2d_pos=None)
¶
Constructs a LayoutLMv2Layer by applying the attention mechanism and feed-forward neural network to the input hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the LayoutLMv2Layer class.
|
hidden_states |
The input hidden states of shape
TYPE:
|
attention_mask |
The attention mask tensor of shape
TYPE:
|
head_mask |
The tensor to mask selected heads of the multi-head attention module. Defaults to None.
TYPE:
|
output_attentions |
Whether to output the attention weights. Defaults to False.
TYPE:
|
rel_pos |
The tensor of relative position encoding of shape
TYPE:
|
rel_2d_pos |
The tensor of 2D relative position encoding of shape
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
outputs
|
A tuple of the following tensors:
TYPE:
|
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Model
¶
Bases: LayoutLMv2PreTrainedModel
LayoutLMv2Model is a LayoutLMv2 model with a visual backbone. It is based on the implementation of LayoutLMv2Model.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Model.__init__(config)
¶
Initializes an instance of the LayoutLMv2Model class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the LayoutLMv2Model class.
|
config |
A configuration object containing various settings and hyperparameters for the model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the provided configuration is missing required keys or has invalid values. |
TypeError
|
If the configuration object is not of the expected type. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Model.forward(input_ids=None, bbox=None, image=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
Return
Union[Tuple, BaseModelOutputWithPooling]
Example
>>> from transformers import AutoProcessor, LayoutLMv2Model, set_seed
>>> from PIL import Image
>>> import torch
>>> from datasets import load_dataset
...
>>> set_seed(88)
...
>>> 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_path = dataset["test"][0]["file"]
>>> image = Image.open(image_path).convert("RGB")
...
>>> encoding = processor(image, return_tensors="pt")
...
>>> outputs = model(**encoding)
>>> last_hidden_states = outputs.last_hidden_state
...
>>> last_hidden_states.shape
ops.Size([1, 342, 768])
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Model.get_input_embeddings()
¶
This method returns the input embeddings of the LayoutLMv2Model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the LayoutLMv2Model class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns the input embeddings of the LayoutLMv2Model. The input embeddings are of type 'None'. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Model.set_input_embeddings(value)
¶
Sets the input embeddings for the LayoutLMv2Model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the LayoutLMv2Model class.
TYPE:
|
value |
The input embeddings to be set. It should be a tensor or any object that can be assigned to the word_embeddings attribute of the embeddings object.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Output
¶
Bases: Module
LayoutLMv2Output is the output layer for LayoutLMv2Intermediate. It is based on the implementation of BertOutput.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Output.__init__(config)
¶
Initializes a new instance of the LayoutLMv2Output class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the LayoutLMv2Output class.
|
config |
An object containing configuration parameters for the LayoutLMv2Output model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of the expected type. |
ValueError
|
If the config parameters do not meet the required constraints. |
RuntimeError
|
If an error occurs during the initialization process. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Output.forward(hidden_states, input_tensor)
¶
Constructs the LayoutLMv2Output for the given hidden states and input tensor.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the LayoutLMv2Output class.
TYPE:
|
hidden_states |
A tensor representing the hidden states. This tensor is expected to have a shape of (batch_size, sequence_length, hidden_size).
TYPE:
|
input_tensor |
A tensor representing the input. This tensor is expected to have the same shape as the hidden states.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: A tensor representing the forwarded LayoutLMv2Output. This tensor has the same shape as the hidden states. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Pooler
¶
Bases: Module
LayoutLMv2Pooler is a simple feedforward network. It is based on the implementation of BertPooler.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Pooler.__init__(config)
¶
Initializes a new instance of the LayoutLMv2Pooler class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the LayoutLMv2Pooler class.
TYPE:
|
config |
The configuration object specifying the settings for the LayoutLMv2Pooler.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Pooler.forward(hidden_states)
¶
Constructs the pooled output tensor for the LayoutLMv2Pooler class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the LayoutLMv2Pooler class.
|
hidden_states |
A tensor of shape (batch_size, sequence_length, hidden_size) representing the hidden states of the input sequence.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
torch.Tensor: A tensor of shape (batch_size, hidden_size) representing the pooled output. |
This method takes the hidden states of the input sequence and applies pooling to obtain a pooled output tensor. It first selects the first token tensor from the hidden states tensor using slicing, and then passes it through a dense layer. The resulting tensor is then activated using the specified activation function. Finally, the pooled output tensor is returned.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2PreTrainedModel
¶
Bases: PreTrainedModel
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2SelfAttention
¶
Bases: Module
LayoutLMv2SelfAttention is the self-attention layer for LayoutLMv2. It is based on the implementation of
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2SelfAttention.__init__(config)
¶
Initializes the LayoutLMv2SelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the LayoutLMv2SelfAttention class.
TYPE:
|
config |
The configuration object that contains the settings for the self-attention layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the hidden size is not a multiple of the number of attention heads and the configuration object does not have an 'embedding_size' attribute. |
This method initializes the LayoutLMv2SelfAttention class by setting the necessary attributes and layers. It checks if the hidden size is divisible by the number of attention heads and raises a ValueError if not. The method also determines if the fast_qkv (fast query, key, value) method should be used based on the configuration. If fast_qkv is enabled, it creates a dense layer for the query, key, and value (qkv_linear), along with biases (q_bias and v_bias). Otherwise, it creates separate dense layers for query, key, and value. Finally, it sets the dropout layer based on the configuration's attention_probs_dropout_prob value.
Source code in mindnlp/transformers/models/layoutlmv2/modeling_layoutlmv2.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|