TensorFlow服务:在运行时更新model_config(添加其他模型)

时间:2019-01-30 12:33:29

标签: python tensorflow-serving

我正忙着配置一个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脚本(或配置服务器以侦听更改并触发重新加载本身)。

3 个答案:

答案 0 :(得分:3)

因此,我花了很长时间来遍历pull请求,终于找到了一个代码示例。对于与我有相同问题的下一个人,这里有一个示例。 (为此,您需要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)


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

答案 1 :(得分:1)

向TF Serving服务器和现有配置文件conf_filepath添加模型:将参数namebase_pathmodel_platform用于新模式。保持原始模型不变。

请注意,与@Karl的答案略有不同-使用MergeFrom代替CopyFrom

  

pip install tensorflow-serving-api

from 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)

答案 2 :(得分:0)

我没有使用配置文件在运行时为更多模型提供服务,我使用了服务的 rest api。我是这样做的。

每当我需要为新模型提供服务时,我都会以编程方式调用此命令,唯一的问题是如果端口已在使用中,我们需要获取未使用的新端口号,因此我创建了一个程序以获取未使用的端口号运行时间。

tensorflow_model_server --rest_api_port=dynamic_port --model_base_path=<path to model> --model_name=<model_name>

我正在使用 kafka、芹菜和 tensorflow 服务。 我知道这个问题是关于更新配置文件的,但我在执行上述解决方案之前来到这里寻找答案。因此,如果其他人来到这里希望为多个模型提供服务并在运行时添加更多模型,请使用上述模式。