Tools to export, convert, and train the RetinaFace face detection model for use the with lightfoot edge pipeline faces process.
The production implementation uses the torch weights provided in the forked repo. Those weights are converted to from torch to ONNX, then ONNX to RKNN whereby the RKNN models are exported in both floating point (fp
) and quantized (i8
) formats. All intermediate and final RKNN models converted by the processes described herein can be found in Google Cloud Storage (GCS) gs://ld-models/RetinaFace
. The full conversion process is reproduced here for completeness.
Note that the ONNX to RKNN conversion script, convert_to_rknn.py
, is adapted from https://github.com/airockchip/rknn_model_zoo/blob/main/examples/RetinaFace/python/convert.py.
git clone https://github.com/lookdeep/lookdeep-Pytorch_Retinaface/tree/master
cd lookdeep-Pytorch_Retinaface
python3.10 -m venv venv # Ensure that requirements.txt refers to the rknn-toolkit2 wheel commensurate with your python version.
. venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
Pretrained weights from the forked repo are available in GCS.
./download_weights.sh
python convert_to_onnx.py -m weights/Resnet50_Final.pth --network resnet50 --long_side 320
python convert_to_onnx.py -m weights/Resnet50_Final.pth --network resnet50 --long_side 640
python convert_to_onnx.py -m weights/mobilenet0.25_Final.pth --network mobile0.25 --long_side 320
python convert_to_onnx.py -m weights/mobilenet0.25_Final.pth--network mobile0.25 --long_side 640
Note: If converting from a newly trained model, ensure specification of --mlflow-run-id
and --mlflow-run-name
.
# Resnet50
python convert_to_rknn.py weights/RetinaFace_resnet50_320_opset17.onnx
python convert_to_rknn.py weights/RetinaFace_resnet50_320_opset17.onnx --quantize
python convert_to_rknn.py weights/RetinaFace_resnet50_640_opset17.onnx
python convert_to_rknn.py weights/RetinaFace_resnet50_640_opset17.onnx --quantize
# Mobilenet0.25
python convert_to_rknn.py weights/RetinaFace_mobile0.25_320_opset17.onnx
python convert_to_rknn.py weights/RetinaFace_mobile0.25_320_opset17.onnx --quantize
python convert_to_rknn.py weights/RetinaFace_mobile0.25_640_opset17.onnx
python convert_to_rknn.py weights/RetinaFace_mobile0.25_640_opset17.onnx --quantize
See README.OG.md