Skip to content

[Performance] Render layout masks within polygon bounds - #5216

Closed
scyyh11 wants to merge 1 commit into
PaddlePaddle:developfrom
scyyh11:codex/local-layout-mask
Closed

scyyh11 wants to merge 1 commit into
PaddlePaddle:developfrom
scyyh11:codex/local-layout-mask

Conversation

@scyyh11

@scyyh11 scyyh11 commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

Layout visualization currently allocates a full-page mask for every polygon, stacks all masks, and scans each full page to locate covered pixels. Documents with many layout regions spend substantial CPU time and memory on this work.

This change rasterizes each polygon into its clipped bounding rectangle and blends that region in place. It preserves the existing class colors, sequential overlap blending, labels, and reading-order annotations. The unused full-page mask helper is removed and its OpenCV dependency guard moves to draw_mask.

The change is limited to paddlex/inference/models/layout_analysis/result.py. It uses the existing LayoutAnalysisResult._to_img contract, where the mask canvas has the same dimensions as the image.

Validation

CPU comparison against the unmodified develop implementation, using OpenCV 4.10.0, NumPy 2.3.5, and Pillow 12.3.0:

  • 50 saved real document pages: complete rendered images are pixel-identical.
  • 1,515 synthetic cases: blended pixels and complete images are identical. Cases cover clipping, concave and self-intersecting polygons, degenerate polygons, fractional coordinates, repeated classes, overlaps, small images, and smaller mask canvases.
  • 15,000 additional rasterization comparisons, including coordinates from -4096 to 4095: identical blended pixels.
  • A 21 × 21 polygon region on an 800 × 600 image allocates a 21 × 21 mask.
  • Black, Flake8, isort, license headers, dependency-import checks, Python 3.8 annotation checks, and git diff --check.

Earlier isolated service measurements

The same patch was measured on PaddleX 3.7.2 with one A800 80 GB, all image outputs enabled, 16 warmup pages, and three repetitions over the same 50 pages. Values below are medians in seconds. Both sides already included the same four earlier pipeline/backend/client optimizations; only local mask rendering was added for this comparison.

Requests Baseline Local mask rendering
50 sequential single-page requests 82.498 57.125
One 50-page request 62.955 33.735

These are prior controlled measurements, not a fresh end-to-end benchmark of this branch or an unmodified PaddleX baseline. Rendering concurrency and other performance changes are outside this PR.

@scyyh11 scyyh11 closed this Oct 8, 2026
@scyyh11
scyyh11 deleted the codex/local-layout-mask branch October 8, 2026 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant