tensorflow serving部署概要

1. 目录结构

[@hbhly_SG21_41_53 pb_models]# tree textcnn/
textcnn/
└── 000
    ├── saved_model.pb
    └── variables
        ├── variables.data-00000-of-00001
        └── variables.index

2. 检查结果pb文件的default配置

saved_model_cli show --dir /root/huxiang/serving/pb_models/textcnn/000 --all

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['voice'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 50)
        name: inputs:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 2)
        name: FC2/Softmax:0
  Method name is: tensorflow/serving/predict

3. 拉取镜像


docker pull tensorflow/serving:1.12.0

4. 部署


docker run -p 8501:8501 -p 8500:8500 -v "/root/huxiang/serving/pb_models/textcnn:/models/textcnn" -e MODEL_NAME=textcnn -e MODEL_BASE_PATH=/models/ -t tensorflow/serving:1.12.0 > d.log &

5. 测试


curl -d '{"inputs": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}' -X POST http://localhost:8501/v1/models/textcnn:predict  

6. 返回结果


{
    "outputs": [
        [
            0.098275,
            0.901725
        ]
    ]
}

7. python测试

import json
import numpy as np
import requests
from contextlib import contextmanager
from time import time

@contextmanager
def timer(name):
    t0 = time()
    yield
    # print(f'[{name}] done in {time() - t0:.2f} s')
    print(name + 'cost:' + str(time() - t0) + 's')

def test():
    data = json.dumps({
        "inputs": [[1] * 50]
    })
    headers = {"content-type": "application/json"}
    json_response = requests.post(
        'http://localhost:8501/v1/models/textcnn:predict',
        data=data, headers=headers)
    predictions = np.array(json.loads(json_response.text)['outputs'])
    # print(np.argmax(predictions, axis=-1))


if __name__ == '__main__':

    with timer('single...'):
        test()

    with timer('test...'):
        for i in range(1000):
            test()

    with timer('single...'):
        test()



"""
# 好像刚开始的时候速度有点慢
single...cost:0.0194900035858s
test...cost:2.31740784645s
single...cost:0.00248503684998s
    
# mac的速度
single...cost:0.016093969345092773s
test...cost:4.734021186828613s
single...cost:0.004168033599853516s
"""
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值