以下是我调用一个关键点检测服务的示例代码from grpc.beta import implementations
import tensorflow as tf
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
class KeyPointDetector:
def __init__(self, hostport, model_name):
self.hostport = hostport
self.model_name = model_name
def do_inference(self,
crop_image,
box,
ori_shape,
shape_bbox,
signature_name=None):
"""Tests PredictionService with concurrent requests.
Args:
hostport: Host:port address of the Prediction Service.
Returns:
pred values, ground truth label
"""
host, port = self.hostport.split(':')
channel = implementations.insecure_channel(host, int(port))
stub = prediction_service_pb2.beta_create_PredictionService_stub(
channel)
# fill in the request object with the necessary data
request = predict_pb2.PredictRequest()
request.model_spec.name = self.model_name
# request.model_spec.signature_name = 'pred_v'
request.inputs['crop_image_processed'].CopyFrom(
tf.contrib.util.make_tensor_proto(np.array(crop_image).astype(
np.float32),
shape=[384, 384, 3]))
request.inputs['boxes'].CopyFrom(
tf.contrib.util.make_tensor_proto(np.array(box).astype(np.float32),
shape=[1, 4]))
request.inputs['ori_shape'].CopyFrom(
tf.contrib.util.make_tensor_proto(np.array(ori_shape).astype(
np.float32),
shape=[3]))
request.inputs['shape_bbox'].CopyFrom(
tf.contrib.util.make_tensor_proto(np.array(shape_bbox).astype(
np.float32),
shape=[3]))
# predict
response = stub.Predict(request, 30.0) # 5 seconds
pred_x_ratio = np.asarray(
response.outputs["pred_x_ratio"].float_val).tolist()
pred_y_ratio = np.asarray(
response.outputs["pred_y_ratio"].float_val).tolist()
preds_class = np.asarray(
response.outputs["preds_class"].int64_val).tolist()
fpred_x_bbox = np.asarray(
response.outputs["fpred_x_bbox"].float_val).tolist()
fpred_y_bbox = np.asarray(
response.outputs["fpred_y_bbox"].float_val).tolist()
pred_max = np.asarray(response.outputs["pred_max"].float_val).tolist()
pred_max = np.asarray(response.outputs["pred_max"].float_val).tolist()
return pred_x_ratio, pred_y_ratio, preds_class, fpred_x_bbox, fpred_y_bbox, pred_max
https://www.jianshu.com/p/77cb3c5bb167