轉(zhuǎn)換paddleocr到瑞芯微rk3588
1 使用fastdeploy+python SDK方式移植padleocr到rk3588
padocr的模型是基于paddlepaddle框架的,因此需要先將paddlepaddle的模型轉(zhuǎn)為onnx后再將onnx轉(zhuǎn)為rknn模型梁钾。獲得rknn模型后跨算,可以直接借助百度的FastDeploy框架使用文字識(shí)別模型。整體思路如下:
- 步驟1:服務(wù)端環(huán)境搭建(rknn-toolkit2 1.5.0)鼓鲁,用于轉(zhuǎn)換RKNN模型戒努。由于rknn-toolkit2-1.5.2轉(zhuǎn)換paddle-ocr的模型之后啄骇,會(huì)出現(xiàn)推理錯(cuò)亂的情況宵蛀,因此將onnx轉(zhuǎn)rknn時(shí)昆著,需要使用rknn-toolkit2-1.5.0版本,但可以使用rknn-toolkit2-1.5.2進(jìn)行推理术陶。
- 步驟2:邊緣端環(huán)境搭建:安裝rknpu2/編譯FastDeploy/編譯FastDeploy Python SDK
- 步驟3:使用FastDeploy Python SDK進(jìn)行模型推理凑懂。
1.1 安裝模型轉(zhuǎn)換環(huán)境
1.1.1 服務(wù)器環(huán)境安裝
下載rknn-toolkit2-1.5.0,然后根據(jù)需要安裝其他依賴即可梧宫。
wget https://github.com/rockchip-linux/rknn-toolkit2/blob/v1.5.0/packages/rknn_toolkit2-1.5.0%2B1fa95b5c-cp38-cp38-linux_x86_64.whl
# numpy的版本過(guò)高是會(huì)出現(xiàn) module 'numpy' has no attribute 'bool'
pip3 install numpy==1.23.1 -i https://mirror.baidu.com/pypi/simple
# 安裝rknn-toolkit2-1.5.0
pip3 install --no-dependencies rknn_toolkit2-1.5.0+1fa95b5c-cp38-cp38-linux_x86_64.whl
1.1.2 板端環(huán)境rknpu2安裝
參考:https://doc.embedfire.com/linux/rk356x/Ai/zh/latest/lubancat_ai/example/pp_ocrv3.html#id3
1.1.3 板端編譯FastDeploy安裝
板端編譯FastDeploy C++ SDK
git clone https://github.com/PaddlePaddle/FastDeploy.git
cd FastDeploy
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
** 配置環(huán)境變量**
# 永久配置
source PathToFastDeploySDK/fastdeploy_init.sh
sudo cp PathToFastDeploySDK/fastdeploy_libs.conf /etc/ld.so.conf.d/
sudo ldconfig
** 編譯FastDeploy Python SDK**
git clone https://github.com/PaddlePaddle/FastDeploy.git
cd FastDeploy
# 如果您使用的是develop分支輸入以下命令
git checkout develop
cd python
export ENABLE_ORT_BACKEND=ON
export ENABLE_RKNPU2_BACKEND=ON
export ENABLE_VISION=ON
export RKNN2_TARGET_SOC=RK3588
# 如果你的核心板的運(yùn)行內(nèi)存大于等于8G接谨,我們建議您執(zhí)行以下命令進(jìn)行編譯。
python3 setup.py build
python3 setup.py bdist_wheel
cd dist
pip3 install fastdeploy_python-0.0.0-cp39-cp39-linux_aarch64.whl
1.2 模型轉(zhuǎn)換
1.2.1 下載paddleocr v4模型
官網(wǎng)地址:https://github.com/PaddlePaddle/PaddleOCR
wget https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
wget https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_rec_infer.tar
1.2.2 paddle轉(zhuǎn)onnx轉(zhuǎn)rknn
文字檢測(cè)模型轉(zhuǎn)換
# paddle模型轉(zhuǎn)onnx
paddle2onnx --model_dir ./model/ch_PP-OCRv4_det_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ./model/ch_PP-OCRv4_det_infer/model.onnx \
--opset_version 12 \
--enable_onnx_checker True \
--enable_dev_version True
# 固定輸出尺寸
python -m paddle2onnx.optimize \
--input_model ./model/ch_PP-OCRv4_det_infer/model.onnx \
--output_model ./model/ch_PP-OCRv4_det_infer/model_fix.onnx \
--input_shape_dict "{'x':[1,3,960,960]}"
# 模型一些類(lèi)型缺少“shape”塘匣,可以使用onnxruntime的工具重新推理下onnx的shape脓豪,參考:https://doc.embedfire.com/linux/rk356x/Ai/zh/latest/lubancat_ai/example/pp_ocrv3.html#pp-ocrv3
python3 /opt/virtualenvs/feiran/lib/python3.8/site-packages/onnxruntime/tools/symbolic_shape_infer.py \
--input ./models/ocr/ch_PP-OCRv4_det_infer/model_fix.onnx \
--output ./models/ocr/ch_PP-OCRv4_det_infer/model_fix_new.onnx
# 編寫(xiě)腳本將onnx轉(zhuǎn)為rknn,參考:https://github.com/PaddlePaddle/FastDeploy/blob/develop/tools/rknpu2/export.py
文字方向判別模型
# paddle模型轉(zhuǎn)onnx
paddle2onnx --model_dir ./model/ch_ppocr_mobile_v2.0_cls_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ./model/ch_ppocr_mobile_v2.0_cls_infer/model.onnx \
--opset_version 12 \
--enable_onnx_checker True \
--enable_dev_version True
# 固定輸出尺寸
python -m paddle2onnx.optimize \
--input_model ./model/ch_ppocr_mobile_v2.0_cls_infer/model.onnx \
--output_model ./model/ch_ppocr_mobile_v2.0_cls_infer/model_fix.onnx \
--input_shape_dict "{'x':[1,3,48,192]}"
# 模型一些類(lèi)型缺少“shape”忌卤,可以使用onnxruntime的工具重新推理下onnx的shape扫夜,參考:https://doc.embedfire.com/linux/rk356x/Ai/zh/latest/lubancat_ai/example/pp_ocrv3.html#pp-ocrv3
python3 /opt/virtualenvs/feiran/lib/python3.8/site-packages/onnxruntime/tools/symbolic_shape_infer.py \
--input ./models/ocr/ch_ppocr_mobile_v2.0_cls_infer/model_fix.onnx \
--output ./models/ocr/ch_ppocr_mobile_v2.0_cls_infer/model_fix_new.onnx
# 編寫(xiě)腳本將onnx轉(zhuǎn)為rknn,參考:https://github.com/PaddlePaddle/FastDeploy/blob/develop/tools/rknpu2/export.py
文字識(shí)別模型轉(zhuǎn)換
# paddle模型轉(zhuǎn)onnx
paddle2onnx --model_dir ./model/ch_PP-OCRv4_rec_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ./model/ch_PP-OCRv4_rec_infer/model.onnx \
--opset_version 12 \
--enable_onnx_checker True \
--enable_dev_version True
# 固定輸出尺寸
python -m paddle2onnx.optimize \
--input_model ./model/ch_PP-OCRv4_rec_infer/model.onnx \
--output_model ./model/ch_PP-OCRv4_rec_infer/model_fix.onnx \
--input_shape_dict "{'x':[1,3,48,320]}"
# 模型一些類(lèi)型缺少“shape”驰徊,可以使用onnxruntime的工具重新推理下onnx的shape笤闯,參考:https://doc.embedfire.com/linux/rk356x/Ai/zh/latest/lubancat_ai/example/pp_ocrv3.html#pp-ocrv3
python3 /opt/virtualenvs/feiran/lib/python3.8/site-packages/onnxruntime/tools/symbolic_shape_infer.py \
--input ./models/ocr/ch_PP-OCRv4_rec_infer/model_fix.onnx \
--output ./models/ocr/ch_PP-OCRv4_rec_infer/model_fix_new.onnx
# 編寫(xiě)腳本將onnx轉(zhuǎn)為rknn,參考:https://github.com/PaddlePaddle/FastDeploy/blob/develop/tools/rknpu2/export.py
1.3 模型推理
import os
import cv2
import fastdeploy as fd
from pathlib import Path
if __name__ == "__main__":
rec_option = fd.RuntimeOption()
rec_option.use_rknpu2()
rec_format = fd.ModelFormat.RKNN
det_option = fd.RuntimeOption()
det_option.use_rknpu2()
det_format = fd.ModelFormat.RKNN
det_model = fd.vision.ocr.DBDetector(
"../model/ocr/ch_PP-OCRv4_det_infer/model_fix.rknn",
"",
runtime_option=det_option,
model_format=det_format)
det_model.preprocessor.static_shape_infer = True
det_model.preprocessor.disable_normalize()
det_model.preprocessor.disable_permute()
rec_model = fd.vision.ocr.Recognizer(
"../model/ocr/ch_PP-OCRv4_rec_infer/model_fix.rknn",
"",
"../model/ocr/dicts/ppocr_keys_v1.txt",
runtime_option=rec_option,
model_format=rec_format)
rec_model.preprocessor.static_shape_infer = True
rec_model.preprocessor.disable_normalize()
rec_model.preprocessor.disable_permute()
ppocr_v3 = fd.vision.ocr.PPOCRv4(
det_model=det_model, cls_model=None, rec_model=rec_model)
ppocr_v3.cls_batch_size = 1
ppocr_v3.rec_batch_size = 1
for file in Path("../data/src_ocr_images/license_plate").rglob("*.png"):
im = cv2.imread(file.as_posix())
result = ppocr_v3.predict(im)
vis_im = fd.vision.vis_ppocr(im, result)
cv2.imwrite(os.path.join("../data/dst_ocr_images/license_plate", os.path.basename(file)), vis_im)
# result = rec_model.predict(im)
print(file.as_posix(), result)
1.4 問(wèn)題
問(wèn)題1:文字識(shí)別模型推理后解析的結(jié)果與圖片中的文字不一樣棍厂?
解決方法:參考https://github.com/PaddlePaddle/FastDeploy/issues/2218
“1.5.2的toolkit2轉(zhuǎn)換rknn模型有問(wèn)題颗味,最后支持的版本是1.5.1b19,你可以用1.5.1b19的工具轉(zhuǎn)換模型牺弹。但是這個(gè)問(wèn)題還是看下吧浦马,因?yàn)榘遄右呀?jīng)支持半自動(dòng)的動(dòng)態(tài)輸入了,目前的靜態(tài)resize輸入識(shí)別率很低张漂,估計(jì)變形太嚴(yán)重都丟了”晶默。使用1.5.1b19版本的rknn導(dǎo)出模型。
找不到對(duì)應(yīng)版本的模型航攒,使用1.5.0可以正常推理荤胁,下載地址https://github.com/rockchip-linux/rknn-toolkit2/blob/v1.5.0/packages/rknn_toolkit2-1.5.0%2B1fa95b5c-cp38-cp38-linux_x86_64.whl。
問(wèn)題2:rknn-toolkit2降低到1.5.0版本后屎债,出現(xiàn)下面錯(cuò)誤
E load_onnx: onnx.onnx_cpp2py_export.checker.ValidationError: Field 'shape' of 'type' is required but missing.
模型一些類(lèi)型缺少“shape”仅政,可以使用onnxruntime的工具重新推理下onnx的shape:
python symbolic_shape_infer.py --input ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer_old.onnx
--output ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.onnx
symbolic_shape_infer.py位于onnxruntime的安裝目錄中/opt/virtualenvs/feiran/lib/python3.8/site-packages/onnxruntime/tools/symbolic_shape_infer.py
問(wèn)題3:轉(zhuǎn)換cls和rec模型時(shí)報(bào)錯(cuò)
E build: AttributeError: module 'numpy' has no attribute 'bool'.
降低numpy的版本:pip3 install numpy==1.23.1 -i https://mirror.baidu.com/pypi/simple
參考
- 百度FastDeploy提供的Demo:https://github.com/PaddlePaddle/FastDeploy/blob/develop/tools/rknpu2/export.py
- 模型轉(zhuǎn)換步驟:https://github.com/PaddlePaddle/FastDeploy/tree/develop/examples/vision/ocr/PP-OCR/rockchip/python
- 編譯步驟:https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/rknpu2.md
- 嵌入式AI應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)指南—基于LubanCat-RK系列板卡:https://doc.embedfire.com/linux/rk356x/Ai/zh/latest/lubancat_ai/example/pp_ocrv3.html