Bootstrap

在OK3588板卡上部署模型实现人工智能OCR应用(十一)

一、主机模型转换

我们依旧采用FastDeploy来部署应用深度学习模型到OK3588板卡上

进入主机Ubuntu的虚拟环境
conda activate ok3588

安装rknn-toolkit2(该工具不能在OK3588板卡上完成模型转换)

git clone https://github.com/rockchip-linux/rknn-toolkit2
cd rknn-toolkit2
注意这里需要1.4的版本
git checkout v1.4.0 -f
cd packages
pip install rknn_toolkit2-1.4.0_22dcfef4-cp36-cp36m-linux_x86_64.whl

下载FastDeploy

git clone https://github.com/PaddlePaddle/FastDeploy
cd FastDeploy/examples/vision/ocr/PP-OCR

下载PP-OCRv3文字检测模型

wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar
tar -xvf ch_PP-OCRv3_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-OCRv3文字识别模型

wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar
tar -xvf ch_PP-OCRv3_rec_infer.tar

安装模型转换工具

pip install paddle2onnx
pip install pyyaml

paddle2onnx --model_dir ch_PP-OCRv3_det_infer \
            --model_filename inference.pdmodel \
            --params_filename inference.pdiparams \
            --save_file ch_PP-OCRv3_det_infer/ch_PP-OCRv3_det_infer.onnx \
            --enable_dev_version True
paddle2onnx --model_dir ch_ppocr_mobile_v2.0_cls_infer \
            --model_filename inference.pdmodel \
            --params_filename inference.pdiparams \
            --save_file ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.onnx \
            --enable_dev_version True
paddle2onnx --model_dir ch_PP-OCRv3_rec_infer \
            --model_filename inference.pdmodel \
            --params_filename inference.pdiparams \
            --save_file ch_PP-OCRv3_rec_infer/ch_PP-OCRv3_rec_infer.onnx \
            --enable_dev_version True
固定模型的输入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]}"
转换成RKNN模型
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_det.yaml \
                              --target_platform rk3588
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_rec.yaml \
                              --target_platform rk3588
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_cls.yaml \
                              --target_platform rk3588

这时生成了三个可以部署在OK3588上的模型文件
ch_ppocr_mobile_v20_cls_infer_rk3588_unquantized.rknn
ch_PP-OCRv3_rec_infer_rk3588_unquantized.rknn
ch_PP-OCRv3_det_infer_rk3588_unquantized.rknn
把这三个文件传输到OK3588板卡上

二、板卡模型部署

进入虚拟环境
conda activate ok3588
cd FastDeploy/examples/vision/ocr/PP-OCR/rockchip/cpp
mkdir build
cd build
cmake .. -DFASTDEPLOY_INSTALL_DIR=/home/forlinx/FastDeploy/build/fastdeploy-0.0.0/
make -j
得到了编译后的文件 infer_demo

三、执行推理

下载图片和字典文件

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

拷贝RKNN模型到build目录

三个模型文件
ch_ppocr_mobile_v20_cls_infer_rk3588_unquantized.rknn
ch_PP-OCRv3_rec_infer_rk3588_unquantized.rknn
ch_PP-OCRv3_det_infer_rk3588_unquantized.rknn
放在build文件夹里面

RKNPU推理
./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
推理结果展示

在这里插入图片描述

在这里插入图片描述

;