1

我正在通过 gRPC 与 tensorflow 模型服务器通信,以便对我的数据进行预测。用于建立连接的 python3 应用程序基于教程并成功完成其工作。

但是,教程代码使用了 python gRPC 的 beta API,我试图尽可能保持最新。与模型服务器建立安全连接并执行预测的 Python gRPC GA API 等效代码是什么?

这是使用虚拟数据工作的代码

# Requirements:
# tensorflow-serving-api==1.9.0
# tensorflow==1.11.0
import os
port = os.getenv("PORT")
host = os.getenv("HOST")
cert = os.getenv("CERT") # SSL certificate
token = os.getenv("TOKEN") # Authorization token

from grpc.beta import implementations as grpc_beta_impl
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2

creds = grpc_beta_impl.ssl_channel_credentials(root_certificates=cert.encode())
channel = grpc_beta_impl.secure_channel(host, int(port), creds)
stub = prediction_service_pb2.beta_create_PredictionService_stub(
    channel,
    metadata_transformer=lambda metadata: tuple(metadata) + (("authorization", "Bearer " + token),))

data = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
request = predict_pb2.PredictRequest()
request.model_spec.name = "housing"
request.model_spec.signature_name = 'serving_default'
tfutil = tf.contrib.util
t_proto = tfutil.make_tensor_proto(data,
                                   shape=[1, len(data)],
                                   dtype="float")
request.inputs["input_data"].CopyFrom(t_proto)
pred = stub.Predict(request, 1500)
price = pred.outputs['dense_5/BiasAdd:0'].float_val[0]
print(str(price))

我试过用他们的 GA API 等效替换 beta API 调用。但是,metadata_transformer存根中不再有 a 这样的东西,我既不了解它的用途,也不知道在哪里填充它。

这是相关的一段代码,后面是错误消息,现在需要 tf-serving 版本 1.11。

# Requirements tensorflow-serving==1.11.0

# ... load envvars. Same as before

import grpc
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc

creds = grpc.ssl_channel_credentials(root_certificates=cert.encode())
channel = grpc.secure_channel(host + ":" + port, creds)
stub = prediction_service_pb2_grpc.PredictionServiceStub(
    channel)

# ... Same as above
Error: <_Rendezvous of RPC that terminated with:
        status = StatusCode.UNAUTHENTICATED
        details = "no authorization metadata found"
        debug_error_string = "{"created":"@1567680306.923100899","description":"Error received from peer ipv4:xx.xxx.xxx.xx:443","file":"src/core/lib/surface/call.cc","file_line":1052,"grpc_message":"no authorization metadata found","grpc_status":16}"
>

我对模型服务器没有任何控制权,我不知道我正在尝试的是否可能(即是否有 python gRPC Beta API 的服务器端挂件,它必须实现 GA API 以便我成功了吗?)

任何帮助是极大的赞赏。

4

0 回答 0