SAVE.md 3.0 KB
Newer Older
M
MRXLT 已提交
1
# How to save a servable model of Paddle Serving?
J
Jiawei Wang 已提交
2 3 4

([简体中文](./SAVE_CN.md)|English)

M
MRXLT 已提交
5 6
## Save from training or prediction script 
Currently, paddle serving provides a save_model interface for users to access, the interface is similar with `save_inference_model` of Paddle.
D
Dong Daxiang 已提交
7 8 9 10 11 12
``` python
import paddle_serving_client.io as serving_io
serving_io.save_model("imdb_model", "imdb_client_conf",
                      {"words": data}, {"prediction": prediction},
                      fluid.default_main_program())
```
M
fix doc  
MRXLT 已提交
13 14 15
`imdb_model` is the server side model with serving configurations. `imdb_client_conf` is the client rpc configurations. 

Serving has a dictionary for `Feed` and `Fetch` variables for client to assign. In the example, `{"words": data}` is the feed dict that specify the input of saved inference model. `{"prediction": prediction}` is the fetch dic that specify the output of saved inference model. An alias name can be defined for feed and fetch variables. An example of how to use alias name
W
wangjiawei04 已提交
16
 is as follows:
D
Dong Daxiang 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 ``` python
 from paddle_serving_client import Client
import sys

client = Client()
client.load_client_config(sys.argv[1])
client.connect(["127.0.0.1:9393"])

for line in sys.stdin:
    group = line.strip().split()
    words = [int(x) for x in group[1:int(group[0]) + 1]]
    label = [int(group[-1])]
    feed = {"words": words, "label": label}
    fetch = ["acc", "cost", "prediction"]
    fetch_map = client.predict(feed=feed, fetch=fetch)
    print("{} {}".format(fetch_map["prediction"][1], label[0]))
 ```
M
MRXLT 已提交
34 35 36

## Export from saved model files
If you have saved model files using Paddle's `save_inference_model` API, you can use Paddle Serving's` inference_model_to_serving` API to convert it into a model file that can be used for Paddle Serving.
M
fix doc  
MRXLT 已提交
37
```python
M
MRXLT 已提交
38
import paddle_serving_client.io as serving_io
M
MRXLT 已提交
39
serving_io.inference_model_to_serving(dirname, serving_server="serving_server", serving_client="serving_client", model_filename=None, params_filename=None )
M
MRXLT 已提交
40
```
W
WangXi 已提交
41
Or you can use a build-in python module called `paddle_serving_client.convert` to convert it.
42
```python
W
WangXi 已提交
43
python -m paddle_serving_client.convert --dirname ./your_inference_model_dir
44 45
```
Arguments are the same as `inference_model_to_serving` API.
W
WangXi 已提交
46 47 48 49 50 51 52
| Argument | Type | Default | Description |
|--------------|------|-----------|--------------------------------|
| `dirname` | str | - | Path of saved model files. Program file and parameter files are saved in this directory. |
| `serving_server` | str | `"serving_server"` | The path of model files and configuration files for server. |
| `serving_client` | str | `"serving_client"` | The path of configuration files for client. |
| `model_filename` | str | None | The name of file to load the inference program. If it is None, the default filename `__model__` will be used. |
| `paras_filename` | str | None | The name of file to load all parameters. It is only used for the case that all parameters were saved in a single binary file. If parameters were saved in separate files, set it as None. |