OvisOCR3-Arabic-workdir / code /docs /architectures.md
aziz9788's picture
Initial commit
cf8c8d4
|
Raw History Blame Contribute Delete
16.8 kB

OCR architectures: how they differ and how to choose

This guide explains the architectures in this toolkit: how each one reads an image, how it writes text, how it is trained, and where it is strong or weak. Paper numbers are as reported by the authors; benchmark versions differ between papers, so compare scores only within the same benchmark version.

Contents

  1. Building blocks every OCR model shares
  2. The four families
  3. Side-by-side comparison
  4. Architecture by architecture
  5. Training recipes
  6. Choosing an architecture

1. Building blocks

1.1 How a model sees the page

Modern OCR models cut the image into square patches (14 or 16 px), turn each patch into a vector, and let a vision encoder (usually a Vision Transformer) mix information between patches. Most then merge neighbouring patches (typically 2×2) into one vision token. One token therefore covers 28×28 px (patch 14) or 32×32 px (patch 16).

The number of vision tokens is the central trade-off:

  • More tokens keep small glyphs, accents, subscripts and dense tables legible, but cost memory and time (attention cost grows with sequence length).
  • Fewer tokens (heavy compression) are fast and cheap, but fine detail can be lost before the decoder ever sees it.

Three resolution strategies appear in this toolkit:

Strategy Used by Effect
Native / dynamic resolution, token budget OvisOCR, Qwen-VL family, MinerU-Diffusion, PaddleOCR-VL, dots.ocr Page keeps its aspect ratio; token count scales with pixels up to a cap
Fixed input size Nougat (896×672), GOT-OCR (1024×1024 → 256 tokens) Simple and predictable; long or wide pages are squashed or tiled
Learned compression DeepSeek-OCR (16× compressor), GOT-OCR Very few tokens per page; accuracy drops as compression rises

1.2 How a model writes text

Decoder type How it works Used by Trade-off
Autoregressive LLM Writes one token at a time, each conditioned on all previous tokens Families A Best accuracy and structure; slow for long pages; can loop
Block diffusion Fills a block of masked tokens (e.g. 32) over a few parallel passes, block after block Family B Fewer decoding steps; less mature tooling
CTC Predicts a character or blank per horizontal position of a line, then collapses repeats Family C Very fast and small; one line at a time, no structure
Attention (seq2seq) head Small decoder over line features PP-OCRv6 (NRTR head), Nougat Handles longer dependencies than CTC; slower

1.3 Tokenizers

An LLM decoder writes tokens, not characters. A larger vocabulary usually means fewer tokens per page, which means faster decoding, less memory and fewer chances to drift on long pages. In this toolkit the Qwen3.5 tokenizer (248,320 entries, OvisOCR) is the largest; most other Qwen-based models use 151,936. When comparing models on your data, measure tokens per character on your own labels: the difference can be 15–25%.

1.4 Learning objectives

Objective What is optimised Where
Next-token cross-entropy (SFT) Probability of each correct target token given image + previous tokens; prompt and image tokens are masked out Every family A model, common.vlm_sft
CTC loss Sum over all alignments of predicted characters/blanks to the target string PP-OCRv6 recognizer
Masked-token loss Recover masked tokens inside a block Block-diffusion models (family B)
Segmentation losses (Dice, focal) Pixel maps of text regions PP-OCRv6 detector
Distillation (forward / reverse KL) Match a teacher's token distribution, not just the final text OvisOCR3 (on-policy, from 4B teachers)
RL (GRPO) Sample several outputs per page, score them with a reward, reinforce the above-average ones FD-RL, OvisOCR3, olmOCR 2

Typical OCR rewards for RL: edit distance to the ground truth, TEDS (table tree-edit similarity), CDM (formula similarity), format checks, and unit tests on synthetic pages (olmOCR 2). RL fixes what cross-entropy is blind to: decoding loops, broken table markup, wrong reading order. It needs an SFT model to start from.


2. The four families

A. Autoregressive end-to-end VLMs. One model reads the whole page and writes the whole page as Markdown (tables as HTML, formulas as LaTeX). Current state of the art. Members: OvisOCR2/3, FD-RL, olmOCR 2, GOT-OCR 2.0, Nougat, PaddleOCR-VL, dots.ocr, DeepSeek-OCR.

B. Block-diffusion VLMs. Same input and output as A, but the decoder generates blocks of tokens in parallel. Research stage. Members: MinerU-Diffusion, DODO.

C. Detector + recognizer pipelines. A detector finds text lines, a recognizer reads each line. No language model. Tiny and fast; no document structure. Member: PP-OCRv6.

D. Wrappers. Not a model: combine several models' outputs. Member: Consensus Entropy.


3. Side-by-side comparison

Vision encoder px per token Decoder Vocab Output Train code Weights
OvisOCR3 ViT 12 layers, patch 16, merge 2 32×32 Qwen3.5-0.8B (hybrid linear + full attention) 248k Markdown, HTML tables, LaTeX, boxes, JSON by prompt No Yes
FD-RL Qwen3-VL ViT 24 layers, DeepStack 32×32 Qwen3-VL-4B 152k Markdown, HTML, LaTeX SFT cmd only Yes
olmOCR 2 Qwen2.5-VL ViT 32 layers, windowed attention 28×28 Qwen2.5-VL-7B 152k Markdown + YAML front matter Yes Yes
GOT-OCR 2.0 ViTDet-style, high compression 1024² → 256 tokens Qwen-0.5B 152k Text, Markdown, LaTeX, TikZ Yes Yes
Nougat Swin Transformer fixed 896×672 mBART 10 layers (from scratch) 50k Markdown + LaTeX Yes Yes
MinerU-Diffusion Qwen2-VL ViT 32 layers 28×28, ≤2,048 tokens SDAR-1.7B block diffusion (block 32) 152k Layout boxes, text, LaTeX, OTSL tables No Yes
DODO Qwen2.5-VL ViT 28×28 Qwen2.5-VL-3B converted to block diffusion 152k Text No No
PP-OCRv6 LCNetV4 CNN + SVTR neck line crops 32–64 px tall CTC + NRTR heads per dictionary Polygons + strings Yes Yes
PaddleOCR-VL NaViT-style dynamic resolution native ERNIE-4.5-0.3B (+ separate layout model) — Per-region text, tables, formulas Not verified Yes
dots.ocr Encoder trained from scratch native ~1.5B LLM — Layout JSON + text No Yes
DeepSeek-OCR DeepEncoder: local attn → 16× compressor → global attn 64–400+ tokens by mode DeepSeek-3B MoE (~0.57B active) — Markdown, text No Yes

4. Architecture by architecture

OvisOCR2 / OvisOCR3 — smallest strong end-to-end model

  • Design. 0.85B parameters. Vision tower of 12 layers (patch 16, merge 2) feeding Qwen3.5-0.8B (24 layers, linear attention mixed with full attention, which keeps long outputs cheap). Pages from 448² to 2,880² px, about 200 to 8,100 vision tokens.
  • Training (OvisOCR3 report). (1) Continued pretraining on ~72M examples, all modules trained. (2) Mid-training on ~8.8M examples balanced across five domains. (3) On-policy distillation from five domain-specific 4B teachers (top-K reverse KL: the student writes, the teachers score each token). (4) GRPO with edit-distance, TEDS and CDM rewards.
  • Strengths. Top of OmniDocBench v1.6 (~96.7) at under 1B parameters; largest vocabulary; one model for parsing, plain OCR, layout JSON and key-information extraction by prompt; LoRA fits an 8 GB GPU.
  • Limitations. No training code or data released; our SFT is not the authors' recipe. Can loop on long repetitive lists. Linear-attention fast kernels are Linux-only.

FD-RL — the clearest published training recipe

  • Design. Qwen3-VL-4B: 24-layer vision encoder with DeepStack (vision features injected into several decoder layers).
  • Training. SFT on 566k pages (240k open, 208k real PDF, 118k synthetic), 1 epoch, lr 1e-5, vision frozen, 8,192 tokens, via LLaMA-Factory. Then GRPO on only ~8k format-heavy samples: 16k candidates, keep the half where the model is most uncertain (highest entropy).
  • Strengths. Simple, reproducible SFT; shows that a small, uncertainty-selected RL set gives large gains on structure.
  • Limitations. RL code and data not released. 4B is too large for full fine-tuning on small GPUs. OmniDocBench ~90.4 (older version).

olmOCR 2 — the most open recipe

  • Design. Qwen2.5-VL-7B; pages rendered from PDF at 1,288 px longest side; output Markdown with YAML front matter.
  • Training. SFT on the open olmOCR-mix data (labels produced by a stronger model), then RL with verifiable rewards: synthetic pages with known content and unit tests per page (reading order, table cells, presence of text).
  • Strengths. Code, data and weights all open; unit-test rewards catch errors that edit distance misses.
  • Limitations. 7B needs ≥40 GB for LoRA at 8k context; Linux + flash-attn; pseudo-labels in the SFT data; English-centric.

Qwen-VL official trainer — the shared base

Most family A models are fine-tuned Qwen VL models (Qwen3.5 for OvisOCR, Qwen3-VL for FD-RL, Qwen2.5-VL for olmOCR and DODO). Qwen's own qwen-vl-finetune trains any of these bases the same way. Use it to build your own OCR model from a general base with full control.

GOT-OCR 2.0 — compact unified OCR

  • Design. 560M: high-compression encoder (1024×1024 → 256 tokens) + Qwen-0.5B decoder.
  • Training. Three stages: pretrain the encoder with a tiny decoder on text images; connect the real decoder and train jointly; post-train for fine-grained regions, multi-page and dynamic resolution.
  • Strengths. Open training code, small, handles text, formulas, sheet music, charts.
  • Limitations. 256 tokens per crop limits dense pages and very small glyphs; behind current leaders.

Nougat — the original page-to-Markdown model

  • Design. Swin encoder + mBART decoder trained from scratch on scientific PDFs; no pretrained LLM.
  • Strengths. Fully open trainer, small enough to train completely on one GPU.
  • Limitations. Prone to repetition loops; fixed input squashes pages; tokenizer targets English and LaTeX (other scripts need a new tokenizer); domain limited to academic papers.

MinerU-Diffusion — block-diffusion decoding

  • Design. Qwen2-VL vision tower (≤2,048 tokens) + SDAR-1.7B decoder that generates 32-token blocks by iterative unmasking.
  • Training. 550k image-caption alignment → 739k VQA → 6.9M layout/OCR samples for 9 epochs → 630k hard samples (chosen by inconsistent model outputs) for 4 epochs.
  • Strengths. Parallel decoding inside a block; the hard-sample stage is a reusable idea for any model.
  • Limitations. No training code; OmniDocBench v1.5 88.9, below autoregressive leaders; immature tooling.

DODO — converting an autoregressive VLM to diffusion

Qwen2.5-VL-3B converted to block diffusion with ~270k olmOCR-mix pages over 200k steps. Shows the conversion works with modest data. No code or weights released.

PP-OCRv6 — detector + recognizer

  • Design. Detector: LCNetV4 + RepLKPAN + DB head, trained on 640² crops (500 epochs, Dice + focal). Recognizer: LCNetV4 + light SVTR neck + CTC and NRTR heads on 32–64 px-tall lines (100 epochs). 1–19M parameters.
  • Strengths. Very fast, runs on CPU or edge devices, full training code; ideal for line-level data.
  • Limitations. The released dictionary covers a fixed character set and max_text_length is 25, so new scripts or long lines require a new dictionary and retraining (handled by make_config.py). No tables, Markdown or page reading order.

PaddleOCR-VL — layout model + compact VLM

A layout model first finds regions (text, tables, formulas, figures); a 0.9B VLM (dynamic-resolution encoder + ERNIE-4.5-0.3B) reads each region. Robust on complex layouts and broadly multilingual, but two models to tune, and reading-order errors in the layout step propagate.

dots.ocr — layout and text in one pass

A ~3B model that outputs layout boxes, categories and text in one generation. Strong multilingual parsing; no training code.

DeepSeek-OCR — optical compression

DeepEncoder (local windowed attention → 16× convolutional compressor → global attention) feeding a DeepSeek-3B mixture-of-experts decoder (~0.57B active). Shows a page can be represented with ~10× fewer vision tokens than its text tokens at high precision. Efficient for large batches; compression hurts small glyphs and marks; MoE is awkward to fine-tune on small GPUs; no training code.

Consensus Entropy — agreement as a quality signal

Run several models on the same page and measure agreement. High agreement → likely correct; low → hard page or wrong output. Use it to filter pseudo-labels, select hard samples, or route pages to a stronger model. Costs several inferences per page; shared blind spots still pass.


5. Training recipes

Runnable versions of the data-efficient recipes below (self-play, GRPO with format rewards, hard-case selection, merging) are described in finetuning_recipes.md.

The leading models share one shape. Each stage is optional; later stages need earlier ones.

Stage Data Objective Examples
1. Alignment / pretraining Millions of mixed image–text pairs Cross-entropy OvisOCR3 (~72M), MinerU (550k + 739k)
2. Main SFT Page images + exact Markdown Cross-entropy on answer tokens FD-RL (566k), MinerU (6.9M), olmOCR
3. Hard samples / mid-training Pages the model gets wrong or disagrees on Cross-entropy MinerU (630k), OvisOCR3 (8.8M balanced)
4. Distillation Teacher scores on the student's own outputs Reverse KL OvisOCR3
5. Reinforcement learning A few thousand format-heavy pages GRPO with edit distance / TEDS / CDM / unit tests FD-RL (~8k), OvisOCR3, olmOCR 2

Practical points:

  • Start from the strongest small model and fine-tune, rather than training from a general base, unless you have millions of pages.
  • LoRA vs full fine-tuning. LoRA trains small adapter matrices and keeps the base frozen: far less memory and less forgetting. Full fine-tuning adapts more but needs much more memory and data.
  • Forgetting. Mix 10–20% replay data the model already handles (other layouts, tables), and/or add a KL term to the frozen base model on replay pages. Training repeatedly on a model's own outputs without ground truth leads to collapse; always keep ground-truth labels in the loop.
  • Hard samples. After a first SFT pass, run the model on its training set and up-weight the pages with the highest error or lowest agreement (see Consensus Entropy).
  • Evaluate on real pages kept out of training (eval split), not only on rendered or synthetic ones.

6. Choosing an architecture

Answer four questions:

  1. Pages with structure, or single lines? Pages → family A. Lines only, or speed matters most → PP-OCRv6.
  2. What hardware? ≤8–12 GB: OvisOCR3 (LoRA), GOT-OCR, Nougat, PP-OCRv6. 24 GB: FD-RL LoRA, Qwen2.5-VL-3B. ≥40 GB or multi-GPU: olmOCR 2, full FD-RL SFT.
  3. Do you need the official recipe? Open trainers: olmOCR, GOT-OCR, Nougat, PaddleOCR, Qwen-VL, LLaMA-Factory (FD-RL SFT). Otherwise common.vlm_sft trains any Qwen-family model.
  4. How dense is the text? Small glyphs, marks or dense tables need many vision tokens: avoid heavily compressed encoders (DeepSeek-OCR, GOT-OCR) and give native-resolution models a generous pixel budget.
Situation Recommended start
General document parsing, small GPU OvisOCR3 + LoRA (01_ovisocr)
Reproducible end-to-end recipe, larger GPUs FD-RL SFT via LLaMA-Factory, then a small GRPO stage
Fully open pipeline including RL olmOCR 2
Line-level text, edge deployment, new character sets PP-OCRv6 with a custom dictionary
Complex multi-column layouts PaddleOCR-VL or dots.ocr (evaluate first)
Cleaning or selecting training data Consensus Entropy over two or three of the above
Research on decoding speed MinerU-Diffusion (inference)

A sound default plan: baseline OvisOCR3 on your eval split → LoRA SFT → hard-sample round → (optional) small GRPO stage on a larger GPU, comparing every step with common.score on the same eval split.