Skip to content

perf: Vectorize anchor generation, detector postprocessing, and elliptical blur - #169

Merged
yakhyo merged 3 commits into
yakhyo:mainfrom
M-Colley:perf/vectorize-hot-paths
Nov 28, 2026
Merged

yakhyo merged 3 commits into
yakhyo:mainfrom
M-Colley:perf/vectorize-hot-paths

Conversation

@M-Colley

Copy link
Copy Markdown
Contributor

Summary

Vectorizes hot NumPy/OpenCV paths and avoids redundant full-frame allocations without changing outputs:

  • EllipticalBlur (uniface/privacy/blur.py): Crop Gaussian feathering, image blur, and alpha blending to each face's bounded ROI instead of blurring and blending the full (H, W, 3) image per face (529.8 ms -> 38.9 ms, 13.6x faster on 1080p with 8 faces; skips zero-area/inverted/off-screen boxes safely).
  • Anchor generation & decoding (uniface/common.py): Vectorize generate_anchors (52.88 ms -> 0.93 ms, 56.9x faster at 1080p), replace np.repeat 5x allocations in decode_landmarks with broadcasting, and vectorize distance2kps.
  • RetinaFace (uniface/detection/retinaface.py): Cache dynamic-size anchors by (height, width) across video frames, filter by confidence_threshold and pre_nms_topk before decode_boxes / decode_landmarks, and keep _scale_detections in float32 (2.34 ms -> 0.48 ms, 4.9x faster at 640x640).
  • Alignment, mesh rendering & gaze/headpose preprocessing: Replace 4 SimilarityTransform instantiations in bbox_center_alignment with the closed-form 2x3 affine (25.5 us -> 1.7 us, 14.7x faster), batch edges via cv2.polylines in draw_mesh, and resize before cvtColor with contiguous float32 NCHW outputs in MobileGaze and HeadPose.

Testing

  • Added numerical/pixel equivalence tests in tests/test_privacy.py and tests/test_perf_equivalents.py.
  • Full test suite and ruff pass.

@yakhyo
yakhyo merged commit 18156e9 into yakhyo:main Nov 28, 2026
8 checks passed
@yakhyo

yakhyo commented Nov 30, 2026

Copy link
Copy Markdown
Owner

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.

2 participants