TL;DR
LayoutLMv3 is a transformer that reads a scanned document as two streams at once: the words, with their positions on the page, and the page itself, cut into small square patches. It is pre-trained with three objectives. Two of them hide parts of the text and parts of the image and ask the model to recover them. The third asks whether each unmasked word still has its image patches visible, which ties the two streams together. Unlike its predecessors, it does not need a convolutional network or an object detector to read the image, and it reaches top scores on form, receipt, page-classification and question-answering benchmarks.
Builds on: LayoutLM, Masked Autoencoder, Vector Quantization · Leads to: no lesson in the graph builds on it yet.
Why it mattered
Document AI systems need to work on text, layout and appearance together, and the earlier LayoutLM family handled the image with a convolutional network or a Faster R-CNN detector. Both are heavy, and the detector needs region annotations to train. The paper's point is that a page is also a sequence of patches, which a transformer can read directly, the same way a vision transformer reads an image. That removes the detector and most of the pre-processing.
The result is a single general model. The same pre-trained weights are fine-tuned for text-centric tasks such as forms, receipts and document question answering, and for image-centric tasks such as page classification and layout analysis. Earlier models were usually strong on one side and weaker on the other.
The paper's authors also note that many multimodal document models use masked language modeling for the text and a different, often noisier, objective for the image. Their unified masking aims to make the two modalities learn with the same kind of target.
The problem
Pre-training a multimodal model needs a target for each modality. For text, the standard target is a word token, as in BERT. For images, earlier document models used pixel reconstruction, which tends to learn noisy detail rather than the structure of the page, or regression of region features, which is harder to learn than choosing among a discrete set. The two targets have different granularities, so the model struggles to line up a word with the part of the page where it appears.
A second problem is the architecture. A convolutional or detector backbone adds parameters and needs object proposals. The paper asks whether a plain transformer on image patches can do the job, provided the image objective and the alignment between words and patches are designed well.
How it works
The steps below follow the order in which the model runs, from scanned page to output. The numbers are illustrative. We imagine a receipt line, "Total 12.50", whose two words sit in one segment, so they share one box. The box is given as fractions of the page width and height.
- Run OCR. An OCR toolkit gives the words "Total" and "12.50" and their positions. Because the two words form one segment, both get the same box: left 0.10, top 0.75, right 0.32, bottom 0.78. Check: the box is of the page wide.
- Encode the layout. The four layout features, the x and y positions and the width and height, each go through their own embedding layer. The box is therefore encoded once for the segment, not once per word. Check: the two words use the same layout embedding.
- Encode the text. Each word is split into subword tokens, using byte-pair encoding, and the word embeddings start from RoBERTa's. A 1D position embedding gives each token its place in the sequence. Check: "12.50" may become several tokens, and they all carry the segment's layout embedding.
- Encode the image. The page is resized to pixels and cut into squares. That gives patches. Each patch is projected by a linear layer to the model's width, and a learnable 1D position embedding is added. Check: , and .
- Concatenate. The text tokens and the image patches form one sequence, with special tokens marking the boundaries. Self-attention then lets every token look at every other token and patch. The attention also gets relative position biases for text and for image.
- Mask and predict. The text objective hides 30 percent of the text tokens, in spans, and predicts them. The image objective hides about 40 percent of the patches, in blocks, and predicts a discrete code for each hidden patch from a visual tokenizer with 8,192 codes. The alignment objective looks at each unmasked word: it is labelled aligned if all its patches are unmasked, and unaligned otherwise.
- Fine-tune. For forms and receipts, a linear layer labels each text token. For page classification, an MLP reads the
[CLS]token. For document question answering, a classifier picks the start and end of the answer span among the text tokens. For layout analysis, the model is used as a backbone for a detector, with only the image side.
flowchart LR S["Scanned page"] --> O["OCR: words, segment boxes"] S --> P["224 by 224 page, 196 patches"] O --> T["Text tokens + 1D position + segment layout"] P --> V["Linear patch projection + 1D position"] T --> C["One sequence: text then patches"] V --> C C --> E["Transformer with 1D and 2D attention bias"] E --> L1["Masked text prediction"] E --> L2["Masked patch code prediction"] E --> L3["Word-patch alignment"] E --> F["Fine-tuning heads"]
The math
Step 2 in symbols: the layout embedding
Let be the normalized layout of the segment that token belongs to, with coordinates divided by the image size as in LayoutLM. The layout part of the embedding is the sum of four lookups, added to the text embedding as LayoutLM adds its layout embeddings,
where each is a learned embedding table indexed by the value of that feature. Tokens of the same segment share the same index, which is what makes the layout segment-level.
Step 4 in symbols: the image patches
The image is , with , . It is split into patches with , and the number of patches is
Each patch is flattened, projected linearly to the model width , and given a learnable 1D position embedding. The paper reports that 2D position embeddings for patches gave no gain in its preliminary tests.
Step 6 in symbols: the three losses
Let be the masked image positions and the masked text positions. The text objective predicts each masked token from the corrupted image and text,
and the image objective predicts the discrete code of each masked patch in the same way,
The codes come from a discrete tokenizer with 8,192 entries, initialized from a document image transformer called DiT. The idea is the same as the discrete latent codes of VQ-VAE and DALL-E.
The alignment objective is a binary classification over the unmasked text tokens. Let be 1 when the word's patches are all unmasked. A two-layer head predicts with binary cross-entropy, and masked text tokens are excluded so the model cannot learn the position of a hidden word from its patches.
The total objective is the sum of the three,
The text masking uses spans whose lengths follow a Poisson distribution with mean 3. The image masking is blockwise, following BEiT.
Results
Form, receipt, page-classification and question-answering benchmarks. The table below uses the base model, which has 133 million parameters, and the large model, which has 368 million. F1 is the score on FUNSD and CORD, accuracy on RVL-CDIP, and ANLS on DocVQA, the edit-distance-based score used for that dataset. Some rows are from earlier papers.
| Model | Image embedding | FUNSD F1 | CORD F1 | RVL-CDIP accuracy | DocVQA ANLS |
|---|---|---|---|---|---|
| BERT base | none | 60.26 | 89.68 | 89.81 | 63.72 |
| LayoutLM base | ResNet-101, fine-tuned | 79.27 | not reported | 94.42 | not reported |
| LayoutLMv2 base | ResNeXt-101 FPN, grid | 82.76 | 94.95 | 95.25 | 78.08 |
| LayoutLMv3 base | linear patches | 90.29 | 96.56 | 95.44 | 78.76 |
| LayoutLMv2 large | ResNeXt-101 FPN, grid | 84.20 | 96.01 | 95.64 | 83.48 |
| LayoutLMv3 large | linear patches | 92.08 | 97.46 | 95.93 | 83.37 |
The paper's comparisons show that the base model improves on LayoutLMv2 base by 0.19 points on RVL-CDIP and by 0.7 ANLS points on DocVQA. For the large size, the paper reports an absolute ANLS gain of 4.61 over LayoutLMv3 base on DocVQA. The large model's FUNSD score, 92.08, is ahead of the 85.14 reported for StructuralLM. The paper cautions that StructuralLM and LayoutLMv3 both use segment-level layout, while many other models use word-level layout, so these FUNSD scores are not strictly like for like.
Document layout analysis (PubLayNet). Here the model is a backbone for a Cascade R-CNN detector, with no text. The overall mean average precision is 95.1, against 94.5 for the DiT base backbone with the same detector, 93.9 for UDoc and 91.0 for the original PubLayNet ResNet-101 baseline. The largest gain is on the title category, which the paper attributes to the language modality learned in pre-training.
What each part contributes. Table 3 of the paper is an ablation at the base size, trained on one million documents for 150,000 steps. It starts from a text-and-layout model with masked text pre-training only, and adds one piece at a time.
| Setting | Image embedding and objectives | FUNSD F1 | CORD F1 | RVL-CDIP accuracy | PubLayNet mAP |
|---|---|---|---|---|---|
| 1 | none, masked text only | 88.64 | 96.27 | 95.33 | not applicable |
| 2 | linear patches, masked text only | 89.39 | 96.11 | 95.00 | loss diverged |
| 3 | linear patches, masked text and masked image | 89.19 | 96.30 | 95.42 | 94.38 |
| 4 | linear patches, all three objectives | 89.78 | 96.49 | 95.53 | 94.43 |
Two findings stand out. Adding patches without an image objective hurt on CORD and RVL-CDIP, and the PubLayNet loss did not converge. Masked image modeling fixed the convergence and helped on CORD and RVL-CDIP. The alignment objective improved every task. The paper also reports that the linear patch projection adds only 0.6 million parameters, against 44 million for a ResNet-101 backbone.
Limits. The model reads at most 512 text tokens and a image, so very dense pages lose detail. Pre-training uses 11 million of the document images in IIT-CDIP, with no image augmentation. Some benchmarks use text extracted by different OCR systems: the paper uses Microsoft's Read API for RVL-CDIP and DocVQA, and the official OCR for CORD. Scores across rows of a table are therefore not always from identical inputs. The paper also notes that the image tokenizer is a separate module and is not counted in the parameter totals.
Reading guide
Read the paper in this order, with these prerequisites:
- Section 1, Introduction, and Figures 1 and 2: the task list and the comparison of image objectives. Needs LayoutLM for the context and Masked Autoencoder for masked reconstruction.
- Section 2.1, Model architecture: text and image embeddings, segment-level layout, and patches. Needs Word Embedding, Positional Encoding, Subword Tokenization, Transformer and Vision Transformer (ViT).
- Section 2.2, Pre-training objectives: the three losses. Needs Masked Language Modeling, Cross-Entropy Loss, Vector Quantization for the image codes, and Poisson Distribution for span lengths.
- Section 3.1 and 3.2, Configurations and pre-training: sizes, data and optimizer. Needs BERT, RoBERTa and Adam Optimizer.
- Section 3.3, Fine-tuning on multimodal tasks: the heads for each benchmark. Needs Axis-Aligned Bounding Box for the layout coordinates.
- Section 3.4 and 3.5, Layout analysis and ablations: the detector use and Table 3. Needs Object Detection and Faster R-CNN for the comparison.
- Sections 4 and 5, Related work and conclusion: the other document models and the future directions.
Check yourself
QuestionWhat does each of the three pre-training objectives predict?Show answer
QuestionA page is resized to 224 by 224 pixels and cut into 16 by 16 patches. A text sequence has 40 tokens, and 30 percent of them are masked for the text objective. How many image patches does the model see, and how many text tokens are masked?Show answer
QuestionWhy does LayoutLMv3 give all the words of one segment the same layout embedding, rather than one embedding per word?Show answer
QuestionA team classifies scanned pages on a small GPU budget. The paper's RVL-CDIP scores are 95.44 percent for the base model with 133 million parameters and 95.93 percent for the large model with 368 million. Which would you choose, and why?Show answer
Sources
- Huang, Y. et al. (2022). LayoutLMv3: Pre-training for Document AI with Unified Text and Image Masking. arXiv: https://arxiv.org/abs/2204.08387
- Huang, Y. et al. (2022). LayoutLMv3, ACM Multimedia 2022 proceedings. DOI: https://doi.org/10.1145/3503161.3548112
- Xu, Y. et al. (2019). LayoutLM: Pre-training of Text and Layout for Document Image Understanding. arXiv: https://arxiv.org/abs/1912.13318