部署Tensorflow模型
保存模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # 指定模型的version
version = 1
# 指定路径
export_path = "../model/model01/{}".format(version)
print('export_path = {}'.format(export_path))
tf.keras.models.save_model(
model,
export_path,
overwrite=True,
include_optimizer=True,
save_format=None,
signatures=None,
options=None
)
|
保存模型目录结构
1
2
3
4
5
6
7
8
9
10
| model_path
└── foo_model
└── 1
├── assets
├── saved_model.pb
└── variables
├── variables.data-00000-of-00001
└── variables.index
4 directories, 3 files
|
部署模型
docker
下载镜像
docker pull tensorflow/serving
运行
1
2
| # MODEL_NAME就是保存模型的时候用的model_name
docker run -it --rm -p 8501:8501 -v "/Users/li/Workspace/py3-labs/lab024/model_path:/models" -e MODEL_NAME=foo_model tensorflow/serving
|
调用模型
url中要试用到之前保存模型的时候定义的model_name
postman
curl
1
2
3
4
5
6
7
8
9
10
11
| curl --location --request POST 'http://localhost:8501/v1/models/foo_model:predict' \
--header 'Content-Type: application/json' \
--data-raw '{
"instances": [
[
230.1,
37.8,
69.2
]
]
}'
|
返回
1
2
3
4
5
6
7
| {
"predictions": [
[
22.6255722
]
]
}
|
参考资料