用tensorflow/serving部署深度学习模型及gRPC远程访问服务器

一.将tensorflow的ckpt模型转换成pbtxt模型

转换模型格式的代码可参考如下demo:

#! /usr/bin/python
# _*_ coding: utf-8 _*_
__author__ = 'Jeffery'
__date__ = '2018/12/29 23:20'
import tensorflow as tf
from mymodel import captcha_model as model
import os
from constant import char_num, classes

def export_model(checkpoint_path,
                 export_model_dir,
                 model_version
                 ):
    """
    :param checkpoint_path: type string, original model path(a dir)
    :param export_model_dir: type string, save dir for exported model
    :param model_version: type int best
    :return:no return
    """
    with tf.get_default_graph().as_default():
        input_images = tf.placeholder(tf.float32, shape=[None, 100, 120, 1], name='input_images')
        output_result, _ = model(input_images, keep_prob=1.0, trainable=False)
        output_result = tf.argmax(tf.reshape(output_result, [-1, char_num, classes]), 2)
        saver = tf.train.Saver()
        with tf.Session() as sess:
            ckpt_state = tf.train.get_checkpoint_state(checkpoint_path)
            model_path = os.path.join(checkpoint_path,
                                      os.path.basename(ckpt_state.model_checkpoint_path))
            saver.restore(sess, model_path)
            print('step1 => Model Restored successfully from {}'.format(model_path))
            # set-up a builder
            export_path_base = export_model_dir
            export_path = os.path.join(
                tf.compat.as_bytes(export_path_base),
                tf.compat.as_bytes(str(model_version)))
            builder = tf.saved_model.builder.SavedModelBuilder(export_path)
            print('step2 => Export path(%s) ready to export trained model' % export_path)
            tensor_info_input = tf.saved_model.utils.build_tensor_info(input_images)
            tensor_info_output = tf.saved_model.utils.build_tensor_info(output_result)
            #1. prediction_signature
            prediction_signature = (
                tf.saved_model.signature_def_utils.build_signature_def(
                    inputs={'images': tensor_info_input},
                    outputs={'result': tensor_info_output},
                    method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

            # #2. classification_signature
            # classification_signature = (
            #     tf.saved_model.signature_def_utils.build_signature_def(
            #         inputs={
            #             tf.saved_model.signature_constants.CLASSIFY_INPUTS: '1'
            #         },
            #         outputs={
            #             tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES: '2',
            #             tf.saved_model.signature_constants.CLASSIFY_OUTPUT_SCORES: '3'
            #         },
            #         method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME
            #     ))

            # #3. regression_signature
            # regression_signature = (
            #     tf.saved_model.signature_def_utils.build_signature_def(
            #         inputs={
            #             tf.saved_model.signature_constants.REGRESS_INPUTS: '1'
            #         },
            #         outputs={
            #             tf.saved_model.signature_constants.REGRESS_OUTPUTS: '2',
            #         },
            #         method_name=tf.saved_model.signature_constants.REGRESS_METHOD_NAME
            #     ))

            print('step3 => prediction_signature created successfully')
            builder.add_meta_graph_and_variables(
                # tags:SERVING,TRAINING,EVAL,GPU,TPU
                sess, [tf.saved_model.tag_constants.SERVING],
                signature_def_map={
                    'prediction_signature': prediction_signature,
                })
            print('step4 => builder successfully add meta graph and variables\nNext is to export model...')
            builder.save(as_text=True)
            print('Done exporting!')


if __name__ == '__main__':
    export_model(
        checkpoint_path='./model_data',
        export_model_dir='./export_model',
        model_version=2
    )

以上代码作者Jeffery,表示鸣谢!项目链接:Captcha-Identifier

二.用docker和tensorflow/serving镜像部署模型

docker run -d --name captcha -p 8500:8500 -p 8501:8501 --mount type=bind,source=/home/heyuvin/deepLearning/modelDeploy/captcha,target=/models/captcha -e MODEL_NAME=captcha -t tensorflow/serving '&'

注意:在/home/heyuvin/deepLearning/modelDeploy/captcha目录下,直接放置以版本号为目录名的模型文件。如下图,放置包含variables、saved_model.pbtxt的目录1或目录2即可。
用tensorflow/serving部署深度学习模型及gRPC远程访问服务器_第1张图片

三、用gRPC远程调用自己部署的模型

#! /usr/bin/python
# _*_ coding: utf-8 _*_
__author__ = 'Jeffery'
__date__ = '2019/1/6 20:18'

from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc
import tensorflow as tf
import numpy as np
import grpc
import os
import cv2
from constant import characters
from utils import get_captcha

def process_img(img_path):
    """
    pre-process img to numpy
    :param img_path: path of img
    :return:
    """
    if os.path.exists(img_path):
        img = cv2.imread(img_path, 0)  # (100, 120)
    else:
        raise FileNotFoundError('img to process not exists')
    img[img < 193] = 0
    img[img >= 193] = 1
    img = np.reshape(img, [1, img.shape[0], img.shape[1], 1])
    float_img = img.astype(np.float32)
    return float_img


def request_server(img_np,
                   server_url,
                   model_name,
                   signature_name,
                   input_name,
                   output_name
                   ):
    """
    below info about model
    :param model_name:
    :param signature_name:
    :param output_name:
    :param input_name:
    :param img_np: processed img , numpy.ndarray type [h,w,c]
    :param server_url: TensorFlow Serving url,str type,e.g.'0.0.0.0:8500'
    :return: type numpy array
    """
    # connect channel
    channel = grpc.insecure_channel(server_url)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    # set up request
    request = predict_pb2.PredictRequest()
    request.model_spec.name = model_name  # request.model_spec.version = "1"
    request.model_spec.signature_name = signature_name
    request.inputs[input_name].CopyFrom(
        tf.contrib.util.make_tensor_proto(img_np, shape=list(img_np.shape)))
    # get response
    response = stub.Predict(request, 5.0)
    # res_from_server_np = np.asarray(response.outputs[output_name].float_val)
    res_from_server_np = tf.make_ndarray(response.outputs[output_name])
    
    s = ''
    for character in res_from_server_np[0]:
        s += characters[character]
    return s


if __name__ == '__main__':
    img = process_img('./img/goko.png')
    res = request_server(
        img_np=img,
        server_url='192.168.133.129:8500', # 虚拟机的IP地址及其端口
        model_name='captcha',  # model_name必须是创建容器时指定的MODEL_NAME的名字(MODEL_NAME=captcha)
        signature_name='prediction_signature',
        input_name="images",
        output_name="result")
    print(res)

以上代码作者Jeffery,表示鸣谢!

四、参考博客

https://blog.csdn.net/jeffery0207/article/details/86072456
https://blog.csdn.net/zhou_438/article/details/103427853

你可能感兴趣的:(用tensorflow/serving部署深度学习模型及gRPC远程访问服务器)