Skip to content

Commit b1f1984

Browse files
authored
Add serving respnse message, add tips for use_multiprocess and use_gpu. (PaddlePaddle#440)
* Add serving respnse message, add tips for use_multiprocess and use_gpu.
1 parent df15dd1 commit b1f1984

File tree

21 files changed

+336
-199
lines changed

21 files changed

+336
-199
lines changed

demo/serving/module_serving/GAN_stgan_celeba/README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ Loading stgan_celeba successful.
1818
这样就完成了一个图像生成服务化API的部署,默认端口号为8866。
1919

2020
## Step2:测试图像生成在线API
21+
首先指定编码格式及引入需要的包:
22+
```python
23+
# coding: utf8
24+
import requests
25+
import json
26+
import base64
27+
import os
28+
```
2129
我们用来测试的样例图片为:
2230

2331
<p align="center">
@@ -36,7 +44,6 @@ info为图像描述,根据示例图像信息,info应为"Male,Black_Hair,Eyeg
3644

3745
image为要生成的图像风格,我们选取"Bald"(秃顶的)作为生成图像的风格。
3846

39-
代码如下:
4047
```python
4148
>>> # 指定要使用的图片文件并生成列表[("image", img_1), ("image", img_2), ... ]
4249
>>> file_list = ["../img/man.png"]
@@ -52,7 +59,14 @@ image为要生成的图像风格,我们选取"Bald"(秃顶的)作为生成图
5259
>>> url = "http://127.0.0.1:8866/predict/image/stgan_celeba"
5360
>>> r = requests.post(url=url, data=data, files=files)
5461
```
55-
stgan_celeba返回的结果包括生成图像的base64编码格式,经过转换可以得到生成图像,代码如下:
62+
stgan_celeba返回的结果包括生成图像的base64编码格式,经过转换可以得到生成图像。
63+
64+
我们建立一个指定的文件夹用于存放结果图片:
65+
```python
66+
>>> if not os.path.exists("stgan_output"):
67+
os.mkdir("stgan_output")
68+
```
69+
然后将图片进行保存,代码如下:
5670
```python
5771
>>> for item in results:
5872
... with open(output_path, "wb") as fp:

demo/serving/module_serving/GAN_stgan_celeba/stgan_celeba_serving_demo.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
# 指定图片生成方法为stgan_celeba并发送post请求
1414
url = "http://127.0.0.1:8866/predict/image/stgan_celeba"
1515
r = requests.post(url=url, data=data, files=files)
16-
print(r.text)
1716

18-
results = eval(r.json()["results"])
1917
# 保存生成的图片到output文件夹,打印模型输出结果
2018
if not os.path.exists("stgan_output"):
2119
os.mkdir("stgan_output")
20+
results = eval(r.json()["results"])
2221
for item in results:
2322
output_path = os.path.join("stgan_output", item["path"].split("/")[-1])
2423
with open(output_path, "wb") as fp:

demo/serving/module_serving/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ PaddleHub Serving是基于PaddleHub的一键模型服务部署工具,能够通
4242

4343
&emsp;&emsp;该示例展示了利用deeplabv3p_xception65_humanseg完成图像分割服务化部署和在线预测,获取识别结果和分割后的图像。
4444

45-
* [中文情感分析-基于simnet_bow](../module_serving/semantic_model_simnet_bow)
45+
* [中文情感分析-基于senta_lstm](../module_serving/sentiment_analysis_senta_lstm)
4646

4747
&emsp;&emsp;该示例展示了利用senta_lstm完成中文文本情感分析服务化部署和在线预测,获取文本的情感分析结果。
4848

demo/serving/module_serving/classification_vgg11_imagenet/README.md

+20-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Loading vgg11_imagenet successful.
1818
这样就完成了一个图像分类服务化API的部署,默认端口号为8866。
1919

2020
## Step2:测试图像分类在线API
21+
首先引入需要的包:
22+
```python
23+
>>> import requests
24+
>>> import json
25+
```
26+
2127
我们用来测试的样例图片为:
2228

2329
<p align="center">
@@ -49,11 +55,20 @@ files = [("image", file_1), ("image", file_2)]
4955
```
5056
vgg11_imagenent返回的结果为图像分类结果及其对应的概率,我们尝试打印接口返回结果:
5157
```python
52-
53-
>>> print(json.dumps(r.json(), indent=4, ensure_ascii=False))
54-
{
55-
"results": "[[{'Egyptian cat': 0.540287435054779}], [{'daisy': 0.9976677298545837}]]"
56-
}
58+
>>> results = eval(r.json()["results"])
59+
>>> print(json.dumps(results, indent=4, ensure_ascii=False))
60+
[
61+
[
62+
{
63+
"Egyptian cat": 0.540287435054779
64+
}
65+
],
66+
[
67+
{
68+
"daisy": 0.9976677298545837
69+
}
70+
]
71+
]
5772
```
5873

5974
这样我们就完成了对图像分类预测服务化部署和测试。

demo/serving/module_serving/classification_vgg11_imagenet/vgg11_imagenet_serving_demo.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
url = "http://127.0.0.1:8866/predict/image/vgg11_imagenet"
1313
r = requests.post(url=url, files=files)
1414

15+
results = eval(r.json()["results"])
16+
1517
# 打印预测结果
16-
print(json.dumps(r.json(), indent=4, ensure_ascii=False))
18+
print(json.dumps(results, indent=4, ensure_ascii=False))

demo/serving/module_serving/lexical_analysis_lac/README.md

+68-20
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ Loading lac successful.
2020
## Step2:测试语言模型在线API
2121
### 不使用自定义词典
2222
在服务部署好之后,我们可以进行测试,用来测试的文本为`今天是个好日子``天气预报说今天要下雨`
23-
23+
首先指定编码格式及引入需要的包:
24+
```python
25+
>>> # coding: utf8
26+
>>> import requests
27+
>>> import json
28+
```
2429
准备的数据格式为:
2530
```python
2631
{"text": [text_1, text_2, ...]}
@@ -46,38 +51,26 @@ Loading lac successful.
4651
# 打印预测结果
4752
>>> print(json.dumps(r.json(), indent=4, ensure_ascii=False))
4853
{
54+
"msg": "",
4955
"results": [
5056
{
5157
"tag": [
52-
"TIME",
53-
"v",
54-
"q",
55-
"n"
58+
"TIME", "v", "q", "n"
5659
],
5760
"word": [
58-
"今天",
59-
"",
60-
"",
61-
"好日子"
61+
"今天", "", "", "好日子"
6262
]
6363
},
6464
{
6565
"tag": [
66-
"n",
67-
"v",
68-
"TIME",
69-
"v",
70-
"v"
66+
"n", "v", "TIME", "v", "v"
7167
],
7268
"word": [
73-
"天气预报",
74-
"",
75-
"今天",
76-
"",
77-
"下雨"
69+
"天气预报", "", "今天", "", "下雨"
7870
]
7971
}
80-
]
72+
],
73+
"status": "0"
8174
}
8275
```
8376
这样我们就完成了对词法分析的预测服务化部署和测试。
@@ -99,3 +92,58 @@ Loading lac successful.
9992
```
10093

10194
完整的测试代码见[lac_with_dict_serving_demo.py](lac_with_dict_serving_demo.py)
95+
96+
### 客户端请求新版模型的方式
97+
对某些新版模型,客户端请求方式有所变化,更接近本地预测的请求方式,以降低学习成本。
98+
以lac(2.1.0)为例,使用上述方法进行请求将提示:
99+
```python
100+
{
101+
"Warnning": "This usage is out of date, please use 'application/json' as content-type to post to /predict/lac. See 'https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.6/docs/tutorial/serving.md' for more details."
102+
}
103+
```
104+
对于lac(2.1.0),请求的方式如下:
105+
```python
106+
# coding: utf8
107+
import requests
108+
import json
109+
110+
if __name__ == "__main__":
111+
# 指定用于预测的文本并生成字典[text_1, text_2, ... ]
112+
text = ["今天是个好日子", "天气预报说今天要下雨"]
113+
# 以key的方式指定text传入预测方法的时的参数,此例中为"texts"
114+
# 对应本地部署,则为lac.analysis_lexical(text=[text1, text2])
115+
data = {"texts": text, "batch_size": 1}
116+
# 指定预测方法为lac并发送post请求
117+
url = "http://127.0.0.1:8866/predict/lac"
118+
# 指定post请求的headers为application/json方式
119+
headers = {"Content-Type": "application/json"}
120+
121+
r = requests.post(url=url, headers=headers, data=json.dumps(data))
122+
123+
# 打印预测结果
124+
print(json.dumps(r.json(), indent=4, ensure_ascii=False))
125+
```
126+
对结果的解析等与前种方式一致,显示如下:
127+
```python
128+
{
129+
"results": [
130+
{
131+
"tag": [
132+
"TIME", "v", "q", "n"
133+
],
134+
"word": [
135+
"今天", "", "", "好日子"
136+
]
137+
},
138+
{
139+
"tag": [
140+
"n", "v", "TIME", "v", "v"
141+
],
142+
"word": [
143+
"天气预报", "", "今天", "", "下雨"
144+
]
145+
}
146+
]
147+
}
148+
```
149+
此Demo的具体信息和代码请参见[LAC Serving_2.1.0](lac_2.1.0_serving_demo.py)

demo/serving/module_serving/lexical_analysis_lac/lac_2.1.0_serving_demo.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
if __name__ == "__main__":
66
# 指定用于预测的文本并生成字典{"text": [text_1, text_2, ... ]}
7-
text = {"text": ["今天是个好日子", "天气预报说今天要下雨"]}
7+
text = ["今天是个好日子", "天气预报说今天要下雨"]
88
# 以key的方式指定text传入预测方法的时的参数,此例中为"data"
9-
# 对应本地部署,则为lac.analysis_lexical(data=text)
10-
data = {"data": text}
11-
# 指定预测方法为lac并发送post请求
9+
# 对应本地部署,则为lac.analysis_lexical(data=text, batch_size=1)
10+
data = {"texts": text, "batch_size": 1}
11+
# 指定预测方法为lac并发送post请求,content-type类型应指定json方式
1212
url = "http://127.0.0.1:8866/predict/lac"
1313
# 指定post请求的headers为application/json方式
1414
headers = {"Content-Type": "application/json"}

demo/serving/module_serving/lexical_analysis_lac/lac_serving_demo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
text_list = ["今天是个好日子", "天气预报说今天要下雨"]
88
text = {"text": text_list}
99
# 指定预测方法为lac并发送post请求
10-
url = "http://127.0.0.1:8866/predict/text/lac"
10+
url = "http://0.0.0.0:8866/predict/text/lac"
1111
r = requests.post(url=url, data=text)
1212

1313
# 打印预测结果

demo/serving/module_serving/object_detection_pyramidbox_lite_server_mask/README.md

+56-33
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ Loading pyramidbox_lite_server_mask successful.
2424
这样就完成了一个口罩检测服务化API的部署,默认端口号为8866。
2525

2626
## Step2:测试图像生成在线API
27+
首先指定编码格式及引入需要的包:
28+
```python
29+
>>> import requests
30+
>>> import json
31+
>>> import base64
32+
>>> import os
33+
```
2734
我们用来测试的样例图片为:
2835

2936
<p align="center">
@@ -56,7 +63,7 @@ files = [("image", file_1), ("image", file_2)]
5663
```python
5764
>>> # 指定检测方法为pyramidbox_lite_server_mask并发送post请求
5865
>>> url = "http://127.0.0.1:8866/predict/image/pyramidbox_lite_server_mask"
59-
>>> r = requests.post(url=url, files=files)
66+
>>> r = requests.post(url=url, files=files, data={"visual_result": "True"})
6067
```
6168
我们可以打印接口返回结果:
6269
```python
@@ -67,63 +74,79 @@ files = [("image", file_1), ("image", file_2)]
6774
"data": [
6875
{
6976
"label": "MASK",
70-
"left": 455.5180733203888,
71-
"right": 658.8289226293564,
72-
"top": 186.38022020459175,
73-
"bottom": 442.67284870147705,
74-
"confidence": 0.92117363
77+
"left": 938.8167103528976,
78+
"right": 1126.8890985250473,
79+
"top": 335.8177453279495,
80+
"bottom": 586.0342741012573,
81+
"confidence": 0.9775171
7582
},
7683
{
77-
"label": "MASK",
78-
"left": 938.9076416492462,
79-
"right": 1121.0804233551025,
80-
"top": 326.9856423139572,
81-
"bottom": 586.0468536615372,
82-
"confidence": 0.997152
84+
"label": "NO MASK",
85+
"left": 1166.563014626503,
86+
"right": 1331.2186390161514,
87+
"top": 298.1251895427704,
88+
"bottom": 496.373051404953,
89+
"confidence": 0.6512484
8390
},
8491
{
85-
"label": "NO MASK",
86-
"left": 1166.189564704895,
87-
"right": 1325.6211009025574,
88-
"top": 295.55220007896423,
89-
"bottom": 496.9406336545944,
90-
"confidence": 0.9346678
92+
"label": "MASK",
93+
"left": 458.2292696237564,
94+
"right": 664.9880893230438,
95+
"top": 179.45007160305977,
96+
"bottom": 446.70506715774536,
97+
"confidence": 0.98069304
9198
}
9299
],
93-
"path": "",
100+
"path": "family_mask.jpg",
94101
"id": 1
95102
},
96103
{
97104
"data": [
98105
{
99106
"label": "MASK",
100-
"left": 1346.7342281341553,
101-
"right": 1593.7974529266357,
102-
"top": 239.36296990513802,
103-
"bottom": 574.6375751495361,
104-
"confidence": 0.95378655
107+
"left": 1340.4194090366364,
108+
"right": 1595.8429119586945,
109+
"top": 251.97067219018936,
110+
"bottom": 584.6931987404823,
111+
"confidence": 0.9681898
105112
},
106113
{
107114
"label": "MASK",
108-
"left": 840.5126552581787,
109-
"right": 1083.8391423225403,
110-
"top": 417.5169044137001,
111-
"bottom": 733.8856244087219,
112-
"confidence": 0.85434145
115+
"left": 839.8990581035614,
116+
"right": 1084.293223142624,
117+
"top": 446.8751857280731,
118+
"bottom": 758.4936121702194,
119+
"confidence": 0.9673422
120+
},
121+
{
122+
"label": "NO MASK",
123+
"left": 1145.4194769859314,
124+
"right": 1253.0083780288696,
125+
"top": 128.66552621126175,
126+
"bottom": 283.0486469864845,
127+
"confidence": 0.97426504
113128
}
114129
],
115-
"path": "",
130+
"path": "woman_mask.jpg",
116131
"id": 2
117132
}
118133
]
119134
```
120135
根据结果可以看出准确识别了请求图片中的人脸位置及戴口罩确信度。
121136

122-
pyramidbox_lite_server_mask返回的结果还包括标注检测框的图像的base64编码格式,经过转换可以得到生成图像,代码如下:
137+
pyramidbox_lite_server_mask返回的结果还包括标注检测框的图像的base64编码格式,经过转换可以得到生成图像。
138+
139+
我们建立一个用于保存结果图片的文件夹:
123140
```python
141+
>>> if not os.path.exists("output"):
142+
>>> os.mkdir("output")
143+
```
144+
然后将图片数据进行解码并保存,代码如下:
145+
```python
146+
>>> results = eval(r.json()["results"])
124147
>>> for item in results:
125-
... with open(output_path, "wb") as fp:
126-
... fp.write(base64.b64decode(item["base64"].split(',')[-1]))
148+
>>> with open(output_path, "wb") as fp:
149+
>>> fp.write(base64.b64decode(item["base64"].split(',')[-1]))
127150
```
128151
查看指定输出文件夹,就能看到生成图像了,如图:
129152

0 commit comments

Comments
 (0)