Skip to content

Run mtcnn on tensorflow, pytorch and onnx, group detectors by engine - #1681

Merged
serengil merged 1 commit into
masterfrom
sefiks/mtcnn-multi-engine-and-detector-folders
Nov 4, 2026
Merged

serengil merged 1 commit into
masterfrom
sefiks/mtcnn-multi-engine-and-detector-folders

Conversation

@serengil

@serengil serengil commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

Summary

mtcnn and fastmtcnn were the same model. Their weights are identical tensor by tensor,
and their boxes differ by only a few pixels because of pre/post-processing. This PR makes
mtcnn a single detector that runs on every backend engine, adds a native onnx version,
and groups face_detection into one folder per engine.

mtcnn on every engine

engine implementation
tensorflow ipazc/mtcnn (unchanged)
pytorch facenet-pytorch (formerly fastmtcnn)
onnx new: native onnx port, no tensorflow or torch needed

fastmtcnn still works on every engine. It now points to the pytorch implementation and
logs a one-time warning that the name will be deprecated in favour of mtcnn on the
pytorch engine.

onnx mtcnn

  • Networks: P-Net, R-Net and O-Net are exported from facenet-pytorch's weights, with
    channels-last inputs like the other deepface onnx graphs. P-Net accepts any input size. Its
    ceil-mode max-pool is swapped for an equivalent that onnx can export with dynamic shapes.
    Each graph matches torch to ≤5e-5.
  • Pipeline: facenet-pytorch's pipeline (image pyramid, area resize, NMS, bbox
    regression, three stages) is ported to numpy. The area resize reproduces torch's
    interpolate(mode="area"); cv2.INTER_AREA would not give the same result.
    identical boxes and eye coordinates. Confidences agree within 4e-7.
  • Speed: on par with pytorch (26.1 s vs 25.6 s for the whole dataset).
  • Weights: about 2 MB in total, served from the deepface_models GitHub release with
    Hugging Face as backup. Both sources were verified byte-identical.

face_detection layout

folder detectors
tf/ MtCnn
pytorch/ MtCnn, Yolo
onnx/ MtCnn, YuNet, CenterFace
opencv/ OpenCv, Ssd
external/ Dlib, MediaPipe, RetinaFace

Detectors other than mtcnn are still offered on every engine, as before.

Dependencies

  • facenet-pytorch>=2.5.3,<2.6.0 moves from requirements_additional.txt to the pytorch
    extra. The cap matters: 2.6.0 pins torch<2.3, numpy<2 and Pillow<10.3, which would
    downgrade users' installs.
  • The pytorch docker image installs facenet-pytorch==2.5.3, pinned like torch==2.1.2.

Fixes

  • deepface/commons/image_utils.py imported werkzeug at module level, but werkzeug comes
    only with the api extra. A base pip install deepface therefore failed as soon as
    detection was imported. werkzeug is now imported for type hints only.

Behaviour changes

  • pytorch users of mtcnn: they now get facenet-pytorch instead of the keras-based
    package. Boxes may shift by a few pixels.
  • onnx users of mtcnn: they get the native port instead of the keras-based package,
    so keras is no longer required.

Test plan

  • New tests/unit/test_mtcnn_engines.py: routing per engine, the fastmtcnn
    deprecation alias, area resize vs torch, NMS, no-face and tiny-image paths, and onnx vs
    pytorch parity
  • Detection-related unit tests pass on tensorflow, pytorch and onnx (99 passed each)
  • The same tests pass in an onnx-only env with no tensorflow, keras or torch (84
    passed; 15 torch/facenet-pytorch checks skip)
  • All 20 detector modules import on every engine
  • onnx mtcnn downloads its weights from scratch and detects faces end to end
  • pylint 10/10, mypy clean

  mtcnn and fastmtcnn were the same model - identical weights, a few pixels
  apart from pre/post-processing - so fastmtcnn is now mtcnn on pytorch.

  - mtcnn is routed by backend engine: ipazc/mtcnn on tensorflow,
    facenet-pytorch on pytorch, and a new native onnx port on onnx
  - onnx mtcnn: P-Net, R-Net and O-Net exported from facenet-pytorch with
    channels last inputs, and its pipeline ported to numpy. It finds the
    same faces as pytorch - identical boxes and eyes on all 76 faces in
    tests/unit/dataset - at the same speed, without tensorflow or torch
  - fastmtcnn stays as an alias of mtcnn on pytorch and logs a one-time
    deprecation warning
  - face_detection is grouped into tf, pytorch, onnx, opencv and external
    subpackages
  - facenet-pytorch moves to the pytorch extra, capped below 2.6.0 since
    2.6.0 pins torch<2.3, numpy<2 and Pillow<10.3. The pytorch docker image
    installs facenet-pytorch==2.5.3
  - werkzeug is imported for type hints only, so a base install without the
    api extra can import deepface again
  - readme no longer mentions fastmtcnn
@serengil
serengil merged commit 01769bf into master Nov 4, 2026
3 checks passed
@serengil
serengil deleted the sefiks/mtcnn-multi-engine-and-detector-folders branch November 4, 2026 18:21
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