Я пытаюсь обслуживать API обнаружения объектов tenorflow, но мой клиент grpc и клиенты Rest дают разные результаты для одного и того же изображения.Я не знаю почему.Ответ GRPC:
[2.8745510860517243e-08, 2.476179972177306e-08, 1.955560691158098e-08, 1.1536828381508712e-08, 1.0335512889980691e-08, 9.396929801, 0969393829016.33630348190195e-09, 5.411928682974576e-09, 4.907114270480406e-09, 4.862884761536179e-09, 4.271269560263136e-09, 3.803687587122795e-09, 3.550610694347258e 0930, 093, 098, 098, 098, 098, 098, 09, 28, 09, 09.3, 09.3, 09.3, 09.3.2.471733706599366e-09, 2.4317983182697844e-09, 2.048162306422796e-09]
В то время как ответ клиента REST:
[0.996831, 0.000675639, 0.000323685, 0.000144264, 0.000144642, 0,000134516, 0,000104812, 0,000104108, 9.99449e-05, 8.9907e-05, 8.72486e-05, 6.28879e-05, 6.16111e-05, 6.06435e-05, 5.47078e-05, 4.88681e-05, 4.87645e-05, 4.73167e-05, 4.13763e-05, 4.01956e-05]
Ясно, что клиент GRPC не смог ничего обнаружить.Вот мой GRPC-клиент
from tensorflow.core.framework import tensor_pb2
from tensorflow.core.framework import tensor_shape_pb2
from tensorflow.core.framework import types_pb2
from grpc.beta import implementations
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
import helper
parser = argparse.ArgumentParser(description='incetion grpc client flags.')
parser.add_argument('--host', default='0.0.0.0', help='inception serving host')
parser.add_argument('--port', default='8500', help='inception serving port')
parser.add_argument('--image', dest='image', type=str,
help='Path to the jpeg image directory')
FLAGS = parser.parse_args()
def main(file):
print("\n\ninput file {}...\n".format(file))
channel = implementations.insecure_channel(FLAGS.host, int(FLAGS.port))
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = 'vedNet'
# request.model_spec.signature_name = 'serving_default'
img = cv2.imread(file).astype(np.uint8)
tensor_shape = [1]+list(img.shape)
dims = [tensor_shape_pb2.TensorShapeProto.Dim(size=dim) for dim in tensor_shape]
tensor_shape = tensor_shape_pb2.TensorShapeProto(dim=dims)
tensor = tensor_pb2.TensorProto(
dtype=types_pb2.DT_UINT8,
tensor_shape=tensor_shape,
float_val=list(img.reshape(-1)))
request.inputs['inputs'].CopyFrom(tensor)
resp = stub.Predict(request, 30.0)
f.close()
# print(resp.outputs['detection_scores'].float_val)
print(resp.outputs['detection_scores'].float_val)
if __name__ == '__main__':
main(FLAGS.image)
и REST-клиент:
from object_detection.utils import plot_util
from PIL import Image
def pre_process(image_path):
image = Image.open(image_path).convert("RGB")
image_np = plot_util.load_image_into_numpy_array(image)
image_tensor = np.expand_dims(image_np, 0)
formatted_json_input = json.dumps({"signature_name": "serving_default", "instances": image_tensor.tolist()})
return formatted_json_input
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Performs call to the tensorflow-serving REST API.')
parser.add_argument('--server_url', dest='server_url', type=str, required=True,
help='URL of the tensorflow-serving accepting API call. '
'e.g. http://localhost:8501/v1/models/vedNet:predict')
parser.add_argument('--image_path', dest='image_path', type=str,
help='Path to the jpeg image')
args = parser.parse_args()
server_url = args.server_url
image_path = args.image_path
print("\n\nPre-processing input file {}...\n".format(image_path))
formatted_json_input = pre_process(image_path)
headers = {"content-type": "application/json"}
server_response = requests.post(server_url, data=formatted_json_input, headers=headers)
response = json.loads(server_response.text)
output_dict = response['predictions'][0]
print(output_dict['detection_scores'])
Любая помощь, пожалуйста ...