python - 如何将 tf.example 发送到 TensorFlow Serving gRPC 预测请求中

标签 python tensorflow tensorflow-serving

我有 tf.example 形式的数据,我正在尝试以预测形式(使用 gRPC)向保存的模型发出请求。我无法确定实现此目的的方法调用。

我从众所周知的汽车定价 DNN 回归模型 (https://github.com/tensorflow/models/blob/master/samples/cookbook/regression/dnn_regression.py) 开始,我已经通过 TF Serving docker 容器导出并安装了它

import grpc
import numpy as np
import tensorflow as tf
from tensorflow_serving.apis import predict_pb2, prediction_service_pb2_grpc

stub = prediction_service_pb2_grpc.PredictionServiceStub(grpc.insecure_channel("localhost:8500"))

tf_ex = tf.train.Example(
    features=tf.train.Features(
        feature={
            'curb-weight': tf.train.Feature(float_list=tf.train.FloatList(value=[5.1])),
            'highway-mpg': tf.train.Feature(float_list=tf.train.FloatList(value=[3.3])),
            'body-style': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"wagon"])),
            'make': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"Honda"])),
        }
    )
)

request = predict_pb2.PredictRequest()
request.model_spec.name = "regressor_test"

# Tried this:
request.inputs['inputs'].CopyFrom(tf_ex)

# Also tried this:
request.inputs['inputs'].CopyFrom(tf.contrib.util.make_tensor_proto(tf_ex))

# This doesn't work either:
request.input.example_list.examples.extend(tf_ex)

# If it did work, I would like to inference on it like this:
result = self.stub.Predict(request, 10.0)

谢谢你的建议

最佳答案

我假设您的 savedModel 有一个 serving_input_receiver_fn,将 string 作为输入并解析为 tf.ExampleUsing SavedModel with Estimators

def serving_example_input_receiver_fn():
    serialized_tf_example = tf.placeholder(dtype=tf.string)
    receiver_tensors = {'inputs': serialized_tf_example}   
    features = tf.parse_example(serialized_tf_example, YOUR_EXAMPLE_SCHEMA)
    return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)

因此,serving_input_receiver_fn 接受一个字符串,因此您必须SerializeToString 您的tf.Example()。此外,serving_input_receiver_fninput_fn 一样进行训练,以批处理方式将数据转储到模型中。

代码可能会变成:

request = predict_pb2.PredictRequest()
request.model_spec.name = "regressor_test"
request.model_spec.signature_name = 'your method signature, check use saved_model_cli'
request.inputs['inputs'].CopyFrom(tf.make_tensor_proto([tf_ex.SerializeToString()], dtype=types_pb2.DT_STRING))

关于python - 如何将 tf.example 发送到 TensorFlow Serving gRPC 预测请求中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53888284/

相关文章:

amazon-web-services - 在 aws sagemaker 上部署预训练的 tensorflow 模型 - ModelError : An error occurred (ModelError) when calling the InvokeEndpoint operation

tensorflow - 将图形原型(prototype) (pb/pbtxt) 转换为 SavedModel 以用于 TensorFlow Serving 或 Cloud ML Engine

python - 反向字符串和正向字符串的区别

machine-learning - 当 TensorFlow 中的学习率衰减时,什么时候应该将阶梯设置为 True?

Python 从导入的模块中模拟一个函数

python-2.7 - Caffe 到 Tensorflow(Ethereon 的 Kaffe): TypeError: Descriptors should not be created directly,,但仅从其父级检索

tensorflow - SSD Inception v2。 VGG16 特征提取器是否被 Inception v2 取代?

python - tf.image.decode_jpeg - 内容必须是标量,具有形状 [1]

python - 如何在 Pygame 中编写步行周期

python Pandas : rename single column label in multi-index dataframe