UniFace: A Unified Face Analysis Library for Python | Detection, alignment, landmarks, face-mesh, recognition, parsing, gaze, attributes and anti-spoofing under one API.
Python computer-visiondeep-learningface-alignmentface-analysisface-anonymizationface-anti-spoofingface-attributesface-detectionface-meshface-parsingface-quality-assessmentface-recognitionface-segmentationface-trackingfacial-landmarksgaze-estimationhead-pose-estimationonnxruntimeportrait-mattingpython
feat: Add RetinaFace with ResNet50 backbone (#128)
Yakhyokhuja Valikhujaeva79d11379197
diff --git a/docs/models.md b/docs/models.mdindex 471eac1..6350360 100644--- a/docs/models.md+++ b/docs/models.md@@ -18,6 +18,11 @@ RetinaFace models are trained on the [WIDER FACE](datasets.md#wider-face) datase| `MNET_V2` :material-check-circle: | 3.2M | 3.5MB | 91.70% | 91.03% | 86.60% || `RESNET18` | 11.7M | 27MB | 92.50% | 91.02% | 86.63% || `RESNET34` | 24.8M | 56MB | 94.16% | 93.12% | 88.90% |+| `RESNET50` | 27.4M | 104MB | 94.72%* | 93.67%* | 88.82%* |++*\* `RESNET50` weights come from [HivisionIDPhotos](https://huggingface.co/TheEeeeLin/HivisionIDPhotos_matting/blob/main/retinaface-resnet50.onnx);+its scores are measured with [widerface_evaluation](https://github.com/yakhyo/widerface_evaluation) rather than quoted+from the paper. It trails `RESNET34` on Hard despite being ~2x the size.*!!! info "Accuracy & Benchmarks"**Accuracy**: WIDER FACE validation set (Easy/Medium/Hard subsets) - from [RetinaFace paper](https://arxiv.org/abs/1905.00641)diff --git a/docs/modules/detection.md b/docs/modules/detection.mdindex 935141c..4a089d2 100644--- a/docs/modules/detection.md+++ b/docs/modules/detection.md@@ -64,6 +64,10 @@ detector = RetinaFace(model_name=RetinaFaceWeights.RESNET34)| **MNET_V2** :material-check-circle: | 3.2M | 3.5 MB | 91.7% | 91.0% | 86.6% || RESNET18 | 11.7M | 27 MB | 92.5% | 91.0% | 86.6% || RESNET34 | 24.8M | 56 MB | 94.2% | 93.1% | 88.9% |+| RESNET50 | 27.4M | 104 MB | 94.7%* | 93.7%* | 88.8%* |++*\* `RESNET50` weights come from [HivisionIDPhotos](https://huggingface.co/TheEeeeLin/HivisionIDPhotos_matting/blob/main/retinaface-resnet50.onnx);+its scores are measured, not quoted from the paper. See [Models](../models.md#retinaface-family).*### Configurationdiff --git a/tools/detect.py b/tools/detect.pyindex 48624e4..80640d6 100644--- a/tools/detect.py+++ b/tools/detect.py@@ -8,6 +8,7 @@ Usage:python tools/detect.py --source path/to/image.jpgpython tools/detect.py --source path/to/video.mp4python tools/detect.py --source 0 # webcam+ python tools/detect.py --source path/to/image.jpg --detector retinaface --model retinaface_r50"""from __future__ import annotations@@ -21,9 +22,18 @@ from _common import get_source_typeimport cv2from tqdm import tqdm+from uniface.constants import RetinaFaceWeights, SCRFDWeights, YOLOv5FaceWeights, YOLOv8FaceWeightsfrom uniface.detection import SCRFD, RetinaFace, YOLOv5Face, YOLOv8Facefrom uniface.draw import draw_detections+# detector name -> (class, weights enum, default weights)+DETECTORS = {+ 'retinaface': (RetinaFace, RetinaFaceWeights, RetinaFaceWeights.MNET_V2),+ 'scrfd': (SCRFD, SCRFDWeights, SCRFDWeights.SCRFD_10G_KPS),+ 'yolov5face': (YOLOv5Face, YOLOv5FaceWeights, YOLOv5FaceWeights.YOLOV5M),+ 'yolov8face': (YOLOv8Face, YOLOv8FaceWeights, YOLOv8FaceWeights.YOLOV8N),+}+def process_image(detector, image_path: str, threshold: float = 0.6, save_dir: str = 'outputs'):"""Process a single image."""@@ -163,7 +173,13 @@ def main():'--method',type=str,default='retinaface',- choices=['retinaface', 'scrfd', 'yolov5face', 'yolov8face'],+ choices=list(DETECTORS),+ )+ parser.add_argument(+ '--model',+ type=str,+ default=None,+ help="Model weights for the chosen detector (e.g. 'retinaface_r50'). Defaults to the detector's own default.",)parser.add_argument('--threshold', type=float, default=0.25, help='Visualization threshold')parser.add_argument('--preview', action='store_true', help='Show live preview during video processing')@@ -172,18 +188,17 @@ def main():args = parser.parse_args()# Initialize detector- if args.detector == 'retinaface':- detector = RetinaFace()- elif args.detector == 'scrfd':- detector = SCRFD()- elif args.detector == 'yolov5face':- from uniface.constants import YOLOv5FaceWeights-- detector = YOLOv5Face(model_name=YOLOv5FaceWeights.YOLOV5M)- else: # yolov8face- from uniface.constants import YOLOv8FaceWeights-- detector = YOLOv8Face(model_name=YOLOv8FaceWeights.YOLOV8N)+ detector_cls, weights_enum, default_weights = DETECTORS[args.detector]+ if args.model is None:+ model_name = default_weights+ else:+ try:+ model_name = weights_enum(args.model)+ except ValueError:+ valid = ', '.join(w.value for w in weights_enum)+ print(f"Error: '{args.model}' is not a valid model for {args.detector}.\nAvailable: {valid}")+ return+ detector = detector_cls(model_name=model_name)source_type = get_source_type(args.source)diff --git a/uniface/constants.py b/uniface/constants.pyindex c611322..873217d 100644--- a/uniface/constants.py+++ b/uniface/constants.py@@ -80,6 +80,7 @@ class RetinaFaceWeights(str, Enum):MNET_V2 = "retinaface_mnet_v2"RESNET18 = "retinaface_r18"RESNET34 = "retinaface_r34"+ RESNET50 = "retinaface_r50"class SCRFDWeights(str, Enum):@@ -281,6 +282,10 @@ MODEL_REGISTRY: dict[Enum, ModelInfo] = {url='https://github.com/yakhyo/uniface/releases/download/weights/retinaface_r34.onnx',sha256='bd0263dc2a465d32859555cb1741f2d98991eb0053696e8ee33fec583d30e630'),+ RetinaFaceWeights.RESNET50: ModelInfo(+ url='https://github.com/yakhyo/uniface/releases/download/weights/retinaface_r50.onnx',+ sha256='905eac744dc165dbcd62960938a191de3fecea6bef198fb1c5e13e690bc6a828'+ ),# MobileFaceMobileFaceWeights.MNET_025: ModelInfo(