我从 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/