繁体   English   中英

Tensorflow 服务响应

[英]Tensorflow Serving Response

我正在尝试与 Tensorflow Serving 会话建立通信。 以下代码有效,但速度有点慢。 有没有办法改善它? 我怀疑问题出在第四行 - 输出是一个 float_val 元素列表,我需要将它们转换为浮点数组并重塑它们。

有没有办法让服务器输出处于正确的形状? 我已经定义了输出签名如下(我认为是正确的)。

prediction_channel, request_form = setup_channel(args.server)    
request_form.inputs['images'].CopyFrom(
                tf.contrib.util.make_tensor_proto(img_transformed, shape=list(img_transformed.shape)))
output = prediction_channel.Predict.future(request_form, 5.0)
output = np.array(output.result().outputs['scores'].float_val).reshape(1, 16, 64, 64)

第一行使用函数打开到服务器的通道

def setup_channel(hostport):
   host, port = hostport.split(':')
   channel = implementations.insecure_channel(host, int(port))
    stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
   request = predict_pb2.PredictRequest()
   request.model_spec.name = 'hg'
   request.model_spec.signature_name = 'predict_images'
   return stub, request

输出签名是:

tensor_info_x = tf.saved_model.utils.build_tensor_info(model.input_tensor)
tensor_info_y = tf.saved_model.utils.build_tensor_info(model.predict)

prediction_signature = (
    tf.saved_model.signature_def_utils.build_signature_def(
        inputs={'images': tensor_info_x},
        outputs={'scores': tensor_info_y},
        method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))**

并且模型预测的形状为 (1, 16, 64, 64)。

我不确定你是如何处理你的Predict.future(request_form, 5.0) ,但同样适用于同步响应处理; tf 提供了一个实用函数make_ndarray

res = stub.Predict(request, timeout).outputs[tensor_name]
arr = tf.make_ndarray(res)

arr将是一个包含正确暗淡的 np 数组。

其中tensor_name是您签名中定义的名称,例如

tf.saved_model.signature_def_utils.build_signature_def(
    inputs={'images': inp_tensor_info},
    outputs={'scores': out_tensor_info},
    method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)

需要

res = stub.Predict(request, timeout).outputs['scores']
arr = tf.make_ndarray(res)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM