一、前言
本教程在solution_zoo/solution_example中的yolov3_mobilenetv2_example基础上,使用yolov5替换yolov3,成功在板端运行yolov5。整个过程基本不需要自己写代码,只需对一些配置文件进行修改。
使用docker开发环境,开发包版本:horizon_x3_open_explorer_v1.3.9_20210805142932
二、Yolov5模型准备
原始模型我使用的是model_zoo中的YOLOv5l.onnx模型。然后用ai_toolchain提供的yolov5工具,将浮点模型转换成定点模型,得到yolov5_672×672_nv12.bin文件。
转换中用到的data没有提供,但是根据sh文件中data的路径,可以知道用的是coco数据集,去coco官网下载对应的data即可。
三、魔改yolov3_mobilenetv2_example
将models文件夹下的yolov3_nv12_hybrid_horizonrt.bin替换成刚才得到的yolov5_672×672_nv12.bin,建议将文件名改成yolov5_nv12_hybrid_horizonrt.bin,方便后面对配置文件的修改。
修改配置文件的过程很简单粗暴,把文件名和文件内容中的yolov3字段全部替换成yolov5。此外,configs文件夹下的infer_yolov3_config_2M.json和infer_yolov3_config_8M.json需要进一步修改,以infer_yolov_config_2M.json为例:
下面列出了所有需要修改的文件:
build_and_deploy.sh
CMakeLists.txt
main.cc
run_example.sh
- configs/infer_yolov3_config_2M.json
- configs/infer_yolov3_config_8M.json
- configs/solution_yolov3.json
- configs/workflow_yolov3.json
plugins/example_smart_plugin/include/example_smart_plugin.h
plugins/example_smart_plugin/src/example_smart_plugin.cc
四、更新model_inference库
yolov3_mobilenetv2_example中调用了model_inference库来完成yolov3的算法推理(predict和postprocess),但没提供yolov5的,需要自定义yolov5的处理method,然后更新model_inference库。
找到model_inference的开发包位置,路径为/ddk/package/host/source_code/model_inference。
/include/model_inference/postprocess存放模型的postprocess.h文件(例如yolov3_postprocess.h),在此新增yolov5_postprocess.h,代码如下:
/src/postprocess存放模型的postprocess.cc文件(例如yolov3_postprocess.cc),在此新增yolov5_postprocess.cc,代码如下:
新增了yolov5的postprocess方法后,需要注册yolov5_postprocess,将/scr/postprocess.cc修改为:
接下来,使用cmake对model_inference进行打包。因为我用的是docker环境,所以需要先将model_inference开发包添加到容器中。需要注意的是,因为model_inference依赖其他的一些库,所以我将整个/ddk/package都添加到了容器中。model_inference开发包中已经提供了cmake的配置文件CMakeLists.txt,所以只需要在model_inference目录下依次执行以下指令:
output文件夹里存放了cmake后的model_inference库,用它更新原来的model_inference库,docker容器中库的路径为/root/.horizon/aarch64/x3/model_inference。
五、运行yolov5_mobilenetv2_example
yolov3_mobilenetv2_example中调用了model_inference库来完成yolov3的算法推理(predict和postprocess),但没提供yolov5的,需要自定义yolov5的处理method,然后更新model_inference库。

