人脸检测模型部署
参考资料:
- 人脸模型仓库:https://github.com/biubug6/Pytorch_Retinaface
- 人脸检测资料包(国内用户推荐):https://dl.100ask.net/Hardware/MPU/RK3576-DshanPi-A1/utils/Pytorch_Retinaface.zip
1.获取原始模型
1.获取源码仓库
git clone https://github.com/biubug6/Pytorch_Retinaface.git
2.创建并激活 conda 环境
conda create -n retinaface python=3.9 -y
conda activate retinaface
3.安装 PyTorch与其他依赖
conda install compilers cmake
pip3 install torch torchvision
pip3 install opencv-python pillow matplotlib numpy tensorboard
pip3 install onnx onnxruntime onnxsim
4.获取预训练权重
cd Pytorch_Retinaface
#新建权重文件夹
mkdir -p weights
下载预训练权重模型和预训练模型,保存至weights
目录下。
5.修改convert_to_onnx.py源码中的torch.onnx._export
为torch.onnx.export
,修改完成如下:
torch_out = torch.onnx.export(net, inputs, output_onnx, export_params=True, verbose=False,
input_names=input_names, output_names=output_names)
6.权重文件转换ONNX模型
python convert_to_onnx.py
转换完成后,可以在当前目录下看到FaceDetector.onnx
模型文件。
(retinaface) baiwen@dshanpi-a1:~/Pytorch_Retinaface$ ls
convert_to_onnx.py data FaceDetector.onnx LICENSE.MIT README.md test_widerface.py utils widerface_evaluate
curve detect.py layers models test_fddb.py train.py weights
7.简化模型
python -m onnxsim FaceDetector.onnx retinaface-sim.onnx --input-shape 1,3,640,640
运行效果如下:
(retinaface) baiwen@dshanpi-a1:~/Pytorch_Retinaface$ python -m onnxsim FaceDetector.onnx retinaface-sim.onnx --input-shape 1,3,640,640
WARNING: "--input-shape" is renamed to "--overwrite-input-shape". Please use it instead.
Simplifying...
Finish! Here is the difference:
┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃ ┃ Original Model ┃ Simplified Model ┃
┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━┩
│ Add │ 2 │ 2 │
│ Concat │ 14 │ 6 │
│ Constant │ 147 │ 117 │
│ Conv │ 56 │ 56 │
│ Gather │ 6 │ 0 │
│ LeakyRelu │ 38 │ 38 │
│ Relu │ 3 │ 3 │
│ Reshape │ 9 │ 9 │
│ Resize │ 2 │ 2 │
│ Shape │ 8 │ 0 │
│ Slice │ 2 │ 0 │
│ Softmax │ 1 │ 1 │
│ Transpose │ 9 │ 9 │
│ Unsqueeze │ 6 │ 0 │
│ Model Size │ 1.6MiB │ 1.7MiB │
└────────────┴────────────────┴──────────────────┘
简化完成后将retinaface-sim.onnx
拷贝至RetinaFace工作目录的模型路径下:
cp retinaface-sim.onnx ~/Projects/rknn_model_zoo/examples/RetinaFace/model
2.模型转换
1.使用Conda激活rknn-toolkit2
环境
conda activate rknn-toolkit2
2.进入RetinaFace模型转换目录
cd ~/Projects/rknn_model_zoo/examples/RetinaFace/python
3.执行模型转换
python3 convert.py ../model/retinaface-sim.onnx rk3576
运行效果如下:
(rknn-toolkit2) baiwen@dshanpi-a1:~/Projects/rknn_model_zoo/examples/RetinaFace/python$ python3 convert.py ../model/retinaface-sim.onnx rk3576
I rknn-toolkit2 version: 2.3.2
--> Config model
done
--> Loading model
I Loading : 100%|██████████████████████████████████████████████| 117/117 [00:00<00:00, 11380.65it/s]
done
--> Building model
I OpFusing 0: 100%|██████████████████████████████████████████████| 100/100 [00:00<00:00, 297.57it/s]
I OpFusing 1 : 100%|█████████████████████████████████████████████| 100/100 [00:00<00:00, 104.34it/s]
I OpFusing 0 : 100%|██████████████████████████████████████████████| 100/100 [00:02<00:00, 46.89it/s]
I OpFusing 1 : 100%|██████████████████████████████████████████████| 100/100 [00:02<00:00, 44.28it/s]
I OpFusing 2 : 100%|██████████████████████████████████████████████| 100/100 [00:03<00:00, 32.20it/s]
W build: found outlier value, this may affect quantization accuracy
const name abs_mean abs_std outlier value
onnx::Conv_634 0.69 0.83 21.961
I GraphPreparing : 100%|█████████████████████████████████████████| 123/123 [00:00<00:00, 882.71it/s]
I Quantizating : 100%|███████████████████████████████████████████| 123/123 [00:00<00:00, 152.58it/s]
I OpFusing 0: 100%|██████████████████████████████████████████████| 100/100 [00:00<00:00, 448.58it/s]
I OpFusing 1 : 100%|█████████████████████████████████████████████| 100/100 [00:00<00:00, 433.87it/s]
I OpFusing 2 : 100%|█████████████████████████████████████████████| 100/100 [00:00<00:00, 424.22it/s]
W build: The default input dtype of 'input0' is changed from 'float32' to 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of 'output0' is changed from 'float32' to 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '592' is changed from 'float32' to 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model with Runtime API!
I rknn building ...
I rknn building done.
done
--> Export rknn model
done
(rknn-toolkit2) baiwen@dshanpi-a1:~/Projects/rknn_model_zoo/examples/RetinaFace/python$ ls ../model/
dataset.txt download_model.sh RetinaFace.rknn retinaface-sim.onnx test.jpg
运行完成后可以在../model/
目录下可以看到转换完成后的端侧推理模型RetinaFace.rknn
。