tensorflow serving部署keras或tf2.0模型

 

一、安装docker

  • 由于apt官方库里的docker版本可能比较旧,所以先卸载可能存在的旧版本:
$ sudo apt-get remove docker docker-engine docker-ce docker.io
  • 更新apt包索引:
 $ sudo apt-get update
  • 安装以下包以使apt可以通过HTTPS使用存储库(repository):
$ sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
  • 添加Docker官方的GPG密钥:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  • 使用下面的命令来设置stable存储库:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  • 再更新一下apt包索引:
$ sudo apt-get update
  • 安装最新版本的Docker CE:
$ sudo apt-get install -y docker-ce

二、模型转为pb文件

def save_model_for_production(model, version='1', path='prod_models'):
    tf.keras.backend.set_learning_phase(1)
    if not os.path.exists(path):
        os.mkdir(path)
    export_path = os.path.join(
        tf.compat.as_bytes(path),
        tf.compat.as_bytes(version))
    builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_path)

    model_input = tf.compat.v1.saved_model.build_tensor_info(model.input)
    model_output = tf.compat.v1.saved_model.build_tensor_info(model.output)

    prediction_signature = (
        tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
            inputs={'inputs': model_input},
            outputs={'outputs': model_output},
            method_name=tf.compat.v1.saved_model.signature_constants.PREDICT_METHOD_NAME))

    with tf.compat.v1.keras.backend.get_session() as sess:
        builder.add_meta_graph_and_variables(
            sess=sess, tags=[tf.compat.v1.saved_model.tag_constants.SERVING],
            signature_def_map={
                'predict':prediction_signature,
                tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:prediction_signature
            })
        builder.save()

三、开启服务 

1、准备TF Serving的Docker环境
docker pull tensorflow/serving

2、开启服务
docker run -dt -p 8501:8501 -v /home/wangdong/TextClassification/export_model:/models/export_model -e MODEL_NAME=export_model tensorflow/serving &

3、验证是否开启成功
curl -d '{"instances":[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,38,508,980,35,140]]}' -X POST http://localhost:8501/v1/models/export_model:predict

注意:其中export_model为存储.pb文件的文件夹名称

四、tensorflow serving发布多个模型

详文见此博客:https://blog.csdn.net/JerryZhang__/article/details/86516428

1、创建一个多模型配置文件

#从tensorflow-serving git文档上抄下来的

sudo vim {你的tf-serving配置文件根目录}/models.config

贴入以下内容

model_config_list: {

  config: {

    name: "half_plus_two",

    base_path: "/models/half_plus_two",

    model_platform: "tensorflow"

  },

  config: {

    name: "half_plus_three",

    base_path: "/models/half_plus_three",

    model_platform: "tensorflow"

  }

}

2、启动一个docker container

sudo docker run -p 8500:8500 -p 8501:8501 \

--mount type=bind,source={你的tf-serving根目录}/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two_cpu,target=/models/half_plus_two\

--mount type=bind,source={你的tf-serving根目录}/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_three,target=/models/half_plus_three\

--mount type=bind,source={你的tf-serving配置文件根目录}/models.config,target=/models/models.config \

-t tensorflow/serving --model_config_file=/models/models.config&

3、测试多个模型调用

curl -d '{"instances": [1.0, 2.0, 5.0]}' -X POST http://127.0.0.1:8501/v1/models/half_plus_two:predict

curl -d '{"instances": [1.0, 2.0, 5.0]}' -X POST http://127.0.0.1:8501/v1/models/half_plus_three:predict

注意,注意,注意:版本很重要,我使用的版本是tensorflow1.13.1,其他版本很可能不成功。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值