TL;DR
Donut is a document reader with no OCR stage. Older systems first find the text boxes in a scanned page, read each box, and then try to understand the layout; any error in the first two steps spreads into the last. Donut takes the page image and writes the answer directly as a sequence of text tokens. The network is an image encoder followed by a text decoder, and it is trained first to read text in images and then to answer a specific task.
Builds on: Swin Transformer: Hierarchical Vision Transformer using Shifted Windows, BART: Denoising Sequence-to-Sequence Pre-training for Text Generation · Leads to: Optical Character Recognition (the pipeline it replaces)
Why it mattered
Document understanding is everywhere in ordinary work: receipts for expenses, invoices for accounts payable, business cards for contact lists, forms for banks and hospitals. Before Donut, the common approach was to run an OCR engine and feed its text and boxes into a language model such as BERT or LayoutLM. The authors argue this is costly in three ways: the OCR engine is expensive to run at high quality, it works badly on new languages and layouts, and its mistakes propagate to every later step.
Donut showed that one network can do the job. On the RVL-CDIP document classification benchmark it reached 95.30% accuracy, a little above LayoutLMv2's 95.25%, while running in 752 ms per image against 1,489 ms for LayoutLMv2. Because pre-training needs only page images and their text, the authors could extend the model to a multilingual setting using synthetic pages in English, Chinese, Japanese and Korean, without retraining an OCR engine. That matters for the many languages where good OCR is hard to get.
The problem
Visual document understanding means turning a page image into a structured answer. The paper lists three typical forms: classifying the page type (letter, memo, email and so on), extracting fields such as the total price on a receipt, and answering a question about the page.
The conventional recipe has two stages. First, read the text: detect each text region and recognise its characters. Second, understand the document: a model reads the recognised words together with their positions and labels the fields. The paper's Figure 1 shows this three-module pipeline: text detection, text recognition, and parsing.
The trouble is in the first stage. An OCR engine is a separate model with its own cost and failure modes. The authors note that OCR-based methods vary a lot with the engine: in their experiments, speed and accuracy of OCR-based models depended heavily on which off-the-shelf engine was used. Errors in reading characters become errors in the fields the second stage extracts. Post-correction modules can fix some of them, but they add a system to build and maintain.
So the question is whether a model can skip the reading stage and map the image straight to the answer, while still learning to read.
How it works
Here is one tiny instance traced from start to finish. The input is an invented receipt image whose only relevant line says total 12.00. The task is to extract the total. The numbers are illustrative; the paper's own experiments are on real datasets.
- The image goes in at full resolution. The page is resized to a large fixed size, 2560 by 1920 pixels in pre-training. Large inputs keep small characters legible. Check: the page is still one image, with no boxes drawn on it.
- The encoder turns the image into features. A Swin Transformer splits the page into patches and passes them through stages of attention restricted to local windows, merging patches as it goes. The output is a grid of feature vectors, one per region of the page. Check: the encoder has no output for text; it outputs features for regions.
- The decoder receives a prompt. The decoder is a text generator initialised from the first four layers of BART. For this task the prompt is a special start token for the field,
[START_total], which tells the model what to produce. - The decoder writes tokens one at a time. At each step it attends to the image features through cross-attention and picks the next token. Here the next tokens are the digits and the decimal point of 12.00, then the closing token
[END_total]. Check: each token is chosen given the image and everything already written. - The token sequence is converted to a structure. The special tokens mark where each field starts and ends. If a start token has no matching end token, the field is treated as missing. Check: the output is
[START_total] 12.00 [END_total], which reads back as the pair total equals 12.00.
The whole system is one pass through one network, with no detector and no separate recogniser. The diagram shows the flow, with the decoder looping over tokens:
flowchart LR A["Document image"] --> B["Swin encoder: image features"] B --> C["BART-based decoder"] P["Task prompt token"] --> C C --> D["Next token"] D --> C D --> E["Structured output"]
The decoder's loop is the same idea as an Autoregressive Model: each token depends on the ones before it. That is why the same machinery writes a field name, a price or a sentence.
The math
The model is an encoder followed by a decoder.
Let be the input image, with height , width and colour channels. The encoder maps it to a set of embedding vectors , each of dimension :
Here is the encoder's parameters, and is the number of image patches or feature positions. The decoder is conditioned on these embeddings and generates a token sequence :
The product means each token is scored given the image features and every earlier token. This is the factorisation used by any Autoregressive Model, with the image added as context.
Step 1 in symbols: reading the page
Pre-training asks the decoder to write all the text of an image in reading order, from top left to bottom right, given the image and the text so far. The objective is the standard next-token cross-entropy:
This is the same loss as in Cross-Entropy Loss, applied to text tokens. The paper calls the task pseudo-OCR: the model learns to read, but without a dedicated character recogniser.
Step 2 in symbols: the prompt and the structure
For a downstream task the decoder is given a prompt token p and trained to produce the structured answer s:
The same formula covers classification, extraction and question answering. Only the prompt and the target change. This is what the paper means by treating every task as a JSON prediction problem.
Step 3 in symbols: scoring an extraction
For information extraction the paper uses a tree-edit-distance score, because a field can be missing, wrongly placed or nested. For a predicted tree , a ground-truth tree , and an empty tree , the accuracy is
Here is the minimum number of edits to turn one tree into another. The denominator is the cost of predicting nothing at all. A prediction that needs two edits when an empty answer would need eight scores 1 - 2/8 = 0.75. A prediction that needs more edits than an empty answer scores 0.
Results
The paper also reports full per-dataset tables for classification, extraction and question answering; this summary shows the headline figures.
| Setting | Donut | Comparison in the paper |
|---|---|---|
| RVL-CDIP classification accuracy | 95.30%, 752 ms, 143M parameters | LayoutLMv2: 95.25%, 1,489 ms, 200M parameters plus the OCR engine |
| CORD extraction at 1280 by 960 pixels | 91.1 accuracy, 0.7 s per image | A smaller input than the 2560 by 1920 pre-training size |
| CORD with 80 training samples (10% of the set) | Outperforms LayoutLMv2 | Compared with LayoutLMv2's accuracy in the paper's low-resource study |
| Smaller pre-training run (8 V100 GPUs, 5 days, 1.2M synthetic pages) | 94.5 on RVL-CDIP, 85.4 on CORD | Appendix A.6 |
The authors state that Donut achieves the best accuracy on all the information extraction domains, and it reaches competitive scores on DocVQA without an external OCR engine. They also report that the decoder's attention maps land on the right text regions even though no localisation labels were used, so the maps can serve as a rough indicator of where text is.
Three findings are worth knowing.
Pre-training by reading text is the most useful of the tasks the authors tried. Image captioning and other general pre-training tasks gave little gain. Synthetic pages were enough for information extraction, but for DocVQA it helped to include real scanned documents from IIT-CDIP.
Input resolution matters most for small text. Donut's score grows as the input grows, and the gain is clearest on DocVQA, where pages are large and dense. The authors note that the cost rises with resolution, and that a more efficient attention mechanism could help but was not used.
Donut stays robust when data is scarce. On CORD, it beats LayoutLMv2 with 10% of the training data, which the paper says is 80 samples.
The limits are real. Pre-training used 64 A100 GPUs for 200,000 steps, so the method is not cheap to reproduce. The pseudo text labels for IIT-CDIP came from a commercial OCR service, so OCR is still used, just once and offline. The paper's qualitative examples show Donut missing some tiny text on large images, and the authors say that scaling the input would mitigate this but adds cost.
Reading guide
Read the paper in this order. Each section depends on the concepts listed with it.
- Section 1, Introduction. The problem and the three problems with OCR. Read it alongside Optical Character Recognition, which the paper treats as the thing to avoid, and VQA: Visual Question Answering for the question-answering task.
- Section 2.1, Background. Earlier OCR-free and OCR-based models. It names BERT: Pre-training of Deep Bidirectional Transformers and LayoutLM: Pre-training of Text and Layout for Document Image Understanding as the main text-based baselines, so that is the comparison to keep in mind.
- Section 2.2, Architecture. The encoder and decoder. Read with Attention Is All You Need for the base architecture, Swin Transformer: Hierarchical Vision Transformer using Shifted Windows for the image encoder, Vision Transformer (ViT) for the alternative of splitting images into patches with a plain Transformer, BART: Denoising Sequence-to-Sequence Pre-training for Text Generation for the decoder, and Encoder-Decoder Architecture for the pattern as a whole. Cross-attention is the mechanism in Attention Mechanism.
- Section 2.2, Model input and output. The prompt tokens and the JSON-like output. The output format is an invertible mapping, so you can recover the structure from the tokens.
- Section 2.3, Pre-training. Reading order, the next-token loss from Cross-Entropy Loss, and the synthetic generator SynthDoG. The text is split into pieces by Subword Tokenization.
- Section 2.4, Fine-tuning. The same loss with a task prompt, as in Fine-Tuning.
- Section 3, Experiments. The datasets and the comparisons. Each table has its own metric, so read the captions carefully. Section 3.4 shows the ablations, including the attention maps.
- Appendix A.2, SynthDoG. How synthetic pages are generated, which is what makes the multilingual setting possible.
Check yourself
QuestionIn one sentence, what does Donut skip compared with the usual pipeline, and why does the paper think that helps?Show answer
QuestionUsing the RVL-CDIP numbers in the text, how many milliseconds faster per image is Donut than LayoutLMv2, and what is the ratio of the two times to two decimal places?Show answer
QuestionA predicted tree needs 3 edits to match the ground truth, and an empty tree would need 12 edits. What does the paper's accuracy score give? Show the computation.Show answer
QuestionYou want Donut to extract the invoice number from a scanned invoice. Describe what the decoder receives and what it outputs, step by step.Show answer
Sources
- Geewook Kim et al., OCR-Free Document Understanding Transformer, arXiv:2111.15664, 2022. arxiv.org/abs/2111.15664
- Official code, pre-trained model and synthetic data for Donut. github.com/clovaai/donut