如何解析 gRPC 存根客户端从 tensorflow 服务服务器接收到的输出?
How to parse the output received by gRPC stub client from tensorflow serving server?
我已经导出了一个 DNNClassifier 模型,并使用 docker 在 tensorflow-serving 服务器上 运行 它。之后,我编写了一个 python 客户端来与该 tensorflow-serving 进行交互以进行新的预测。
我已经编写了以下代码来获取 tensorflow-serving 服务器的响应。
host, port = FLAGS.server.split(':')
channel = implementations.insecure_channel(host, int(port))
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = FLAGS.model
request.model_spec.signature_name = 'serving_default'
feature_dict = {'a': _float_feature(value=400),
'b': _float_feature(value=5),
'c': _float_feature(value=200),
'd': _float_feature(value=30),
'e': _float_feature(value=60),
'f': _float_feature(value=5),
'g': _float_feature(value=7500),
'h': _int_feature(value=1),
'i': _int_feature(value=1234),
'j': _int_feature(value=1),
'k': _int_feature(value=4),
'l': _int_feature(value=1),
'm': _int_feature(value=0)}
example= tf.train.Example(features=tf.train.Features(feature=feature_dict))
serialized = example.SerializeToString()
request.inputs['inputs'].CopyFrom(
tf.contrib.util.make_tensor_proto(serialized, shape=[1]))
result_future = stub.Predict.future(request, 5.0)
print(result_future.result())
现在我得到的输出是:-
我不知道如何解析 float_val 数字,因为那是我的输出。请帮助。
您可以执行以下操作
result = stub.Predict(request, 5.0)
float_val = result.outputs['outputs'].float_val
请注意,此方法调用 stub.Predict
而不是 stub.Predict.future
如果你有多个输出,你可以做类似下面的事情,它基本上创建一个字典,其中的键对应于输出和值对应于任何模型 returns 的列表。
results = dict()
for output in output_names:
results[output] = response.outputs[output].float_val
这是对 @Maxime De Bruyn
、
给出的答案的补充
在使用 mobilenet/inception 模型预测具有多个预测输出的 API 中,以下代码段对我不起作用。
result = stub.Predict(request, 5.0)
float_val = result.outputs['outputs'].float_val
print("Output: ", float_val)
Output: []
相反,我不得不在输出中使用“预测”键。
result = stub.Predict(request, 5.0)
predictions = result.outputs['prediction'].float_val
print("Output: ", predictions)
Output: [0.016111543402075768, 0.2446805089712143, 0.06016387417912483, 0.12880375981330872, 0.035926613956689835, 0.026000071316957474, 0.04009509086608887, 0.35264086723327637, 0.0762331634759903, 0.019344471395015717]
您正在寻找的可能是 tf.make_ndarray
,它从 TensorProto 创建一个 numpy 数组(即 tf.make_tensor_proto
的倒数)。这样你的输出就会恢复它应该有的形状,所以根据 Jasmine 的回答,你可以将多个输出存储在一个字典中:
response = prediction_service.Predict(request, 5.0)
results = {}
for output in response.outputs.keys():
results[output] = tf.make_ndarray(response.outputs[output])
我已经导出了一个 DNNClassifier 模型,并使用 docker 在 tensorflow-serving 服务器上 运行 它。之后,我编写了一个 python 客户端来与该 tensorflow-serving 进行交互以进行新的预测。
我已经编写了以下代码来获取 tensorflow-serving 服务器的响应。
host, port = FLAGS.server.split(':')
channel = implementations.insecure_channel(host, int(port))
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = FLAGS.model
request.model_spec.signature_name = 'serving_default'
feature_dict = {'a': _float_feature(value=400),
'b': _float_feature(value=5),
'c': _float_feature(value=200),
'd': _float_feature(value=30),
'e': _float_feature(value=60),
'f': _float_feature(value=5),
'g': _float_feature(value=7500),
'h': _int_feature(value=1),
'i': _int_feature(value=1234),
'j': _int_feature(value=1),
'k': _int_feature(value=4),
'l': _int_feature(value=1),
'm': _int_feature(value=0)}
example= tf.train.Example(features=tf.train.Features(feature=feature_dict))
serialized = example.SerializeToString()
request.inputs['inputs'].CopyFrom(
tf.contrib.util.make_tensor_proto(serialized, shape=[1]))
result_future = stub.Predict.future(request, 5.0)
print(result_future.result())
您可以执行以下操作
result = stub.Predict(request, 5.0)
float_val = result.outputs['outputs'].float_val
请注意,此方法调用 stub.Predict
而不是 stub.Predict.future
如果你有多个输出,你可以做类似下面的事情,它基本上创建一个字典,其中的键对应于输出和值对应于任何模型 returns 的列表。
results = dict()
for output in output_names:
results[output] = response.outputs[output].float_val
这是对 @Maxime De Bruyn
、
在使用 mobilenet/inception 模型预测具有多个预测输出的 API 中,以下代码段对我不起作用。
result = stub.Predict(request, 5.0)
float_val = result.outputs['outputs'].float_val
print("Output: ", float_val)
Output: []
相反,我不得不在输出中使用“预测”键。
result = stub.Predict(request, 5.0)
predictions = result.outputs['prediction'].float_val
print("Output: ", predictions)
Output: [0.016111543402075768, 0.2446805089712143, 0.06016387417912483, 0.12880375981330872, 0.035926613956689835, 0.026000071316957474, 0.04009509086608887, 0.35264086723327637, 0.0762331634759903, 0.019344471395015717]
您正在寻找的可能是 tf.make_ndarray
,它从 TensorProto 创建一个 numpy 数组(即 tf.make_tensor_proto
的倒数)。这样你的输出就会恢复它应该有的形状,所以根据 Jasmine 的回答,你可以将多个输出存储在一个字典中:
response = prediction_service.Predict(request, 5.0)
results = {}
for output in response.outputs.keys():
results[output] = tf.make_ndarray(response.outputs[output])