用例:在tf服务中服务多个模型服务:对于每个托管的模型,我需要在client.py文件中进行哪些更改?

时间:2018-12-07 02:09:49

标签: tensorflow machine-learning deep-learning artificial-intelligence tensorflow-serving

我是这个领域的新手。 我的用例是:在tf服务中服务多个模型。我正在使用docker

我可以理解我需要在配置文件中进行的更改(如下所示):

model_config_list: {

  config: {
    name: "model1",
    base_path: "/tmp/model",
    model_platform: "tensorflow"
  },
  config: {
     name: "model2",
     base_path: "/tmp/model2",
     model_platform: "tensorflow"
  }
}

但是,我面临两个挑战:

挑战1:

client.py文件需要进行哪些更改才能与多个模型交互? 我提到了mnist_client.py:

def do_inference(hostport, work_dir, concurrency, num_tests):

  test_data_set = mnist_input_data.read_data_sets(work_dir).test
  channel = grpc.insecure_channel(hostport)
  stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
  result_counter = _ResultCounter(num_tests, concurrency)
  for _ in range(num_tests):
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'mnist'
    request.model_spec.signature_name = 'predict_images'
    image, label = test_data_set.next_batch(1)
    request.inputs['images'].CopyFrom(
        tf.contrib.util.make_tensor_proto(image[0], shape=[1, image[0].size]))
    result_counter.throttle()
    result_future = stub.Predict.future(request, 5.0)  # 5 seconds
    result_future.add_done_callback(
        _create_rpc_callback(label[0], result_counter))
  return result_counter.get_error_rate()

以上代码仅适用于单个模型。

我需要在代码中添加哪些特定字段/变量,以唯一地标识我的多个模型中的每个模型,以及在将请求发送到由docker容器中服务的tensorflow托管的模型时可以使用的唯一值?

挑战2:

对于单个模型,我使用了这种客户请求格式:

python mnist_client.py --server=172.17.0.2:9000 --image=<image folder>

当我托管多个模型并且每次请求不同模型时,确切的格式应该是什么。那么客户请求的正确格式应该是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

将“ request.model_spec.name”设置为您在配置文件中提到的名称,例如“ model1”。

将“ request.model_spec.signature_name”更改为模型的签名。

在“ request.inputs ['images']”行中,将“ images”更改为模型中的输入张量名称,并将make_tensor_proto中的args更改为您自己的input。如果您有多个输入,请重复添加该行。