Example 18: Recognize faces and train an ArcFace or AdaFace classifier#
Description: Use a JSON/YAML configuration to recognize a cropped face or train and reload a model-specific identity classifier.
- Featuring:
ℹ️ Source code and input file(s) -> {pyppbox repo}/examples
Install requirements/faces.txt using python -m pip install -r from the
repository root and either the ArcFace or AdaFace v1.6.0 data wheel. For NVIDIA
GPU inference, use requirements/faces_gpu.txt with compatible CUDA/cuDNN libraries.
For direct inference with the packaged defaults, see
Supported ReIDers. This command-line example accepts a JSON/YAML
configuration for custom model paths and identity classifiers.
Save an arcface.json file with absolute paths matching your installation:
{
"ri_name": "ArcFace",
"model_det": "/path/to/pyppbox/data/modules/arcface/models/det_10g.onnx",
"model_file": "/path/to/pyppbox/data/modules/arcface/models/w600k_r50.onnx",
"classifier_pkl": "/path/to/pyppbox/data/modules/arcface/classifier/gta5.pkl",
"device": "cpu"
}
Run from the examples/ directory:
python example_18_onnx_face_reid.py --config arcface.json --image /path/to/face.jpg
For AdaFace, use "ri_name": "AdaFace" and the detector, IR18 ONNX model, and
classifier under data/modules/adaface. The command prints the identity and
SVM confidence percentage. A probability threshold does not provide a calibrated
unknown-person probability.
To train your own identities, set train_data to a directory containing one
image subfolder per person and classifier_pkl to a new writable output file.
Create its parent directory first. Add --train to the command to train, reload,
and run inference. Training fits an identity SVM using frozen embeddings, skips
images with no face, and rejects an identity without usable faces. Model and
preprocessing fingerprints prevent incompatible classifier reuse.
Example source#
#################################################################################
# Example of recognizing faces and training an ArcFace or AdaFace classifier
#################################################################################
"""Train or use an ArcFace/AdaFace identity SVM with a JSON/YAML config.
Run from examples/: python example_18_onnx_face_reid.py --config arcface.json --image face.jpg.
Add --train to train the identity SVM first. Training needs identity subfolders.
"""
import argparse
import json
from pathlib import Path
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--config', type=Path, required=True)
parser.add_argument('--image', type=Path, required=True)
parser.add_argument('--train', action='store_true')
args = parser.parse_args()
from pyppbox.config.configtools import getCFGDict
from pyppbox.config.myconfig import RCFGArcFace, RCFGAdaFace
from pyppbox.modules.reiders.onnxface import MyArcFace, MyAdaFace
document = getCFGDict(str(args.config))
name = str(document.get('ri_name', '')).lower()
if name not in ('arcface', 'adaface'):
parser.error('The configuration must select ArcFace or AdaFace.')
cls, factory = (RCFGArcFace, MyArcFace) if name == 'arcface' else (RCFGAdaFace, MyAdaFace)
cfg = cls()
cfg.set(document)
backend = factory(cfg)
try:
if args.train:
backend.train_classifier()
backend.load_classifier()
label, confidence = backend.recognize_file(str(args.image))
print(json.dumps(dict(identity=label, svm_confidence_percent=confidence)))
finally:
backend.close()
if __name__ == '__main__':
main()