如何从PredictResponse对象中检索float_val?

时间:2017-06-27 16:53:31

标签: tensorflow deep-learning tensorflow-serving tensorflow-gpu

我正在对tensorflow服务模型运行预测,我将这个PredictResponse对象作为输出返回:

结果:

outputs {
  key: "outputs"
  value {
    dtype: DT_FLOAT
    tensor_shape {
      dim {
        size: 1
      }
      dim {
        size: 20
      }
    }
    float_val: 0.000343723397236
    float_val: 0.999655127525
    float_val: 3.96821117632e-11
    float_val: 1.20521548297e-09
    float_val: 2.09611101809e-08
    float_val: 1.46216549979e-09
    float_val: 3.87274603497e-08
    float_val: 1.83520256769e-08
    float_val: 1.47733780764e-08
    float_val: 8.00914179422e-08
    float_val: 2.29388191997e-07
    float_val: 6.27798826258e-08
    float_val: 1.08802950649e-07
    float_val: 4.39628813353e-08
    float_val: 7.87182985462e-10
    float_val: 1.31638898893e-07
    float_val: 1.42612295306e-08
    float_val: 3.0768305237e-07
    float_val: 1.12661648899e-08
    float_val: 1.68554503688e-08
  }
}

我想把浮动值作为列表。或者,或者,返回argmax float_val的值/索引!

这是由:

生成的
  

stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)   result = stub.Predict(request,200.0)

提前感谢您的帮助。

5 个答案:

答案 0 :(得分:7)

答案是:

floats = result.outputs['outputs'].float_val

答案 1 :(得分:2)

如果您想将整个PredictResponse转换为numpy数组(包括其维数)

<script src="https://gist.github.com/eavidan/22ad044f909e5739ceca9ff9e6feaa43.js"></script>

答案 2 :(得分:1)

此答案适用于tensorflow-serving-api-python3 1.8.0

result.outputs['your key name'].float_val #key name in your case is outputs

这将返回一个重复的标量容器对象。可以传递给python list()或np.array()等

答案 3 :(得分:0)

table.reloadData()应该是一个python列表

答案 4 :(得分:0)

您通常希望恢复具有某种形状的张量(不仅仅是一堆浮点数)。方法如下:

outputs_tensor_proto = result.outputs["outputs"]
shape = tf.TensorShape(outputs_tensor_proto.tensor_shape)
outputs = tf.constant(outputs_tensor_proto.float_val, shape=shape)

如果您希望获得NumPy数组,则只需替换最后一行:

outputs = np.array(outputs_tensor_proto.float_val).reshape(shape.as_list())

由于某种原因,如果您根本不想依赖TensorFlow库,

outputs_tensor_proto = result.outputs["outputs"]
shape = [dim.size for dim in outputs_tensor_proto.tensor_shape.dim]
outputs = np.array(outputs_tensor_proto.float_val).reshape(shape)