python模型如何部署上线_python如何调用tensorflow Serving部署的模型

以下是我调用一个关键点检测服务的示例代码from grpc.beta import implementations

import tensorflow as tf

from tensorflow_serving.apis import predict_pb2

from tensorflow_serving.apis import prediction_service_pb2

class KeyPointDetector:

def __init__(self, hostport, model_name):

self.hostport = hostport

self.model_name = model_name

def do_inference(self,

crop_image,

box,

ori_shape,

shape_bbox,

signature_name=None):

"""Tests PredictionService with concurrent requests.

Args:

hostport: Host:port address of the Prediction Service.

Returns:

pred values, ground truth label

"""

host, port = self.hostport.split(':')

channel = implementations.insecure_channel(host, int(port))

stub = prediction_service_pb2.beta_create_PredictionService_stub(

channel)

# fill in the request object with the necessary data

request = predict_pb2.PredictRequest()

request.model_spec.name = self.model_name

# request.model_spec.signature_name = 'pred_v'

request.inputs['crop_image_processed'].CopyFrom(

tf.contrib.util.make_tensor_proto(np.array(crop_image).astype(

np.float32),

shape=[384, 384, 3]))

request.inputs['boxes'].CopyFrom(

tf.contrib.util.make_tensor_proto(np.array(box).astype(np.float32),

shape=[1, 4]))

request.inputs['ori_shape'].CopyFrom(

tf.contrib.util.make_tensor_proto(np.array(ori_shape).astype(

np.float32),

shape=[3]))

request.inputs['shape_bbox'].CopyFrom(

tf.contrib.util.make_tensor_proto(np.array(shape_bbox).astype(

np.float32),

shape=[3]))

# predict

response = stub.Predict(request, 30.0) # 5 seconds

pred_x_ratio = np.asarray(

response.outputs["pred_x_ratio"].float_val).tolist()

pred_y_ratio = np.asarray(

response.outputs["pred_y_ratio"].float_val).tolist()

preds_class = np.asarray(

response.outputs["preds_class"].int64_val).tolist()

fpred_x_bbox = np.asarray(

response.outputs["fpred_x_bbox"].float_val).tolist()

fpred_y_bbox = np.asarray(

response.outputs["fpred_y_bbox"].float_val).tolist()

pred_max = np.asarray(response.outputs["pred_max"].float_val).tolist()

pred_max = np.asarray(response.outputs["pred_max"].float_val).tolist()

return pred_x_ratio, pred_y_ratio, preds_class, fpred_x_bbox, fpred_y_bbox, pred_max

https://www.jianshu.com/p/77cb3c5bb167

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值