一、前言
工作中,有时需要线下验证训练好的模型,是否能在线上serving成功,所以需要利用docker来简单部署tensorflow-serving,然后线下进行验证模型能否成serving,避免出现模型过大,tensorflow-serving版本不对等情况,导致serving失败。
二、Docker部署tf-serving步骤
1、Docker安装(详见Docker简单入门)
2、拉取tensorflow-serving镜像
tensorflow-serving是向下兼容的,所以可拉取最近的
#默认是latest 其中registry.docker-cn.com为官方中国区镜像加速
docker pull registry.docker-cn.com/tensorflow/serving
3、准备好模型(pb格式),如下图
放置某目录,如: /tfserving/tf15img/100/ (这里需要注意,pb模型上级目录必须是个版本号,也就是说,上级目录名称必须是数字,如:这里是100)
4、利用上面下载好的镜像启动tf_serving服务
#输入该命令
docker run -p 8501:8501 --mount type=bind,source=/tfserving/tf15img/,target=/models/ctrcvr -e MODEL_NAME=ctrcvr -t tensorflow/serving
-p: 指定主机到docker容器的端口映射
--mount: 表示要进行挂载
source: 要部署的模型地址
target: 模型挂载到docker容器中的目录
-t: 指定挂载到目标容器
-e: 指定模型名称
docker run: 启动这个容器并启动模型服务
启动成功效果:
5、调用,另开一个shell窗口,输入下面命令:
curl -X POST \
http://localhost:8501/v1/models/ctrcvr:predict \
-d '{
"signature_name": "serving_default",
"instances":[
{
"device_t": ["ios"],
"device_b": ["apple"],
"device_m": ["iphone_se"],
"network": ["1"],
"system": ["ios"],
"city_id": ["5350937"],
"goodsid_clicks": ["653389", "675647", "705347", "525684", "773155"],
"goodsid_collects": ["653389"],
"goodsid_carts": ["653389"],
"content": ["cases", "iphone", "xs", "max", "", ""]
}]}'
6、最后输出的结果
"predictions": [[0.00267601665, 0.145611033, -0.0924021602, -0.0186253022, -0.13342154, 0.208785236, -0.112708218, 0.151671633, -0.0421785899, 0.0956602693, -0.0120493015, 0.136680216, -0.191573888, -0.141872212, -0.118211851, 0.0447497517, -0.115030959, -0.0443719514, -0.111011751, 0.139456689, 0.0404875278, 0.0229826104, 0.0780395493, -0.00982834585, -0.0615824722, -0.35205093, -0.0789170116, -0.0734161586, 0.0273077711, 0.21346961, 0.142884254, -0.0829004869, -0.145836487, -0.0888566896, 0.0892312154, 0.0952859297, 0.00696363347, -0.152534887, -0.213381305, 0.00924424734, 0.101483539, -0.0553223528, -0.0186863281, -0.0176770855, -0.0150718167, -0.182791546, -0.0509930961, -0.222394854, 0.153857246, -0.0752918795, 0.126283228, -0.170853496, -0.21987386, 0.11312595, -0.171711594, 0.0305050053, -0.117043316, 0.0611188486, 0.189837858, -0.00537029793, -0.112312466, -0.039727509, -0.0315540843, -0.228862554]]
7、成功,完结