TensorFlow Serving:在运行时更新 model_config(添加其他模型)
TensorFlow Serving: Update model_config (add additional models) at runtime
我正忙于配置一个 TensorFlow Serving 客户端,它要求 TensorFlow Serving 服务器针对给定模型对给定输入图像进行预测。
如果所请求的模型尚未提供,则会从远程 URL 下载到服务器模型所在的文件夹中。 (客户这样做)。此时我需要更新 model_config
并触发服务器重新加载它。
此功能似乎存在(基于 https://github.com/tensorflow/serving/pull/885 and https://github.com/tensorflow/serving/blob/master/tensorflow_serving/apis/model_service.proto#L22),但我找不到任何关于如何实际使用它的文档。
我基本上是在寻找一个 python 脚本,我可以用它从客户端触发重新加载(或者以其他方式配置服务器以侦听更改并触发重新加载本身)。
因此,我花了很长时间通过拉取请求进行拖网,最终找到了一个代码示例。对于下一个和我有同样问题的人,这里有一个如何做到这一点的例子。 (为此你需要 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")
将模型 添加到 TF 服务服务器和现有配置文件 conf_filepath
:使用参数 name
、base_path
、model_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)
如果您使用 this answer 中描述的方法,请注意您实际上是在启动多个 tensorflow 模型服务器实例而不是单个模型服务器,有效地使服务器竞争资源而不是工作共同优化尾延迟。
我正忙于配置一个 TensorFlow Serving 客户端,它要求 TensorFlow Serving 服务器针对给定模型对给定输入图像进行预测。
如果所请求的模型尚未提供,则会从远程 URL 下载到服务器模型所在的文件夹中。 (客户这样做)。此时我需要更新 model_config
并触发服务器重新加载它。
此功能似乎存在(基于 https://github.com/tensorflow/serving/pull/885 and https://github.com/tensorflow/serving/blob/master/tensorflow_serving/apis/model_service.proto#L22),但我找不到任何关于如何实际使用它的文档。
我基本上是在寻找一个 python 脚本,我可以用它从客户端触发重新加载(或者以其他方式配置服务器以侦听更改并触发重新加载本身)。
因此,我花了很长时间通过拉取请求进行拖网,最终找到了一个代码示例。对于下一个和我有同样问题的人,这里有一个如何做到这一点的例子。 (为此你需要 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")
将模型 添加到 TF 服务服务器和现有配置文件 conf_filepath
:使用参数 name
、base_path
、model_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)
如果您使用 this answer 中描述的方法,请注意您实际上是在启动多个 tensorflow 模型服务器实例而不是单个模型服务器,有效地使服务器竞争资源而不是工作共同优化尾延迟。