【问题标题】:TensorFlow Serving: Update model_config (add additional models) at runtimeTensorFlow Serving:在运行时更新 model_config(添加其他模型)
【发布时间】:2019-06-23 17:51:34
【问题描述】:

我正忙于配置 TensorFlow Serving 客户端,该客户端要求 TensorFlow Serving 服务器针对给定模型对给定输入图像生成预测。

如果请求的模型尚未提供服务,则会从远程 URL 将其下载到服务器模型所在的文件夹。 (客户这样做)。此时我需要更新model_config并触发服务器重新加载。

此功能似乎存在(基于https://github.com/tensorflow/serving/pull/885https://github.com/tensorflow/serving/blob/master/tensorflow_serving/apis/model_service.proto#L22),但我找不到任何有关如何实际使用它的文档。

我本质上是在寻找一个 python 脚本,我可以使用它来触发从客户端重新加载(或者配置服务器以侦听更改并触发重新加载本身)。

【问题讨论】:

  • 更好的选择是根本不使用配置文件,而是使用我们在命令行中提到模型路径和模型名称的其他模式。

标签: python tensorflow-serving


【解决方案1】:

如果您使用this answer中描述的方法,请注意,您实际上正在启动多个Tensorflow模型服务器实例而不是单个模型服务器,有效地使服务器竞争资源而不是一起工作以优化尾部延迟。

【讨论】:

【解决方案2】:

添加模型到 TF Serving 服务器和现有配置文件 conf_filepath:为新模型使用参数 namebase_pathmodel_platform。保持原始模型完好无损。

注意与@Karl 的回答有一点不同——使用MergeFrom 而不是CopyFrom

pip install tensorflow-serving-api

import grpc
from google.protobuf import text_format
from tensorflow_serving.apis import model_service_pb2_grpc, model_management_pb2
from tensorflow_serving.config import model_server_config_pb2


def add_model_config(conf_filepath, host, name, base_path, model_platform):
    with open(conf_filepath, 'r+') as f:
        config_ini = f.read()
    channel = grpc.insecure_channel(host)
    stub = model_service_pb2_grpc.ModelServiceStub(channel)
    request = model_management_pb2.ReloadConfigRequest()
    model_server_config = model_server_config_pb2.ModelServerConfig()
    config_list = model_server_config_pb2.ModelConfigList()
    model_server_config = text_format.Parse(text=config_ini, message=model_server_config)

    # Create a config to add to the list of served models
    one_config = config_list.config.add()
    one_config.name = name
    one_config.base_path = base_path
    one_config.model_platform = model_platform

    model_server_config.model_config_list.MergeFrom(config_list)
    request.config.CopyFrom(model_server_config)

    response = stub.HandleReloadConfigRequest(request, 10)
    if response.status.error_code == 0:
        with open(conf_filepath, 'w+') as f:
            f.write(request.config.__str__())
        print("Updated TF Serving conf file")
    else:
        print("Failed to update model_config_list!")
        print(response.status.error_code)
        print(response.status.error_message)

【讨论】:

  • 一个小但非常有用的区别!
  • 我遵循了两个答案,服务的 docker 已经打开了 8501 和 8500 端口。我仍然发现了一个错误。 raise _Rendezvous(state, None, None, deadline) grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with: `status = StatusCode.DEADLINE_EXCEEDED` ` details = "Deadline Exceeded"` debug_error_string = {"created":"@1568010109.337260837","description":"Error received from peer ipv6:[::1]:8500","file":"src/core/lib/surface/call.cc","file_line":1052,"grpc_message":"Deadline Exceeded","grpc_status":4}"
  • 您能否详细说明使用MergeFromCopyFrom 之间的区别?
  • CopyFrom 用您的新模型覆盖当前可用模型列表。 MergeFrom 将模型添加到当前可用的模型中。
  • 您好,感谢您回答这个问题,请问您如何将“model_version_policy”字段添加到新添加的配置中?
【解决方案3】:

所以我花了很长时间浏览拉取请求,最终找到了一个代码示例。对于下一个和我有同样问题的人,这里有一个如何做到这一点的例子。 (为此,您需要 tensorflow_serving packagepip install tensorflow-serving-api)。

基于此拉取请求(在撰写本文时尚未接受并已关闭,因为它需要审查):https://github.com/tensorflow/serving/pull/1065

from tensorflow_serving.apis import model_service_pb2_grpc
from tensorflow_serving.apis import model_management_pb2
from tensorflow_serving.config import model_server_config_pb2

import grpc

def add_model_config(host, name, base_path, model_platform):
  channel = grpc.insecure_channel(host) 
  stub = model_service_pb2_grpc.ModelServiceStub(channel)
  request = model_management_pb2.ReloadConfigRequest() 
  model_server_config = model_server_config_pb2.ModelServerConfig()

  #Create a config to add to the list of served models
  config_list = model_server_config_pb2.ModelConfigList()       
  one_config = config_list.config.add()
  one_config.name= name
  one_config.base_path=base_path
  one_config.model_platform=model_platform

  model_server_config.model_config_list.CopyFrom(config_list)

  request.config.CopyFrom(model_server_config)

  print(request.IsInitialized())
  print(request.ListFields())

  response = stub.HandleReloadConfigRequest(request,10)
  if response.status.error_code == 0:
      print("Reload sucessfully")
  else:
      print("Reload failed!")
      print(response.status.error_code)
      print(response.status.error_message)


add_model_config(host="localhost:8500", 
                    name="my_model", 
                    base_path="/models/my_model", 
                    model_platform="tensorflow")

【讨论】:

  • 很好的答案,谢谢!我想知道是否可以使用 REST API 而不是 gRPC 调用对 model_config 进行相同的更新。原因是我无法使用位于 Nginx 和 API Gateway (Kong) 后面的主机定义通道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-03
  • 1970-01-01
  • 2019-11-27
  • 2018-02-01
  • 2017-05-31
  • 2018-05-16
  • 2018-02-26
相关资源
最近更新 更多