OCRv5 系列包含以下5个核心模型,本文参考 ModelZoo适配PaddleOCR 思路,完成了PP-OCRv5涉及的5个模型的NPU迁移适配,供参考使用。
| 模型名称 | 作用 | 推理模型链接 |
|---|---|---|
| PP-OCRv5_server_det | 文本检测模型,定位图像中的文本区域 | 下载链接 |
| PP-OCRv5_server_rec | 文本识别模型,识别检测到的文本内容 | 下载链接 |
| PP-LCNet_x1_0_doc_ori | 文档图像方向分类模块(可选),用于将文档图像的方向区分出来,并使用后处理将其矫正 | 下载链接 |
| UVDoc | 文本图像矫正模块(可选),针对图像进行几何变换,以纠正图像中的文档扭曲、倾斜、透视变形等问题 | 下载链接 |
| PP-LCNet_x1_0_textline_ori | 文本行方向分类模块(可选),将文本行的方向区分出来,并使用后处理将其矫正 | 下载链接 |
注:参考 PaddleOCR官方文档 获取最新信息。
| 配套 | 版本 | 环境准备指导 |
|---|---|---|
| 固件与驱动 | 25.3.rc1 | Pytorch框架推理环境准备 |
| CANN | 8.5.1 | 昇腾镜像仓库 |
| Python | 3.11 | - |
| 算力设备 | Atlas 300I Duo | - |
docker pull swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.1-310p-ubuntu22.04-py3.11
export IMAGE=swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.1-310p-ubuntu22.04-py3.11
docker run --privileged \
--name PaddleOCR \
--net=host \
--shm-size=1g \
--device /dev/davinci_manager \
--device /dev/devmm_svm \
--device /dev/hisi_hdc \
-v /usr/local/dcmi:/usr/local/dcmi \
-v /usr/local/Ascend/driver/tools/hccn_tool:/usr/local/Ascend/driver/tools/hccn_tool \
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
-v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \
-v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
-v /etc/ascend_install.info:/etc/ascend_install.info \
-v /mnt:/mnt \
-w /workspace \
-itd $IMAGE /bin/bash# 1 安装依赖
git clone https://gitcode.com/ascend/ModelZoo-PyTorch.git
cd ModelZoo-PyTorch/ACL_PyTorch/built-in/ocr/PPOCRv5
python -m pip install paddlepaddle==3.2.2
git clone --depth 1 --branch v3.3.2 https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR
pip3 install -e .
git apply ../paddleocr.patch
cd ..
pip install -r requirements.txt
# 2 安装msit工具
pip install msit
msit install surgeon
wget https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/ait/ais_bench-0.0.2-py3-none-any.whl
wget https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/ait/aclruntime-0.0.2-cp311-cp311-linux_aarch64.whl
pip install ais_bench-0.0.2-py3-none-any.whl
pip install aclruntime-0.0.2-cp311-cp311-linux_aarch64.whl
# 3 环境变量注入
source /usr/local/Ascend/ascend-toolkit/set_env.sh下载链接参考 一、模型概述,下载后解压即可,这里以 PP-OCRv5_server_det 模型为例
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_det_infer.tar
tar -xf PP-OCRv5_server_det_infer.tar原生模型权重需要先使用paddle2onnx工具转换为onnx格式,然后再转为om模型,下面以det模型为例
# 1 转换onnx模型格式
paddlex --paddle2onnx --paddle_model_dir ./PP-OCRv5_server_det_infer --onnx_model_dir ./PP-OCRv5_server_det_infer
# 2 onnx模型优化
onnxsim PP-OCRv5_server_det_infer/inference.onnx PP-OCRv5_server_det_infer/inference_sim.onnx
python onnx_optimizer.py PP-OCRv5_server_det_infer/inference_sim.onnx PP-OCRv5_server_det_infer/inference_opt.onnx
# 3 转换om模型格式
# 注意:
# 1) ${soc_version} 需要使用npu-smi info查询芯片名称
# 2) input_shape需要通过查看onnx模型的输入shape确认
# PP-OCRv5_server_det
atc --model=./PP-OCRv5_server_det_infer/inference_opt.onnx --framework=5 --output=./PP-OCRv5_server_det_infer/inference --soc_version=Ascend${soc_version} --input_shape "x:-1,3,-1,-1"
# PP-OCRv5_server_rec
atc --model=./PP-OCRv5_server_rec_infer/inference_opt.onnx --framework=5 --output=./PP-OCRv5_server_rec_infer/inference --soc_version=Ascend${soc_version} --input_shape "x:-1,3,48,-1"
# PP-LCNet_x1_0_doc_ori
atc --model=./PP-LCNet_x1_0_doc_ori_infer/inference_opt.onnx --framework=5 --output=./PP-LCNet_x1_0_doc_ori_infer/inference --soc_version=Ascend${soc_version} --input_shape "x:-1,3,224,224"
# PP-LCNet_x1_0_textline_ori
atc --model=./PP-LCNet_x1_0_textline_ori_infer/inference_opt.onnx --framework=5 --output=./PP-LCNet_x1_0_textline_ori_infer/inference --soc_version=Ascend${soc_version} --input_shape "x:-1,3,80,160"
# UVDoc
atc --model=./UVDoc_infer/inference.onnx --framework=5 --output=./UVDoc_infer/inference --soc_version=Ascend${soc_version} --input_shape "image:-1,3,-1,-1"# 获取推理示例图片
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_002.png
# 附件中获取推理脚本infer_all.py
python3 infer_all.py --image_dir general_ocr_002.png推理脚本 infer_all.py 参数说明
- image_dir:推理图片路径,可以是图片文件或目录
- device_id:NPU 设备 id,默认为 0
- use_doc_orientation_classify:指定是否使用文档方向分类模型,默认为 False
- use_doc_unwarping:指定是否使用文本图像矫正模型,默认为 False
- use_textline_orientation:指定是否使用文本行方向分类模型,默认为 False
- custom_size:ais_bench使用动态shape时指定的buffer size,具体数值根据数据集而异,值过小会导致内存越界,默认值为1e8推理结果展示

模型推理图片效果(注:测试图片来源于paddle官方测试集,下载链接)

推理数据取5次同图片推理请求平均时长
| 图片大小 | det+rec 2模型推理 | det+rec+可选 5模型推理 |
|---|---|---|
| 896 * 528 | 316ms | 609ms |
| 1261 * 1783 | 707ms | 2219ms |
推理测试过程中发现,UVDoc(文本图像矫正)模型对于较大分辨率(如1200*1600)平整图片的推理结果存在一定误差,这种误差在ONNX模型中依然存在,根本原因是模型使用了GridSample算子,此算子在图片尺寸过大时会放大绝对误差,造成推理结果有一些小误差。 针对此问题,推荐使用如下方式进行规避:
class AscendUVDocPredictorZoom:
# ========== 1: 计算缩放因子 ==========
def _compute_scale_factor(self, h, w, max_hw=1300):
"""计算缩放因子,将H/W等比例缩放到max_hw以内"""
if h <= max_hw and w <= max_hw:
return 1.0 # 无需缩放
scale = max_hw / max(h, w) # 按最大边计算比例
return scale
# ========== 2: 图像缩放 ==========
def _resize_image(self, img, scale):
"""按scale因子缩放图像"""
if scale == 1.0:
return img
new_h = int(img.shape[0] * scale)
new_w = int(img.shape[1] * scale)
return cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_LINEAR)
# ========== 3: 恢复原始尺寸 ==========
def _restore_size(self, warped_img, orig_h, orig_w):
"""将去畸变后的图像恢复到原始尺寸"""
if warped_img.shape[0] == orig_h and warped_img.shape[1] == orig_w:
return warped_img
return cv2.resize(warped_img, (orig_w, orig_h), interpolation=cv2.INTER_LINEAR)# infer_all_zoom.py 推理脚本中增加了 --max_hw 参数
# 如果图片尺寸超过 max_hw,则在进行 UVDoc 模型推理前后进行缩放
python3 infer_all_zoom.py --image_dir general_ocr_002.png --max_hw 1300