前提:要實現多模型部署,首先要了解並且熟練實現單模型部署,可以借助官網文檔,使用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 感謝~
多模型部署已經復現,模型版本暫時沒有這個需求,未復現~