paddle_serving_deploy.md 10.0 KB
Newer Older
1
# 模型服务化部署
S
sibo2rr 已提交
2 3 4 5 6 7 8 9 10 11 12
- [1. 简介](#1)
- [2. Serving 安装](#2)
- [3. 图像分类服务部署](#3)
    - [3.1 模型转换](#3.1)
    - [3.2 服务部署和请求](#3.2)
- [4. 图像识别服务部署](#4)
  - [4.1 模型转换](#4.1)
  - [4.2 服务部署和请求](#4.2)
- [5. FAQ](#5)

<a name="1"></a>
13 14 15
## 1. 简介
[Paddle Serving](https://github.com/PaddlePaddle/Serving) 旨在帮助深度学习开发者轻松部署在线预测服务,支持一键部署工业级的服务能力、客户端和服务端之间高并发和高效通信、并支持多种编程语言开发客户端。

B
Bin Lu 已提交
16
该部分以 HTTP 预测服务部署为例,介绍怎样在 PaddleClas 中使用 PaddleServing 部署模型服务。目前只支持 Linux 平台部署,暂不支持 Windows 平台。
17

S
sibo2rr 已提交
18 19
<a name="2"></a>
## 2. Serving 安装
20 21 22
Serving 官网推荐使用 docker 安装并部署 Serving 环境。首先需要拉取 docker 环境并创建基于 Serving 的 docker。

```shell
S
stephon 已提交
23
# 启动GPU docker
S
stephon 已提交
24 25
docker pull paddlepaddle/serving:0.7.0-cuda10.2-cudnn7-devel
nvidia-docker run -p 9292:9292 --name test -dit paddlepaddle/serving:0.7.0-cuda10.2-cudnn7-devel bash
26
nvidia-docker exec -it test bash
S
stephon 已提交
27 28 29 30 31

# 启动CPU docker
docker pull paddlepaddle/serving:0.7.0-devel
docker run -p 9292:9292 --name test -dit paddlepaddle/serving:0.7.0-devel bash
docker exec -it test bash
32 33 34 35
```

进入 docker 后,需要安装 Serving 相关的 python 包。
```shell
S
stephon 已提交
36 37
pip3 install paddle-serving-client==0.7.0
pip3 install paddle-serving-app==0.7.0
B
Bin Lu 已提交
38
pip3 install faiss-cpu==1.7.1post2
S
stephon 已提交
39 40 41 42 43 44 45 46 47 48

#若为CPU部署环境:
pip3 install paddle-serving-server==0.7.0 # CPU
pip3 install paddlepaddle==2.2.0          # CPU

#若为GPU部署环境
pip3 install paddle-serving-server-gpu==0.7.0.post102 # GPU with CUDA10.2 + TensorRT6
pip3 install paddlepaddle-gpu==2.2.0     # GPU with CUDA10.2

#其他GPU环境需要确认环境再选择执行哪一条
S
stephon 已提交
49 50
pip3 install paddle-serving-server-gpu==0.7.0.post101 # GPU with CUDA10.1 + TensorRT6
pip3 install paddle-serving-server-gpu==0.7.0.post112 # GPU with CUDA11.2 + TensorRT8
51 52 53
```

* 如果安装速度太慢,可以通过 `-i https://pypi.tuna.tsinghua.edu.cn/simple` 更换源,加速安装过程。
S
stephon 已提交
54
* 其他环境配置安装请参考: [使用Docker安装Paddle Serving](https://github.com/PaddlePaddle/Serving/blob/v0.7.0/doc/Install_CN.md)
55

S
sibo2rr 已提交
56
<a name="3"></a>
W
Wu Sibo 已提交
57

S
stephon 已提交
58
## 3. 图像分类服务部署
S
sibo2rr 已提交
59
<a name="3.1"></a>
S
stephon 已提交
60
### 3.1 模型转换
S
sibo2rr 已提交
61
使用 PaddleServing 做服务化部署时,需要将保存的 inference 模型转换为 Serving 模型。下面以经典的 ResNet50_vd 模型为例,介绍如何部署图像分类服务。
B
Bin Lu 已提交
62
- 进入工作目录:
63
```shell
S
stephon 已提交
64
cd deploy/paddleserving
65
```
S
sibo2rr 已提交
66
- 下载 ResNet50_vd 的 inference 模型:
67
```shell
S
sibo2rr 已提交
68
# 下载并解压 ResNet50_vd 模型
S
stephon 已提交
69
wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/ResNet50_vd_infer.tar && tar xf ResNet50_vd_infer.tar
70
```
S
sibo2rr 已提交
71
- 用 paddle_serving_client 把下载的 inference 模型转换成易于 Server 部署的模型格式:
S
stephon 已提交
72
```
S
sibo2rr 已提交
73
# 转换 ResNet50_vd 模型
S
stephon 已提交
74 75 76 77 78 79
python3 -m paddle_serving_client.convert --dirname ./ResNet50_vd_infer/ \
                                         --model_filename inference.pdmodel  \
                                         --params_filename inference.pdiparams \
                                         --serving_server ./ResNet50_vd_serving/ \
                                         --serving_client ./ResNet50_vd_client/
```
S
sibo2rr 已提交
80
ResNet50_vd 推理模型转换完成后,会在当前文件夹多出 `ResNet50_vd_serving``ResNet50_vd_client` 的文件夹,具备如下格式:
S
stephon 已提交
81
```
S
stephon 已提交
82
|- ResNet50_vd_server/
S
stephon 已提交
83 84
  |- inference.pdiparams
  |- inference.pdmodel
S
stephon 已提交
85 86 87 88 89 90
  |- serving_server_conf.prototxt  
  |- serving_server_conf.stream.prototxt
|- ResNet50_vd_client
  |- serving_client_conf.prototxt  
  |- serving_client_conf.stream.prototxt
```
S
stephon 已提交
91
得到模型文件之后,需要修改 `ResNet50_vd_server` 下文件 `serving_server_conf.prototxt` 中的 alias 名字:将 `fetch_var` 中的 `alias_name` 改为 `prediction`
B
Bin Lu 已提交
92

S
sibo2rr 已提交
93 94
**备注**:  Serving 为了兼容不同模型的部署,提供了输入输出重命名的功能。这样,不同的模型在推理部署时,只需要修改配置文件的 alias_name 即可,无需修改代码即可完成推理部署。
修改后的 serving_server_conf.prototxt 如下所示:
S
stephon 已提交
95 96 97
```
feed_var {
  name: "inputs"
S
stephon 已提交
98
  alias_name: "inputs"
S
stephon 已提交
99 100 101 102 103 104 105 106 107
  is_lod_tensor: false
  feed_type: 1
  shape: 3
  shape: 224
  shape: 224
}
fetch_var {
  name: "save_infer_model/scale_0.tmp_1"
  alias_name: "prediction"
S
stephon 已提交
108
  is_lod_tensor: false
S
stephon 已提交
109
  fetch_type: 1
S
stephon 已提交
110
  shape: 1000
S
stephon 已提交
111 112
}
```
S
sibo2rr 已提交
113
<a name="3.2"></a>
S
stephon 已提交
114
### 3.2 服务部署和请求
S
sibo2rr 已提交
115
paddleserving 目录包含了启动 pipeline 服务和发送预测请求的代码,包括:
S
stephon 已提交
116 117 118 119 120 121
```shell
__init__.py
config.yml                 # 启动服务的配置文件
pipeline_http_client.py    # http方式发送pipeline预测请求的脚本
pipeline_rpc_client.py     # rpc方式发送pipeline预测请求的脚本
classification_web_service.py    # 启动pipeline服务端的脚本
122
```
B
Bin Lu 已提交
123

S
stephon 已提交
124 125
- 启动服务:
```shell
S
sibo2rr 已提交
126
# 启动服务,运行日志保存在 log.txt
S
stephon 已提交
127 128
python3 classification_web_service.py &>log.txt &
```
S
sibo2rr 已提交
129
成功启动服务后,log.txt 中会打印类似如下日志
S
stephon 已提交
130
![](../../../deploy/paddleserving/imgs/start_server.png)
B
Bin Lu 已提交
131

B
Bin Lu 已提交
132
- 发送请求:
S
stephon 已提交
133 134 135
```shell
# 发送服务请求
python3 pipeline_http_client.py
136
```
S
sibo2rr 已提交
137
成功运行后,模型预测的结果会打印在 cmd 窗口中,结果示例为:
S
stephon 已提交
138
![](../../../deploy/paddleserving/imgs/results.png)
139

S
sibo2rr 已提交
140
<a name="4"></a>
B
Bin Lu 已提交
141
## 4.图像识别服务部署
S
sibo2rr 已提交
142 143
使用 PaddleServing 做服务化部署时,需要将保存的 inference 模型转换为 Serving 模型。 下面以 PP-ShiTu 中的超轻量图像识别模型为例,介绍图像识别服务的部署。
<a name="4.1"></a>
B
Bin Lu 已提交
144
## 4.1 模型转换
S
sibo2rr 已提交
145
- 下载通用检测 inference 模型和通用识别 inference 模型
B
Bin Lu 已提交
146 147
```
cd deploy
B
Bin Lu 已提交
148
# 下载并解压通用识别模型
B
Bin Lu 已提交
149 150 151
wget -P models/ https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/general_PPLCNet_x2_5_lite_v1.0_infer.tar
cd models
tar -xf general_PPLCNet_x2_5_lite_v1.0_infer.tar
B
Bin Lu 已提交
152
# 下载并解压通用检测模型
B
Bin Lu 已提交
153 154 155
wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer.tar
tar -xf picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer.tar
```
S
sibo2rr 已提交
156
- 转换识别 inference 模型为 Serving 模型:
B
Bin Lu 已提交
157
```
B
Bin Lu 已提交
158
# 转换识别模型
B
Bin Lu 已提交
159 160 161 162 163 164
python3 -m paddle_serving_client.convert --dirname ./general_PPLCNet_x2_5_lite_v1.0_infer/ \
                                         --model_filename inference.pdmodel  \
                                         --params_filename inference.pdiparams \
                                         --serving_server ./general_PPLCNet_x2_5_lite_v1.0_serving/ \
                                         --serving_client ./general_PPLCNet_x2_5_lite_v1.0_client/
```
S
stephon 已提交
165
识别推理模型转换完成后,会在当前文件夹多出 `general_PPLCNet_x2_5_lite_v1.0_serving/``general_PPLCNet_x2_5_lite_v1.0_client/` 的文件夹。修改 `general_PPLCNet_x2_5_lite_v1.0_serving/` 目录下的 serving_server_conf.prototxt 中的 alias 名字: 将 `fetch_var` 中的 `alias_name` 改为 `features`
S
sibo2rr 已提交
166
修改后的 serving_server_conf.prototxt 内容如下:
B
Bin Lu 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179
```
feed_var {
  name: "x"
  alias_name: "x"
  is_lod_tensor: false
  feed_type: 1
  shape: 3
  shape: 224
  shape: 224
}
fetch_var {
  name: "save_infer_model/scale_0.tmp_1"
  alias_name: "features"
S
stephon 已提交
180
  is_lod_tensor: false
B
Bin Lu 已提交
181
  fetch_type: 1
S
stephon 已提交
182
  shape: 512
B
Bin Lu 已提交
183 184
}
```
S
sibo2rr 已提交
185
- 转换通用检测 inference 模型为 Serving 模型:
B
Bin Lu 已提交
186 187 188 189 190 191 192 193
```
# 转换通用检测模型
python3 -m paddle_serving_client.convert --dirname ./picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer/ \
                                         --model_filename inference.pdmodel  \
                                         --params_filename inference.pdiparams \
                                         --serving_server ./picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving/ \
                                         --serving_client ./picodet_PPLCNet_x2_5_mainbody_lite_v1.0_client/
```
194
检测 inference 模型转换完成后,会在当前文件夹多出 `picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving/``picodet_PPLCNet_x2_5_mainbody_lite_v1.0_client/` 的文件夹。
B
Bin Lu 已提交
195

196
**注意:** 此处不需要修改 `picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving/` 目录下的 serving_server_conf.prototxt 中的 alias 名字。
B
Bin Lu 已提交
197

S
sibo2rr 已提交
198
- 下载并解压已经构建后的检索库 index
B
Bin Lu 已提交
199 200 201 202
```
cd ../
wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/data/drink_dataset_v1.0.tar && tar -xf drink_dataset_v1.0.tar
```
S
sibo2rr 已提交
203
<a name="4.2"></a>
B
Bin Lu 已提交
204
## 4.2 服务部署和请求
S
sibo2rr 已提交
205
**注意:** 识别服务涉及到多个模型,出于性能考虑采用 PipeLine 部署方式。Pipeline 部署方式当前不支持 windows 平台。
B
Bin Lu 已提交
206 207 208 209
- 进入到工作目录
```shell
cd ./deploy/paddleserving/recognition
```
S
sibo2rr 已提交
210
paddleserving 目录包含启动 pipeline 服务和发送预测请求的代码,包括:
B
Bin Lu 已提交
211 212 213 214 215 216 217 218 219
```
__init__.py
config.yml                    # 启动服务的配置文件
pipeline_http_client.py       # http方式发送pipeline预测请求的脚本
pipeline_rpc_client.py        # rpc方式发送pipeline预测请求的脚本
recognition_web_service.py    # 启动pipeline服务端的脚本
```
- 启动服务:
```
S
sibo2rr 已提交
220
# 启动服务,运行日志保存在 log.txt
B
Bin Lu 已提交
221 222
python3 recognition_web_service.py &>log.txt &
```
S
sibo2rr 已提交
223
成功启动服务后,log.txt 中会打印类似如下日志
B
Bin Lu 已提交
224
![](../../../deploy/paddleserving/imgs/start_server_shitu.png)
B
Bin Lu 已提交
225 226 227 228 229

- 发送请求:
```
python3 pipeline_http_client.py
```
S
sibo2rr 已提交
230
成功运行后,模型预测的结果会打印在 cmd 窗口中,结果示例为:
B
Bin Lu 已提交
231 232
![](../../../deploy/paddleserving/imgs/results_shitu.png)

S
sibo2rr 已提交
233
<a name="5"></a>
B
Bin Lu 已提交
234 235 236 237 238 239 240 241 242
## 5.FAQ
**Q1**: 发送请求后没有结果返回或者提示输出解码报错

**A1**: 启动服务和发送请求时不要设置代理,可以在启动服务前和发送请求前关闭代理,关闭代理的命令是:
```
unset https_proxy
unset http_proxy
```

S
stephon 已提交
243
更多的服务部署类型,如 `RPC 预测服务` 等,可以参考 Serving 的[github 官网](https://github.com/PaddlePaddle/Serving/tree/v0.7.0/examples)