README.md 3.3 KB
Newer Older
G
guru4elephant 已提交
1 2
# Paddle Serving
Paddle Serving是PaddlePaddle的在线预估服务框架,能够帮助开发者轻松实现从移动端、服务器端调用深度学习模型的远程预测服务。当前Paddle Serving以支持PaddlePaddle训练的模型为主,可以与Paddle训练框架联合使用,快速部署预估服务。
D
Dong Daxiang 已提交
3

G
guru4elephant 已提交
4
## 客户端快速上手
G
guru4elephant 已提交
5
Paddle Serving当前的develop版本支持轻量级Python API进行快速预测,我们假设远程已经部署的Paddle Serving的文本分类模型,您可以在自己的服务器快速安装客户端并进行快速预测。
D
Dong Daxiang 已提交
6

G
guru4elephant 已提交
7
### 客户端安装
G
guru4elephant 已提交
8 9 10
```
pip install paddle-serving-client
```
D
Dong Daxiang 已提交
11

G
guru4elephant 已提交
12
### 训练过程中保存Serving的配置
G
guru4elephant 已提交
13
``` python
G
guru4elephant 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 67 68 69
import os
import sys
import paddle
import logging
import paddle.fluid as fluid
import paddle_serving as serving

logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger("fluid")
logger.setLevel(logging.INFO)

def load_vocab(filename):
    vocab = {}
    with open(filename) as f:
        wid = 0
        for line in f:
            vocab[line.strip()] = wid
            wid += 1
    vocab["<unk>"] = len(vocab)
    return vocab

if __name__ == "__main__":
    vocab = load_vocab('imdb.vocab')
    dict_dim = len(vocab)

    data = fluid.layers.data(name="words", shape=[1], dtype="int64", lod_level=1)
    label = fluid.layers.data(name="label", shape=[1], dtype="int64")

    dataset = fluid.DatasetFactory().create_dataset()
    filelist = ["train_data/%s" % x for x in os.listdir("train_data")]
    dataset.set_use_var([data, label])
    pipe_command = "python imdb_reader.py"
    dataset.set_pipe_command(pipe_command)
    dataset.set_batch_size(4)
    dataset.set_filelist(filelist)
    dataset.set_thread(10)
    from nets import cnn_net
    avg_cost, acc, prediction = cnn_net(data, label, dict_dim)
    optimizer = fluid.optimizer.SGD(learning_rate=0.01)
    optimizer.minimize(avg_cost)

    exe = fluid.Executor(fluid.CPUPlace())
    exe.run(fluid.default_startup_program())
    epochs = 30
    save_dirname = "cnn_model"

    for i in range(epochs):
        exe.train_from_dataset(program=fluid.default_main_program(),
                               dataset=dataset, debug=False)
        logger.info("TRAIN --> pass: {}".format(i))
        fluid.io.save_inference_model("%s/epoch%d.model" % (save_dirname, i),
                                      [data.name, label.name], [acc], exe)
        serving.save_model("%s/epoch%d.model" % (save_dirname, i), "client_config{}".format(i),
                           {"words": data, "label": label},
                           {"acc": acc, "cost": avg_cost, "prediction": prediction})
```
D
Dong Daxiang 已提交
70

G
guru4elephant 已提交
71
### 启动服务
G
guru4elephant 已提交
72
TBA
D
Dong Daxiang 已提交
73

G
guru4elephant 已提交
74
### 客户端访问
G
guru4elephant 已提交
75 76 77
``` python
from paddle_serving import Client
import sys
D
Dong Daxiang 已提交
78

G
guru4elephant 已提交
79 80 81
client = Client()
client.load_client_config(sys.argv[1])
client.connect(["127.0.0.1:9292"])
W
wangguibao 已提交
82

G
guru4elephant 已提交
83 84 85 86 87 88 89 90
for line in sys.stdin:
    group = line.strip().split()
    words = [int(x) for x in group[1:int(group[0])]]
    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]))
W
wangguibao 已提交
91

G
guru4elephant 已提交
92
```
W
wangguibao 已提交
93 94


G
guru4elephant 已提交
95
### 完成操作截屏
G
guru4elephant 已提交
96
TBA
W
wangguibao 已提交
97

G
guru4elephant 已提交
98
## 文档
W
wangguibao 已提交
99

G
guru4elephant 已提交
100
[设计文档](doc/DESIGN.md)
W
wangguibao 已提交
101

G
guru4elephant 已提交
102
[从零开始写一个预测服务](doc/CREATING.md)
W
wangguibao 已提交
103

G
guru4elephant 已提交
104
[编译安装](doc/INSTALL.md)
W
wangguibao 已提交
105

G
guru4elephant 已提交
106
[FAQ](doc/FAQ.md)
W
wangguibao 已提交
107