关于部署方面,建议使用官方的docker方式:https://github.com/tensorflow/serving
单模型部署时,
执行命令如下:
docker run -t --rm -p 8501:8501 \
-v "/home/tensorflow_serving/first_test:/models/first_test" \
-e MODEL_NAME=first_test \
tensorflow/serving &
其中,关于/home/tensorflow_serving/first_test的目录结构如下:
first_test
1
assets
variables
fingerprint.pb
keras_metadata.pb
saved_model.pb
2
assets
variables
fingerprint.pb
keras_metadata.pb
saved_model.pb
其中1/2等为对应的版本信息,必须为数字,tensorflow会按照数字大小排序,自动更新为最新版本的模型进行自动化部署
模型请求方面
例如模型代码如下:
import tensorflow as tf
input3 = tf.keras.layers.Input(shape=(1,),name="string_test",dtype=tf.string)
my_string_lookup = tf.keras.layers.StringLookup(vocabulary=["世界","你","good", "d"])(input3)
model = tf.keras.Model(inputs=[input3], outputs=my_string_lookup)
通过tensorflow-serving部署启动后,对应的客户端请求方式为:
方式1-按行请求:
POST --header 'Content-Type: application/json' http://IP:8501/v1/models/first_test:predict
{
"instances": [
{
"string_test": "世界"
}
]
}
方式2:-按列请求:
POST --header 'Content-Type: application/json' http://IP:8501/v1/models/first_test:predict
{
"inputs": {
"string_test": [
"世界",
"你",
"good",
"e"
]
}
}
tf.keras模型部署到tfs
对应的模型保存
,使用tf.keras.models.save_model(model,filepath="my_model_multi"),勿写成tf.keras.models.save_model(model,filepath="my_model_multi.h5")