python - 如何在 Tensorflow Serving 中进行批处理?

标签 python tensorflow tensorflow-serving

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

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

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

感谢对这个问题的一些见解。与此相关的任何代码片段都将非常有帮助。

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

更新了 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()

最佳答案

您应该能够通过对 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()

关于python - 如何在 Tensorflow Serving 中进行批处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42519010/

相关文章:

python - 不同类型的 TensorFlow SignatureDef 的目的是什么?

Python 正则表达式匹配所有 5 位数字但没有更大的数字

python - 如何将 PyQt 脚本 (.py) 编译为适用于 windows (.exe) 和/或 linux 的单个独立可执行文件?

android - 如何为 Keras 模型获取 Android 像素 RGB 数组

tensorflow - 如何使用 tensorflow 服务部署 parsey 的表亲

TensorFlow Serving 将图像作为 Cloud ML Engine 上的 base64 编码字符串

python - 如何检查键盘按钮是否收到垃圾邮件并在再次按下之前添加冷却时间

python - 在没有互联网连接的情况下在 Ubuntu 中安装 OpenCV

tensorflow - 解析在 tensorflow 摘要对象上评估的 `summary_str` 字节字符串

tensorflow - 在 Tensorflow 中将 3 阶张量与 2 阶张量相乘