1.在此记录下测试步骤,方便以后查询,tensorflow serving可以解决模型生产部署问题,在使用之前先安装docker,参考官方文档安装即可,如果需要使用gpu的话,还得安装nvidia-docker,且tensorflow serving的镜像得是对应cuda的版本,例如tensorflow serving:1.12.0-gpu对应最高的cuda版本是9,cudnn版本为7;
2.docker下拉tensorflow serving镜像,因为使用的tensorflow的版本是1.12.0,所以下拉了对应版本的镜像;
docker pull tensorflow/serving:1.12.0
3.tensorflow serving使用的模型格式是特定的save_model格式,效果如下。
import tensorflow as tf
def save_model():
v1 = tf.Variable(1, name='v1')
v2 =tf.Variable(2, name='v2')
v3 = tf.placeholder(tf.int32, name='input')
y = tf.multiply(tf.add(v1, v2),v3,name='output')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(y, feed_dict={v3: 3}))
model_path = './model/saved_model_builder/1/'
builder = tf.saved_model.builder.SavedModelBuilder(model_path)
inputs = {'input': tf.saved_model.utils.build_tensor_info(v3)}
outputs = {'output': tf.saved_model.utils.build_tensor_info(y)}
method_name = tf.saved_model.signature_constants.PREDICT_METHOD_NAME
prediction_signature = tf.saved_model.signature_def