Document layout detection, tested on DocLayNet
1,734 of 1,903
regions found by Docling's Heron model
Heron found the most regions
| Model | Found of 1,903 | False boxes |
|---|---|---|
| Heron | 1,734 | 509 |
| Aryn Deformable DETR | 1,618 | 82 |
| cmarkea DETR | 1,574 | 851 |
| PP-DocLayoutV3 | 1,509 | 422 |
| DocLayout-YOLO | 1,087 | 252 |
People drew the 1,903 regions: paragraphs, headings, tables, pictures and more. A region counts as found when a box of the same kind covers it with IoU 0.5 or more. A false box matches no region.
Want fewer false boxes? Aryn's model drew about a sixth as many as Heron, and still found 1,618.
Heron is the model inside Docling. All five are free on Hugging Face. DocLayout-YOLO's code is AGPL-3.0, the rest Apache-2.0.
Scores at each model card's threshold
| Model, threshold | Found | False boxes |
|---|---|---|
| Heron, 0.6 | 1,694 | 308 |
| PP-DocLayoutV3, 0.5 | 1,509 | 422 |
| Aryn Deformable DETR, 0.7 | 1,409 | 35 |
| cmarkea DETR, 0.4 | 1,593 | 1,099 |
| DocLayout-YOLO, 0.2 | 1,224 | 453 |
Tables were the easy part
| Model | Tables of 39 | Pictures of 132 |
|---|---|---|
| Heron | 35 | 114 |
| PP-DocLayoutV3 | 33 | 80 |
| cmarkea DETR | 33 | 93 |
| Aryn Deformable DETR | 31 | 91 |
| DocLayout-YOLO | 28 | 30 |
Every model found most tables. Pictures split them, from logos to chemical drawings.
Headings: Heron found 210 of 233, Aryn and cmarkea 198 each.
Two models follow other box rules
Heron, Aryn and cmarkea learned from DocLayNet, so they draw boxes the way its people did. PP-DocLayoutV3 learned from 38,000 in-house pages, and DocLayout-YOLO from DocStructBench.
So some misses are rules, not mistakes. PP-DocLayoutV3 also boxes math inside a sentence, 239 boxes that DocLayNet does not have. We left them out of the score.
DocLayout-YOLO called 30 of 132 pictures a figure, and 18 logos in page headers and footers "abandon", its label for page clutter. It left 79 with no box.
If your pages are not like DocLayNet's, test a few models on 10 of your own pages first.
Heron was the fastest on a CPU
| Model | Seconds a page |
|---|---|
| Heron | 1.7 |
| PP-DocLayoutV3 | 2.3 |
| Aryn Deformable DETR | 3.6 |
| DocLayout-YOLO | 4.0 |
| cmarkea DETR | 53 |
cmarkea's model also computes a mask for every region, even when you only want boxes. On the Mac GPU, every model took a median of under 0.2 seconds a page.
A free layout script in Python
layout.py runs Heron at its card's threshold, 0.6, and drops boxes that overlap a box with a higher score. On the test pages: 1,688 found, 271 false boxes.
pip install torch transformers pillow pypdfium2
python layout.py report.pdf --crops crops > layout.tsv
Each line is one box: file, page, label, score and corners in pixels. --crops saves each table and picture as a PNG.
For the page below it printed, among others:
pat-15.png 1 section_header 0.78 162 484 459 494
pat-15.png 1 picture 0.77 129 541 482 679
Show the code (75 lines)
"""Find the layout of a page: titles, headings, text, lists, tables, pictures, page headers and footers.
Free, from brasspage.com.
Usage: python layout.py page.png [more.pdf ...] [--crops DIR]
Needs: pip install torch transformers pillow pypdfium2
Runs heron, Docling's layout model (docling-project/docling-layout-heron, Apache-2.0), on each image or PDF page.
Prints one line per box, top to bottom: file, page, label, score, x0 y0 x1 y1 (pixels; PDFs at 144 dpi).
With --crops DIR it also saves each table and picture as a PNG.
"""
import sys
from pathlib import Path
import pypdfium2 as pdfium
import torch
from PIL import Image
from transformers import AutoImageProcessor, AutoModelForObjectDetection
MODEL = "docling-project/docling-layout-heron" # or "Aryn/deformable-detr-DocLayNet": fewer false boxes, slower
THRESHOLD = 0.6 # the model card's
OVERLAP = 0.5 # drop a box that overlaps a box with a higher score this much (IoU)
def pages(path):
if path.suffix.lower() == ".pdf":
for n, page in enumerate(pdfium.PdfDocument(path), 1):
yield n, page.render(scale=2).to_pil().convert("RGB")
else:
yield 1, Image.open(path).convert("RGB")
def iou(a, b):
w = min(a[2], b[2]) - max(a[0], b[0])
h = min(a[3], b[3]) - max(a[1], b[1])
if w <= 0 or h <= 0:
return 0.0
inter = w * h
return inter / (
(a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter
)
args = sys.argv[1:]
crops = None
if "--crops" in args:
i = args.index("--crops")
crops = Path(args[i + 1])
del args[i : i + 2]
crops.mkdir(parents=True, exist_ok=True)
dev = "cpu"
if torch.cuda.is_available():
dev = "cuda"
elif torch.backends.mps.is_available():
dev = "mps"
proc = AutoImageProcessor.from_pretrained(MODEL)
model = AutoModelForObjectDetection.from_pretrained(MODEL).eval().to(dev)
for f in map(Path, args):
for n, im in pages(f):
with torch.inference_mode():
out = model(**proc(images=im, return_tensors="pt").to(dev))
r = proc.post_process_object_detection(
out, threshold=THRESHOLD, target_sizes=[im.size[::-1]]
)[0]
found = sorted(
zip(r["scores"].tolist(), r["labels"].tolist(), r["boxes"].tolist()),
reverse=True,
)
keep = []
for score, label, box in found:
if all(iou(box, k[2]) < OVERLAP for k in keep):
keep.append((score, model.config.id2label[label], box))
keep.sort(key=lambda k: (k[2][1], k[2][0]))
for i, (score, label, box) in enumerate(keep):
print(f, n, label, f"{score:.2f}", *(round(v) for v in box), sep="\t")
if crops and label in ("table", "picture"):
im.crop(box).save(crops / f"{f.stem}-p{n}-{i}-{label}.png")
How we tested
- Pages: the test split of DocLayNet v1.2 (CDLA-Permissive-1.0), 20 pages of each of its 6 kinds: financial reports, scientific articles, laws, tenders, manuals and patents.
- A seeded random pick, one page per source PDF before a second one: 120 pages from 103 PDFs, as the dataset's own 1025 by 1025 PNG images.
- Every label of every model goes into one of 6 groups: tables, pictures, headings, text (paragraphs, lists, captions, footnotes), formulas, and page headers and footers. Labels outside them, such as math inside a sentence, are left out.
- Found: a box of the same group at IoU 0.5 or more, one box per region, best overlap first. Boxes with a score of 0.5 or more, for every model.
- Each model as its card runs it, with no Docling or PaddleX clean-up steps. transformers 5.23.0 and torch 2.15.0 for four, doclayout-yolo 0.0.4 at 1024 pixels for DocLayout-YOLO.
- Boxes from the Mac GPU. Speed on 6 pages, one of each kind, with other jobs running. On those pages the CPU drew the same boxes. Apple M5 Max, macOS 26.6.
- Heron's report evaluates on the DocLayNet test split. It does not say in words that those pages were left out of training.
- Not tested: LayoutParser, Surya, PaddleX's full layout pipeline, LLMs and paid APIs.
Scanned PDFs to Markdown, no install
Run it yourself. Our Apify Actor turns scanned PDFs and images into Markdown with headings and tables. $0.006 a scanned page.
It writes text, not boxes. On the 31 test pages with a table, its Markdown had a table on 21.
It also made a table on 33 of the 89 pages with none, 16 of them on a page with a picture.
Questions people ask
What is the best free document layout detection model?
On our 120 DocLayNet test pages, Heron, the layout model inside Docling, found the most regions: 1,734 of 1,903. Aryn's Deformable DETR drew the fewest false boxes: 82.
What is DocLayNet?
A dataset of 80,863 PDF pages from 6 kinds of documents, with boxes drawn by people in 11 classes. Its license is CDLA-Permissive-1.0, and its test split has 4,999 pages.
What is the difference between DocLayNet and PubLayNet?
PubLayNet has over 360,000 pages from medical journal articles, labeled by a program in 5 classes. DocLayNet has fewer pages, but people drew its boxes, in 11 classes, on 6 kinds of documents.
Is LayoutParser still a good choice?
Its last release, 0.3.4, came out on April 6, 2022. Its model files on Hugging Face carry no license, so we did not run them.
Does DocLayout-YOLO run on a Mac?
Yes. doclayout-yolo 0.0.4 ran on the CPU, about 4 seconds a page, and on the Mac GPU. Its code is AGPL-3.0.