PaddleOCR/deploy/pdserving/README.md

159 lines
6.9 KiB
Markdown
Raw Normal View History

2021-03-23 15:04:55 +08:00
# OCR Pipeline WebService
(English|[简体中文](./README_CN.md))
2021-03-24 10:10:43 +08:00
PaddleOCR provides two service deployment methods:
2021-03-23 21:30:27 +08:00
- Based on **PaddleHub Serving**: Code path is "`./deploy/hubserving`". Please refer to the [tutorial](../../deploy/hubserving/readme_en.md)
- Based on **PaddleServing**: Code path is "`./deploy/pdserving`". Please follow this tutorial.
2021-03-23 15:04:55 +08:00
2021-03-23 21:30:27 +08:00
# Service deployment based on PaddleServing
This document will introduce how to use the [PaddleServing](https://github.com/PaddlePaddle/Serving/blob/develop/README.md) to deploy the PPOCR dynamic graph model as a pipeline online service.
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).
2021-03-23 15:04:55 +08:00
## Contents
2021-03-23 21:30:27 +08:00
- [Environmental preparation](#environmental-preparation)
- [Model conversion](#model-conversion)
- [Paddle Serving pipeline deployment](#paddle-serving-pipeline-deployment)
- [FAQ](#faq)
2021-03-23 15:04:55 +08:00
2021-03-23 21:30:27 +08:00
<a name="environmental-preparation"></a>
2021-03-23 15:04:55 +08:00
## Environmental preparation
2021-03-24 10:10:43 +08:00
PaddleOCR operating environment and Paddle Serving operating environment are needed.
2021-03-23 15:04:55 +08:00
2021-03-24 10:10:43 +08:00
1. Please prepare PaddleOCR operating environment reference [link](../../doc/doc_ch/installation.md).
2021-03-23 15:04:55 +08:00
2021-03-24 10:10:43 +08:00
2. The steps of PaddleServing operating environment prepare are as follows:
2021-03-23 15:04:55 +08:00
2021-03-23 21:30:27 +08:00
Install serving which used to start the service
```
pip3 install paddle-serving-server==0.5.0 # for CPU
pip3 install paddle-serving-server-gpu==0.5.0 # 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.5.0.post9 # GPU with CUDA9.0
pip3 install paddle-serving-server-gpu==0.5.0.post10 # GPU with CUDA10.0
pip3 install paddle-serving-server-gpu==0.5.0.post101 # GPU with CUDA10.1 + TensorRT6
pip3 install paddle-serving-server-gpu==0.5.0.post11 # GPU with CUDA10.1 + TensorRT7
```
3. Install the client to send requests to the service
```
pip3 install paddle-serving-client==0.5.0 # for CPU
pip3 install paddle-serving-client-gpu==0.5.0 # for GPU
```
4. Install serving-app
```
pip3 install paddle-serving-app==0.3.0
# fix local_predict to support load dynamic model
# find the install directoory of paddle_serving_app
vim /usr/local/lib/python3.7/site-packages/paddle_serving_app/local_predict.py
# replace line 85 of local_predict.py config = AnalysisConfig(model_path) with:
if os.path.exists(os.path.join(model_path, "__params__")):
config = AnalysisConfig(os.path.join(model_path, "__model__"), os.path.join(model_path, "__params__"))
else:
config = AnalysisConfig(model_path)
```
**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>
2021-03-23 15:04:55 +08:00
## 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.
2021-03-23 21:30:27 +08:00
Firstly, download the [inference model](https://github.com/PaddlePaddle/PaddleOCR#pp-ocr-20-series-model-listupdate-on-dec-15) of PPOCR
2021-03-23 15:04:55 +08:00
```
# Download and unzip the OCR text detection model
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_det_infer.tar && tar xf ch_ppocr_server_v2.0_det_infer.tar
# Download and unzip the OCR text recognition model
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_rec_infer.tar && tar xf ch_ppocr_server_v2.0_rec_infer.tar
2021-03-24 10:10:43 +08:00
```
Then, you can use installed paddle_serving_client tool to convert inference model to server model.
```
2021-03-23 21:32:57 +08:00
# Detection model conversion
2021-03-23 15:04:55 +08:00
python3 -m paddle_serving_client.convert --dirname ./ch_ppocr_server_v2.0_det_infer/ \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--serving_server ./ppocr_det_server_2.0_serving/ \
--serving_client ./ppocr_det_server_2.0_client/
2021-03-23 21:32:57 +08:00
# Recognition model conversion
2021-03-23 15:04:55 +08:00
python3 -m paddle_serving_client.convert --dirname ./ch_ppocr_server_v2.0_rec_infer/ \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--serving_server ./ppocr_rec_server_2.0_serving/ \
--serving_client ./ppocr_rec_server_2.0_client/
```
After the detection model is converted, there will be additional folders of `ppocr_det_server_2.0_serving` and `ppocr_det_server_2.0_client` in the current folder, with the following format:
```
|- ppocr_det_server_2.0_serving/
|- __model__
|- __params__
|- serving_server_conf.prototxt
|- serving_server_conf.stream.prototxt
|- ppocr_det_server_2.0_client
|- serving_client_conf.prototxt
|- serving_client_conf.stream.prototxt
```
The recognition model is the same.
2021-03-23 21:30:27 +08:00
<a name="paddle-serving-pipeline-deployment"></a>
2021-03-23 15:04:55 +08:00
## Paddle Serving pipeline deployment
1. Download the PaddleOCR code, if you have already downloaded it, you can skip this step.
2021-03-23 21:30:27 +08:00
```
git clone https://github.com/PaddlePaddle/PaddleOCR
# Enter the working directory
cd PaddleOCR/deploy/pdserver/
```
The pdserver directory contains the code to start the pipeline service and send prediction requests, including:
```
__init__.py
config.yml # Start the service configuration file
ocr_reader.py # OCR model pre-processing and post-processing code implementation
pipeline_http_client.py # Script to send pipeline prediction request
web_service.py # Start the script of the pipeline server
```
2021-03-23 15:04:55 +08:00
2. Run the following command to start the service.
2021-03-23 21:30:27 +08:00
```
# Start the service and save the running log in log.txt
python3 web_service.py &>log.txt &
```
After the service is successfully started, a log similar to the following will be printed in log.txt
![](./imgs/start_server.png)
2021-03-23 15:04:55 +08:00
3. Send service request
2021-03-23 21:30:27 +08:00
```
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:
![](./imgs/results.png)
2021-03-23 15:04:55 +08:00
2021-03-23 21:30:27 +08:00
<a name="faq"></a>
2021-03-23 15:04:55 +08:00
## FAQ
2021-03-24 13:20:44 +08:00
**Q1**: No result return after sending the request.
2021-03-23 21:30:27 +08:00
2021-03-24 13:20:44 +08:00
**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:
2021-03-23 15:04:55 +08:00
```
unset https_proxy
unset http_proxy
```