Metadata-Version: 2.4
Name: pp-doclayoutv4
Version: 0.1.0
Summary: PP-DocLayoutV4 document layout analysis on CPUs and GPUs from several makers: ONNX Runtime, OpenVINO or PaddlePaddle; 25 classes, four-point boxes, reading order
Author: OmniDoc
License: Apache-2.0
Project-URL: Homepage, https://github.com/omnidocai/PP-DocLayoutV4
Project-URL: Models, https://huggingface.co/omnidoc/PP-DocLayoutV4
Project-URL: Paper, https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.pdf
Keywords: document layout analysis,reading order,PP-DocLayoutV4,ONNX,OpenVINO,PaddlePaddle
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering :: Image Recognition
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: numpy>=1.24
Requires-Dist: opencv-python-headless>=4.8
Requires-Dist: PyMuPDF>=1.23
Provides-Extra: cpu
Requires-Dist: onnxruntime<2,>=1.18; extra == "cpu"
Provides-Extra: cuda
Requires-Dist: onnxruntime-gpu<2,>=1.18; extra == "cuda"
Provides-Extra: directml
Requires-Dist: onnxruntime-directml<2,>=1.18; extra == "directml"
Provides-Extra: openvino
Requires-Dist: openvino>=2024.4; extra == "openvino"
Provides-Extra: paddle
Requires-Dist: paddlepaddle<4,>=3.0; extra == "paddle"
Dynamic: license-file

# PP-DocLayoutV4

Document layout analysis with **PP-DocLayoutV4** on CPUs and GPUs from several makers: 25 layout classes, a four-point
box per region and the reading order of the page. One Python file runs the model on ONNX Runtime, OpenVINO or
PaddlePaddle (the official Paddle model); a Rust program runs it on ONNX Runtime. Every path returns the regions of
official Paddle inference.

**Paper**: [PP-DocLayoutV4 Everywhere: An ONNX Port with Box-Level Parity across CPUs and GPUs](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.pdf)
(English; [34 translations](#paper)) · **Models**: [huggingface.co/omnidoc/PP-DocLayoutV4](https://huggingface.co/omnidoc/PP-DocLayoutV4)
· **Website**: [omnidoc.top](https://omnidoc.top)

![Speed against PaddleX's official Python API: (a) V100, pages/s; (b) Xeon, s/page](https://raw.githubusercontent.com/omnidocai/PP-DocLayoutV4/main/docs/speed.svg)

One page at a time on one machine, the ONNX model against PaddleX's official Python API: **1.32x** on an NVIDIA V100
(14.4 against 10.9 pages/s), **1.42x** with ONNX Runtime and **1.36x** with OpenVINO on an Intel Xeon (4.61 and 4.80
against 6.53 s/page), with the regions of official Paddle inference (see *Results*).

| | runs | model file |
|---|---|---|
| Python, `--device cpu / cuda / dml / migraphx / rocm` | ONNX Runtime: x86-64 CPUs (Intel, AMD), NVIDIA GPUs, any DirectX 12 GPU on Windows, AMD GPUs on Linux | `PP-DocLayoutV4.onnx` |
| Python, `--device openvino[:CPU / GPU / NPU]` | OpenVINO: Intel CPUs, integrated and Arc GPUs, NPUs (AMD CPUs too) | `PP-DocLayoutV4.onnx` |
| Python, `--device paddle[:gpu]` | PaddlePaddle Inference, CPU or NVIDIA GPU | `PP-DocLayoutV4_infer/` (official Paddle model) |
| Rust, [`rust/`](rust/) | ONNX Runtime (CPU; CUDA, DirectML, OpenVINO with cargo features) | `PP-DocLayoutV4.onnx` |

Everything runs in FP32 (OpenVINO is told so: its GPU plugin would otherwise use FP16).

## Quick start

```bash
git clone https://github.com/omnidocai/PP-DocLayoutV4 && cd PP-DocLayoutV4
python download_model.py            # PP-DocLayoutV4.onnx from Hugging Face, SHA-256 checked (paddle / all for the Paddle model)
bash run.sh page.png scans/ paper.pdf -o out --vis                                 # CPU, ONNX Runtime
V4_RUNTIME=openvino bash run.sh scans/ -o out --device openvino                    # Intel CPU
V4_RUNTIME=openvino bash run.sh scans/ -o out --device openvino:GPU                # Intel GPU
V4_RUNTIME=cuda bash run.sh scans/ -o out --device cuda                            # NVIDIA GPU
V4_RUNTIME=paddle-gpu bash run.sh scans/ -o out --device paddle:gpu                # official Paddle model, NVIDIA GPU
```

Windows:

```bat
python download_model.py
run.bat page.png scans\ paper.pdf -o out --vis
set V4_RUNTIME=directml
run.bat scans\ -o out --device dml
```

The first run creates `.venv-<runtime>` next to the script and installs `requirements.txt` (numpy, OpenCV, PyMuPDF)
plus `requirements-<runtime>.txt` (`cpu`, `cuda`, `directml`, `openvino`, `paddle`, `paddle-gpu`); each runtime keeps
its own environment. Set `PIP_INDEX_URL` to use a pip mirror and `HF_ENDPOINT` (e.g. `https://hf-mirror.com`) for a
Hugging Face mirror. `pip install ".[cpu]"` (or `[cuda]`, `[openvino]`, `[directml]`, `[paddle]`) installs the package
with the commands `pp-doclayoutv4` and `pp-doclayoutv4-download`.

Rust:

```bash
cd rust && cargo build --release
target/release/pp-doclayoutv4 page.png scans/ -o out --model ../PP-DocLayoutV4.onnx
```

### AMD GPUs (Linux)

ONNX Runtime's AMD builds are published by AMD with ROCm rather than on PyPI. Install ROCm and ONNX Runtime with the
MIGraphX execution provider as AMD's ROCm documentation describes, then `pip install -r requirements.txt` in the same
environment and run with `--device migraphx` (older builds: `--device rocm`). The script checks that the runtime has
the execution provider it needs and says which package to install if not. On Windows, AMD GPUs use `--device dml`.

## In your own code

```python
import v4_layout as v4

model = v4.open_model(device="openvino")   # load once; "cpu", "cuda", "dml", "paddle", "paddle:gpu", ...
bgr = v4.load_page("page.png", None, 150)
h, w = bgr.shape[:2]
boxes = v4.postprocess(model.run(v4.preprocess(bgr)[None], [(h, w)]), 0, h, w, conf=0.5)
for b in boxes:
    print(b["read_index"], b["class_name"], b["conf"], b["box"])
```

`example.py` does the same from the command line: `python example.py page.png --device openvino`. The Rust library
(`pp_doclayoutv4::Model`, `load_page`, `detect`) is described in [`rust/README.md`](rust/README.md).

## Output

One JSON per image (`out/<name>.json`) or PDF page (`out/<name>_p0001.json`), the same from Python and Rust:

```json
{"source": "paper.pdf", "page": 1, "width": 1275, "height": 1650, "conf_thres": 0.5, "device": "cpu",
 "detections": [
   {"read_index": 1, "class": 17, "class_name": "paragraph_title", "conf": 0.93,
    "box": [150.0, 200.0, 600.0, 230.0], "quad": [[150.2, 199.8], [600.1, 200.0], [600.0, 230.1], [150.1, 230.0]]}
 ]}
```

Detections come in reading order; `box` is the axis-aligned rectangle in page pixels, `quad` the model's four-point
box. `--vis` also writes `<name>.vis.jpg` with the boxes, reading order numbers, classes and scores drawn.

| option | default | |
|---|---|---|
| `-o / --out` | `v4_out` | output folder |
| `--model` | `PP-DocLayoutV4.onnx` or `PP-DocLayoutV4_infer/` in the current folder, beside the script or in `~/.cache/pp-doclayoutv4` | model file or Paddle model folder |
| `--device` | `cpu` | see the table above |
| `--conf` | 0.5 | score threshold |
| `--threads` | 0 (the runtime's default) | CPU threads per worker |
| `--workers` | 1 | pages in parallel, one model each |
| `--dpi` | 150 | PDF render resolution |
| `--vis` | off | draw the boxes |

## Pipeline

1. **Pre-processing**: decode, bicubic resize to 800 x 800, BGR to RGB, x / 255, NCHW float32; `im_shape` = 800,
   `scale_factor` = (800 / h, 800 / w).
2. **Network**: the official PP-DocLayoutV4 inference graph, in PaddlePaddle Inference or exported to ONNX with
   paddle2onnx 2.1.0 (opset 17, FP32). Inputs `image` [N, 3, 800, 800], `im_shape` [N, 2], `scale_factor` [N, 2];
   outputs: detection rows [M, 10] (class, score, four corners), rows per page [N], query indices [N, 300], relation
   and successor logits [N, 300, 300], [N, 5].
3. **Post-processing**: score threshold, reading order from the relation and successor logits (PaddleX's V4 decoding),
   rectangle NMS (0.6 within a class, 0.98 across classes), the official full-page image drop, sort by reading order,
   +0.5 px shift in the 800 x 800 frame, integer coordinates.

The Rust program does the same in Rust; its resize reproduces OpenCV's default cubic resize (which runs Intel IPP code
on x86) up to 0.0014% of the pixel values, and it decodes JPEG files with the `image` crate rather than libjpeg-turbo.

## Results

**Same regions as official Paddle.** 470 public pages (arXiv papers and DocLayNet pages), score threshold 0.5, the
post-processing above; reference: the official inference graph in Paddle Inference 3.3.1 on an NVIDIA V100, FP32.
Regions are matched one to one (same class, IoU >= 0.5).

| | ONNX Runtime CPU | OpenVINO CPU | ONNX Runtime CUDA | Rust (ONNX Runtime CPU) |
|---|---|---|---|---|
| regions (reference 9,845) | 9,845 | 9,845 | 9,845 | 9,845 |
| lost + added + reclassified | 0 | 0 | 0 | 0 |
| identical coordinates | 9,841 | 9,838 | 9,838 | 9,839 |
| off by one pixel | 4 | 7 | 7 | 6 |
| off by more than one pixel | 0 | 0 | 0 | 0 |
| reading-order swaps | 0 | 0 | 0 | 0 |
| pages identical in every region | 466 | 464 | 463 | 464 |

The one-pixel differences are coordinates within float rounding of a half-pixel boundary.

**Same accuracy as official Paddle on PaddleX's own demo dataset.** `det_layout_examples`, the layout dataset of the
PaddleX documentation (110 annotated Chinese pages, 1,185 boxes, the 10 CDLA classes; figure and table captions
evaluated together since V4 has one caption class):

| | official Paddle (V100) | Python `paddle:gpu` | ONNX Runtime CUDA | ONNX Runtime CPU | OpenVINO CPU | Rust |
|---|---|---|---|---|---|---|
| regions returned | 2,414 | 2,414 | 2,414 | 2,414 | 2,414 | 2,414 |
| lost + added + reclassified / reading-order swaps | | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 |
| identical coordinates (off by one pixel) | | 2,414 (0) | 2,413 (1) | 2,413 (1) | 2,413 (1) | 2,365 (49) |
| mAP@0.50:0.95 / mAP@0.50 / recall@0.50 (%) | 36.18 / 65.35 / 83.52 | 36.18 / 65.35 / 83.52 | 36.18 / 65.35 / 83.52 | 36.18 / 65.35 / 83.52 | 36.18 / 65.35 / 83.52 | 36.17 / 65.34 / 83.52 |

The Python Paddle path returns official Paddle's output bit for bit, scores included; the Rust program's one-pixel
differences on these JPEG pages come from its JPEG decoder.

**Speed.** One page at a time, from decoding to regions, medians of three interleaved rounds on one machine (NVIDIA
V100-SXM2 16 GB; Intel Xeon E5-2696 v4). The baseline is PaddleX's official Python API (`create_model` + `predict`);
no released PaddleX contains PP-DocLayoutV4 yet, so it runs PP-DocLayoutV3, the same family at the same input size.

| | V100 (pages/s) | Xeon, one socket (s/page) |
|---|---|---|
| PaddleX API, PP-DocLayoutV3 (CPU: its defaults, 10 threads) | 10.9 | 6.53 |
| this package, ONNX Runtime (CPU: 8 threads) | 14.4 | 4.61 |
| this package, OpenVINO (8 threads) | | 4.80 |

GPU processes run on the GPU's own CPU socket (`numactl --cpunodebind=0 --membind=0` on this two-socket machine):
unpinned, Paddle's threads spread over both sockets and the API ran at 6.1 to 7.1 pages/s. The Python Paddle path runs
at the official Paddle model's own speed (19.9 pages/s on the V100). Speeds on other hardware differ.

## Model files

On Hugging Face, [huggingface.co/omnidoc/PP-DocLayoutV4](https://huggingface.co/omnidoc/PP-DocLayoutV4);
`python download_model.py [onnx|paddle|all]` fetches them and checks their SHA-256:

| file | bytes | SHA-256 |
|---|---|---|
| `PP-DocLayoutV4.onnx` | 153,111,775 | `800db25f046af9395f7acde36c51a93fe2fce8019cfbb1bacb5a663d83a4c233` |
| `PP-DocLayoutV4_infer/inference.json` | 1,272,437 | `ab841469f943b754d4e93d43bf90899274d266b1f28e439197bc7199dd146d07` |
| `PP-DocLayoutV4_infer/inference.pdiparams` | 132,771,206 | `7d6057d36b99d2f8f8df39e173f10efd5eff512196620c7912b8d29172bc4f88` |
| `PP-DocLayoutV4_infer/inference.yml` | 1,482 | `12073ce49d0f7c96fc5ad5ebda2098267913b6b4a21fe453a9085765034ee408` |

The ONNX file is the official PP-DocLayoutV4 inference graph exported to ONNX (opset 17, FP32) with added operators
that leave every output unchanged (checked bit for bit on 32 pages with ONNX Runtime CPU, OpenVINO CPU and ONNX
Runtime CUDA). `PP-DocLayoutV4_infer/` is the official Paddle inference model.

## Paper

[PP-DocLayoutV4 Everywhere: An ONNX Port with Box-Level Parity across CPUs and GPUs](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.pdf)
(English, 10 pages). Translations, made with OmniDoc's document translation:

[简体中文](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.zh.pdf) · [繁體中文](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.zh-TW.pdf) · [日本語](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.ja.pdf) · [한국어](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.ko.pdf) · [Français](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.fr.pdf) · [Deutsch](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.de.pdf) · [Español](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.es.pdf) · [Português](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.pt.pdf) · [Italiano](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.it.pdf) · [Русский](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.ru.pdf) · [العربية](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.ar.pdf) · [हिन्दी](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.hi.pdf) · [বাংলা](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.bn.pdf) · [Bahasa Indonesia](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.id.pdf) · [Tiếng Việt](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.vi.pdf) · [ไทย](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.th.pdf) · [Türkçe](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.tr.pdf) · [Nederlands](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.nl.pdf) · [Polski](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.pl.pdf) · [Українська](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.uk.pdf) · [Svenska](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.sv.pdf) · [Čeština](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.cs.pdf) · [Ελληνικά](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.el.pdf) · [עברית](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.he.pdf) · [فارسی](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.fa.pdf) · [Bahasa Melayu](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.ms.pdf) · [Română](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.ro.pdf) · [Magyar](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.hu.pdf) · [Dansk](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.da.pdf) · [Suomi](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.fi.pdf) · [Norsk](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.no.pdf) · [Slovenčina](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.sk.pdf) · [Български](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.bg.pdf) · [Hrvatski](https://github.com/omnidocai/PP-DocLayoutV4/blob/main/paper/PP-DocLayoutV4-ONNX.hr.pdf)

## License

Code: Apache License 2.0 (see `LICENSE`). The reading-order decoding follows PaddleX (Apache-2.0, see `NOTICE`).
PP-DocLayoutV4 is a model by the PaddlePaddle team; the model files are subject to its upstream terms.
