33- [ 基于PaddleHub Serving的服务部署] ( #基于paddlehub-serving的服务部署 )
44 - [ 1. 近期更新] ( #1-近期更新 )
55 - [ 2. 快速启动服务] ( #2-快速启动服务 )
6- - [ 2.1 准备环境 ] ( #21-准备环境 )
6+ - [ 2.1 安装PaddleHub ] ( #21-安装PaddleHub )
77 - [ 2.2 下载推理模型] ( #22-下载推理模型 )
88 - [ 2.3 安装服务模块] ( #23-安装服务模块 )
99 - [ 2.4 启动服务] ( #24-启动服务 )
1515
1616
1717PaddleOCR提供2种服务部署方式:
18- - 基于PaddleHub Serving的部署:代码路径为" ` ./deploy/hubserving ` " ,按照本教程使用;
19- - 基于PaddleServing的部署:代码路径为" ` ./deploy/pdserving ` " ,使用方法参考[ 文档] ( ../../deploy/pdserving/README_CN.md ) 。
18+ - 基于PaddleHub Serving的部署:代码路径为` ./deploy/hubserving ` ,按照本教程使用;
19+ - 基于PaddleServing的部署:代码路径为` ./deploy/pdserving ` ,使用方法参考[ 文档] ( ../../deploy/pdserving/README_CN.md ) 。
2020
2121# 基于PaddleHub Serving的服务部署
2222
@@ -51,120 +51,77 @@ deploy/hubserving/ocr_system/
5151
5252## 2. 快速启动服务
5353以下步骤以检测+识别2阶段串联服务为例,如果只需要检测服务或识别服务,替换相应文件路径即可。
54- ### 2.1 准备环境
55- ``` shell
56- # 安装paddlehub
57- # paddlehub 需要 python>3.6.2
54+ ### 2.1 安装PaddleHub
55+ paddlehub 需要 python>3.6.2
56+ ``` bash
5857pip3 install paddlehub==2.1.0 --upgrade -i https://mirror.baidu.com/pypi/simple
5958```
6059
6160### 2.2 下载推理模型
6261安装服务模块前,需要准备推理模型并放到正确路径。默认使用的是PP-OCRv3模型,默认模型路径为:
62+ | 模型 | 路径 |
63+ | ------- | - |
64+ | 检测模型 | ` ./inference/ch_PP-OCRv3_det_infer/ ` |
65+ | 识别模型 | ` ./inference/ch_PP-OCRv3_rec_infer/ ` |
66+ | 方向分类器 | ` ./inference/ch_ppocr_mobile_v2.0_cls_infer/ ` |
67+ | 版面分析模型 | ` ./inference/picodet_lcnet_x1_0_fgd_layout_infer/ ` |
68+ | 表格结构识别模型 | ` ./inference/ch_ppstructure_mobile_v2.0_SLANet_infer/ ` |
69+ | 关键信息抽取SER模型 | ` ./inference/ser_vi_layoutxlm_xfund_infer/ ` |
70+ | 关键信息抽取RE模型 | ` ./inference/re_vi_layoutxlm_xfund_infer/ ` |
6371
64- ```
65- 检测模型:./inference/ch_PP-OCRv3_det_infer/
66- 识别模型:./inference/ch_PP-OCRv3_rec_infer/
67- 方向分类器:./inference/ch_ppocr_mobile_v2.0_cls_infer/
68- 版面分析模型:./inference/picodet_lcnet_x1_0_fgd_layout_infer/
69- 表格结构识别模型:./inference/ch_ppstructure_mobile_v2.0_SLANet_infer/
70- 关键信息抽取SER模型:./inference/ser_vi_layoutxlm_xfund_infer/
71- 关键信息抽取RE模型:./inference/re_vi_layoutxlm_xfund_infer/
72- ```
72+ ** 模型路径可在` params.py ` 中查看和修改。**
7373
74- ** 模型路径可在 ` params.py ` 中查看和修改。 ** 更多模型可以从PaddleOCR提供的模型库[ PP-OCR] ( ../../doc/doc_ch/models_list.md ) 和[ PP-Structure] ( ../../ppstructure/docs/models_list.md ) 下载,也可以替换成自己训练转换好的模型。
74+ 更多模型可以从PaddleOCR提供的模型库[ PP-OCR] ( ../../doc/doc_ch/models_list.md ) 和[ PP-Structure] ( ../../ppstructure/docs/models_list.md ) 下载,也可以替换成自己训练转换好的模型。
7575
7676### 2.3 安装服务模块
7777PaddleOCR提供5种服务模块,根据需要安装所需模块。
7878
79- * 在Linux环境下,安装示例如下:
80- ``` shell
81- # 安装检测服务模块:
82- hub install deploy/hubserving/ocr_det/
83-
84- # 或,安装分类服务模块:
85- hub install deploy/hubserving/ocr_cls/
86-
87- # 或,安装识别服务模块:
88- hub install deploy/hubserving/ocr_rec/
89-
90- # 或,安装检测+识别串联服务模块:
91- hub install deploy/hubserving/ocr_system/
92-
93- # 或,安装表格识别服务模块:
94- hub install deploy/hubserving/structure_table/
95-
96- # 或,安装PP-Structure服务模块:
97- hub install deploy/hubserving/structure_system/
98-
99- # 或,安装版面分析服务模块:
100- hub install deploy/hubserving/structure_layout/
101-
102- # 或,安装关键信息抽取SER服务模块:
103- hub install deploy/hubserving/kie_ser/
104-
105- # 或,安装关键信息抽取SER+RE服务模块:
106- hub install deploy/hubserving/kie_ser_re/
107- ```
108-
109- * 在Windows环境下(文件夹的分隔符为` \ ` ),安装示例如下:
110- ``` shell
111- # 安装检测服务模块:
112- hub install deploy\h ubserving\o cr_det\
113-
114- # 或,安装分类服务模块:
115- hub install deploy\h ubserving\o cr_cls\
116-
117- # 或,安装识别服务模块:
118- hub install deploy\h ubserving\o cr_rec\
119-
120- # 或,安装检测+识别串联服务模块:
121- hub install deploy\h ubserving\o cr_system\
122-
123- # 或,安装表格识别服务模块:
124- hub install deploy\h ubserving\s tructure_table\
125-
126- # 或,安装PP-Structure服务模块:
127- hub install deploy\h ubserving\s tructure_system\
128-
129- # 或,安装版面分析服务模块:
130- hub install deploy\h ubserving\s tructure_layout\
131-
132- # 或,安装关键信息抽取SER服务模块:
133- hub install deploy\h ubserving\k ie_ser\
134-
135- # 或,安装关键信息抽取SER+RE服务模块:
136- hub install deploy\h ubserving\k ie_ser_re\
137- ```
79+ 在Linux环境(Windows环境请将` / ` 替换为` \ ` )下,安装模块命令如下表:
80+ | 服务模块 | 命令 |
81+ | ------- | - |
82+ | 检测 | ` hub install deploy/hubserving/ocr_det ` |
83+ | 分类 | ` hub install deploy/hubserving/ocr_cls ` |
84+ | 识别 | ` hub install deploy/hubserving/ocr_rec ` |
85+ | 检测+识别串联 | ` hub install deploy/hubserving/ocr_system ` |
86+ | 表格识别 | ` hub install deploy/hubserving/structure_table ` |
87+ | PP-Structure | ` hub install deploy/hubserving/structure_system ` |
88+ | 版面分析 | ` hub install deploy/hubserving/structure_layout ` |
89+ | 关键信息抽取SER | ` hub install deploy/hubserving/kie_ser ` |
90+ | 关键信息抽取SER+RE | ` hub install deploy/hubserving/kie_ser_re ` |
13891
13992### 2.4 启动服务
14093#### 2.4.1. 命令行命令启动(仅支持CPU)
141- ** 启动命令:**
142- ``` shell
143- $ hub serving start --modules [ Module1== Version1, Module2== Version2, ...] \
144- --port XXXX \
145- --use_multiprocess \
146- --workers \
94+ ** 启动命令:**
95+ ``` bash
96+ hub serving start --modules Module1==Version1, Module2==Version2, ... \
97+ --port 8866 \
98+ --use_multiprocess \
99+ --workers \
147100```
148101
149- ** 参数:**
150-
151- | 参数| 用途|
152- | ---| ---|
153- | --modules/-m| PaddleHub Serving预安装模型,以多个Module==Version键值对的形式列出<br >* ` 当不指定Version时,默认选择最新版本 ` * |
154- | --port/-p| 服务端口,默认为8866|
155- | --use_multiprocess| 是否启用并发方式,默认为单进程方式,推荐多核CPU机器使用此方式<br >* ` Windows操作系统只支持单进程方式 ` * |
156- | --workers| 在并发方式下指定的并发任务数,默认为` 2*cpu_count-1 ` ,其中` cpu_count ` 为CPU核数|
157-
158- 如启动串联服务: ``` hub serving start -m ocr_system ```
102+ ** 参数:**
103+ | 参数| 用途|
104+ | ---| ---|
105+ | ` --modules ` /` -m ` | PaddleHub Serving预安装模型,以多个Module==Version键值对的形式列出<br >** 当不指定Version时,默认选择最新版本** |
106+ | ` --port ` /` -p ` | 服务端口,默认为8866|
107+ | ` --use_multiprocess ` | 是否启用并发方式,默认为单进程方式,推荐多核CPU机器使用此方式<br >** Windows操作系统只支持单进程方式** |
108+ | ` --workers ` | 在并发方式下指定的并发任务数,默认为` 2*cpu_count-1 ` ,其中` cpu_count ` 为CPU核数|
109+
110+ 如启动串联服务:
111+ ``` bash
112+ hub serving start -m ocr_system
113+ ```
159114
160115这样就完成了一个服务化API的部署,使用默认端口号8866。
161116
162117#### 2.4.2 配置文件启动(支持CPU、GPU)
163- ** 启动命令:**
164- ``` hub serving start -c config.json ```
118+ ** 启动命令:**
119+ ``` bash
120+ hub serving start -c config.json
121+ ```
165122
166123其中,` config.json ` 格式如下:
167- ``` python
124+ ``` json
168125{
169126 "modules_info" : {
170127 "ocr_system" : {
@@ -182,90 +139,112 @@ $ hub serving start --modules [Module1==Version1, Module2==Version2, ...] \
182139}
183140```
184141
185- - ` init_args ` 中的可配参数与` module.py ` 中的` _initialize ` 函数接口一致。其中,** 当` use_gpu ` 为` true ` 时,表示使用GPU启动服务** 。
142+ - ` init_args ` 中的可配参数与` module.py ` 中的` _initialize ` 函数接口一致。
143+
144+ ** 当` use_gpu ` 为` true ` 时,表示使用GPU启动服务。**
186145- ` predict_args ` 中的可配参数与` module.py ` 中的` predict ` 函数接口一致。
187146
188- ** 注意: **
147+ ** 注意: **
189148- 使用配置文件启动服务时,其他参数会被忽略。
190- - 如果使用GPU预测(即,` use_gpu ` 置为` true ` ),则需要在启动服务之前,设置CUDA_VISIBLE_DEVICES环境变量,如:``` export CUDA_VISIBLE_DEVICES=0 ``` ,否则不用设置。
149+ - 如果使用GPU预测(即,` use_gpu ` 置为` true ` ),则需要在启动服务之前,设置CUDA_VISIBLE_DEVICES环境变量,如:
150+ ``` bash
151+ export CUDA_VISIBLE_DEVICES=0
152+ ```
191153- ** ` use_gpu ` 不可与` use_multiprocess ` 同时为` true ` ** 。
192154
193- 如,使用GPU 3号卡启动串联服务:
194- ``` shell
155+ 如,使用GPU 3号卡启动串联服务:
156+ ``` bash
195157export CUDA_VISIBLE_DEVICES=3
196158hub serving start -c deploy/hubserving/ocr_system/config.json
197159```
198160
199161## 3. 发送预测请求
200- 配置好服务端,可使用以下命令发送预测请求,获取预测结果:
201-
202- ``` python tools/test_hubserving.py --server_url=server_url --image_dir=image_path ```
203-
204- 需要给脚本传递2个参数:
205- - ** server_url** :服务地址,格式为
206- ` http://[ip_address]:[port]/predict/[module_name] `
207- 例如,如果使用配置文件启动分类,检测、识别,检测+分类+识别3阶段,表格识别和PP-Structure服务,那么发送请求的url将分别是:
208- ` http://127.0.0.1:8865/predict/ocr_det `
209- ` http://127.0.0.1:8866/predict/ocr_cls `
210- ` http://127.0.0.1:8867/predict/ocr_rec `
211- ` http://127.0.0.1:8868/predict/ocr_system `
212- ` http://127.0.0.1:8869/predict/structure_table `
213- ` http://127.0.0.1:8870/predict/structure_system `
214- ` http://127.0.0.1:8870/predict/structure_layout `
215- ` http://127.0.0.1:8871/predict/kie_ser `
216- ` http://127.0.0.1:8872/predict/kie_ser_re `
217- - ** image_dir** :测试图像路径,可以是单张图片路径,也可以是图像集合目录路径
218- - ** visualize** :是否可视化结果,默认为False
219- - ** output** :可视化结果保存路径,默认为` ./hubserving_result `
220-
221- 访问示例:
222- ``` python tools/test_hubserving.py --server_url=http://127.0.0.1:8868/predict/ocr_system --image_dir=./doc/imgs/ --visualize=false ```
162+ 配置好服务端,可使用以下命令发送预测请求,获取预测结果:
163+ ``` bash
164+ python tools/test_hubserving.py --server_url=server_url --image_dir=image_path
165+ ```
166+
167+ 需要给脚本传递2个参数:
168+ - ` server_url ` :服务地址,格式为` http://[ip_address]:[port]/predict/[module_name] `
169+
170+ 例如,如果使用配置文件启动分类,检测、识别,检测+分类+识别3阶段,表格识别和PP-Structure服务
171+
172+ 并为每个服务修改了port,那么发送请求的url将分别是:
173+ ```
174+ http://127.0.0.1:8865/predict/ocr_det
175+ http://127.0.0.1:8866/predict/ocr_cls
176+ http://127.0.0.1:8867/predict/ocr_rec
177+ http://127.0.0.1:8868/predict/ocr_system
178+ http://127.0.0.1:8869/predict/structure_table
179+ http://127.0.0.1:8870/predict/structure_system
180+ http://127.0.0.1:8870/predict/structure_layout
181+ http://127.0.0.1:8871/predict/kie_ser
182+ http://127.0.0.1:8872/predict/kie_ser_re
183+ ```
184+ - ` image_dir ` :测试图像路径,可以是单张图片路径,也可以是图像集合目录路径
185+ - ` visualize ` :是否可视化结果,默认为False
186+ - ` output ` :可视化结果保存路径,默认为` ./hubserving_result `
187+
188+ 访问示例:
189+ ``` bash
190+ python tools/test_hubserving.py --server_url=http://127.0.0.1:8868/predict/ocr_system --image_dir=./doc/imgs/ --visualize=false
191+ ```
223192
224193## 4. 返回结果格式说明
225194返回结果为列表(list),列表中的每一项为词典(dict),词典一共可能包含3种字段,信息如下:
226-
227195| 字段名称| 数据类型| 意义|
228196| ---| ---| ---|
229197| angle| str| 文本角度|
230198| text| str| 文本内容|
231199| confidence| float| 文本识别置信度或文本角度分类置信度|
232200| text_region| list| 文本位置坐标|
233201| html| str| 表格的html字符串|
234- | regions| list| 版面分析+表格识别+OCR的结果,每一项为一个list, 包含表示区域坐标的` bbox ` ,区域类型的` type ` 和区域结果的` res ` 三个字段|
202+ | regions| list| 版面分析+表格识别+OCR的结果,每一项为一个list< br > 包含表示区域坐标的` bbox ` ,区域类型的` type ` 和区域结果的` res ` 三个字段|
235203| layout| list| 版面分析的结果,每一项一个dict,包含版面区域坐标的` bbox ` ,区域类型的` label ` |
236204
237205不同模块返回的字段不同,如,文本识别服务模块返回结果不含` text_region ` 字段,具体信息如下:
238-
239- | 字段名/模块名 | ocr_det | ocr_cls | ocr_rec | ocr_system | structure_table | structure_system | Structure_layout | kie_ser | kie_re |
240- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
241- | angle| | ✔ | | ✔ | |||
242- | text| | | ✔| ✔| | ✔ | | ✔ | ✔ |
243- | confidence| | ✔ | ✔| | | ✔| | ✔ | ✔ |
244- | text_region| ✔| | | ✔ | | ✔| | ✔ | ✔ |
245- | html| | | | | ✔ | ✔||| |
246- | regions| | | | | ✔ | ✔ | || |
247- | layout| | | | | | | ✔ || |
248- | ser_res| | | | | | | | ✔ | |
249- | re_res| | | | | | | | | ✔ |
250-
206+ | 字段名/模块名 | ocr_det | ocr_cls | ocr_rec | ocr_system | structure_table | structure_system | structure_layout | kie_ser | kie_re |
207+ | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
208+ | angle | | ✔ | | ✔ | | | |
209+ | text | | | ✔ | ✔ | | ✔ | | ✔ | ✔ |
210+ | confidence | | ✔ | ✔ | ✔ | | ✔ | | ✔ | ✔ |
211+ | text_region | ✔ | | | ✔ | | ✔ | | ✔ | ✔ |
212+ | html | | | | | ✔ | ✔ | | | |
213+ | regions | | | | | ✔ | ✔ | | | |
214+ | layout | | | | | | | ✔ | | |
215+ | ser_res | | | | | | | | ✔ | |
216+ | re_res | | | | | | | | | ✔ |
251217
252218** 说明:** 如果需要增加、删除、修改返回字段,可在相应模块的` module.py ` 文件中进行修改,完整流程参考下一节自定义修改服务模块。
253219
254220## 5. 自定义修改服务模块
255- 如果需要修改服务逻辑,你一般需要操作以下步骤(以修改` ocr_system ` 为例):
256-
257- - 1、 停止服务
258- ``` hub serving stop --port/-p XXXX ```
259-
260- - 2、 到相应的` module.py ` 和` params.py ` 等文件中根据实际需求修改代码。
261- 例如,如果需要替换部署服务所用模型,则需要到` params.py ` 中修改模型路径参数` det_model_dir ` 和` rec_model_dir ` ,如果需要关闭文本方向分类器,则将参数` use_angle_cls ` 置为` False ` ,当然,同时可能还需要修改其他相关参数,请根据实际情况修改调试。 ** 强烈建议修改后先直接运行` module.py ` 调试,能正确运行预测后再启动服务测试。**
262- ** 注意** PPOCR-v3识别模型使用的图片输入shape为` 3,48,320 ` ,因此需要修改` params.py ` 中的` cfg.rec_image_shape = "3, 48, 320" ` ,如果不使用PPOCR-v3识别模型,则无需修改该参数。
263-
264- - 3、 卸载旧服务包
265- ``` hub uninstall ocr_system ```
266-
267- - 4、 安装修改后的新服务包
268- ``` hub install deploy/hubserving/ocr_system/ ```
269-
270- - 5、重新启动服务
271- ``` hub serving start -m ocr_system ```
221+ 如果需要修改服务逻辑,一般需要操作以下步骤(以修改` deploy/hubserving/ocr_system ` 为例):
222+
223+ 1 . 停止服务:
224+ ``` bash
225+ hub serving stop --port/-p XXXX
226+ ```
227+ 2 . 到` deploy/hubserving/ocr_system ` 下的` module.py ` 和` params.py ` 等文件中根据实际需求修改代码。
228+
229+ 例如,如果需要替换部署服务所用模型,则需要到` params.py ` 中修改模型路径参数` det_model_dir ` 和` rec_model_dir ` ,如果需要关闭文本方向分类器,则将参数` use_angle_cls ` 置为` False `
230+
231+ 当然,同时可能还需要修改其他相关参数,请根据实际情况修改调试。
232+
233+ ** 强烈建议修改后先直接运行` module.py ` 调试,能正确运行预测后再启动服务测试。**
234+
235+ ** 注意:** PPOCR-v3识别模型使用的图片输入shape为` 3,48,320 ` ,因此需要修改` params.py ` 中的` cfg.rec_image_shape = "3, 48, 320" ` ,如果不使用PPOCR-v3识别模型,则无需修改该参数。
236+ 3 . (可选)如果想要重命名模块需要更改` module.py ` 文件中的以下行:
237+ - [ ` from deploy.hubserving.ocr_system.params import read_params ` 中的` ocr_system ` ] ( https://github.com/PaddlePaddle/PaddleOCR/blob/a923f35de57b5e378f8dd16e54d0a3e4f51267fd/deploy/hubserving/ocr_system/module.py#L35 )
238+ - [ ` name="ocr_system", ` 中的` ocr_system ` ] ( https://github.com/PaddlePaddle/PaddleOCR/blob/a923f35de57b5e378f8dd16e54d0a3e4f51267fd/deploy/hubserving/ocr_system/module.py#L39 )
239+ 4 . (可选)可能需要删除` __pycache__ ` 目录以强制刷新CPython缓存:
240+ ``` bash
241+ find deploy/hubserving/ocr_system -name ' __pycache__' -exec rm -r {} \;
242+ ```
243+ 5 . 安装修改后的新服务包:
244+ ``` bash
245+ hub install deploy/hubserving/ocr_system
246+ ```
247+ 6 . 重新启动服务:
248+ ``` bash
249+ hub serving start -m ocr_system
250+ ```
0 commit comments