TensorFlow Serving实现多模型部署以及不同版本模型的调用
前提:要实现多模型部署,首先要了解并且熟练实现单模型部署,可以借助官网文档,使用Docker实现部署。
1. 首先准备两个你需要部署的模型,统一的放在multiModel/文件夹下(文件夹名字可以任意取),其目录结构如下:
multiModel/
├── model1 │ └── 00000123 │ ├── saved_model.pb │ └── variables │ ├── variables.data-00000-of-00001 │ └── variables.index ├── model2 │ └── 00000123 │ ├── saved_model.pb │ └── variables │ ├── variables.data-00000-of-00001 │ └── variables.index └── models.config
2. 在 multiModel/目录中创建models.config配置文件,内容如下:
model_config_list:{ config:{ name:"model1", base_path:"/models/multiModel/model1", model_platform:"tensorflow" }, config:{ name:"model2", base_path:"/models/multiModel/model2", model_platform:"tensorflow" }, }
# 注:
base_path路径前面的/models/是固定写法,后面写上你的目录名加上模型路径
否则报错:FileSystemStoragePathSource encountered a filesystem access error: Could not find base path /home/node1/model/multiModel/model1 for servable model1
3. 配置文件定义了模型的名称和模型在容器内的路径,现在运行tf-serving容器 :
sudo docker run -p 8501:8501 --mount type=bind,source=/home/node1/model/multiModel/,target=/models/multiModel -t tensorflow/serving:latest-gpu --model_config_file=/models/multiModel/models.config
# 注:
1. taget后面写的是模型总路径,与单模型有些区别,单模型写的是具体的某一个模型路径;
2. --model_config_file 后面配置文件的路径前面是固定写法/models/,后面加上配置文件的路径,如果写绝对路径或者相对路径找models.config,会报错找不到文件或者路径;
4. 最后看到这样的界面,并查看GPU已经占用,表明多模型已经部署成功:
——————————————————————————————指定模型版本————————————————————————————————————————
如果一个模型有多个版本,并在预测的时候希望指定模型的版本,可以通过以下方式实现。
修改model.config文件,增加model_version_policy参数:
model_config_list:{ config:{ name:"model1", base_path:"/models/multiModel/model1", model_platform:"tensorflow", model_version_policy:{ all:{} } }, config:{ name:"model2", base_path:"/models/multiModel/model2", model_platform:"tensorflow" }, }
请求预测的时候,如果要使用版本为00000124的模型,只要修改请求URL为:
URL = 'http://公网ip:8501/v1/models/model1/versions/00000124:predict'
上述容器运行起来之后,如果在宿主机的 /home/node1/model/multiModel/model1/ 文件夹下新增模型文件如00000124/,tfserving会自动加载新模型;同样如果移除现有模型,tfserving也会自动卸载模型。
上述文档参考:https://blog.csdn.net/JerryZhang__/article/details/86516428 感谢~
多模型部署已经复现,模型版本暂时没有这个需求,未复现~