【问题标题】:How do I call out to a specific model version with Python and a TensorFlow serving model?如何使用 Python 和 TensorFlow 服务模型调用特定模型版本?
【发布时间】:2021-03-10 21:54:47
【问题描述】:

我有一些机器学习模型通过 Kubernetes 上的 TensorFlow Serving 运行。我希望能够对特定模型进行一次部署,然后加载多个版本。

这似乎比为我们拥有的每个模型的每个版本维护单独的 Kubernetes 部署要容易。

但是如何将我想使用 Python gRPC 接口调用的版本或模型风格传递给 TF Serving 并不明显。如何指定版本并传入?

【问题讨论】:

    标签: python tensorflow tensorflow-serving


    【解决方案1】:

    无论出于何种原因,在您构建拉取请求时,都无法就地更新模型规范。相反,您需要单独构建一个包含所需版本的ModelSpec 实例,然后将其传递给预测请求的构造函数。

    还值得指出的是,您需要使用 Google 特定的 Int64Value 来获取该版本。

    from google.protobuf.wrappers_pb2 import Int64Value
    from tensorflow_serving.apis.model_pb2 import ModelSpec
    from tensorflow_serving.apis import predict_pb2, get_model_metadata_pb2, \
                                        prediction_service_pb2_grpc
    from tensorflow import make_tensor_proto
    import grpc
    
    model_name = 'mymodel'
    input_name = 'model_input'
    model_uri = 'mymodel.svc.cluster.local:8500'
    
    X = # something that works
    
    channel = grpc.insecure_channel(model_uri, options=MESSAGE_OPTIONS)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    
    version = Int64Value(value=1)
    model_spec = ModelSpec(version=version, name=model_name, signature_name='serving_default')
    
    request = predict_pb2.PredictRequest(model_spec=model_spec)
    request.inputs[input_name].CopyFrom(make_tensor_proto(X.astype(np.float32), shape=X.shape))
    result = stub.Predict(request, 1.0)
    channel.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 2018-01-26
      • 1970-01-01
      • 2021-09-10
      • 2019-04-01
      • 1970-01-01
      • 2018-09-12
      相关资源
      最近更新 更多