README.md 6.9 KB
Newer Older
B
Bin Lu 已提交
1
# PaddleClas Pipeline WebService
2

S
stephon 已提交
3
(English|[简体中文](./README_CN.md))
4

B
Bin Lu 已提交
5
PaddleClas provides two service deployment methods:
S
stephon 已提交
6
- Based on **PaddleHub Serving**: Code path is "`./deploy/hubserving`". Please refer to the [tutorial](../../deploy/hubserving/readme_en.md)
7
- Based on **PaddleServing**: Code path is "`./deploy/paddleserving`".  if you prefer retrieval_based image reocognition service, please refer to [tutorial](./recognition/README.md),if you'd like image classification service, Please follow this tutorial.
S
stephon 已提交
8

9
# Image Classification Service deployment based on PaddleServing  
S
stephon 已提交
10

11
This document will introduce how to use the [PaddleServing](https://github.com/PaddlePaddle/Serving/blob/develop/README.md) to deploy the ResNet50_vd model as a pipeline online service.
S
stephon 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

Some Key Features of Paddle Serving:
- Integrate with Paddle training pipeline seamlessly, most paddle models can be deployed with one line command.
- Industrial serving features supported, such as models management, online loading, online A/B testing etc.
- Highly concurrent and efficient communication between clients and servers supported.

The introduction and tutorial of Paddle Serving service deployment framework reference [document](https://github.com/PaddlePaddle/Serving/blob/develop/README.md).


## Contents
- [Environmental preparation](#environmental-preparation)
- [Model conversion](#model-conversion)
- [Paddle Serving pipeline deployment](#paddle-serving-pipeline-deployment)
- [FAQ](#faq)

<a name="environmental-preparation"></a>
## Environmental preparation

B
Bin Lu 已提交
30
PaddleClas operating environment and PaddleServing operating environment are needed.
S
stephon 已提交
31

B
Bin Lu 已提交
32 33
1. Please prepare PaddleClas operating environment reference [link](../../docs/zh_CN/tutorials/install.md).
   Download the corresponding paddle whl package according to the environment, it is recommended to install version 2.1.0.
S
stephon 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

2. The steps of PaddleServing operating environment prepare are as follows:

    Install serving which used to start the service
    ```
    pip3 install paddle-serving-server==0.6.1 # for CPU
    pip3 install paddle-serving-server-gpu==0.6.1 # for GPU
    # Other GPU environments need to confirm the environment and then choose to execute the following commands
    pip3 install paddle-serving-server-gpu==0.6.1.post101 # GPU with CUDA10.1 + TensorRT6
    pip3 install paddle-serving-server-gpu==0.6.1.post11 # GPU with CUDA11 + TensorRT7
    ```

3. Install the client to send requests to the service
    In [download link](https://github.com/PaddlePaddle/Serving/blob/develop/doc/LATEST_PACKAGES.md) find the client installation package corresponding to the python version.
    The python3.7 version is recommended here:

    ```
    wget https://paddle-serving.bj.bcebos.com/test-dev/whl/paddle_serving_client-0.0.0-cp37-none-any.whl
    pip3 install paddle_serving_client-0.0.0-cp37-none-any.whl
    ```

4. Install serving-app
    ```
    pip3 install paddle-serving-app==0.6.1
    ```

   **note:** If you want to install the latest version of PaddleServing, refer to [link](https://github.com/PaddlePaddle/Serving/blob/develop/doc/LATEST_PACKAGES.md).


<a name="model-conversion"></a>
## Model conversion
When using PaddleServing for service deployment, you need to convert the saved inference model into a serving model that is easy to deploy.

B
Bin Lu 已提交
67
Firstly, download the inference model of ResNet50_vd
S
stephon 已提交
68
```
B
Bin Lu 已提交
69 70
# Download and unzip the ResNet50_vd model
wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/ResNet50_vd_infer.tar  && tar xf ResNet50_vd_infer.tar
S
stephon 已提交
71
```
B
Bin Lu 已提交
72

S
stephon 已提交
73 74
Then, you can use installed paddle_serving_client tool to convert inference model to mobile model.
```
B
Bin Lu 已提交
75 76 77 78 79 80
#  ResNet50_vd model conversion
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
stephon 已提交
81 82
```

B
Bin Lu 已提交
83 84 85 86 87 88 89 90 91 92 93
After the ResNet50_vd inference model is converted, there will be additional folders of `ResNet50_vd_serving` and `ResNet50_vd_client` in the current folder, with the following format:
```
|- ResNet50_vd_client/
  |- __model__  
  |- __params__
  |- serving_server_conf.prototxt  
  |- serving_server_conf.stream.prototxt

|- ResNet50_vd_client
  |- serving_client_conf.prototxt  
  |- serving_client_conf.stream.prototxt
94
```
S
stephon 已提交
95

B
Bin Lu 已提交
96
Once you have the model file for deployment, you need to change the alias name in `serving_server_conf.prototxt`: Change `alias_name` in `feed_var` to `image`, change `alias_name` in `fetch_var` to `prediction`,
B
Bin Lu 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
The modified serving_server_conf.prototxt file is as follows:
```
feed_var {
  name: "inputs"
  alias_name: "image"
  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"
  is_lod_tensor: true
  fetch_type: 1
  shape: -1
}
115
```
S
stephon 已提交
116 117 118 119

<a name="paddle-serving-pipeline-deployment"></a>
## Paddle Serving pipeline deployment

B
Bin Lu 已提交
120
1. Download the PaddleClas code, if you have already downloaded it, you can skip this step.
S
stephon 已提交
121
    ```
B
Bin Lu 已提交
122
    git clone https://github.com/PaddlePaddle/PaddleClas
S
stephon 已提交
123 124

    # Enter the working directory  
B
Bin Lu 已提交
125
    cd PaddleClas/deploy/paddleserving/
S
stephon 已提交
126 127
    ```

B
Bin Lu 已提交
128
    The paddleserving directory contains the code to start the pipeline service and send prediction requests, including:
S
stephon 已提交
129 130
    ```
    __init__.py
B
Bin Lu 已提交
131 132 133
    config.yml                # configuration file of starting the service
    pipeline_http_client.py   # script to send pipeline prediction request by http
    pipeline_rpc_client.py    # script to send pipeline prediction request by rpc
134
    classification_web_service.py   # start the script of the pipeline server
S
stephon 已提交
135 136 137 138 139
    ```

2. Run the following command to start the service.
    ```
    # Start the service and save the running log in log.txt
140
    python3 classification_web_service.py &>log.txt &
S
stephon 已提交
141 142 143 144 145 146 147 148 149
    ```
    After the service is successfully started, a log similar to the following will be printed in log.txt
    ![](./imgs/start_server.png)

3. Send service request
    ```
    python3 pipeline_http_client.py
    ```
    After successfully running, the predicted result of the model will be printed in the cmd window. An example of the result is:
150
    ![](./imgs/results.png)
S
stephon 已提交
151

B
Bin Lu 已提交
152
    Adjust the number of concurrency in config.yml to get the largest QPS. 
S
stephon 已提交
153 154

    ```
B
Bin Lu 已提交
155
    op:
S
stephon 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168
        concurrency: 8
        ...
    ```

    Multiple service requests can be sent at the same time if necessary.

    The predicted performance data will be automatically written into the `PipelineServingLogs/pipeline.tracer` file.

<a name="faq"></a>
## FAQ
**Q1**: No result return after sending the request.

**A1**: Do not set the proxy when starting the service and sending the request. You can close the proxy before starting the service and before sending the request. The command to close the proxy is:
169
```
S
stephon 已提交
170 171 172
unset https_proxy
unset http_proxy
```