rknn_zoo RKNPU2_SDK RKNN Model Zoo
(经测试已经跑通)
一、PC电脑是Ubuntu22.04系统中完成环境搭建(板子是20.04)
- 安装模型转换环境
- conda create -n rknn2 python==3.10
- conda activate rknn2
- 安装Ubuntu依赖包
sudo apt-get install libxslt1-dev zlib1g zlib1g-dev libglib2.0-0 libsm6 libgl1-mesa-glx libprotobuf-dev gcc g++
安装RKNNtoolkit2
RKNNtoolkit2的作用是将onnx模型转为rknn模型
pip install numpy==1.19.4 --only-binary=:all: -i https://pypi.tuna.tsinghua.edu.cn/simple
git clone https://github.com/rockchip-linux/rknn-toolkit2
安装 python3.9版本对应的 RKNN-Toolkit2(看清楚python版本在安装)
cd rknn-toolkit2/rknn-toolkit2/packages/
pip install -r requirements_cp39-1.6.0.txt -i https://pypi.mirrors.ustc.edu.cn/simple
pip install rknn_toolkit2-1.6.0+81f21f4d-cp39-cp39-linux_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
python3.10版本
pip install rknn_toolkit2-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
报错:
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorboard 2.10.1 requires protobuf<3.20,>=3.9.2, but you have protobuf 3.20.3 which is incompatible.
tensorflow 2.10.0 requires protobuf<3.20,>=3.9.2, but you have protobuf 3.20.3 which is incompatible.
解决方法:
pip install tensorboard==2.15.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tensorflow==2.15.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
PC 端安装 Paddle2ONNX
安装paddle2onnx
这一步为下面模型转换做打算:
paddle2onnx的作用:
paddle模型------>onnx模型
RKNNtoolkit2的作用:
onnx模型------>rknn模型
安装paddle2onnx的过程极为简单,在终端输入:
pip install paddle2onnx -i https://pypi.tuna.tsinghua.edu.cn/simple
至此,ubuntu上面的环境已经搭建完毕。
2、在板子上完成环境搭建
FastDeploy库的编译(在rk3588板子上进行)
rk3588性能强劲,可以直接在板子上借助图形界面编译FastDeploy库
使用git clone https://github.com/PaddlePaddle/FastDeploy.git指令拉取代码
- sudo apt-get update
- sudo apt-get install cmake
拉去代码在本地后执行如下操作:拉去代码在本地后执行如下操作:
- cd FastDeploy
-
- # 如果您使用的是develop分支输入以下命令 git checkout develop
-
- mkdir build && cd build
- cmake .. -DENABLE_ORT_BACKEND=OFF \
- -DENABLE_RKNPU2_BACKEND=ON \
- -DENABLE_VISION=ON \
- -DRKNN2_TARGET_SOC=RK3588 \
- -DCMAKE_INSTALL_PREFIX=${PWD}/fastdeploy-0.0.0
# build if soc is RK3588
make -j8
make install
### 配置环境变量
为了方便大家配置环境变量,FastDeploy提供了一键配置环境变量的脚本,在运行程序前,你需要执行以下命令
```bash
# 临时配置
source /home/orangepi/work_11.7/FastDeploy/build/fastdeploy-0.0.0/fastdeploy_init.sh
# 永久配置
source /home/orangepi/work_11.7/FastDeploy/build/fastdeploy-0.0.0/fastdeploy_init.sh
sudo cp /home/orangepi/work_11.7/FastDeploy/build/fastdeploy-0.0.0/fastdeploy_libs.conf /etc/ld.so.conf.d/
sudo ldconfig
执行完毕后FastDeploy库就编译完成了。请留意现在的地址,后面编译ocr程序时会用上
- OCR程序的编译
官方教程
按照官方教程安装三个模型:
此处装模型操作均在Ubuntu系统上,注意不是在板子上!!!
- #新建一个文件夹
- mkdir ppocr
- cd ppocr
- # 下载PP-OCRv4文字检测模型
- wget https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar
- tar -xvf ch_PP-OCRv4_det_infer.tar
- # 下载文字方向分类器模型
- wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
- tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar
- # 下载PP-OCRv4文字识别模型
- wget https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_rec_infer.tar
- tar -xvf ch_PP-OCRv4_rec_infer.tar
-
-
上面操作结束后会得到三个装有paddle模型的文件夹,随即进入paddle----onnx模型步骤
新的转onnx命令:
- paddle2onnx --model_dir ch_PP-OCRv4_det_infer \
- --model_filename inference.pdmodel \
- --params_filename inference.pdiparams \
- --save_file det4.onnx
-
- paddle2onnx --model_dir ch_ppocr_mobile_v2.0_cls_infer \
- --model_filename inference.pdmodel \
- --params_filename inference.pdiparams \
- --save_file cls2.onnx
-
- paddle2onnx --model_dir ch_PP-OCRv4_rec_infer \
- --model_filename inference.pdmodel \
- --params_filename inference.pdiparams \
- --save_file rec4.onnx
然后固定onnx模型的形状:
这里需要注意的是,根据ubuntu系统上python版本的不同,python指令可能会替换为python3
- python3 -m paddle2onnx.optimize --input_model cls2.onnx \
- --output_model cls2_shape.onnx \
- --input_shape_dict "{'x':[1,3,48,192]}"
-
- python3 -m paddle2onnx.optimize --input_model rec4.onnx \
- --output_model rec4_shape.onnx \
- --input_shape_dict "{'x':[1,3,48,320]}"
-
- python3 -m paddle2onnx.optimize --input_model det4.onnx \
- --output_model det4_shape.onnx \
- --input_shape_dict "{'x':[1,3,960,960]}"
或
- # 固定模型的输入shape
- python -m paddle2onnx.optimize --input_model ch_PP-OCRv3_det_infer/ch_PP-OCRv3_det_infer.onnx \
- --output_model ch_PP-OCRv3_det_infer/ch_PP-OCRv3_det_infer.onnx \
- --input_shape_dict "{'x':[1,3,960,960]}"
-
- python -m paddle2onnx.optimize --input_model ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.onnx \
- --output_model ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.onnx \
- --input_shape_dict "{'x':[1,3,48,192]}"
-
- python -m paddle2onnx.optimize --input_model ch_PP-OCRv3_rec_infer/ch_PP-OCRv3_rec_infer.onnx \
- --output_model ch_PP-OCRv3_rec_infer/ch_PP-OCRv3_rec_infer.onnx \
- --input_shape_dict "{'x':[1,3,48,320]}"
python3指令:
- python3 -m paddle2onnx.optimize --input_model ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.onnx \
- --output_model ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.onnx \
- --input_shape_dict "{'x':[1,3,48,192]}"
-
- python3 -m paddle2onnx.optimize --input_model ch_PP-OCRv3_rec_infer/ch_PP-OCRv3_rec_infer.onnx \
- --output_model ch_PP-OCRv3_rec_infer/ch_PP-OCRv3_rec_infer.onnx \
- --input_shape_dict "{'x':[1,3,48,320]}"
-
- python3 -m paddle2onnx.optimize --input_model ch_PP-OCRv3_det_infer/ch_PP-OCRv3_det_infer.onnx \
- --output_model ch_PP-OCRv3_det_infer/ch_PP-OCRv3_det_infer.onnx \
- --input_shape_dict "{'x':[1,3,960,960]}"
-
至此执行完毕后,paddle模型转到onnx模型完毕,接下来是onnx模型转到rknn模型。
将转换完成的onnx放到个rknpu2_tools文件夹下
随后输入以下三条指令:
- python3 export.py --config_path ppocrv3_det.yaml --target_platform rk3588
- python3 export.py --config_path ppocrv3_rec.yaml --target_platform rk3588
- python3 export.py --config_path ppocrv3_cls.yaml --target_platform rk3588
如下图完成转换:
如果遇到问题,试用单个命令找问题:
python3 rknpu2_tools/export.py --config_path rknpu2_tools/config/ppocrv3_det.yaml --target_platform rk3588
python3 rknpu2_tools/export.py --config_path rknpu2_tools/config/ppocrv3_rec.yaml --target_platform rk3588
python3 rknpu2_tools/export.py --config_path rknpu2_tools/config/ppocrv3_cls.yaml --target_platform rk3588
当三条指令结束运行时,终端内容:
这时候在每个模型的解压文件夹下就得到了rknn模型
将这些文件拷贝到板子上:
首先进入板子上刚刚编译完毕的FastDeploy文件夹下的
/FastDeploy/examples/vision/ocr/PP-OCR/rockchip/cpp
创建一个新的文件夹build
回到cpp文件夹下,打开CMakeLists.txt,对include部分进行修改。
- PROJECT(infer_demo C CXX)
- CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
-
- # 指定下载解压后的fastdeploy库路径
- option(FASTDEPLOY_INSTALL_DIR "/home/orangepi/xiazai/FastDeploy/build/fastdeploy-0.0.0.")
-
- include(/home/orangepi/xiazai/FastDeploy/build/fastdeploy-0.0.0/FastDeploy.cmake)
-
- # 添加FastDeploy依赖头文件
- include_directories(${FASTDEPLOY_INCS})
-
- add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc)
- # 添加FastDeploy库依赖
- target_link_libraries(infer_demo ${FASTDEPLOY_LIBS})
cd到刚刚创建的FastDeploy/examples/vision/ocr/PP-OCR/rockchip/cpp/build文件夹下,进行如下操作:
- # 使用编译完成的FastDeploy库编译infer_demo
- cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-rockchip
- make -j
如果报错:
cmake -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-rockchip .
make -j
过程:
可以看到/home/orangepi/xiazai/FastDeploy/examples/vision/ocr/PP-OCR/rockchip/cpp/build/下多了一个infer_demo
我跑通的rknn库的版本2.2.0
这时候就把之前在Ubuntu上转的rknn模型拷贝到板子上,然后下载图片和字典文件
- # 下载图片和字典文件
- wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg
- wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/ppocr/utils/ppocr_keys_v1.txt
这时候就可以运行程序了:
./infer_demo det4_shape_rk3588_unquantized.rknn cls2_shape_rk3588_unquantized.rknn rec4_shape_rk3588_unquantized.rknn ppocr_keys_v1.txt 12.jpg
报错:
./infer_demo: error while loading shared libraries: libopencv_video.so.3.4: cannot open shared object file: No such file or directory
解决方法:
# NPU推理
- ./infer_demo ./ch_PP-OCRv3_det_infer/ch_PP-OCRv3_det_infer_rk3588_unquantized.rknn \
- ./ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v20_cls_infer_rk3588_unquantized.rknn \
- ./ch_PP-OCRv3_rec_infer/ch_PP-OCRv3_rec_infer_rk3588_unquantized.rknn \
- ./ppocr_keys_v1.txt \
- ./12.jpg \
- 1
或
./infer_demo ./ch_PP-OCRv3_det_infer/ch_PP-OCRv3_det_infer_rk3588_unquantized.rknn ./ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v20_cls_infer_rk3588_unquantized.rknn ./ch_PP-OCRv3_rec_infer/ch_PP-OCRv3_rec_infer_rk3588_unquantized.rknn ./ppocr_keys_v1.txt ./41.jpg 1
# CPU推理
python3 infer.py \
--det_model ./ch_PP-OCRv3_det_infer/ch_PP-OCRv3_det_infer.onnx \
--cls_model ./ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.onnx \
--rec_model ./ch_PP-OCRv3_rec_infer/ch_PP-OCRv3_rec_infer.onnx \
--rec_label_file ./ppocr_keys_v1.txt \
--image 12.jpg \
--device cpu
参考:记录如何在RK3588板子上跑通paddle的OCR模型。重点是对齐rknntoolkit版本和板子上的librknnrt.so库_paddleocr rk3588-CSDN博客
评论记录:
回复评论: