python - 如何使用结构化数据创建 ClassificationRequest 以使用 gRPC 发送到 TensorFlow Serving

标签 python tensorflow grpc tensorflow-serving

我从 this guide 训练并导出了 Iris 分类器.我通过将以下内容添加到 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)

但我不知道应该如何指定 ClassificationRequest 的输入。我最好的猜测是沿着这些方向:

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 #...?

但是我找不到关于如何设置输入的任何信息,而且我到目前为止尝试的所有操作都会抛出某种类型错误。

最佳答案

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

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

关于python - 如何使用结构化数据创建 ClassificationRequest 以使用 gRPC 发送到 TensorFlow Serving,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53562518/

相关文章:

javascript - 如何从 stub 函数获取返回值?

python - 在 Python 中解析一个 cron 条目

python - 将行中的值分组在一起并为其分配一个整数值

python - 在 Python 中下载网页及其所有资源文件

tensorflow - Keras 前 5 名预测

python - TensorFlow 2.0 凯拉斯 : How to write image summaries for TensorBoard

python - 初始化 DVC 存储库会引发错误

multidimensional-array - Tensorflow nn.conv3d() 和 max_pool3d

c# - grpc:服务器重启后大约 15 秒无法恢复连接

c# - 如何从运行 .NET 4.7.2 的 WPF 访问 gRPC 服务?