Skip to content

Commit 9dd4315

Browse files
committed
docs: add v5 convert
1 parent ee7dc4b commit 9dd4315

File tree

3 files changed

+79
-98
lines changed

3 files changed

+79
-98
lines changed

docs/blog/posts/about_model/adapt_PP-OCRv5_mobile_det.md

Lines changed: 79 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ pip install "paddlex[ocr]==3.0.0rc1"
3939

4040
测试PP-OCRv5_mobile_det模型能否正常识别:
4141

42-
测试用图:
43-
44-
![alt text](../images/1.jpg)
45-
4642
!!! tip
4743

4844
运行以下代码时,模型会自动下载到 **/Users/用户名/.paddlex/official_models** 下。
@@ -79,59 +75,114 @@ for res in result:
7975
PaddleX官方集成了paddle2onnx的转换代码:
8076

8177
```bash linenums="1"
82-
paddlex --paddle2onnx --paddle_model_dir models/PP-OCRv5_mobile_det --onnx_model_dir models/PP-OCRv5_mobile_det
78+
paddle2onnx --model_dir models/official_models/PP-OCRv5_mobile_det --model_filename inference.json --params_filename inference.pdiparams --save_file models/PP-OCRv5_mobile_det/inference.onnx
8379
```
8480

85-
输出日志如下,表明转换成功
81+
输出日志如下,日志中存在报错信息,但是最终ONNX模型仍然生成了
8682

87-
```bash linenums="1"
88-
Input dir: models/PP-OCRv5_mobile_det
89-
Output dir: models/PP-OCRv5_mobile_det
90-
Paddle2ONNX conversion starting...
83+
```bash linenums="1" hl_lines="11 16"
84+
/Users/xxxx/miniconda3/envs/py310/lib/python3.10/site-packages/paddle/utils/cpp_extension/extension_utils.py:711: UserWarning: No ccache found. Please be aware that recompiling all source files may be required. You can download and install ccache from: https://github.com/ccache/ccache/blob/master/doc/INSTALL.md
9185
warnings.warn(warning_message)
9286
[Paddle2ONNX] Start parsing the Paddle model file...
93-
[Paddle2ONNX] Use opset_version = 7 for ONNX export.
87+
[Paddle2ONNX] Use opset_version = 14 for ONNX export.
9488
[Paddle2ONNX] PaddlePaddle model is exported as ONNX format now.
95-
2025-05-14 08:21:23 [INFO] Try to perform optimization on the ONNX model with onnxoptimizer.
96-
2025-05-14 08:21:23 [INFO] ONNX model saved in models/PP-OCRv5_mobile_det/inference.onnx.
97-
Paddle2ONNX conversion succeeded
98-
Done
89+
2025-05-26 11:20:46 [INFO] Try to perform constant folding on the ONNX model with Polygraphy.
90+
[W] 'colored' module is not installed, will not use colors when logging. To enable colors, please install the 'colored' module: python3 -m pip install colored
91+
[I] Folding Constants | Pass 1
92+
[W] colored module is not installed, will not use colors when logging. To enable colors, please install the colored module: python3 -m pip install colored
93+
[W] Inference failed. You may want to try enabling partitioning to see better results. Note: Error was:
94+
[ONNXRuntimeError] : 1 : FAIL : /Users/runner/work/1/s/onnxruntime/core/graph/model.cc:182 onnxruntime::Model::Model(ModelProto &&, const PathString &, const IOnnxRuntimeOpSchemaRegistryList *, const logging::Logger &, const ModelOptions &) Unsupported model IR version: 11, max supported IR version: 10
95+
[I] Total Nodes | Original: 925, After Folding: 612 | 313 Nodes Folded
96+
[I] Folding Constants | Pass 2
97+
[W] colored module is not installed, will not use colors when logging. To enable colors, please install the colored module: python3 -m pip install colored
98+
[W] Inference failed. You may want to try enabling partitioning to see better results. Note: Error was:
99+
[ONNXRuntimeError] : 1 : FAIL : /Users/runner/work/1/s/onnxruntime/core/graph/model.cc:182 onnxruntime::Model::Model(ModelProto &&, const PathString &, const IOnnxRuntimeOpSchemaRegistryList *, const logging::Logger &, const ModelOptions &) Unsupported model IR version: 11, max supported IR version: 10
100+
[I] Total Nodes | Original: 612, After Folding: 612 | 0 Nodes Folded
101+
2025-05-26 11:20:52 [INFO] ONNX model saved in models/PP-OCRv5_mobile_det/inference.onnx.
99102
```
100103

101-
### 3. 模型推理验证
104+
此时得到的模型,直接用`rapidocr`推理会报错:
105+
106+
```python linenums="1"
107+
from rapidocr import RapidOCR
102108

103-
该部分主要是在RapidOCR项目中测试能否直接使用onnx模型。要点主要是确定模型前后处理是否兼容。从PaddleX[官方文档](https://paddlepaddle.github.io/PaddleX/latest/module_usage/tutorials/ocr_modules/text_recognition.html#_2)中可以看到:
109+
model_path = "models/PP-OCRv5_mobile_det/inference.onnx"
110+
engine = RapidOCR(params={"Det.model_path": model_path})
104111

105-
> PP-OCRv5_mobile_det是在PP-OCRv4_server_rec的基础上,在更多中文文档数据和PP-OCR训练数据的混合数据训练而成,增加了部分繁体字、日文、特殊字符的识别能力,可支持识别的字符为1.5万+,除文档相关的文字识别能力提升外,也同时提升了通用文字的识别能力
112+
img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
113+
result = engine(img_url)
114+
print(result)
106115

107-
以上说明了该模型与PP-OCRv4_server_rec模型结构相同,前后处理也相同。唯一做的就是添加了更多数据,扩展了字典个数,从6623扩展到15630个。因此,可以直接使用RapidOCR来快速推理验证。代码如下:
116+
result.vis("vis_result.jpg")
117+
```
118+
119+
报错信息如下:
120+
121+
```bash linenums="1" hl_lines="15"
122+
[INFO] 2025-05-26 11:21:27,698 [RapidOCR] base.py:41: Using engine_name: onnxruntime
123+
Traceback (most recent call last):
124+
File "/Users/xxxx/projects/RapidOCR/python/demo.py", line 9, in <module>
125+
engine = RapidOCR(params={"Det.model_path": model_path})
126+
File "/Users/xxxx/projects/RapidOCR/python/rapidocr/main.py", line 60, in __init__
127+
self.text_det = TextDetector(config.Det)
128+
File "/Users/xxxx/projects/RapidOCR/python/rapidocr/ch_ppocr_det/main.py", line 45, in __init__
129+
self.session = get_engine(config.engine_name)(config)
130+
File "/Users/xxxx/projects/RapidOCR/python/rapidocr/inference_engine/onnxruntime.py", line 60, in __init__
131+
self.session = InferenceSession(
132+
File "/Users/xxxx/miniconda3/envs/py310/lib/python3.10/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 472, in __init__
133+
self._create_inference_session(providers, provider_options, disabled_optimizers)
134+
File "/Users/xxxx/miniconda3/envs/py310/lib/python3.10/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 550, in _create_inference_session
135+
sess = C.InferenceSession(session_options, self._model_path, True, self._read_config_from_model)
136+
onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from /Users/xxxx/projects/LittleCode/models/PP-OCRv5_mobile_det/inference.onnx failed:/Users/runner/work/1/s/onnxruntime/core/graph/model.cc:182 onnxruntime::Model::Model(ModelProto &&, const PathString &, const IOnnxRuntimeOpSchemaRegistryList *, const logging::Logger &, const ModelOptions &) Unsupported model IR version: 11, max supported IR version: 10
137+
```
138+
139+
经过一系列的查阅资料,终于在onnxruntime issue [#23602](https://github.com/microsoft/onnxruntime/issues/23602#issuecomment-2642348849) 中发现了解决方案。运行下面代码,将上一步所得模型重新指定一下**IR_VERSION**,就可以用`rapidocr`加载推理了。
140+
141+
```python linenums="1"
142+
import onnx
143+
from onnx import version_converter
144+
145+
OPT_VERSION = 14
146+
IR_VERSION = 10
147+
148+
source_path = "models/PP-OCRv5_mobile_det/inference.onnx"
149+
dist_path = "models/PP-OCRv5_mobile_det/inference_v2.onnx"
150+
151+
model = onnx.load(source_path)
152+
model.ir_version = IR_VERSION
153+
model = version_converter.convert_version(model, OPT_VERSION)
154+
onnx.save(model, dist_path)
155+
```
156+
157+
### 3. 模型推理验证
158+
159+
该部分主要是在RapidOCR项目中测试能否直接使用onnx模型。要点主要是确定模型前后处理是否兼容。从PaddleOCR config文件中比较[PP-OCRv4](https://github.com/PaddlePaddle/PaddleOCR/blob/549d83a88b7c75144120e6ec03de80d3eb9e48a5/configs/det/PP-OCRv4/PP-OCRv4_mobile_det.yml)和[PP-OCRv5 mobile det](https://github.com/PaddlePaddle/PaddleOCR/blob/549d83a88b7c75144120e6ec03de80d3eb9e48a5/configs/det/PP-OCRv5/PP-OCRv5_mobile_det.yml)文件差异:
160+
161+
![alt text](../images/v4_v5_mobile_det.png)
108162
109163
```python linenums="1"
110164
from rapidocr import RapidOCR
111165
112166
model_path = "models/PP-OCRv5_mobile_det/inference.onnx"
113-
key_path = "models/ppocrv4_doc_dict.txt"
114-
engine = RapidOCR(params={"Rec.model_path": model_path, "Rec.rec_keys_path": key_path})
167+
engine = RapidOCR(params={"Det.model_path": model_path})
115168
116169
img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
117-
result = engine(img_path)
170+
result = engine(img_url)
118171
print(result)
119172
120173
result.vis("vis_result.jpg")
121174
```
122175
123-
![alt text](../images/vis_result.jpg)
176+
![alt text](../images/v5_mobile_det_vis_result.jpg)
124177
125178
### 4. 模型精度测试
126179
127-
该部分主要使用[TextRecMetric](https://github.com/SWHL/TextRecMetric)和测试集[text_rec_test_dataset](https://huggingface.co/datasets/SWHL/text_rec_test_dataset)来评测。
128-
129-
需要注意的是,**PP-OCRv5_mobile_det模型更加侧重生僻字和一些符号识别。** 当前测试集并未着重收集生僻字和一些符号的数据,因此以下指标会有些偏低。如需自己使用,请在自己场景下测试效果。
180+
该部分主要使用[TextDetMetric](https://github.com/SWHL/TextDetMetric)和测试集[text_det_test_dataset](https://huggingface.co/datasets/SWHL/text_det_test_dataset)来评测。
130181
131-
相关测试步骤请参见[TextRecMetric](https://github.com/SWHL/TextRecMetric)的README,一步一步来就行。我这里测试最终精度如下:
182+
相关测试步骤请参见[TextDetMetric](https://github.com/SWHL/TextRecMetric)的README,一步一步来就行。我这里测试最终精度如下:
132183
133184
```json
134-
{'ExactMatch': 0.8097, 'CharMatch': 0.9444, 'avg_elapse': 0.0818}
185+
{'precision': 0.7861, 'recall': 0.8266, 'hmean': 0.8058, 'avg_elapse': 0.1499}
135186
```
136187
137188
该结果已经更新到[开源OCR模型对比](./model_summary.md)中。
@@ -140,76 +191,6 @@ result.vis("vis_result.jpg")
140191
141192
该部分主要包括将字典文件写入到ONNX模型中、托管模型到魔搭、更改rapidocr代码适配等。
142193
143-
#### 字典文件写入ONNX模型
144-
145-
该步骤仅存在文本识别模型中,文本检测模型没有这个步骤。
146-
147-
??? info "详细代码"
148-
149-
```python linenums="1"
150-
from pathlib import Path
151-
from typing import List, Union
152-
153-
import onnx
154-
import onnxruntime as ort
155-
from onnx import ModelProto
156-
157-
158-
def read_txt(txt_path: Union[Path, str]) -> List[str]:
159-
with open(txt_path, "r", encoding="utf-8") as f:
160-
data = [v.rstrip("\n") for v in f]
161-
return data
162-
163-
164-
class ONNXMetaOp:
165-
@classmethod
166-
def add_meta(
167-
cls,
168-
model_path: Union[str, Path],
169-
key: str,
170-
value: List[str],
171-
delimiter: str = "\n",
172-
) -> ModelProto:
173-
model = onnx.load_model(model_path)
174-
meta = model.metadata_props.add()
175-
meta.key = key
176-
meta.value = delimiter.join(value)
177-
return model
178-
179-
@classmethod
180-
def get_meta(
181-
cls, model_path: Union[str, Path], key: str, split_sym: str = "\n"
182-
) -> List[str]:
183-
sess = ort.InferenceSession(model_path)
184-
meta_map = sess.get_modelmeta().custom_metadata_map
185-
key_content = meta_map.get(key)
186-
key_list = key_content.split(split_sym)
187-
return key_list
188-
189-
@classmethod
190-
def del_meta(cls, model_path: Union[str, Path]) -> ModelProto:
191-
model = onnx.load_model(model_path)
192-
del model.metadata_props[:]
193-
return model
194-
195-
@classmethod
196-
def save_model(cls, save_path: Union[str, Path], model: ModelProto):
197-
onnx.save_model(model, save_path)
198-
199-
200-
dicts = read_txt(
201-
"models/ppocrv4_doc_dict.txt"
202-
)
203-
model_path = "models/PP-OCRv5_mobile_det.onnx"
204-
model = ONNXMetaOp.add_meta(model_path, key="character", value=dicts)
205-
206-
new_model_path = "models/PP-OCRv5_mobile_det_with_dict.onnx"
207-
ONNXMetaOp.save_model(new_model_path, model)
208-
209-
t = ONNXMetaOp.get_meta(new_model_path, key="character")
210-
print(t)
211-
```
212-
213194
#### 托管模型到魔搭
214195
215196
该部分主要是涉及模型上传到对应位置,并合理命名。注意上传完成后,需要打Tag,避免后续rapidocr whl包中找不到模型下载路径。
270 KB
Loading
53.8 KB
Loading

0 commit comments

Comments
 (0)