Navigation

Introduction to AI

Machine Learning

Deep Learning

Generative AI

Tools & Frameworks

General

Document Layout Analysis and Visual Document Understanding

Modern enterprises run on documents: invoices, receipts, tax forms, insurance claims, medical records, and legal contracts. However, traditional Natural Language Processing (NLP) fails when applied to documents. A standard OCR engine converts a 2D invoice page into a flat 1D string of text tokens, destroying the critical spatial layout, reading order, table hierarchies, and visual alignments.

Visual Document Understanding (VDU) and Document Layout Analysis (DLA) bridge computer vision and language modeling. By jointly modeling text content, 2D bounding box coordinates, and visual appearance, modern document AI systems extract structured key-value entities and tabular data with high fidelity.


Why 1D NLP Fails on 2D Documents

Consider a two-column financial balance sheet or a multi-line invoice:

Invoice Layout (2D Visual Layout):
Item Description            Quantity    Unit Price    Total
Industrial Ball Bearing     50          $12.00        $600.00
Hydraulic Valve Seal        10          $45.00        $450.00

Naive 1D OCR Serialization (Reading Left-to-Right Across Columns):
"Item Description Quantity Unit Price Total Industrial Ball Bearing 50 $12.00 $600.00..."

If the OCR engine misinterprets multi-column boundaries, horizontal text lines merge erroneously into meaningless sentences. Furthermore, key-value pairs (such as a checkbox next to a waiver clause or a signature above a printed date line) can only be resolved by understanding 2D spatial proximity.


Multimodal Document Architectures

                           Document AI Paradigms

          ┌──────────────────────────┴──────────────────────────┐
          ▼                                                     ▼
  OCR-Based Multimodal Models                           OCR-Free Vision-to-Text Models
  • Requires external OCR engine (Tesseract/Paddle)     • Reads raw page images directly
  • Fuses: Text + 2D Bounding Boxes + Visual Patches    • Autoregressive Vision Transformer + Decoder
  • Examples: LayoutLMv1 / v2 / v3, BROS                 • Examples: Donut, Nougat

1. The LayoutLM Family (Text + 2D Layout + Visuals)

Developed by Microsoft Research, LayoutLM established the standard multimodal framework for document understanding:

Text Tokens:        [ "Total", "Due", ":", "$1,050" ]

2D Spatial Boxes:   [ (x0, y0, x1, y1) normalized to 0-1000 ]

Visual Image:       [ Document ResNet / ViT Patches ]


            [ Multimodal Transformer Layer ]
    (Cross-Attention across text, layout, and visual features)

2D Spatial Positional Embeddings

In addition to standard 1D sequence position embeddings ($1, 2, \dots, N$), LayoutLM introduces four separate embedding tables for the normalized 2D bounding box coordinates of each word:

$$\mathbf{e}{\text{layout}}(w) = \mathbf{E}{x_0}(x_0) + \mathbf{E}{y_0}(y_0) + \mathbf{E}{x_1}(x_1) + \mathbf{E}_{y_1}(y_1)$$

where $(x_0, y_0)$ is the top-left coordinate and $(x_1, y_1)$ is the bottom-right coordinate normalized to an integer scale of $[0, 1000]$.

Self-Supervised Pretraining Objectives

LayoutLM models are pretrained on millions of scanned business documents (IIT-CDIP) using three self-supervised objectives:

  1. Masked Visual-Language Modeling (MVLM): Masks words and forces the model to recover them using both surrounding text and 2D spatial layout.
  2. Text-Image Alignment: Predicts whether an image patch contains a corresponding text token.
  3. Text-Image Matching: Predicts whether an image slice matches the textual description.

2. OCR-Free Document Understanding: Donut & Nougat

Donut (Document Understanding Transformer) and Nougat (Neural Optical Grammar Auto-Encoder) eliminate external OCR engines entirely:

Scanned Document Image (H x W x 3)


      [ Swin Transformer Encoder ] ──► Visual Token Representations


 [ mBART Autoregressive Text Decoder ]


  Structured Output: JSON string / Markdown table directly!
  {"invoice_number": "INV-9821", "total_amount": 1050.00}

Benefits of the OCR-Free Approach:

  • Zero OCR Cascading Errors: Traditional OCR engines fail on skewed scans, low-contrast stamps, or cursive handwriting. An error in Stage 1 permanently corrupts Stage 2.
  • Speed: Eliminates computationally expensive text-detection and polygon-cropping routines.
  • Direct Structured JSON Generation: The decoder emits structured JSON syntax directly using constrained decoding.

Key Downstream Tasks in Document AI

TaskObjectiveReal-World Application
Document Layout Analysis (DLA)Detects bounding boxes of page regions (Header, Paragraph, Table, Figure)Automated PDF restructuring, accessibility screen readers
Key Information Extraction (KIE)Identifies semantic entities (Total, Due Date, Tax ID)Automated accounts payable invoice processing
Table Structure Recognition (TSR)Reconstructs HTML row/column span hierarchy of grid cellsFinancial earnings statement extraction
Document ClassificationClassifies whole document types (Passport, Paystub, W-2, Bank Statement)Automated mortgage loan approval pipelines

Practical Extraction with LayoutLMv3

from transformers import AutoProcessor, AutoModelForTokenClassification
from PIL import Image

# Load processor and fine-tuned LayoutLMv3 model for receipt extraction
processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=True)
model = AutoModelForTokenClassification.from_pretrained("nielsr/layoutlmv3-finetuned-cord")

image = Image.open("receipt.png").convert("RGB")

# Processor runs internal OCR, computes 2D bounding boxes, and normalizes coordinates
inputs = processor(image, return_tensors="pt")

outputs = model(**inputs)
predictions = outputs.logits.argmax(-1).squeeze().tolist()
tokens = processor.tokenizer.convert_ids_to_tokens(inputs["input_ids"].squeeze().tolist())

# Map predictions to entity classes (e.g., 'MENU.PRICE', 'TOTAL.CASH')
for token, pred in zip(tokens, predictions):
    label = model.config.id2label[pred]
    if label != "O":
        print(f"{token:<15} -> {label}")

Key Takeaways

  • 2D spatial coordinates and visual styling are as critical as text tokens when parsing business documents.
  • LayoutLM bridges this divide by injecting 2D spatial coordinate embeddings directly into multi-head self-attention layers.
  • OCR-free vision-to-text models (Donut, Nougat) streamline the pipeline by generating structured JSON or LaTeX tables directly from raw pixels.