如何使用结构化数据创建分类请求以发送到TensorFlow使用gRPC服务

时间:2018-11-30 17:52:28

标签: python tensorflow grpc tensorflow-serving

我从this guide训练并导出了虹膜分类器。我通过将以下内容添加到premade_estimator.py来导出它:

feature_spec = tf.feature_column.make_parse_example_spec(my_feature_columns)
serving_input_receiver_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)
classifier.export_saved_model("iris_export_base", serving_input_receiver_fn)

我可以像这样使用REST API进行推论:

import requests
response = requests.post('http://localhost:8501/v1/models/foo:classify', 
                         json={"examples": [{"SepalLength": 2.3, 
                                             "SepalWidth": 3.4, 
                                             "PetalLength": 2.2, 
                                             "PetalWidth": 0.81}]})

我还能够使用gRPC成功推断出其他模型,例如该对象检测模型将图像作为数组输入:

channel = grpc.insecure_channel(SERVER_ADDR)
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = MODEL_SPEC_NAME
request.inputs['inputs'].CopyFrom(tf.contrib.util.make_tensor_proto(image_ary))
result = stub.Predict(request, 10.0)

但是我不知道应该如何为分类请求指定输入。我最好的猜测是沿着这些思路:

channel = grpc.insecure_channel(SERVER_ADDR)
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
request = classification_pb2.ClassificationRequest()
request.model_spec.name = MODEL_SPEC_NAME
request.input #...?

但是我找不到有关如何设置输入的任何信息,到目前为止,我尝试过的所有操作都引发了某种TypeError。

1 个答案:

答案 0 :(得分:1)

您可以在此处找到指定输入的示例:https://github.com/tensorflow/serving/blob/master/tensorflow_serving/model_servers/tensorflow_model_server_test.py#L354

example = request.input.example_list.examples.add() example.features.feature ['x']。float_list.value.extend([2.0])