How to retrieve float_val from a PredictResponse object?

后端 未结 5 1410
猫巷女王i
猫巷女王i 2020-12-31 09:01

I am running a prediction on a tensorflow-serving model, and I get back this PredictResponse object as output:

Result:



        
相关标签:
5条回答
  • 2020-12-31 09:41

    result["outputs"].float_val should be a python list

    0 讨论(0)
  • 2020-12-31 09:45

    You generally want to recover a tensor, with a shape (not just a long list of floats). Here's how:

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

    If you prefer to get a NumPy array, then just replace the last line:

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

    If you don't want to depend on the TensorFlow library at all, for some reason:

    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)
    
    0 讨论(0)
  • 2020-12-31 09:48

    If you would like to convert the entire PredictResponse to a numpy array (including its dimentions)

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

    0 讨论(0)
  • 2020-12-31 10:06

    The answer is:

    floats = result.outputs['outputs'].float_val
    
    0 讨论(0)
  • 2020-12-31 10:08

    This answer is for tensorflow-serving-api-python3 1.8.0

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

    This will return a repeated scalar container object. Which can be passed to python list() or np.array() etc

    0 讨论(0)
提交回复
热议问题