作者:Huan-TH | 来源:互联网 | 2023-02-10 14:01
1> mrry..:
您应该能够通过对请求构造代码进行少量更改来计算一批图像的预测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()