2017-02-28 89 views
1

部署的Tensorflow为Inception-V3服务并运行测试。工作正常。如何在Tensorflow服务中进行批处理?

现在,想为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() 
+0

您能检查粘贴代码的缩进吗? (这可能是堆栈溢出格式化的问题,但它可能隐藏了一个错误。)当前出现的错误是什么? – mrry

+0

看起来像堆栈溢出格式问题。将尝试解决这个问题。这里是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 回溯(最近一次调用最后一次): 文件“/home/gpuadmin/serving/bazel-bin/tensorflow_serving/example/inception_batch_client.runfiles/tf_serving/tensorflow_serving/example/inception_batch_client.py”,第63行,在 中打开图像,'rb')为f: IOError:[Errno 2]没有这样的文件或目录:'' –

+0

由于您正尝试读取未找到的文件而引发错误。它似乎试图打开''''(空字符串),所以也许'FLAGS.image'没有正确的格式?也许尝试打印'FLAGS.image.split(',')'来找出发生了什么问题? – mrry

回答

3

您应该能够计算对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() 
+0

谢谢@mmry。进行更改后,再次为客户端创建构建。在查询推理时收到request.inputs ['images']的错误CopyFrom( NameError:name'request'is not defined。 –

+0

传递2个图像用于推断使用bazel-bin/tensorflow_serving/example/inception_batch_client --server = localhost :9000 --image =/home/gpuadmin/serving/images/boat.jpg,/ home/gpuadmin/serving/images/table.jpg 回溯(最近一次通话最后): 文件“/ home/useradmin/serving /巴泽勒滨/ tensorflow_serving /示例/ inception_batch_client.runfiles/tf_serving/tensorflow_serving /示例/ inception_batch_client。py“,第55行,在 request.inputs ['images']。CopyFrom( NameError:name'request'is not defined –

+0

也许是代码中的拼写错误?request'的名字应该由行' request = predict_pb2.PredictRequest()'。 – mrry

相关问题