3. Inference - Third-party Models
推理 - 第三方模型¶
1. 第三方模型支持列表¶
MindOCR可以支持第三方模型(如PaddleOCR、MMOCR等)的推理,本文档展示了已适配的模型列表。 性能测试基于Ascend310P,部分模型暂无测试数据集。
1.1 文本检测¶
名称 | 模型 | 骨干网络 | 数据集 | F-score(%) | FPS | 来源 | 配置文件 | 下载 | 参考链接 |
---|---|---|---|---|---|---|---|---|---|
ch_pp_server_det_v2.0 | DBNet | ResNet18_vd | MLT17 | 46.22 | 21.65 | PaddleOCR | yaml | weight | ch_ppocr_server_v2.0_det |
ch_pp_det_OCRv3 | DBNet | MobileNetV3 | MLT17 | 33.89 | 22.40 | PaddleOCR | yaml | weight | ch_PP-OCRv3_det |
ch_pp_det_OCRv2 | DBNet | MobileNetV3 | MLT17 | 42.99 | 21.90 | PaddleOCR | yaml | weight | ch_PP-OCRv2_det |
ch_pp_mobile_det_v2.0_slim | DBNet | MobileNetV3 | MLT17 | 31.66 | 19.88 | PaddleOCR | yaml | weight | ch_ppocr_mobile_slim_v2.0_det |
ch_pp_mobile_det_v2.0 | DBNet | MobileNetV3 | MLT17 | 31.56 | 21.96 | PaddleOCR | yaml | weight | ch_ppocr_mobile_v2.0_det |
en_pp_det_OCRv3 | DBNet | MobileNetV3 | IC15 | 42.14 | 55.55 | PaddleOCR | yaml | weight | en_PP-OCRv3_det |
ml_pp_det_OCRv3 | DBNet | MobileNetV3 | MLT17 | 66.01 | 22.48 | PaddleOCR | yaml | weight | ml_PP-OCRv3_det |
en_pp_det_dbnet_resnet50vd | DBNet | ResNet50_vd | IC15 | 79.89 | 21.17 | PaddleOCR | yaml | weight | DBNet |
en_pp_det_psenet_resnet50vd | PSE | ResNet50_vd | IC15 | 80.44 | 7.75 | PaddleOCR | yaml | weight | PSE |
en_pp_det_east_resnet50vd | EAST | ResNet50_vd | IC15 | 85.58 | 20.70 | PaddleOCR | yaml | weight | EAST |
en_pp_det_sast_resnet50vd | SAST | ResNet50_vd | IC15 | 81.77 | 22.14 | PaddleOCR | yaml | weight | SAST |
en_mm_det_dbnetpp_resnet50 | DBNet++ | ResNet50 | IC15 | 81.36 | 10.66 | MMOCR | yaml | weight | DBNetpp |
en_mm_det_fcenet_resnet50 | FCENet | ResNet50 | IC15 | 83.67 | 3.34 | MMOCR | yaml | weight | FCENet |
注意:在使用en_pp_det_psenet_resnet50vd模型进行推理时,需要使用以下命令修改onnx文件
python deploy/models_utils/onnx_optim/insert_pse_postprocess.py \
--model_path=./pse_r50vd.onnx \
--binary_thresh=0.0 \
--scale=1.0
1.2 文本识别¶
名称 | 模型 | 骨干网络 | 数据集 | Acc(%) | FPS | 来源 | 字典文件 | 配置文件 | 下载 | 参考链接 |
---|---|---|---|---|---|---|---|---|---|---|
ch_pp_server_rec_v2.0 | CRNN | ResNet34 | MLT17 (ch) | 49.91 | 154.16 | PaddleOCR | ppocr_keys_v1.txt | yaml | weight | ch_ppocr_server_v2.0_rec |
ch_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | MLT17 (ch) | 49.91 | 408.38 | PaddleOCR | ppocr_keys_v1.txt | yaml | weight | ch_PP-OCRv3_rec |
ch_pp_rec_OCRv2 | CRNN | MobileNetV1Enhance | MLT17 (ch) | 44.59 | 203.34 | PaddleOCR | ppocr_keys_v1.txt | yaml | weight | ch_PP-OCRv2_rec |
ch_pp_mobile_rec_v2.0 | CRNN | MobileNetV3 | MLT17 (ch) | 24.59 | 167.67 | PaddleOCR | ppocr_keys_v1.txt | yaml | weight | ch_ppocr_mobile_v2.0_rec |
en_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | MLT17 (en) | 79.79 | 917.01 | PaddleOCR | en_dict.txt | yaml | weight | en_PP-OCRv3_rec |
en_pp_mobile_rec_number_v2.0_slim | CRNN | MobileNetV3 | / | / | / | PaddleOCR | en_dict.txt | yaml | weight | en_number_mobile_slim_v2.0_rec |
en_pp_mobile_rec_number_v2.0 | CRNN | MobileNetV3 | / | / | / | PaddleOCR | en_dict.txt | yaml | weight | en_number_mobile_v2.0_rec |
korean_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | / | / | / | PaddleOCR | korean_dict.txt | yaml | weight | korean_PP-OCRv3_rec |
japan_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | / | / | / | PaddleOCR | japan_dict.txt | yaml | weight | japan_PP-OCRv3_rec |
chinese_cht_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | / | / | / | PaddleOCR | chinese_cht_dict.txt | yaml | weight | chinese_cht_PP-OCRv3_rec |
te_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | / | / | / | PaddleOCR | te_dict.txt | yaml | weight | te_PP-OCRv3_rec |
ka_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | / | / | / | PaddleOCR | ka_dict.txt | yaml | weight | ka_PP-OCRv3_rec |
ta_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | / | / | / | PaddleOCR | ta_dict.txt | yaml | weight | ta_PP-OCRv3_rec |
latin_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | / | / | / | PaddleOCR | latin_dict.txt | yaml | weight | latin_PP-OCRv3_rec |
arabic_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | / | / | / | PaddleOCR | arabic_dict.txt | yaml | weight | arabic_PP-OCRv3_rec |
cyrillic_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | / | / | / | PaddleOCR | cyrillic_dict.txt | yaml | weight | cyrillic_PP-OCRv3_rec |
devanagari_pp_rec_OCRv3 | SVTR | MobileNetV1Enhance | / | / | / | PaddleOCR | devanagari_dict.txt | yaml | weight | devanagari_PP-OCRv3_rec |
en_pp_rec_crnn_resnet34vd | CRNN | ResNet34_vd | IC15 | 66.35 | 420.80 | PaddleOCR | ic15_dict.txt | yaml | weight | CRNN |
en_pp_rec_rosetta_resnet34vd | Rosetta | Resnet34_vd | IC15 | 64.28 | 552.40 | PaddleOCR | ic15_dict.txt | yaml | weight | Rosetta |
en_pp_rec_vitstr_vitstr | ViTSTR | ViTSTR | IC15 | 68.42 | 364.67 | PaddleOCR | EN_symbol_dict.txt | yaml | weight | ViTSTR |
en_mm_rec_nrtr_resnet31 | NRTR | ResNet31 | IC15 | 67.26 | 32.63 | MMOCR | english_digits_symbols.txt | yaml | weight | NRTR |
en_mm_rec_satrn_shallowcnn | SATRN | ShallowCNN | IC15 | 73.52 | 32.14 | MMOCR | english_digits_symbols.txt | yaml | weight | SATRN |
1.3 文本方向分类¶
名称 | 模型 | 数据集 | Acc(%) | FPS | 来源 | 配置文件 | 下载 | 参考链接 |
---|---|---|---|---|---|---|---|---|
ch_pp_mobile_cls_v2.0 | MobileNetV3 | / | / | / | PaddleOCR | yaml | weight | ch_ppocr_mobile_v2.0_cls |
2. 第三方推理流程总览¶
3. 第三方模型推理方法¶
3.1 文本检测¶
下面以第三方模型支持列表中的en_pp_det_dbnet_resnet50vd
为例介绍推理方法:
-
下载第三方模型支持列表中的权重文件weight并解压;
-
由于该模型为paddle训练模型,需要先转换为推理模型(已为推理模型则跳过此步):
git clone https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR
python tools/export_model.py \
-c configs/det/det_r50_vd_db.yml \
-o Global.pretrained_model=./det_r50_vd_db_v2.0_train/best_accuracy \
Global.save_inference_dir=./det_db
det_db/
├── inference.pdmodel
├── inference.pdiparams
├── inference.pdiparams.info
- 下载并使用paddle2onnx工具(
pip install paddle2onnx
),将推理模型转换成onnx文件:
paddle2onnx \
--model_dir det_db \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file det_db.onnx \
--opset_version 11 \
--input_shape_dict="{'x':[-1,3,-1,-1]}" \
--enable_onnx_checker True
参数 | 参数说明 |
---|---|
--model_dir | 配置包含Paddle模型的目录路径 |
--model_filename | [可选] 配置位于 --model_dir 下存储网络结构的文件名 |
--params_filename | [可选] 配置位于 --model_dir 下存储模型参数的文件名称 |
--save_file | 指定转换后的模型保存目录路径 |
--opset_version | [可选] 配置转换为ONNX的OpSet版本,目前支持 7~16 等多个版本,默认为 9 |
--input_shape_dict | 输入Tensor的形状,用于生成动态ONNX模型,格式为 "{'x':[N,C,H,W]}",-1表示动态shape |
--enable_onnx_checker | [可选] 配置是否检查导出为 ONNX 模型的正确性, 建议打开此开关, 默认为 False |
参数中--input_shape_dict
的值,可以通过Netron工具打开推理模型查看。
了解更多paddle2onnx
上述命令执行完成后会生成det_db.onnx
文件;
- 在Ascend310/310P上使用converter_lite工具将onnx文件转换为mindir:
创建config.txt
并指定模型输入shape,一个示例如下:
[ascend_context]
input_format=NCHW
input_shape=x:[1,3,736,1280]
参数 | 属性 | 功能描述 | 参数类型 | 取值说明 |
---|---|---|---|---|
input_format | 可选 | 指定模型输入format | String | 可选有"NCHW"、"NHWC"、"ND" |
input_shape | 可选 | 指定模型输入Shape,input_name必须是转换前的网络模型中的输入名称,按输入次序排列,用;隔开 | String | 例如:"input1:[1,64,64,3];input2:[1,256,256,3]" |
dynamic_dims | 可选 | 指定动态BatchSize和动态分辨率参数 | String | 例如:"dynamic_dims=[48,520],[48,320],[48,384]" |
了解更多配置参数
执行以下命令:
converter_lite \
--saveType=MINDIR \
--fmk=ONNX \
--optimize=ascend_oriented \
--modelFile=det_db.onnx \
--outputFile=det_db_output \
--configFile=config.txt
det_db_output.mindir
模型文件;
converter_lite参数简要说明如下:
参数 | 是否必选 | 参数说明 | 取值范围 | 默认值 | 备注 |
---|---|---|---|---|---|
fmk | 是 | 输入模型的原始格式 | MINDIR、CAFFE、TFLITE、TF、ONNX | - | - |
saveType | 否 | 设定导出的模型为mindir模型或者ms模型 | MINDIR、MINDIR_LITE | MINDIR | 云侧推理版本只有设置为MINDIR转出的模型才可以推理 |
modelFile | 是 | 输入模型的路径 | - | - | - |
outputFile | 是 | 输出模型的路径,不需加后缀,可自动生成.mindir后缀 | - | - | - |
configFile | 否 | 1)可作为训练后量化配置文件路径;2)可作为扩展功能配置文件路径 | - | - | - |
optimize | 否 | 设置针对设备的模型优化类型。若未设置,则不做优化 | none、general、gpu_oriented、ascend_oriented | - | - |
了解更多converter_lite
了解更多模型转换教程
- 使用
/deploy/py_infer/infer.py
脚本和det_db_output.mindir
文件执行推理:
python infer.py \
--input_images_dir=/path/to/ic15/ch4_test_images \
--det_model_path=/path/to/mindir/det_db_output.mindir \
--det_model_name_or_config=en_pp_det_dbnet_resnet50vd \
--res_save_dir=/path/to/dbnet_resnet50vd_results
--res_save_dir
所指目录下生成预测文件det_results.txt
;
在进行推理时,可使用--vis_det_save_dir
参数进行结果可视化:
文本检测结果可视化
了解更多infer.py推理参数
- 使用以下命令评估结果:
结果为:
python deploy/eval_utils/eval_det.py \ --gt_path=/path/to/ic15/test_det_gt.txt \ --pred_path=/path/to/dbnet_resnet50vd_results/det_results.txt
{'recall': 0.8281174771304767, 'precision': 0.7716464782413638, 'f-score': 0.7988852763585693}
3.2 文本识别¶
下面以第三方模型支持列表中的en_pp_rec_OCRv3
为例介绍推理方法:
-
下载第三方模型支持列表中的权重文件weight并解压;
-
由于该模型为paddle推理模型,直接进行第三步paddle转onnx(否则需要将训练模型转换为推理模型,参考上述文本检测);
-
下载并使用paddle2onnx工具(
pip install paddle2onnx
),将推理模型转换成onnx文件:
paddle2onnx \
--model_dir en_PP-OCRv3_rec_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file en_PP-OCRv3_rec_infer.onnx \
--opset_version 11 \
--input_shape_dict="{'x':[-1,3,48,-1]}" \
--enable_onnx_checker True
参数中--input_shape_dict
的值,可以通过Netron工具打开推理模型查看。
了解更多paddle2onnx
参数中--input_shape_dict
的值,可以通过Netron工具打开推理模型查看。
上述命令执行完成后会生成en_PP-OCRv3_rec_infer.onnx
文件;
- 在Ascend310/310P上使用converter_lite工具将onnx文件转换为mindir:
创建config.txt
并指定模型输入shape,一个示例如下:
[ascend_context]
input_format=NCHW
input_shape=x:[1,3,-1,-1]
dynamic_dims=[48,520],[48,320],[48,384],[48,360],[48,394],[48,321],[48,336],[48,368],[48,328],[48,685],[48,347]
了解更多配置参数
执行以下命令:
converter_lite \
--saveType=MINDIR \
--fmk=ONNX \
--optimize=ascend_oriented \
--modelFile=en_PP-OCRv3_rec_infer.onnx \
--outputFile=en_PP-OCRv3_rec_infer \
--configFile=config.txt
en_PP-OCRv3_rec_infer.mindir
模型文件;
converter_lite参数简要说明请见上述文本检测样例。
了解更多converter_lite
了解更多模型转换教程
- 下载模型对应的字典文件en_dict.txt,使用
/deploy/py_infer/infer.py
脚本和en_PP-OCRv3_rec_infer.mindir
文件执行推理:
python infer.py \
--input_images_dir=/path/to/mlt17_en \
--rec_model_path=/path/to/mindir/en_PP-OCRv3_rec_infer.mindir \
--rec_model_name_or_config=en_pp_rec_OCRv3 \
--character_dict_path=/path/to/en_dict.txt \
--res_save_dir=/path/to/en_rec_infer_results
--res_save_dir
所指目录下生成预测文件rec_results.txt
。
了解更多infer.py推理参数
- 使用以下命令评估结果:
python deploy/eval_utils/eval_rec.py \
--gt_path=/path/to/mlt17_en/english_gt.txt \
--pred_path=/path/to/en_rec_infer_results/rec_results.txt
{'acc': 0.7979344129562378, 'norm_edit_distance': 0.8859519958496094}