Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
DimWalker 4a80adc434 | 1 year ago | |
---|---|---|
onnx_v1 | 1 year ago | |
vx_images | 1 year ago | |
vx_notebook | 1 year ago | |
.gitignore | 1 year ago | |
ForAiStudio.md | 1 year ago | |
ForHorizon.md | 1 year ago | |
LICENSE | 1 year ago | |
README.md | 1 year ago | |
vx.json | 1 year ago |
本项目属于AI达人创造营三期项目,主要探索如何将PaddleOCR训练的车牌识别模型部署在地平线X3的板子上,并实现实时推理。
项目地址:https://aistudio.baidu.com/aistudio/projectdetail/4730476
PaddlePaddle之于Arm对应的平台为paddle-lite。paddle-lite主要为c语言,开发及部署难度较大,虽然支持fpga、npu计算加速,但是并不支持本次项目所用到的地平线X3的bpu。
故为实现本项目,需要将paddleocr -> onnx -> onnx bpu的转换路线。
本项目涉及的技术栈非常的多,且越往后越难,笔者已实现在地平线使用CPU的推理,但FPS只有1帧。BPU方面虽然转换模型成功了,并貌似实现能推理。但是笔者能力有限和精力有限,并不清楚BPU的推理结果如何转换为可视化结果。
最终并未如笔者所期望,实现完整BPU加速程序。
接下来,笔者将会步步拆解,从易到难(到自己也不懂),还原实现过程。
本篇主要介绍如何将PaddleOCR模型转换为ONNX模型,转化后的模型均可直接部署于PC和板子上。
unzip onnx_v1.zip
cd /home/aistudio/onnx_v1
pip install -r requirements.txt
# 0是摄像头的索引,不填时默认为0
python3 main.py 0
# PS: 按q退出,按空格暂停
实际上除了paddle2onnx的模型转换可以在ai studio上执行,其他代码都无法运行
本篇开篇前,想吐槽一下笔者本次项目的霉运体质。
第一块的板子是Edgeboard,据助教说板子只支持1.8.5的模型,跟着PaddleOCR上的lite教程跑了一遍,果然不行,编译不了。
第二块板子是地平线X3,跟着教程刷机失败,接着让国晟大佬远程帮忙看看咋回事,确定板子是坏的……
第三块也是X3,终于正常了,刷机一气呵成。
# 修改设置文件
vim /etc/network/interfaces
# 保存后重启网络
/etc/init.d/networking restart
# a安装软件包
apt install x11vnc
# 设置密码
# 设置后会有一个保存路径,如果读者用的不是root账号,下面脚本内容要相应修改。
x11vnc -storepasswd
# 创建开机服务
vim /lib/systemd/system/x11vnc.service
开机脚本
[Unit]
Description=Start x11vnc at startup.
After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/bin/x11vnc -auth guess -forever -loop -capslock -nomodtweak -noxdamage -repeat -rfbauth /root/.vnc/passwd -rfbport 5900 -shared
[Install]
WantedBy=multi-user.target
启动服务
systemctl daemon-reload
systemctl enable x11vnc.service
systemctl start x11vnc.service
systemctl status x11vnc.service
操作完毕后,继续使用MOBAXterm,新建Session -> VNC
端口在开机脚本中,要一致。
需要注意,桌面的默认登录账号密码为sunrise/sunrise。
故,在这里输入某些命令,是需要加sudo的,不要和上面串口的root账号搞混。
笔者以下命令是通过vnc打开板子里的终端操作的,不是串口。
先使用FileZilla上传onnx_v1的代码。
笔者用的是usb 摄像头,使用以下命令查找摄像头。
# 这里会有很多个设备,如果不确定是哪个,先拔掉输入一次命令,再插入输入一次
ls /dev/video*
# 改为你上传的路径
cd /home/sunrise/LPR/onnx_v1/ppocr_onnx
# 改为你的摄像头索引
python3 main.py 8
至此笔者能完整运行的部分,已说明完毕。
下面的BPU篇,笔者能力有限,并未实现一个完整的落地方案。
先重新说明一下转换路线,paddleocr -> onnx -> onnx bpu。
onnx转onnx bpu需要在天工开物的docker交叉编译环境中进行。
首先,强烈建议你读完这两篇文章中的bpu模型转换部分再来操作,否则可能会有点一头雾水。
[BPU部署教程] 一文带你轻松走出模型部署新手村
[BPU部署教程] 教你搞定YOLOV5部署 (版本_ 6.2)
在这两篇文章中,你需要搞懂:
# 笔者用的是Ubuntu虚拟机里运行docker,和上面文章里的不要一样
# 第一个-v参数,为了方便上传文件到宿主后,不用再复制进docker容器
# 第二个-v参数,是天工开物工具包的文件路径
sudo docker run -it --rm \
-v /home/walker/horizon:/horizon \
-v /home/walker/horizon/horizon_xj3_open_explorer_v2.3.3_20220727:/open_explorer \
hub.hobot.cc/aitools/ai_toolchain_centos_7_xj3:v2.3.3
# convert_det.yaml
model_parameters:
onnx_model: 'model.onnx'
output_model_file_prefix: 'model'
march: 'bernoulli2'
input_parameters:
input_type_train: 'bgr'
input_layout_train: 'NCHW'
input_type_rt: 'nv12'
norm_type: 'data_scale'
scale_value: 0.003921568627451
input_layout_rt: 'NCHW'
calibration_parameters:
cal_data_dir: './calibration_data'
calibration_type: 'max'
max_percentile: 0.9999
compiler_parameters:
compile_mode: 'latency'
optimize_level: 'O3'
debug: False
core_num: 2
# convert_rec.yaml
model_parameters:
onnx_model: 'model.onnx'
output_model_file_prefix: 'model'
march: 'bernoulli2'
input_parameters:
input_type_train: 'bgr'
input_layout_train: 'NCHW'
input_type_rt: 'nv12'
norm_type: 'data_scale'
scale_value: 0.003921568627451
input_layout_rt: 'NCHW'
calibration_parameters:
cal_data_dir: './calibration_data'
calibration_type: 'max'
max_percentile: 0.9999
compiler_parameters:
compile_mode: 'latency'
optimize_level: 'O3'
debug: False
core_num: 2
其次,你需要重新回顾paddle2onnx的内容。因为paddleocr的例子,输入的sharp是动态的,需要将sharp修改为静态。
❗以下命令在paddlepaddle的环境里执行。
# 转化det模型
paddle2onnx --model_dir ./det/LPR_db_mv3_det/inference ^
--model_filename inference.pdmodel ^
--params_filename inference.pdiparams ^
--save_file ./det/LPR_db_mv3_det/det_onnx/model.onnx ^
--opset_version 10 ^
--input_shape_dict="{'x':[1,3,480,640]}" ^
--enable_onnx_checker True ^
--enable_dev_version False
# 转化rec模型
paddle2onnx --model_dir ./rec/LPR_svtr_mv1e_rec/inference ^
--model_filename inference.pdmodel ^
--params_filename inference.pdiparams ^
--save_file ./rec/LPR_svtr_mv1e_rec/rec_onnx/model.onnx ^
--opset_version 10 ^
--input_shape_dict="{'x':[1,3,48,320]}" ^
--enable_onnx_checker True ^
--enable_dev_version False
按PaddleOCR官方说明,改为静态精度会有所降低。
一些说明:
{'x':[1,3,480,640]}
,对应bpu转换yaml文件的NCHW
模式,即{batchsize,颜色,高,宽}。其中高宽是笔者的摄像头分辨率,读者可按实际情况修改。{'x':[1,3,48,320]}
,这个是v3模型训练时输入的图像大小。按笔者的理解,这两个sharp估计不会减少转换的精度。
❗以下命令在天工开物的环境操作(docker)执行。
由于笔者没有完整实现,就不放测试代码了,因为比较混乱可能会误导读者。这里以onnx_v1为例。
# 先cd到output父目录再执行
hb_mapper checker --model-type onnx --march bernoulli2 --model output/det/LPR_db_mv3_det/det_onnx/model.onnx
hb_mapper checker --model-type onnx --march bernoulli2 --model output/rec/LPR_svtr_mv1e_rec/rec_onnx/model.onnx
# 先cd到det_onnx
hb_mapper makertbin --config convert_det.yaml --model-type onnx
成功转接的截图,bin模型文件会输出在model_output里。
同理rec的转化:
# 先cd到rec_onnx
hb_mapper makertbin --config convert_det.yaml --model-type onnx
❗以下命令在paddlepaddle的环境里执行
虽然无法确定结果正不正确,但从性能方面分析,det≈220ms,rec<100ms,预估性能从cpu的1帧每秒提升到2~3帧每秒吧。
对笔者来说,是首次接触arm方面的编程,过程也是一波三折,借了3块板子才做到现在的程度。只可惜最后还是能力有限,没完成后处理部分,略感遗憾。
至于车牌识别这个立项,是笔者毕业那年所在公司的主营业务。当时笔者作为一个新手,并未接触甚至完全不清楚其中用到什么技术。现在能用自己的技术经验,做个技术路线不一样的车牌识别demo出来,也算是人生的一个回顾吧。
【AI达人创造营三期】在地平线X3上部署车牌识别系统
Markdown Python Shell Text other
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》