【问题标题】:How to retrieve float_val from a PredictResponse object?如何从 PredictResponse 对象中检索 float_val?
【发布时间】:2017-11-30 20:03:48
【问题描述】:

我正在对 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 的值/索引!

这是由以下人员生成的:

存根 = prediction_service_pb2.beta_create_PredictionService_stub(通道) 结果 = stub.Predict(request, 200.0)

提前感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    答案是:

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

    【讨论】:

    • 同样的问题,但result.outputs['outputs'].float_val 返回类型<type 'google.protobuf.pyext._message.RepeatedScalarContainer'>。您是如何将其转换为浮动列表的?
    • TF 1.2+ 的答案是:result.result().outputs['outputs'].float_val
    • 将其转换为列表,然后根据需要转换为 numpy 数组,floats = numpy.array(list(result.outputs['outputs'].float_val))
    【解决方案2】:

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

    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)
    

    【讨论】:

    • 这应该是公认的答案,因为它解决了一般情况。
    • 我必须做 .reshape((shape)) 才能让它工作。但我同意 jamix 的观点。这应该是公认的答案。
    • 请注意,结果可能不在 ["outputs"] 中。就我而言,它在 [“score”] 中。但这对我来说是正确的答案。
    【解决方案3】:

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

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

    【讨论】:

    • 你能帮我如何得到预测和概率吗,我从 TF-serving 得到的输出相同,只是概率。?
    【解决方案4】:

    此答案适用于 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() 等

    【讨论】:

      【解决方案5】:

      result["outputs"].float_val应该是一个python列表

      【讨论】:

      • 您好 Alexandre,感谢您的回复。但我得到了:Traceback(最近一次通话最后一次):line 95, in main vals = result['outputs'].float_val TypeError: 'PredictResponse' object has no attribute '__getitem__'
      • results.value[0].float_val then
      • 不起作用:AttributeError:“PredictResponse”对象没有属性“值”
      猜你喜欢
      • 1970-01-01
      • 2019-03-28
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多