Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.jpgproduces 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 remain512, 512, 512, 512, 285. The internalcrop_candidateshelper now yields batches, andrun_in_batchesowns 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 isMtCnnClient.detect_faceson the existingcouple.jpg(1588 × 2280).Two fresh processes per revision, six consecutive detections in each:
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
tracemallocstays 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.jpgandselfie-many-people.jpg, using real inference with the official weights. No recognition-accuracy or accelerator-performance claim is made.How to test
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 --checkpass. 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.