How to retrieve float_val from a PredictResponse object?

旧巷老猫 提交于 2019-11-30 13:38:20

The answer is:

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

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>

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

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

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!