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
¶
MindSpore LayoutLMv2 model.
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
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 | |
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForQuestionAnswering
¶
Bases: LayoutLMv2PreTrainedModel
Source code in mindnlp\transformers\models\layoutlmv2\modeling_layoutlmv2.py
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 | |
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)
¶
start_positions (mindspore.Tensor of shape (batch_size,), optional):
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 (sequence_length). Position outside of the sequence
are not taken into account for computing the loss.
end_positions (mindspore.Tensor of shape (batch_size,), optional):
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 (sequence_length). Position outside of the sequence
are not taken into account for computing the loss.
Returns:
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(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", trust_remote_code=True)
>>> image_path = dataset["test"][0]["file"]
>>> image = Image.open(image_path).convert("RGB")
>>> question = "When is coffee break?"
>>> encoding = processor(image, question, return_tensors="ms")
>>> 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
(30, 191)
>>> 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
>>> target_start_index = mindspore.tensor([7])
>>> target_end_index = mindspore.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
(30, 191)
Source code in mindnlp\transformers\models\layoutlmv2\modeling_layoutlmv2.py
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 | |
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForSequenceClassification
¶
Bases: LayoutLMv2PreTrainedModel
Source code in mindnlp\transformers\models\layoutlmv2\modeling_layoutlmv2.py
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 879 880 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 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 | |
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)
¶
labels (mindspore.Tensor of shape (batch_size,), optional):
Labels for computing the sequence classification/regression loss. Indices should be in [0, ...,
config.num_labels - 1]. If config.num_labels == 1 a regression loss is computed (Mean-Square loss), If
config.num_labels > 1 a classification loss is computed (Cross-Entropy).
Returns:
Example:
>>> 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, trust_remote_code=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="ms")
>>> sequence_label = mindspore.tensor([data["label"]])
>>> outputs = model(**encoding, labels=sequence_label)
>>> loss, logits = outputs.loss, outputs.logits
>>> predicted_idx = logits.argmax(dim=-1).item()
>>> predicted_answer = dataset.info.features["label"].names[4]
>>> predicted_idx, predicted_answer # results are not good without further fine-tuning
(7, 'advertisement')
Source code in mindnlp\transformers\models\layoutlmv2\modeling_layoutlmv2.py
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 879 880 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 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 | |
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2ForTokenClassification
¶
Bases: LayoutLMv2PreTrainedModel
Source code in mindnlp\transformers\models\layoutlmv2\modeling_layoutlmv2.py
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 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 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 | |
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)
¶
labels (mindspore.Tensor of shape (batch_size, sequence_length), optional):
Labels for computing the token classification loss. Indices should be in [0, ..., config.num_labels - 1].
Returns:
Example:
>>> 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", trust_remote_code=True)
>>> 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="ms",
... )
>>> 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']
Source code in mindnlp\transformers\models\layoutlmv2\modeling_layoutlmv2.py
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 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 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 | |
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.LayoutLMv2Model
¶
Bases: LayoutLMv2PreTrainedModel
Source code in mindnlp\transformers\models\layoutlmv2\modeling_layoutlmv2.py
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 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 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 | |
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)
¶
Examples:
>>> 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", trust_remote_code=True)
>>> image_path = dataset["test"][0]["file"]
>>> image = Image.open(image_path).convert("RGB")
>>> encoding = processor(image, return_tensors="ms")
>>> outputs = model(**encoding)
>>> last_hidden_states = outputs.last_hidden_state
>>> last_hidden_states.shape
[1, 342, 768])
Source code in mindnlp\transformers\models\layoutlmv2\modeling_layoutlmv2.py
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 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 | |
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
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
mindnlp.transformers.models.layoutlmv2.modeling_layoutlmv2.relative_position_bucket(relative_position, bidirectional=True, num_buckets=32, max_distance=128)
¶
Adapted from Mesh Tensorflow: https://github.com/tensorflow/mesh/blob/0cb87fe07da627bf0b7e60475d59f95ed6b5be3d/mesh_tensorflow/transformer/transformer_layers.py#L593 Translate relative position to a bucket number for relative attention. The relative position is defined as memory_position - query_position, i.e. the distance in tokens from the attending position to the attended-to position. If bidirectional=False, then positive relative positions are invalid. We use smaller buckets for small absolute relative_position and larger buckets for larger absolute relative_positions. All relative positions
=max_distance map to the same bucket. All relative positions <=-max_distance map to the same bucket. This should allow for more graceful generalization to longer sequences than the model has been trained on.
| PARAMETER | DESCRIPTION |
|---|---|
relative_position
|
an int32 Tensor
|
bidirectional
|
a boolean - whether the attention is bidirectional
DEFAULT:
|
num_buckets
|
an integer
DEFAULT:
|
max_distance
|
an integer
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
a Tensor with the same shape as relative_position, containing int32 values in the range [0, num_buckets) |
Source code in mindnlp\transformers\models\layoutlmv2\modeling_layoutlmv2.py
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.processing_layoutlmv2
¶
Processor class for LayoutLMv2.
mindnlp.transformers.models.layoutlmv2.processing_layoutlmv2.LayoutLMv2Processor
¶
Bases: ProcessorMixin
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).
| PARAMETER | DESCRIPTION |
|---|---|
image_processor
|
An instance of [
TYPE:
|
tokenizer
|
An instance of [
TYPE:
|
Source code in mindnlp\transformers\models\layoutlmv2\processing_layoutlmv2.py
28 29 30 31 32 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 | |
mindnlp.transformers.models.layoutlmv2.processing_layoutlmv2.LayoutLMv2Processor.feature_extractor
property
¶
Deprecated property, will be removed in v5. Use image_processor instead.
mindnlp.transformers.models.layoutlmv2.processing_layoutlmv2.LayoutLMv2Processor.feature_extractor_class
property
¶
Deprecated property, will be removed in v5. Use image_processor_class instead.
mindnlp.transformers.models.layoutlmv2.processing_layoutlmv2.LayoutLMv2Processor.model_input_names
property
¶
This method returns a list of input names used by the LayoutLMv2Processor.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the LayoutLMv2Processor.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list
|
A list containing the input names, including 'input_ids', 'bbox', 'token_type_ids', 'attention_mask', and 'image'. |
mindnlp.transformers.models.layoutlmv2.processing_layoutlmv2.LayoutLMv2Processor.__call__(images, text=None, text_pair=None, boxes=None, word_labels=None, add_special_tokens=True, padding=False, truncation=False, max_length=None, stride=0, pad_to_multiple_of=None, return_token_type_ids=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_length=False, verbose=True, return_tensors=None, **kwargs)
¶
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.
Source code in mindnlp\transformers\models\layoutlmv2\processing_layoutlmv2.py
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 | |
mindnlp.transformers.models.layoutlmv2.processing_layoutlmv2.LayoutLMv2Processor.__init__(image_processor=None, tokenizer=None, **kwargs)
¶
Initialize the LayoutLMv2Processor class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
TYPE:
|
image_processor
|
An object representing the image processor. It can be an instance of a specific image processing class or None. If None, it will default to the value of 'feature_extractor'.
TYPE:
|
tokenizer
|
An object representing the tokenizer to be used. This should be a valid tokenizer object required for processing the input data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If either 'image_processor' is not provided or if 'tokenizer' is not specified. |
FutureWarning
|
If the 'feature_extractor' argument is used (deprecated) in place of 'image_processor'. |
Source code in mindnlp\transformers\models\layoutlmv2\processing_layoutlmv2.py
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 | |
mindnlp.transformers.models.layoutlmv2.processing_layoutlmv2.LayoutLMv2Processor.batch_decode(*args, **kwargs)
¶
This method forwards all its arguments to PreTrainedTokenizer's [~PreTrainedTokenizer.batch_decode]. Please
refer to the docstring of this method for more information.
Source code in mindnlp\transformers\models\layoutlmv2\processing_layoutlmv2.py
196 197 198 199 200 201 | |
mindnlp.transformers.models.layoutlmv2.processing_layoutlmv2.LayoutLMv2Processor.decode(*args, **kwargs)
¶
This method forwards all its arguments to PreTrainedTokenizer's [~PreTrainedTokenizer.decode]. Please refer
to the docstring of this method for more information.
Source code in mindnlp\transformers\models\layoutlmv2\processing_layoutlmv2.py
203 204 205 206 207 208 | |
mindnlp.transformers.models.layoutlmv2.processing_layoutlmv2.LayoutLMv2Processor.get_overflowing_images(images, overflow_to_sample_mapping)
¶
| PARAMETER | DESCRIPTION |
|---|---|
images
|
List of images
|
overflow_to_sample_mapping
|
List of indices of samples that have overflowing tokens
|
| RETURNS | DESCRIPTION |
|---|---|
|
List of images that correspond to samples with overflowing tokens |
Source code in mindnlp\transformers\models\layoutlmv2\processing_layoutlmv2.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2
¶
Tokenization class for LayoutLMv2.
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.BasicTokenizer
¶
Constructs a BasicTokenizer that will run basic tokenization (punctuation splitting, lower casing, etc.).
| PARAMETER | DESCRIPTION |
|---|---|
do_lower_case
|
Whether or not to lowercase the input when tokenizing.
TYPE:
|
never_split
|
Collection of tokens which will never be split during tokenization. Only has an effect when
TYPE:
|
tokenize_chinese_chars
|
Whether or not to tokenize Chinese characters. This should likely be deactivated for Japanese (see this issue).
TYPE:
|
strip_accents
|
Whether or not to strip all accents. If this option is not specified, then it will be determined by the
value for
TYPE:
|
do_split_on_punc
|
In some instances we want to skip the basic punctuation splitting so that later tokenization can capture the full context of the words, such as contractions.
TYPE:
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
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 1648 1649 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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.BasicTokenizer.__init__(do_lower_case=True, never_split=None, tokenize_chinese_chars=True, strip_accents=None, do_split_on_punc=True)
¶
Initializes a new instance of the BasicTokenizer class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The object itself.
|
do_lower_case
|
A boolean indicating whether to convert the text to lowercase. Defaults to True.
TYPE:
|
never_split
|
A list of tokens that should never be split during tokenization. Defaults to None.
TYPE:
|
tokenize_chinese_chars
|
A boolean indicating whether to tokenize Chinese characters. Defaults to True.
TYPE:
|
strip_accents
|
A string indicating whether to strip accents from the text. Defaults to None.
TYPE:
|
do_split_on_punc
|
A boolean indicating whether to split tokens on punctuation. Defaults to True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.BasicTokenizer.tokenize(text, never_split=None)
¶
Basic Tokenization of a piece of text. For sub-word tokenization, see WordPieceTokenizer.
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer
¶
Bases: PreTrainedTokenizer
Construct a LayoutLMv2 tokenizer. Based on WordPiece. [LayoutLMv2Tokenizer] can be used to turn words, word-level
bounding boxes and optional word labels to token-level input_ids, attention_mask, token_type_ids, bbox, and
optional labels (for token classification).
This tokenizer inherits from [PreTrainedTokenizer] which contains most of the main methods. Users should refer to
this superclass for more information regarding those methods.
[LayoutLMv2Tokenizer] runs end-to-end tokenization: punctuation splitting and wordpiece. It also turns the
word-level bounding boxes into token-level bounding boxes.
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
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 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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 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 486 487 488 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 536 537 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 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 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 879 880 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 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 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 1067 1068 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 1446 1447 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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.do_lower_case
property
¶
Whether or not to lowercase the input when tokenizing.
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.vocab_size
property
¶
Return the size of the vocabulary used by the LayoutLMv2Tokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the LayoutLMv2Tokenizer class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.__call__(text, text_pair=None, boxes=None, word_labels=None, add_special_tokens=True, padding=False, truncation=None, max_length=None, stride=0, pad_to_multiple_of=None, return_tensors=None, return_token_type_ids=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_length=False, verbose=True, **kwargs)
¶
Main method to tokenize and prepare for the model one or several sequence(s) or one or several pair(s) of sequences with word-level normalized bounding boxes and optional labels.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
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).
TYPE:
|
text_pair
|
The sequence or batch of sequences to be encoded. Each sequence should be a list of strings (pretokenized string).
TYPE:
|
boxes
|
Word-level bounding boxes. Each bounding box should be normalized to be on a 0-1000 scale.
TYPE:
|
word_labels
|
Word-level integer labels (for token classification tasks such as FUNSD, CORD).
TYPE:
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
528 529 530 531 532 533 534 535 536 537 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 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.__init__(vocab_file, do_lower_case=True, do_basic_tokenize=True, never_split=None, unk_token='[UNK]', sep_token='[SEP]', pad_token='[PAD]', cls_token='[CLS]', mask_token='[MASK]', cls_token_box=[0, 0, 0, 0], sep_token_box=[1000, 1000, 1000, 1000], pad_token_box=[0, 0, 0, 0], pad_token_label=-100, only_label_first_subword=True, tokenize_chinese_chars=True, strip_accents=None, model_max_length=512, additional_special_tokens=None, **kwargs)
¶
Initializes a LayoutLMv2Tokenizer object.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
vocab_file
|
The path to the vocabulary file.
TYPE:
|
do_lower_case
|
Whether to lowercase the input text. Defaults to True.
TYPE:
|
do_basic_tokenize
|
Whether to perform basic tokenization. Defaults to True.
TYPE:
|
never_split
|
List of tokens that should not be split. Defaults to None.
TYPE:
|
unk_token
|
The unknown token. Defaults to '[UNK]'.
TYPE:
|
sep_token
|
The separator token. Defaults to '[SEP]'.
TYPE:
|
pad_token
|
The padding token. Defaults to '[PAD]'.
TYPE:
|
cls_token
|
The classification token. Defaults to '[CLS]'.
TYPE:
|
mask_token
|
The masking token. Defaults to '[MASK]'.
TYPE:
|
cls_token_box
|
The bounding box coordinates for the classification token. Defaults to [0, 0, 0, 0].
TYPE:
|
sep_token_box
|
The bounding box coordinates for the separator token. Defaults to [1000, 1000, 1000, 1000].
TYPE:
|
pad_token_box
|
The bounding box coordinates for the padding token. Defaults to [0, 0, 0, 0].
TYPE:
|
pad_token_label
|
The label for the padding token. Defaults to -100.
TYPE:
|
only_label_first_subword
|
Whether to only label the first subword. Defaults to True.
TYPE:
|
tokenize_chinese_chars
|
Whether to tokenize Chinese characters. Defaults to True.
TYPE:
|
strip_accents
|
The accents to strip. Defaults to None.
TYPE:
|
model_max_length
|
The maximum length of the model. Defaults to 512.
TYPE:
|
additional_special_tokens
|
Additional special tokens. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the vocabulary file cannot be found at the specified path. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.batch_encode_plus(batch_text_or_text_pairs, is_pair=None, boxes=None, word_labels=None, add_special_tokens=True, padding=False, truncation=None, max_length=None, stride=0, pad_to_multiple_of=None, return_tensors=None, return_token_type_ids=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_length=False, verbose=True, **kwargs)
¶
Encodes a batch of text or text pairs using the LayoutLMv2 model.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the LayoutLMv2Tokenizer class.
TYPE:
|
batch_text_or_text_pairs
|
A list of input texts or text pairs to be encoded. The input can be either a single text, a text pair, or a pre-tokenized input.
TYPE:
|
is_pair
|
Indicates whether the input is a text pair. Defaults to None.
TYPE:
|
boxes
|
A list of bounding boxes for each token in the input. Defaults to None.
TYPE:
|
word_labels
|
A list of word labels for each token in the input. Defaults to None.
TYPE:
|
add_special_tokens
|
Indicates whether to add special tokens to the input. Defaults to True.
TYPE:
|
padding
|
Specifies the padding strategy to use. Defaults to False.
TYPE:
|
truncation
|
Specifies the truncation strategy to use. Defaults to None.
TYPE:
|
max_length
|
The maximum sequence length after tokenization. Defaults to None.
TYPE:
|
stride
|
The stride for splitting the input into multiple chunks. Defaults to 0.
TYPE:
|
pad_to_multiple_of
|
Pad the sequence length to a multiple of this value. Defaults to None.
TYPE:
|
return_tensors
|
Specifies the type of tensors to return. Defaults to None.
TYPE:
|
return_token_type_ids
|
Indicates whether to return token type IDs. Defaults to None.
TYPE:
|
return_attention_mask
|
Indicates whether to return attention masks. Defaults to None.
TYPE:
|
return_overflowing_tokens
|
Indicates whether to return overflowing tokens. Defaults to False.
TYPE:
|
return_special_tokens_mask
|
Indicates whether to return a mask indicating the special tokens. Defaults to False.
TYPE:
|
return_offsets_mapping
|
Indicates whether to return the offsets mapping of tokens to original text. Defaults to False.
TYPE:
|
return_length
|
Indicates whether to return the lengths of encoded sequences. Defaults to False.
TYPE:
|
verbose
|
Indicates whether to print informative messages. Defaults to True.
TYPE:
|
**kwargs
|
Additional keyword arguments for customizing the encoding process.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
BatchEncoding
|
A dictionary-like object containing the encoded batch, with the following keys:
TYPE:
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format:
- single sequence:
[CLS] X [SEP] - pair of sequences:
[CLS] A [SEP] B [SEP]
| PARAMETER | DESCRIPTION |
|---|---|
token_ids_0
|
List of IDs to which the special tokens will be added.
TYPE:
|
token_ids_1
|
Optional second list of IDs for sequence pairs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[int]
|
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
408 409 410 411 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.create_token_type_ids_from_sequences(token_ids_0, token_ids_1=None)
¶
Create a mask from the two sequences passed to be used in a sequence-pair classification task. A BERT sequence pair mask has the following format:
:: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 | first sequence | second sequence |
If token_ids_1 is None, this method only returns the first portion of the mask (0s).
Args:
token_ids_0 (List[int]):
List of IDs.
token_ids_1 (List[int], optional):
Optional second list of IDs for sequence pairs.
Returns:
List[int]: List of token type IDs according to the given sequence(s).
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.encode(text, text_pair=None, boxes=None, word_labels=None, add_special_tokens=True, padding=False, truncation=None, max_length=None, stride=0, pad_to_multiple_of=None, return_tensors=None, return_token_type_ids=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_length=False, verbose=True, **kwargs)
¶
This method encodes the input text and returns a list of integer input ids.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The LayoutLMv2Tokenizer instance.
|
text
|
The input text to encode. It can be either a TextInput object or a PreTokenizedInput object.
TYPE:
|
text_pair
|
The optional second input text to be encoded. It should be a PreTokenizedInput object.
TYPE:
|
boxes
|
The optional bounding boxes for each token in the input text. Each box is represented as a list of four integers [x_min, y_min, x_max, y_max].
TYPE:
|
word_labels
|
The optional word labels associated with each token in the input text. It should be a list of integers.
TYPE:
|
add_special_tokens
|
Whether to add special tokens like [CLS], [SEP], etc. Default is True.
TYPE:
|
padding
|
The padding strategy to apply. It can be a boolean value, a string, or a PaddingStrategy object. Default is False.
TYPE:
|
truncation
|
The truncation strategy to apply. It can be a boolean value, a string, or a TruncationStrategy object. Default is None.
TYPE:
|
max_length
|
The maximum length of the encoded sequence. If provided, the sequence is truncated or padded to this length.
TYPE:
|
stride
|
The stride used for tokenization. Default is 0.
TYPE:
|
pad_to_multiple_of
|
If specified, the sequence is padded to a multiple of this value.
TYPE:
|
return_tensors
|
The type of tensor to return. It can be a string or a TensorType object.
TYPE:
|
return_token_type_ids
|
Whether to return token type ids.
TYPE:
|
return_attention_mask
|
Whether to return attention mask.
TYPE:
|
return_overflowing_tokens
|
Whether to return overflowing tokens.
TYPE:
|
return_special_tokens_mask
|
Whether to return special tokens mask.
TYPE:
|
return_offsets_mapping
|
Whether to return the mapping from tokens to character offsets.
TYPE:
|
return_length
|
Whether to return the length of the encoded inputs.
TYPE:
|
verbose
|
Whether to print verbose logs. Default is True.
TYPE:
|
**kwargs
|
Additional keyword arguments.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
List[int]
|
List[int]: A list of integer input ids representing the encoded input text. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.encode_plus(text, text_pair=None, boxes=None, word_labels=None, add_special_tokens=True, padding=False, truncation=None, max_length=None, stride=0, pad_to_multiple_of=None, return_tensors=None, return_token_type_ids=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_length=False, verbose=True, **kwargs)
¶
Tokenize and prepare for the model a sequence or a pair of sequences.
.. warning:: This method is deprecated, __call__ should be used instead.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The first sequence to be encoded. This can be a string, a list of strings or a list of list of strings.
TYPE:
|
text_pair
|
Optional second sequence to be encoded. This can be a list of strings (words of a single example) or a list of list of strings (words of a batch of examples).
TYPE:
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
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 1067 1068 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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.get_special_tokens_mask(token_ids_0, token_ids_1=None, already_has_special_tokens=False)
¶
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer prepare_for_model method.
| PARAMETER | DESCRIPTION |
|---|---|
token_ids_0
|
List of IDs.
TYPE:
|
token_ids_1
|
Optional second list of IDs for sequence pairs.
TYPE:
|
already_has_special_tokens
|
Whether or not the token list is already formatted with special tokens for the model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[int]
|
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.get_vocab()
¶
Returns the combined vocabulary of the LayoutLMv2Tokenizer instance and any additional tokens that have been added.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the LayoutLMv2Tokenizer class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary representing the combined vocabulary of the LayoutLMv2Tokenizer instance and any additional tokens that have been added. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.prepare_for_model(text, text_pair=None, boxes=None, word_labels=None, add_special_tokens=True, padding=False, truncation=None, max_length=None, stride=0, pad_to_multiple_of=None, return_tensors=None, return_token_type_ids=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_length=False, verbose=True, prepend_batch_axis=False, **kwargs)
¶
Prepares a sequence or a pair of sequences so that it can be used by the model. It adds special tokens,
truncates sequences if overflowing while taking into account the special tokens and manages a moving window
(with user defined stride) for overflowing tokens. Please Note, for text_pair different than None and
truncation_strategy = longest_first or True, it is not possible to return overflowing tokens. Such a
combination of arguments will raise an error.
Word-level boxes are turned into token-level bbox. If provided, word-level word_labels are turned into
token-level labels. The word label is used for the first token of the word, while remaining tokens are
labeled with -100, such that they will be ignored by the loss function.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The first sequence to be encoded. This can be a string, a list of strings or a list of list of strings.
TYPE:
|
text_pair
|
Optional second sequence to be encoded. This can be a list of strings (words of a single example) or a list of list of strings (words of a batch of examples).
TYPE:
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary to a file in the specified directory.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the LayoutLMv2Tokenizer class.
TYPE:
|
save_directory
|
The directory where the vocabulary file will be saved.
TYPE:
|
filename_prefix
|
A prefix to be added to the filename. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the file path of the saved vocabulary. |
| RAISES | DESCRIPTION |
|---|---|
IOError
|
If an I/O error occurs while writing the vocabulary file. |
ValueError
|
If the provided save_directory is invalid or if the vocabulary indices are not consecutive. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.truncate_sequences(ids, token_boxes, pair_ids=None, pair_token_boxes=None, labels=None, num_tokens_to_remove=0, truncation_strategy='longest_first', stride=0)
¶
Truncates a sequence pair in-place following the strategy.
| PARAMETER | DESCRIPTION |
|---|---|
ids
|
Tokenized input ids of the first sequence. Can be obtained from a string by chaining the
TYPE:
|
token_boxes
|
Bounding boxes of the first sequence.
TYPE:
|
pair_ids
|
Tokenized input ids of the second sequence. Can be obtained from a string by chaining the
TYPE:
|
pair_token_boxes
|
Bounding boxes of the second sequence.
TYPE:
|
labels
|
Labels of the first sequence (for token classification tasks).
TYPE:
|
num_tokens_to_remove
|
Number of tokens to remove using the truncation strategy.
TYPE:
|
truncation_strategy
|
The strategy to follow for truncation. Can be:
TYPE:
|
stride
|
If set to a positive number, the overflowing tokens returned will contain some tokens from the main sequence returned. The value of this argument defines the number of additional tokens.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[List[int], List[int], List[int]]
|
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
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 1446 1447 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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.WordpieceTokenizer
¶
Runs WordPiece tokenization.
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.WordpieceTokenizer.__init__(vocab, unk_token, max_input_chars_per_word=100)
¶
Initializes a new instance of the WordpieceTokenizer class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the WordpieceTokenizer class.
TYPE:
|
vocab
|
A list of strings representing the vocabulary.
TYPE:
|
unk_token
|
The unknown token to be used for out-of-vocabulary words.
TYPE:
|
max_input_chars_per_word
|
The maximum number of characters per word. Defaults to 100.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.WordpieceTokenizer.tokenize(text)
¶
Tokenizes a piece of text into its word pieces. This uses a greedy longest-match-first algorithm to perform tokenization using the given vocabulary.
For example, input = "unaffable" wil return as output ["un", "##aff", "##able"].
| PARAMETER | DESCRIPTION |
|---|---|
text
|
A single token or whitespace separated tokens. This should have already been passed through BasicTokenizer.
|
| RETURNS | DESCRIPTION |
|---|---|
|
A list of wordpiece tokens. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.load_vocab(vocab_file)
¶
Loads a vocabulary file into a dictionary.
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
169 170 171 172 173 174 175 176 177 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.subfinder(mylist, pattern)
¶
| PARAMETER | DESCRIPTION |
|---|---|
mylist
|
A list in which to search for the pattern.
|
pattern
|
A list that we are trying to find in mylist.
|
| RETURNS | DESCRIPTION |
|---|---|
|
Conditional return: The first matching pattern found in mylist and its starting index. If no match is found, returns None and 0. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2.whitespace_tokenize(text)
¶
Runs basic whitespace cleaning and splitting on a piece of text.
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2.py
180 181 182 183 184 185 186 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2_fast
¶
Fast tokenization class for LayoutLMv2. It overwrites 2 methods of the slow tokenizer class, namely _batch_encode_plus and _encode_plus, in which the Rust tokenizer is used.
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast
¶
Bases: PreTrainedTokenizerFast
Construct a "fast" LayoutLMv2 tokenizer (backed by HuggingFace's tokenizers library). Based on WordPiece.
This tokenizer inherits from [PreTrainedTokenizerFast] which contains most of the main methods. Users should
refer to this superclass for more information regarding those methods.
| PARAMETER | DESCRIPTION |
|---|---|
vocab_file
|
File containing the vocabulary.
TYPE:
|
do_lower_case
|
Whether or not to lowercase the input when tokenizing.
TYPE:
|
unk_token
|
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.
TYPE:
|
sep_token
|
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.
TYPE:
|
pad_token
|
The token used for padding, for example when batching sequences of different lengths.
TYPE:
|
cls_token
|
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.
TYPE:
|
mask_token
|
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.
TYPE:
|
cls_token_box
|
The bounding box to use for the special [CLS] token.
TYPE:
|
sep_token_box
|
The bounding box to use for the special [SEP] token.
TYPE:
|
pad_token_box
|
The bounding box to use for the special [PAD] token.
TYPE:
|
pad_token_label
|
The label to use for padding tokens. Defaults to -100, which is the
TYPE:
|
only_label_first_subword
|
Whether or not to only label the first subword, in case word labels are provided.
TYPE:
|
tokenize_chinese_chars
|
Whether or not to tokenize Chinese characters. This should likely be deactivated for Japanese (see this issue).
TYPE:
|
strip_accents
|
Whether or not to strip all accents. If this option is not specified, then it will be determined by the
value for
TYPE:
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2_fast.py
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 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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 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 486 487 488 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 536 537 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 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 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 879 880 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 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.__call__(text, text_pair=None, boxes=None, word_labels=None, add_special_tokens=True, padding=False, truncation=None, max_length=None, stride=0, pad_to_multiple_of=None, return_tensors=None, return_token_type_ids=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_length=False, verbose=True, **kwargs)
¶
Main method to tokenize and prepare for the model one or several sequence(s) or one or several pair(s) of sequences with word-level normalized bounding boxes and optional labels.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
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).
TYPE:
|
text_pair
|
The sequence or batch of sequences to be encoded. Each sequence should be a list of strings (pretokenized string).
TYPE:
|
boxes
|
Word-level bounding boxes. Each bounding box should be normalized to be on a 0-1000 scale.
TYPE:
|
word_labels
|
Word-level integer labels (for token classification tasks such as FUNSD, CORD).
TYPE:
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2_fast.py
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 343 344 345 346 347 348 349 350 351 352 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.__init__(vocab_file=None, tokenizer_file=None, do_lower_case=True, unk_token='[UNK]', sep_token='[SEP]', pad_token='[PAD]', cls_token='[CLS]', mask_token='[MASK]', cls_token_box=[0, 0, 0, 0], sep_token_box=[1000, 1000, 1000, 1000], pad_token_box=[0, 0, 0, 0], pad_token_label=-100, only_label_first_subword=True, tokenize_chinese_chars=True, strip_accents=None, **kwargs)
¶
This method initializes an instance of the LayoutLMv2TokenizerFast class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
vocab_file
|
Path to the vocabulary file. Defaults to None.
TYPE:
|
tokenizer_file
|
Path to the tokenizer file. Defaults to None.
TYPE:
|
do_lower_case
|
Flag indicating whether to convert tokens to lowercase. Defaults to True.
TYPE:
|
unk_token
|
The token representing unknown words. Defaults to '[UNK]'.
TYPE:
|
sep_token
|
The separator token. Defaults to '[SEP]'.
TYPE:
|
pad_token
|
The padding token. Defaults to '[PAD]'.
TYPE:
|
cls_token
|
The classification token. Defaults to '[CLS]'.
TYPE:
|
mask_token
|
The masking token. Defaults to '[MASK]'.
TYPE:
|
cls_token_box
|
A list of four integer values representing the bounding box for the classification token. Defaults to [0, 0, 0, 0].
TYPE:
|
sep_token_box
|
A list of four integer values representing the bounding box for the separator token. Defaults to [1000, 1000, 1000, 1000].
TYPE:
|
pad_token_box
|
A list of four integer values representing the bounding box for the padding token. Defaults to [0, 0, 0, 0].
TYPE:
|
pad_token_label
|
The label for padding tokens. Defaults to -100.
TYPE:
|
only_label_first_subword
|
Flag indicating whether to only label the first subword. Defaults to True.
TYPE:
|
tokenize_chinese_chars
|
Flag indicating whether to tokenize Chinese characters. Defaults to True.
TYPE:
|
strip_accents
|
Method for stripping accents. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If an invalid argument is provided. |
TypeError
|
If input types are incorrect. |
FileNotFoundError
|
If the specified vocab_file or tokenizer_file is not found. |
JSONDecodeError
|
If there is an issue decoding the pre_tok_state JSON. |
AttributeError
|
If there is an issue with setting the backend_tokenizer normalizer. |
KeyError
|
If required keys are missing in the pre_tok_state. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2_fast.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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.batch_encode_plus(batch_text_or_text_pairs, is_pair=None, boxes=None, word_labels=None, add_special_tokens=True, padding=False, truncation=None, max_length=None, stride=0, pad_to_multiple_of=None, return_tensors=None, return_token_type_ids=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_length=False, verbose=True, **kwargs)
¶
This method encodes a batch of text or text pairs using LayoutLMv2TokenizerFast.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the LayoutLMv2TokenizerFast class.
|
batch_text_or_text_pairs
|
A list of text inputs or text pairs to be encoded.
TYPE:
|
is_pair
|
Specifies whether the input is a text pair. Default is None.
TYPE:
|
boxes
|
Optional bounding boxes for text elements in the input text. Default is None.
TYPE:
|
word_labels
|
Optional word labels for the input text. Default is None.
TYPE:
|
add_special_tokens
|
Whether to add special tokens to the encoded inputs. Default is True.
TYPE:
|
padding
|
Padding strategy to apply. Default is False.
TYPE:
|
truncation
|
Truncation strategy to apply. Default is None.
TYPE:
|
max_length
|
Maximum length of the encoded inputs. Default is None.
TYPE:
|
stride
|
The stride to use for overflowing tokens. Default is 0.
TYPE:
|
pad_to_multiple_of
|
Pad the sequence length to a multiple of this value. Default is None.
TYPE:
|
return_tensors
|
Specifies the tensor type to return. Default is None.
TYPE:
|
return_token_type_ids
|
Whether to return token type IDs. Default is None.
TYPE:
|
return_attention_mask
|
Whether to return attention masks. Default is None.
TYPE:
|
return_overflowing_tokens
|
Whether to return overflowing tokens. Default is False.
TYPE:
|
return_special_tokens_mask
|
Whether to return a special tokens mask. Default is False.
TYPE:
|
return_offsets_mapping
|
Whether to return offsets mapping. Default is False.
TYPE:
|
return_length
|
Whether to return the lengths of the encoded inputs. Default is False.
TYPE:
|
verbose
|
Verbosity flag. Default is True.
TYPE:
|
**kwargs
|
Additional keyword arguments for customization.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
BatchEncoding
|
A dictionary-like object containing the encoded inputs with various attributes.
TYPE:
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2_fast.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format:
- single sequence:
[CLS] X [SEP] - pair of sequences:
[CLS] A [SEP] B [SEP]
| PARAMETER | DESCRIPTION |
|---|---|
token_ids_0
|
List of IDs to which the special tokens will be added.
TYPE:
|
token_ids_1
|
Optional second list of IDs for sequence pairs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2_fast.py
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.create_token_type_ids_from_sequences(token_ids_0, token_ids_1=None)
¶
Create a mask from the two sequences passed to be used in a sequence-pair classification task. A BERT sequence pair mask has the following format:
:: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 | first sequence | second
sequence | If token_ids_1 is None, this method only returns the first portion of the mask (0s).
Args:
token_ids_0 (List[int]):
List of IDs.
token_ids_1 (List[int], optional):
Optional second list of IDs for sequence pairs.
Returns:
List[int]: List of token type IDs according to the given sequence(s).
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2_fast.py
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.encode_plus(text, text_pair=None, boxes=None, word_labels=None, add_special_tokens=True, padding=False, truncation=None, max_length=None, stride=0, pad_to_multiple_of=None, return_tensors=None, return_token_type_ids=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_length=False, verbose=True, **kwargs)
¶
Tokenize and prepare for the model a sequence or a pair of sequences. .. warning:: This method is deprecated,
__call__ should be used instead.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The first sequence to be encoded. This can be a string, a list of strings or a list of list of strings.
TYPE:
|
text_pair
|
Optional second sequence to be encoded. This can be a list of strings (words of a single example) or a list of list of strings (words of a batch of examples).
TYPE:
|
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2_fast.py
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 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 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary files of the LayoutLMv2TokenizerFast model.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
Instance of the LayoutLMv2TokenizerFast class.
|
save_directory
|
The directory where the vocabulary files will be saved.
TYPE:
|
filename_prefix
|
Prefix to be added to the filename of the vocabulary files. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the paths to the saved vocabulary files. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2_fast.py
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 | |
mindnlp.transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.tokenize(text, pair=None, add_special_tokens=False, **kwargs)
¶
Tokenizes a given text using the LayoutLMv2TokenizerFast.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the LayoutLMv2TokenizerFast class.
TYPE:
|
text
|
The input text to be tokenized.
TYPE:
|
pair
|
The second input text if tokenizing a pair of texts. Defaults to None.
TYPE:
|
add_special_tokens
|
Whether to add special tokens to the input sequence. Defaults to False.
TYPE:
|
**kwargs
|
Additional keyword arguments to be passed to the underlying tokenizer.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
List[str]
|
List[str]: A list of tokens representing the tokenized input text. |
Source code in mindnlp\transformers\models\layoutlmv2\tokenization_layoutlmv2_fast.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |