How to retrieve float_val from a PredictResponse object?

后端 未结 5 1434
猫巷女王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: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)
    

提交回复
热议问题