【问题标题】:How to do batching in Tensorflow Serving?如何在 Tensorflow Serving 中进行批处理?
【发布时间】:2017-07-20 00:42:39
【问题描述】:

部署了 Tensorflow Serving 并对 Inception-V3 进行了测试。工作正常。

现在,想为 Inception-V3 服务进行批处理。 例如。想发送 10 张图像而不是一张进行预测。

如何做到这一点?要更新哪些文件(inception_saved_model.py 或 inception_client.py)?这些更新是什么样的?以及图像如何传递给服务 - 它是作为包含图像的文件夹传递还是如何传递?

感谢您对此问题的一些见解。任何与此相关的代码 sn-p 都会非常有帮助。

==================================

更新了 inception_client.py

# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

#!/usr/bin/env python2.7

"""Send JPEG image to tensorflow_model_server loaded with inception model.
"""

from __future__ import print_function

"""Send JPEG image to tensorflow_model_server loaded with inception model.
"""

from __future__ import print_function

# This is a placeholder for a Google-internal import.

from grpc.beta import implementations
import tensorflow as tf
from tensorflow.python.platform import flags
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2


tf.app.flags.DEFINE_string('server', 'localhost:9000',
                            'PredictionService host:port')
tf.app.flags.DEFINE_string('image', '', 'path to image in JPEG format')
FLAGS = tf.app.flags.FLAGS


def main(_):
   host, port = FLAGS.server.split(':')
   channel = implementations.insecure_channel(host, int(port))
   stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
   # Send request
   #with open(FLAGS.image, 'rb') as f:
     # See prediction_service.proto for gRPC request/response details.
     #data = f.read()
     #request = predict_pb2.PredictRequest()
     #request.model_spec.name = 'inception'
     #request.model_spec.signature_name = 'predict_images'


 #    request.inputs['images'].CopyFrom(
 #        tf.contrib.util.make_tensor_proto(data, shape=[1]))
 #    result = stub.Predict(request, 10.0)  # 10 secs timeout
 #    print(result)


# Build a batch of images

    request = predict_pb2.PredictRequest()
 request.model_spec.name = 'inception'
 request.model_spec.signature_name = 'predict_images'
  
  image_data = []
  for image in FLAGS.image.split(','):
   with open(image, 'rb') as f:
     image_data.append(f.read())
  
  request.inputs['images'].CopyFrom(
      tf.contrib.util.make_tensor_proto(image_data, shape=[len(image_data)]))
  
  result = stub.Predict(request, 10.0)  # 10 secs timeout
  print(result)
 if __name__ == '__main__':
   tf.app.run()

【问题讨论】:

  • 你能检查你粘贴的代码的缩进吗? (这可能是 Stack Overflow 格式的问题,但它可能隐藏了一个错误。)您当前遇到的错误是什么?
  • 看起来像堆栈溢出格式问题。将尝试解决此问题。这里是error.bazel-bin/tensorflow_serving/example/inception_batch_client --server=localhost:9000 --image=/home/gpuadmin/serving/images/boat.jpg,/home/gpuadmin/serving/images/boat.jpg Traceback (最近一次通话最后一次):文件“/home/gpuadmin/serving/bazel-bin/tensorflow_serving/example/inception_batch_client.runfiles/tf_serving/tensorflow_serving/example/inception_batch_client.py”,第 63 行,在 中,带有 open(image , 'rb') as f: IOError: [Errno 2] No such file or directory: ''
  • 由于您正在尝试读取未找到的文件,因此引发了错误。似乎一直在尝试打开''(空字符串),所以FLAGS.image 的格式可能不正确?也许尝试打印FLAGS.image.split(',') 以找出问题所在?
  • 感谢@mrry 的帮助和指点。搞定了..
  • @mrry 有一个后续问题。如何使用 max_batch_size、batch_timeout_micros、num_batch_threads 等参数对批处理进行性能调优?尝试在 Query 客户端使用这些参数,它不起作用。

标签: python tensorflow tensorflow-serving


【解决方案1】:

只需对inception_client.py 中的请求构造代码稍作更改,您就可以计算一批图像的预测值。该文件中的以下行使用包含单个图像的“批处理”创建请求(注意shape=[1],表示“长度为 1 的向量”):

with open(FLAGS.image, 'rb') as f:
  # See prediction_service.proto for gRPC request/response details.
  data = f.read()
  request = predict_pb2.PredictRequest()
  request.model_spec.name = 'inception'
  request.model_spec.signature_name = 'predict_images'
  request.inputs['images'].CopyFrom(
      tf.contrib.util.make_tensor_proto(data, shape=[1]))
  result = stub.Predict(request, 10.0)  # 10 secs timeout
  print(result)

您可以在同一个向量中传递更多图像来对一批数据进行预测。例如,如果 FLAGS.image 是一个逗号分隔的文件名列表:

request = predict_pb2.PredictRequest()
request.model_spec.name = 'inception'
request.model_spec.signature_name = 'predict_images'

# Build a batch of images.
image_data = []
for image in FLAGS.image.split(','):
  with open(image, 'rb') as f:
    image_data.append(f.read())

request.inputs['images'].CopyFrom(
    tf.contrib.util.make_tensor_proto(image_data, shape=[len(image_data)]))

result = stub.Predict(request, 10.0)  # 10 secs timeout
print(result)

 if __name__ == '__main__':
   tf.app.run()

【讨论】:

  • 谢谢@mmry。进行更改后,再次为客户端创建构建。在查询推理时,request.inputs['images'].CopyFrom(NameError: name 'request' is not defined.
  • 使用 bazel-bin/tensorflow_serving/example/inception_batch_client 传递 2 个图像进行推理 --server=localhost:9000 --image=/home/gpuadmin/serving/images/boat.jpg,/home/ gpuadmin/serving/images/table.jpg Traceback(最近一次调用最后):文件“/home/useradmin/serving/bazel-bin/tensorflow_serving/example/inception_batch_client.runfiles/tf_serving/tensorflow_serving/example/inception_batch_client.py”,行55、在 request.inputs['images'].CopyFrom(NameError: name 'request' is not defined
  • 可能是代码中的拼写错误?名称request 应由request = predict_pb2.PredictRequest() 行定义。
  • 谢谢@mmry。在 with 子句中包含 request = ...。存根、通道和主机面临类似问题 - 未定义。尽管它们在def main中。这是范围界定问题吗?试图将它们放在 image_data = [] 下并收到以下错误
  • 我很难从描述中想象您的代码是什么样子。您可以将您当前的代码发布为问题的编辑,以及错误的完整堆栈跟踪吗?
猜你喜欢
  • 2020-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多