【问题标题】:Unable to take Input values from Tensorflow Serving client request无法从 Tensorflow Serving 客户端请求中获取输入值
【发布时间】:2019-05-29 19:18:18
【问题描述】:

我正在尝试使用 tensorflow 保存模型将我的 tensorflow 模型部署在 tensorflow 服务中。我的 tf 模型的输入是一个字符串值,我已经定义了我的签名如下

        prediction_signature = (
            tf.saved_model.signature_def_utils.build_signature_def(
                inputs={'input_path': tensor_info_input},
                outputs={'output_prediction': tensor_info_output},
                method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

        builder.add_meta_graph_and_variables(
            sess, [tf.saved_model.tag_constants.SERVING],
            signature_def_map={
                'predict_images':
                    prediction_signature,
            })

我的目标是从签名定义中指定的路径读取图像。如何从输入定义中获取 input_path 并将张量转换为实际字符串值以读取路径

【问题讨论】:

    标签: arguments tensorflow-serving


    【解决方案1】:

    我认为您需要在模型文件中添加一些“读取操作”。

    像这样:

    def inference(image_path_string):
        # read image from a image path string
        image_file = tf.read_file(image_path_string)
        image_raw = tf.cond(tf.image.is_jpeg(image_file),
                            lambda: tf.image.decode_jpeg(image_file, channels= 3 if rgb, else 1),
                            lambda: tf.image.decode_bmp(image_file))
        # image preprocessing code
        ...
        ...
        # your prediction model code
        ...
        ...
    

    【讨论】:

    • 我同意了,但我的问题是如何从签名中定义的 'input_path' 获取值并将值传递给您提到的函数 def inference(image_path_string): .
    【解决方案2】:

    您可以将下面提到的代码放在 Python 文件中,例如 Client.py。对于推理,您可以在终端中运行下面提到的命令

    python Client.py --image abc/0.png --model mnist --signature_name predict
    

    这将获取图像的路径“abc/0.png”并将其转换为数值,并运行推理。

    Client.py 的代码如下所述。以下代码适用于 MNIST 图像。您可以相应地重塑图像:

    from __future__ import print_function
    
    import argparse
    import time
    import numpy as np
    from scipy.misc import imread
    
    import grpc
    from tensorflow.contrib.util import make_tensor_proto
    
    from tensorflow_serving.apis import predict_pb2
    from tensorflow_serving.apis import prediction_service_pb2_grpc
    
    
    def run(host, port, image, model, signature_name):
    
        channel = grpc.insecure_channel('{host}:{port}'.format(host=host, port=port))
        stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    
        # Read an image
        data = imread(image)
        data = data.astype(np.float32)
        print(data)
    
        start = time.time()
    
        # Call classification model to make prediction on the image
        request = predict_pb2.PredictRequest()
        request.model_spec.name = model
        request.model_spec.signature_name = signature_name
        request.inputs['image'].CopyFrom(make_tensor_proto(data, shape=[1, 28, 28, 1]))
    
        result = stub.Predict(request, 10.0)
    
        end = time.time()
        time_diff = end - start
    
        # Reference:
        # How to access nested values
        # https://stackoverflow.com/questions/44785847/how-to-retrieve-float-val-from-a-predictresponse-object
        print(result)
        print('time elapased: {}'.format(time_diff))
    
    
    if __name__ == '__main__':
        parser = argparse.ArgumentParser()
        parser.add_argument('--host', help='Tensorflow server host name', default='localhost', type=str)
        parser.add_argument('--port', help='Tensorflow server port number', default=8500, type=int)
        parser.add_argument('--image', help='input image', type=str)
        parser.add_argument('--model', help='model name', type=str)
        parser.add_argument('--signature_name', help='Signature name of saved TF model',
                            default='serving_default', type=str)
    
        args = parser.parse_args()
        run(args.host, args.port, args.image, args.model, args.signature_name)
    

    有关更多信息,您可以参考这篇关于 Tensorflow Serving 的精美文章, https://medium.com/@yuu.ishikawa/serving-pre-modeled-and-custom-tensorflow-estimator-with-tensorflow-serving-12833b4be421

    【讨论】:

      猜你喜欢
      • 2019-07-28
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 2017-05-22
      • 2018-09-16
      • 2021-11-16
      • 1970-01-01
      相关资源
      最近更新 更多