This is a face detection model for high-precision facial localization based on RetinaFace: Single-stage Dense Face Localisation in the Wild. This model accurately detects facial landmarks and bounding boxes for faces in images. This repository provides custom training & inference code, and several new backbone models have been integrated for improved performance and flexibility.
[!NOTE]
Weโve updated the codebase with new trained models and a refactored structure, enhancing functionality and maintainability. These improvements include support for MobileNetV1 (including v1_025 and v1_050), MobileNetV2, and various ResNet versions (18, 34, 50), offering a cleaner and more reproducible experience.
In this implementation, we use several lightweight and powerful backbone architectures to provide flexibility between performance and accuracy.
RetinaFace Backbones | Pretrained on ImageNet | Easy | Medium | Hard |
---|---|---|---|---|
MobileNetV1 (width mult=0.25) | True | 88.48% | 87.02% | 80.61% |
MobileNetV1 (width mult=0.50) | False | 89.42% | 87.97% | 82.40% |
MobileNetV1 | False | 90.59% | 89.14% | 84.13% |
MobileNetV2 | True | 91.70% | 91.03% | 86.60% |
ResNet18 | True | 92.50% | 91.02% | 86.63% |
ResNet34 | True | 94.16% | 93.12% | 88.90% |
ResNet50 | True | ย | ย | ย |
RetinaFace Backbones | Pretrained on ImageNet | Easy | Medium | Hard |
---|---|---|---|---|
MobileNetV1 (width mult=0.25) | True | 90.70% | 88.12% | 73.82% |
MobileNetV1 (width mult=0.50) | False | 91.56% | 89.46% | 76.56% |
MobileNetV1 | False | 92.19% | 90.41% | 79.56% |
MobileNetV2 | True | 94.04% | 92.26% | 83.59% |
ResNet18 | True | 94.28% | 92.69% | 82.95% |
ResNet34 | True | 95.07% | 93.48% | 84.40% |
ResNet50 | True | ย | ย | ย |
Clone the repository:
git clone https://github.com/yakhyo/retinaface-pytorch.git
cd retinaface-pytorch
Install dependencies:
pip install -r requirements.txt
This RetinaFace implementation supports the following feature extractor backbones:
Download the Dataset:
fstq
) or Dropbox.Organize the Dataset Directory:
Structure your dataset directory as follows:
data/
โโโ widerface/
โโโ train/
โ โโโ images/
โ โโโ label.txt
โโโ val/
โโโ images/
โโโ wider_val.txt
[!NOTE]
wider_val.txt
only includes val file names but not label information.
There is also an organized dataset (as shown above): Link from Google Drive or Baidu Cloud (password: ruck). Thanks to biubug6 for the organized dataset.
To train the RetinaFace model with a specific backbone, use the following command:
python train.py --network mobilenetv1 # Replace 'mobilenetv1' with your choice of backbone
Download mobilenetv1_0.25.pretrained (pre-trained weights on ImageNet, weights ported from @biubug6) to reproduce the results.
mobilenetv1_0.25
mobilenetv1_0.50
mobilenetv1
mobilenetv2
resnet18
resnet34
resnet50
Model Name | PyTorch Weights | ONNX Weights Filename |
---|---|---|
MobileNetV1_025 | retinaface_mv1_0.25.pth | retinaface_mv1_0.25.onnx |
MobileNetV1_050 | retinaface_mv1_0.50.pth | retinaface_mv1_0.50.onnx |
MobileNetV1 | retinaface_mv1.pth | retinaface_mv1.onnx |
MobileNetV2 | retinaface_mv2.pth | retinaface_mv2.onnx |
Model Name | PyTorch Weights | ONNX Weights Filename |
---|---|---|
ResNet18 | retinaface_r18.pth | retinaface_r18.onnx |
ResNet34 | retinaface_r34.pth | retinaface_r34.onnx |
ResNet50 | not available | not available |
Inference the model using:
python detect.py --network mobilenetv1 --weights retinaface_mv1.pth
Using MobileNet v2 as a backbone, 632 faces found on large selfi image, see the `assets` folder.
retinaface-pytorch
folder:
git clone https://github.com/yakhyo/widerface_evaluation
widerface_evaluation
folder and build the required extension:
cd widerface_evaluation
python3 setup.py build_ext --inplace
retinaface-pytorch
folder after installation is complete:
cd ..
Run the following command to evaluate your RetinaFace model with WiderFace, specifying the model architecture (mobilenetv1
in this example) and the path to the trained weights. Predictions will be stored in widerface_txt
inside the widerface_evaluation
folder.
python evaluate_widerface.py --network mobilenetv1 --weights weights/mobilenetv1.pth
After generating predictions, navigate to the widerface_evaluation folder and run the following command to compare predictions with the ground truth annotations:
cd widerface_evaluation
python evaluation.py -p widerface_txt -g ground_truth
[!NOTE]
Ensureground_truth
is the path to the WiderFace ground truth directory.
This will begin the evaluation process of your model on the WiderFace dataset.
This project is licensed under the MIT License. See the LICENSE file for details.