This project implements a face detection and embedding extraction pipeline using the Triton Inference Server with a Python backend. It uses RetinaFace_int.onnx for face detection and arcface.onnx for generating 512-dimensional face embeddings. The pipeline is optimized for 704x576 images containing up to 5 small to medium-sized faces (50-150 pixels).
face_recognition/
├── 1/
│ ├── arcface.onnx # ArcFace model for face feature extraction
│ ├── model.py # Triton Python model implementation
│ └── RetinaFace_int.onnx # RetinaFace model for face detection and landmarks
├── client.py # Test client script
├── config.pbtxt # Triton model configuration
└── output_image.jpg # Inference output with visualized detections- Face Detection: Detects faces and 5 facial landmarks using RetinaFace_int.onnx.
- Embedding Extraction: Generates 512D embeddings with arcface.onnx.
- Optimized Parameters: Tuned for small to medium faces in 704x576 images.
- Debugging: Saves debug_output.jpg (server-side) and output_image.jpg (client-side) for visualization.
- Triton Inference Server (v2.x or later)
- Python 3.8+
- Dependencies: numpy, opencv-python, onnxruntime, tritonclient[grpc]
- Models: RetinaFace_int.onnx (input: [1, 3, 608, 640], NCHW), arcface.onnx (input: [1, 3, 112, 112], NCHW)
- Hardware: CPU (GPU supported with appropriate ONNX Runtime providers)
- Build custom Triton Inference Server:
docker build . -t hub.yiqisoft.cn//nvidia/tritonserver:23.07-py3-cv-onnxruntime- Set Up Model Repository: Ensure the face_recognition directory follows the structure above, with model.py, RetinaFace_int.onnx, arcface.onnx, and config.pbtxt in place.
Download onnx models and replace the empty files:
wget https://huggingface.co/amd/retinaface/resolve/main/weights/RetinaFace_int.onnx -O ./face_recognition/1/RetinaFace_int.onnx
wget https://huggingface.co/garavv/arcface-onnx/resolve/main/arcface.onnx -O ./face_recognition/1/arcface.onnx
- Run Triton Server:
bashdocker run --rm \
-p 8000:8000 \
-p 8001:8001 \
-p 8002:8002 \
-v $(pwd)/face_recognition:/models \
hub.yiqisoft.cn/nvidia/tritonserver:23.07-py3-cv-onnxruntime \
tritonserver --model-repository=/models- Test with Client: Run the provided client.py to test inference:
python client.py --image test_image.jpg --url localhost:8001 --model-name face_recognition- Input: 704x576 BGR image from IP Camera stream
- Output: output_image.jpg with bounding boxes (green), landmarks (red), and face indices
- Console logs: Number of detected faces, bounding boxes, landmarks, and embedding samples
- Server-side: debug_output.jpg shows detections with bounding boxes (red), landmarks (green), and confidence scores.
- Client-side: output_image.jpg shows detections with bounding boxes (green), landmarks (red), and face indices.
Key parameters in model.py:
- Input Size: [608, 640] (matches RetinaFace_int.onnx)
- Confidence Threshold: conf_threshold = 0.4 (filters low-confidence detections)
- NMS Threshold: nms_threshold = 0.85 (handles overlapping boxes, tuned for dense face distributions)
- Visualization Threshold: vis_thres = 0.3 (controls visualization in debug_output.jpg)
- Missed Small Faces: Lower conf_threshold to 0.3 or increase nms_threshold to 0.9.
- Duplicate Detections: Decrease nms_threshold to 0.8.
- Very Small Faces (<30 pixels): Consider a higher-resolution model or adjusting min_sizes (requires model retraining).
- Visualize Results: Check debug_output.jpg (server) and output_image.jpg (client) for detection accuracy.
- Logs: Review server logs for anchor counts, detection shapes, and confidence scores.
- Face Crops: Add cv2.imwrite(f"aligned_face_{i}.jpg", aligned_face) in execute to verify ArcFace inputs.
- The model is optimized for 704x576 images with up to 5 faces.
- Ensure RetinaFace_int.onnx expects [1, 3, 608, 640] input (verify with Netron).
- For large images or denser face distributions, test nms_threshold = 0.9.
This README.md provides a clear structure and minimal instructions, focusing on setup, usage, and tuning for your specific use case. Let me know if you need adjustments or additional sections!
