美文网首页程序员
使用docker + tensorflow serving 快速

使用docker + tensorflow serving 快速

作者: 锦男 | 来源:发表于2020-05-23 15:25 被阅读0次

    参考文献
    serving-ml-quickly-with-tensorflow-serving-and-docker
    tensorflow-serving-of-multiple-ml-models-simultaneously-to-a-rest-api-python-client

    本文先部署单个模型,然后部署多个模型以及同一模型的多个版本

    各组件版本信息:

    tensorflow/serving:2.2.0-rc2
    Docker version 19.03.8
    

    部署单个模型

    1. 从这个链接 下载模型文件并解压。
      解压后的目录 :
    └─resnet_v2_fp32_savedmodel_NHWC_jpg
        └─1538687457(版本号)
            │  saved_model.pb(模型pb文件)
            │
            └─variables
                    variables.data-00000-of-00001
                    variables.index
    
    
    1. 启动tensorflow serving:
    docker run --rm -p 8501:8501 --name tfserving_resnet --mount type=bind,source=E:/dev/tensorflow/tensorflow-serving/model-files/resnet_v2_fp32_savedmodel_NHWC_jpg,target=/models/resnet -e MODEL_NAME=resnet -t tensorflow/serving:2.2.0-rc2 &
    

    然后就可以发起http调用了。
    调用方式有gRPC和restful两种,详见这个链接

    部署多个模型

    1. 先准备好2个模型文件。这里为了方便,我直接把上一个模型复制一份:
    ├─resnet_v2_fp32_savedmodel_NHWC_jpg
    │  ├─1538687457
    │    └─variables
    │  
    └─resnet_v2_fp32_savedmodel_NHWC_jpg_2
        └─1538687457
            └─variables
    

    2.编写model_config.config,将2个模型命名为r1和r2,同时base_path也变了:

    model_config_list: {
        config: {
          name: "r1",
          base_path: "/models/resnet1",
          model_platform: "tensorflow"
        },
        config: {
          name: "r2",
          base_path: "/models/resnet2",
          model_platform: "tensorflow"
        }
    }
    
    1. 类似部署单个模型,通过docker启动多个模型
    docker run --rm -p 8500:8500 -p 8501:8501 
    --mount type=bind,source=E:/dev/tensorflow/tensorflow-serving/model-files/resnet_v2_fp32_savedmodel_NHWC_jpg,target=/models/resnet1  
    --mount type=bind,source=E:/dev/tensorflow/tensorflow-serving/model-files/resnet_v2_fp32_savedmodel_NHWC_jpg_2,target=/models/resnet2 
    --mount type=bind,source=E:/dev/tensorflow/tensorflow-serving/model-files/model_config.config,target=/models/model_config.config  
    -t tensorflow/serving:2.2.0-rc2 --model_config_file=/models/model_config.config
    
    

    通过日志看到2个模型都已经启动成功:

    Successfully loaded servable version {name: r2 version: 1538687457}
    Successfully loaded servable version {name: r1 version: 1538687457}
    

    4、测试调用
    模型1:

    http://localhost:8501/v1/models/r1:predict
    http://localhost:8501/v1/models/r1/metadata
    

    模型2:

    http://localhost:8501/v1/models/r2:predict
    http://localhost:8501/v1/models/r2/metadata
    

    同一模型的多个版本

    同样,简单处理,把模型r1复制一份,然后版本号加1:

    ├─resnet_v2_fp32_savedmodel_NHWC_jpg
    │  ├─1538687457
    │  │  └─variables
    │  └─1538687458(版本号加1)
    │      └─variables
    └─resnet_v2_fp32_savedmodel_NHWC_jpg_2
        └─1538687457
            └─variables
    

    此时,tensorflow-serving自动发现了1538687458这个版本,触发加载,同时unload旧版本:

    2020-05-23 06:51:49.453468: I tensorflow_serving/core/loader_harness.cc:87] Successfully loaded servable version {name: r1 version: 1538687458}
    2020-05-23 06:51:49.453587: I tensorflow_serving/core/loader_harness.cc:138] Quiescing servable version {name: r1 version: 1538687457}
    2020-05-23 06:51:49.453628: I tensorflow_serving/core/loader_harness.cc:145] Done quiescing servable version {name: r1 version: 1538687457}
    2020-05-23 06:51:49.453891: I tensorflow_serving/core/loader_harness.cc:120] Unloading servable version {name: r1 version: 1538687457}
    2020-05-23 06:51:49.488530: I ./tensorflow_serving/core/simple_loader.h:363] Calling MallocExtension_ReleaseToSystem() after servable unload with 123549680
    2020-05-23 06:51:49.488595: I tensorflow_serving/core/loader_harness.cc:128] Done unloading servable version {name: r1 version: 1538687457}
    
    

    通过查看r1的metadata发现版本号已经更新:
    http://localhost:8501/v1/models/r1/metadata 输出:
    {"name": "r1", "signature_name": "", "version": "1538687458"}......

    总结

    通过上面的实验,我们发现,tensorflow-serving支持部署多个模型的多个版本,并且模型有了新版本后,还能自动加载最新版本,因此在生产环境部署和更新模型都很方便。

    相关文章

      网友评论

        本文标题:使用docker + tensorflow serving 快速

        本文链接:https://www.haomeiwen.com/subject/slhoohtx.html