代码之家  ›  专栏  ›  技术社区  ›  Karl

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

  •  2
  • Karl  · 技术社区  · 7 年前

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

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

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

    1 回复  |  直到 7 年前
        1
  •  16
  •   Avi Avidan    6 年前

    因此,我花了好几年的时间在pull请求中摸索,终于找到了一个代码示例。对于下一个和我有同样问题的人,这里有一个如何做到这一点的例子。(你需要 tensorflow_serving package 为此,; pip 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")
    
        2
  •  3
  •   Avi Avidan    6 年前

    添加模型 conf_filepath :使用参数 name base_path , model_platform 对于新模型。保持原始模型完好无损。

    注意到@Karl的答案有一点不同-使用 MergeFrom CopyFrom

    pip安装tensorflow服务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)
    
        3
  •  0
  •   H S Rathore    5 年前

    我并没有在运行时使用配置文件来提供更多的模型,而是使用了servings的RESTAPI。我是这样做的。

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

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

        4
  •  0
  •   utkarshgupta137    4 年前

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