How to do batching in Tensorflow Serving?

大城市里の小女人 提交于 2019-11-30 04:06:01

You should be able to compute predictions for a batch of images with a small change to the request construction code in inception_client.py. The following lines in that file create a request with a "batch" containing a single image (note shape=[1], which means "a vector of length 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)

You can pass more images in the same vector to run predictions on a batch of data. For example, if FLAGS.image were a comma-separated list of filenames:

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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!