我是tensorflow的新手,我正在学习如何部署生产模型。我已经在GCP云存储上部署了Tensorflow对象检测模型,并且该模型在Google Kubernetes引擎上可用并且具有端点。现在,我想部署使用gRPC的客户端脚本(而不是REST,以加快响应时间)。如何部署客户端脚本,以使其可以作为对外部世界的API请求使用,并且可以获取编码的图像数组并向服务模型发出请求,然后将其响应返回给用户?我希望它具有快速的响应时间,并可能一次处理多个请求。客户端脚本为:
import os
import argparse
from argparse import RawTextHelpFormatter
from grpc.beta import implementations
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
from object_detection.core.standard_fields import \
DetectionResultFields as dt_fields
tf.logging.set_verbosity(tf.logging.INFO)
def load_image_into_numpy_array(input_image):
image = Image.open(input_image)
(im_width, im_height) = image.size
image_arr = np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
image.close()
return image_arr
def load_input_tensor(input_image):
image_np = load_image_into_numpy_array(input_image)
image_np_expanded = np.expand_dims(image_np, axis=0).astype(np.uint8)
tensor = tf.contrib.util.make_tensor_proto(image_np_expanded)
return tensor
def main(args):
host, port = args.server.split(':')
channel = implementations.insecure_channel(host, int(port))._channel
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = args.model_name
input_tensor = load_input_tensor(args.input_image)
request.inputs['inputs'].CopyFrom(input_tensor)
result = stub.Predict(request, 60.0)
image_np = load_image_into_numpy_array(args.input_image)
output_dict = {}
output_dict[dt_fields.detection_classes] = np.squeeze(
result.outputs[dt_fields.detection_classes].float_val).astype(np.uint8)
output_dict[dt_fields.detection_boxes] = np.reshape(
result.outputs[dt_fields.detection_boxes].float_val, (-1, 4))
output_dict[dt_fields.detection_scores] = np.squeeze(
result.outputs[dt_fields.detection_scores].float_val)
category_index = label_map_util.create_category_index_from_labelmap(args.label_map,
use_display_name=True)
vis_util.visualize_boxes_and_labels_on_image_array(image_np,
output_dict[dt_fields.detection_boxes],
output_dict[dt_fields.detection_classes],
output_dict[dt_fields.detection_scores],
category_index,
instance_masks=None,
use_normalized_coordinates=True,
line_thickness=8)
output_img = Image.fromarray(image_np.astype(np.uint8))
base_filename = os.path.splitext(os.path.basename(args.input_image))[0]
output_image_path = os.path.join(args.output_directory, base_filename + "_output.jpg")
tf.logging.info('Saving labeled image: %s' % output_image_path)
output_img.save(output_image_path)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Object detection grpc client.",
formatter_class=RawTextHelpFormatter)
parser.add_argument('--server',
type=str,
required=True,
help='PredictionService host:port')
parser.add_argument('--model_name',
type=str,
required=True,
help='Name of the model')
parser.add_argument('--input_image',
type=str,
required=True,
help='Path to input image')
parser.add_argument('--output_directory',
type=str,
required=True,
help='Path to output directory')
parser.add_argument('--label_map',
type=str,
required=True,
help='Path to label map file')
args = parser.parse_args()
main(args)
我正在考虑使用docker构建微服务,其中包含tensorflow,tensorflow服务api和客户端脚本。
更新1 :我尝试使用grpc客户端并遇到问题。 由于我使用了kubeflow,因此请求的网址格式为http://HOST:8000/model/:predict。但是客户端脚本似乎不接受这种url格式。它仅接受HOST:PORT NUMBER格式,但出现错误
Traceback (most recent call last):
File "object_detection_grpc_client.py", line 104, in <module>
main(args)
File "object_detection_grpc_client.py", line 41, in main
stub = prediction_service_pb2_grpc.PredictionServiceStub('http://A.B.C.D:8000/model/my-model:predict')
File "/usr/local/lib/python3.5/dist-packages/tensorflow_serving/apis/prediction_service_pb2_grpc.py", line 40, in __init__
self.Classify = channel.unary_unary(
AttributeError: 'str' object has no attribute 'unary_unary'
当我仅使用A.B.C.D:8000作为服务器参数值时,出现错误:
Traceback (most recent call last):
File "object_detection_grpc_client.py", line 104, in <module>
main(args)
File "object_detection_grpc_client.py", line 48, in main
result = stub.Predict(request, 60.0)
File "/usr/local/lib/python3.5/dist-packages/grpc/_channel.py", line 550, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/usr/local/lib/python3.5/dist-packages/grpc/_channel.py", line 467, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Socket closed"
debug_error_string = "{"created":"@1550755989.677583779","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1036,"grpc_message":"Socket closed","grpc_status":14}"
>
我直接使用了this存储库中的预测原虫。如何修改代码以解决问题?
答案 0 :(得分:0)
如果使用grpc客户端,则主机格式应为HOST:PORT。 http://HOST:PORT/model是rest_api格式。确保您的端口正确。您能显示开始进行tf服务的方式吗?