DTrOCR: Decoder-only Transformer for Optical Character Recognition
- Publication
- WACV 2024Conference
- First submitted to arXiv
- arXiv:2308.15996
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{textrecognitiondtrocr2023,
title = {{DTrOCR: Decoder-only Transformer for Optical Character Recognition}},
author = {Masato Fujitake},
year = {2023},
eprint = {2308.15996},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2308.15996}
}
This paper was authored by a Japanese OCR company, which is an uncommon group of authors in this field.
The model is named D-TrOCR, where "Decoder-only" is the key concept.
When we first saw it, we couldn’t help but think of pronouncing it as "Doctor." (?)
Problem Definition
We previously reviewed TrOCR, and you might still recall some of it.
TrOCR extensively employs a pretraining architecture, combining encoder and decoder models to create a complete OCR processing pipeline.
However, the authors of this paper questioned the necessity of the encoder model for OCR tasks:
- What if we only need the decoder to accomplish OCR?
Does this idea sound familiar?
The idea recalls the decoder-only architecture of GPT. DTrOCR tests whether the same architectural direction can handle text generation conditioned on an image.
When writing papers, paying homage to groundbreaking ideas by applying them to different fields can often result in something remarkable. This might just be one of those cases!
Problem Solving
Model Architecture
In simple terms, the encoder component is removed, and the image input is directly fed into the decoder.
The input image is processed through the Patch Embedding module, where it is divided into small patches and converted into vectors. Positional encodings are added to these input vectors, which are then passed to the decoder for text generation. This process is similar to the approach used in ViT.
The decoder receives these vectors and appends a special token [SEP] at the end. It then begins generating text starting from [SEP] and continues until it encounters [EOS], indicating the end.
Text is represented by subword tokens (BPE in the implementation), and Beam Search is employed to produce the final text output.
The decoder architecture directly utilizes the pre-trained GPT-2 model, eliminating the need for training from scratch and saving a significant amount of time.
Synthetic Dataset Pretraining
The original language model is trained on large-scale text datasets, but such models are not suitable for handling OCR tasks. This is because the model lacks the ability to recognize image features and cannot associate images with text.
To address this, the authors further pre-trained the model using artificially synthesized datasets. These datasets include various types of text, such as scene text, handwritten text, and printed text.
In the TrOCR paper, the training data was annotated using PDF files and commercial OCR systems. However, this data preparation process is time-consuming and labor-intensive. Instead, the authors opted for text corpora to generate synthetic data for better reproducibility. The relevant corpora include PILE, CC100, and Chinese NLP Corpus.
The process of generating synthetic data is as follows:
- Three open-source libraries were used to generate synthetic datasets, splitting the text corpus into three categories: scene text, printed text, and handwritten text recognition, with a distribution of 60%, 20%, and 20%, respectively.
- For scene text recognition, 4 billion horizontal text images and 2 billion vertical text images were generated using SynthTIGER, utilizing default fonts for English and 64 commonly used fonts for Chinese.
- Using SynthTIGER's multi-line text image configuration and the MJSynth and SynthText corpora, the authors generated 100 million multi-line English text images with the word count set to five.
- For printed text recognition, 2 billion images were generated using the default settings of TextRender.
- For handwritten text recognition, 2 billion images were generated using TRDG, leveraging 5,427 English handwriting fonts and 4 Chinese handwriting fonts.
Real Data Fine-Tuning
To enhance the model's adaptability in real-world environments, fine-tuning with real datasets is still necessary.
The real datasets used in the paper include: COCO-Text, RCTW, Uber-Text, ArT, LSVT, MLT19, and ReCTS.
Implementation Details
-
Model Configuration
-
Decoder Model:
- Utilized English GPT-2 and Chinese GPT-2 models.
- Model specifications:
- 12-layer Transformer.
- Each layer has a 768-dimensional hidden size.
- 12 attention heads in the multi-head attention mechanism.
- Used Byte Pair Encoding (BPE) vocabulary.
-
Image Patch Embedding:
- Following prior research, an 8 × 4 patch size was used.
- Relative positional encoding was applied.
- Maximum token length was set to 512.
-
-
Training Settings
-
Pretraining:
- English model: Pretrained on English datasets.
- Chinese model: Pretrained on a combination of Chinese and English datasets.
- Training parameters:
- 1 epoch with a batch size of 32.
- AdamW optimizer with a learning rate of .
-
Fine-Tuning:
- Initialized with pretrained weights.
- Fine-tuned on target datasets:
- Most datasets were trained for 1 epoch with a learning rate of .
- The SROIE dataset was trained for 4 epochs.
- The same optimizer and batch size as in pretraining were used.
-
-
Data Augmentation
- Applied RandAugment (excluding the "Sharpness" augmentation).
- Added the following augmentations:
- Invert: Color inversion.
- Gaussian Blur: Apply Gaussian blurring.
- Poisson Noise: Add Poisson noise.
- RandAugment configuration:
- Three-layer policy.
- Augmentation magnitude set to 5.
- All images were resized to 128 × 32 pixels.
- Retained the original image orientation and applied random rotations:
- Keep original orientation (95% probability).
- Rotate 90 degrees clockwise (2.5% probability).
- Rotate 90 degrees counterclockwise (2.5% probability).
- Normalized images to the range .
Discussion
STR Comparison
The table groups methods by training on synthetic or real data. For every reported benchmark result within each group, DTrOCR has higher word accuracy than the listed methods. Their training data and settings differ, so this table alone cannot attribute the gains to the decoder architecture.
TrOCR, ABINet, PARSeq, and MaskOCR combine visual encoding and sequence modeling in different ways. DTrOCR achieves the reported accuracy without a separate visual encoder.
In the examples below, DTrOCR correctly reads occluded or irregular text and the two-line text. The other two methods make errors on these examples.
SROIE Comparison
The table reports word-level recall, precision, and F1 on SROIE Task 2. DTrOCR uses no separate CNN or ViT visual encoder and scores higher than the listed methods on all three measures.
Handwritten Performance
The handwriting evaluation uses the IAM Handwriting Database, an area with fewer studies. The table includes Diaz et al., who use internal annotated data and an external language model, and the Transformer-based TrOCR. Their training conditions differ, so those conditions matter when interpreting the results.
DTrOCR uses synthetic data and the IAM training set without an external language model. It has the lowest character error rate in the table (CER 2.38; lower is better).
CTR Performance
The Chinese benchmark tests include four subsets: scene, web, document, and handwritten text.
DTrOCR has higher word accuracy than the listed methods on all four subsets. The margins are larger for scene, web, and handwriting text; on documents, it leads the next-best result by 0.5 percentage points.
TrOCR uses a decoder initialized from masked-language-model (MLM) pretraining, and MaskOCR also uses MLM-based pretraining. DTrOCR instead uses generative pretraining to predict the next text token. TrOCR still generates OCR output autoregressively; the contrast here concerns pretraining, not whether its final OCR output is autoregressive.
The results support this decoder-only design for the evaluated Chinese recognition tasks; they do not isolate generative pretraining as the sole cause of the gains.
Conclusion
In this paper, the authors presented an extremely simple OCR model structure, demonstrating that OCR tasks can be accomplished effectively using only a decoder model. They validated the effectiveness of this approach.
Across multiple benchmarks, DTrOCR consistently surpassed state-of-the-art methods in accuracy and exhibited strong generalization capabilities. Moving forward, this model might evolve along the path of large language models (LLMs), focusing on improvements in speed and performance.