Skip to content

Bound MTCNN crop buffers to inference batches - #1772

Open
Afloat16 wants to merge 1 commit into
serengil:masterfrom
Afloat16:perf/mtcnn-bounded-crop-batches-20261129
Open

Afloat16 wants to merge 1 commit into
serengil:masterfrom
Afloat16:perf/mtcnn-bounded-crop-batches-20261129

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Tickets

Context: the ONNX MTCNN pipeline introduced in #1681.

What has been done

R-Net and O-Net already run in batches of at most 512 candidates, but the pipeline first cropped, stacked and normalized every candidate. The network batch limit therefore did not bound its input buffers. The repository's couple.jpg produces 2,333 R-Net candidates: its full float32 crop array alone occupies 16,125,696 bytes before normalization and inference.

This change prepares and normalizes crops inside the batch loop. Empty crops are filtered before splitting, so the networks receive the same valid candidates in the same order and the same batch sizes as before. Each batch is filled directly into a contiguous array, avoiding a list of retained crop arrays followed by another stack allocation. R-Net's two outputs and O-Net's three outputs are concatenated in candidate order. Detection thresholds, NMS, resize arithmetic, weights and public return values are unchanged.

For couple.jpg, the largest R-Net crop array becomes 3,538,944 bytes (512 candidates), and the batch sizes remain 512, 512, 512, 512, 285. The internal crop_candidates helper now yields batches, and run_in_batches owns cropping, normalization and inference together.

Memory and result checks

Measured with the official P-Net/R-Net/O-Net ONNX weights, ONNX Runtime 1.29.0, NumPy 2.5.5, Python 3.12, CPU execution and two inference threads. Base: 8cec026045be164e14b510c55c1264e743dc4999. The public call is MtCnnClient.detect_faces on the existing couple.jpg (1588 × 2280).

Two fresh processes per revision, six consecutive detections in each:

Measurement Base Change
Process RSS high-water mark after the first detection 542.22–542.28 MiB 500.63–500.89 MiB
Process RSS high-water mark after six detections 719.03–719.29 MiB 708.98–709.12 MiB
Resident memory after the sixth detection 706.04–706.08 MiB 622.02–622.13 MiB

These are measurements of this CPU workload, not general RSS guarantees. ONNX Runtime's native arenas and the allocator retain memory across calls. The whole-detection NumPy/Python allocation peak measured by tracemalloc stays at 106,055,550 bytes because an earlier P-Net stage dominates it. The deterministic improvement is that R-Net/O-Net crop and normalization buffers are bounded by the existing batch size instead of the total candidate count.

Unpinned warm process medians varied from 0.78 to 0.97 seconds, including a slower branch process. A follow-up comparison ran base/branch/branch/base subprocesses within one session pinned to the same two logical CPUs, with eight detections per process and the first two as warmup. The pooled medians of the twelve warm calls per revision were 1.139 seconds (base) and 1.135 seconds (branch); observed ranges were 1.079–1.208 and 1.031–1.320 seconds. These timings do not establish a speed improvement.

All face rectangles, eye coordinates and confidence values are exactly equal to the base revision on img1.jpg, img11.jpg, couple.jpg and selfie-many-people.jpg, using real inference with the official weights. No recognition-accuracy or accelerator-performance claim is made.

How to test

DEEPFACE_BACKEND_ENGINE=onnx python -m pytest test_mtcnn_engines.py -q

Run from tests/unit. Result: 20 passed, 4 skipped. The four existing optional facenet-pytorch comparisons skip because that dependency is absent; the separate real ONNX base/branch comparison above was executed.

The public detection regression fails on the base revision because a 1,056-candidate tensor reaches normalization before batching. It passes with the change. Additional cases cover empty/clipped candidates, filtering before batch boundaries, exact crop contents, original candidate ordering, both two-output and three-output networks, and no inference call for an empty candidate set.

MyPy for the changed implementation, Ruff E/F checks at the repository's 100-character line limit, and git diff --check pass. The pinned local Pylint 2.15.10/astroid stack crashes on both the unchanged base file and this file while reading Python 3.12 type-alias syntax (TreeRebuilder.visit_typealias); a local Pylint pass is therefore not claimed.

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