Tensorflow预测grpc无法正常运行,但RESTful API正常运行

时间:2018-12-19 19:29:15

标签: python-3.x machine-learning grpc tensorflow-serving

当我尝试执行下面的一段客户端代码时,我遇到了错误,但通过RESTful API端点调用时成功了
curl -d '{"signature_name":"predict_output","instances":[2.0,9.27]}' -X POST http://10.110.110.13:8501/v1/models/firstmodel:predict

请在下面的代码中纠正我

import tensorflow as tf
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc
import numpy as np
import grpc
server = '10.110.110.13:8501'
channel = grpc.insecure_channel(server)
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = 'firstmodel'
request.model_spec.signature_name = 'predict_output'
request.inputs['input_x'].CopyFrom(tf.contrib.util.make_tensor_proto([12.0], shape=[1]))
result_future = stub.Predict(request,40.)
print(result_future.outputs['output_y'])

收到以下错误消息:

_Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Trying to connect an http1.x server"
debug_error_string = "{"created":"@1545248014.367000000","description":"Error received from peer",
    "file":"src/core/lib/surface/call.cc","file_line":1083,"grpc_message":"Trying to connect an http1.x server","grpc_status":14}"

下面是组成的请求信息供您参考

model_spec {
  name: "firstmodel"
  signature_name: "predict_output"
}
inputs {
  key: "input_x"
  value {
    dtype: DT_FLOAT
    tensor_shape {
      dim {
        size: 1
      }
    }
    float_val: 12.0
  }
}

1 个答案:

答案 0 :(得分:2)

GRPC端口和HTTP端口不同。由于您正在监听8501上的http服务,因此GRPC服务必须使用另一个端口。默认值为8500,但是您可以在启动tf服务器时使用--port=参数对其进行更改。

docker run -p 8500:8500 --mounttype=bind,source=/root/serving/Ser_Model,target=/models/firstmodel -e MODEL_NAME=firstmodel -t tensorflow/serving