首先安装tfserving,此处选择比较简单的方式,你也可以选择Bazel编译方式安装。(注安装环境为Ubuntu,安装部分参考了此篇文章https://blog.csdn.net/koibiki/article/details/84584975)
1.全局安装grpcio
sudo pip3 install grpcio
2.安装依赖库
sudo apt-get update && sudo apt-get install -y automake build-essential curl libcurl3-dev git libtool libfreetype6-dev libpng12-dev libzmq3-dev pkg-config python3-dev python3-numpy python3-pip software-properties-common swig zip zlib1g-dev
3.安装tensorflow-serving-api
pip3 install tensorflow-serving-api
4.把Serving的发行URI添加为package源
# 把Serving的发行URI添加为package源
echo "deb [arch=amd64] http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" | sudo tee /etc/apt/sources.list.d/tensorflow-serving.list
curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | sudo apt-key add -
# 安装更新,之后即可通过tensorflow_model_server命令调用
sudo apt-get update && sudo apt-get install tensorflow-model-server
以后可以通过以下方式把ModelServer升级到指定版本:
sudo apt-get upgrade tensorflow-model-server
5.模型的训练和导出
#编写train_model.py脚本
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
import os
tf.app.flags.DEFINE_integer('model_version', 1, 'version number of the model.')
FLAGS = tf.app.flags.FLAGS
N = 200 # 样本点数目
x = np.linspace(-1, 1, N)
y = 2.0*x + np.random.standard_normal(x.shape)*0.3+0.5 # 生成线性数据
x = x.reshape([N, 1]) # 转换一下格式,准备feed进placeholder
y = y.reshape([N, 1])
# 建图
graph = tf.Graph()
with graph.as_default():
inputx = tf.placeholder(dtype=tf.float32, shape=[None, 1], name="inputx")
inputy = tf.placeholder(dtype=tf.float32, shape=[None, 1],name="inputy")
W = tf.Variable(tf.random_normal([1, 1], stddev=0.01))
b = tf.Variable(tf.random_normal([1], stddev=0.01))
pred = tf.matmul(inputx, W)+b
loss = tf.reduce_sum(tf.pow(pred-inputy, 2))
# 优化目标函数
train = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
# 初始化所有变量
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session(graph=graph) as sess:
sess.run(init)
for i in range(20):
sess.run(train,feed_dict={inputx:x, inputy:y})
predArr, lossArr = sess.run([pred, loss], feed_dict={inputx:x, inputy:y})
print(lossArr)
export_path_base = os.path.join('/tmp','test')
export_path = os.path.join(
tf.compat.as_bytes(export_path_base),
tf.compat.as_bytes(str(FLAGS.model_version)))
print ('Exporting trained model to', export_path)
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
tensor_info_x = tf.saved_model.utils.build_tensor_info(inputx) # 输入
tensor_info_pre = tf.saved_model.utils.build_tensor_info(pred)
prediction_signature = (
tf.saved_model.signature_def_utils.build_signature_def(
inputs={'inputx': tensor_info_x},
outputs={'pred': tensor_info_pre},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
builder.add_meta_graph_and_variables(
sess, [tf.saved_model.tag_constants.SERVING],
signature_def_map={
'predict_images':
prediction_signature,
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
prediction_signature,
},
legacy_init_op=legacy_init_op)
builder.save()
print('Done exporting!')
执行python3 train_model.py训练并导出模型,导出模型路径下会生成saved_model.pb variables两个文件。
6.开启Serving服务
#mode_name是模型的名字,model_base_path为模型导出路径
tensorflow_model_server --port=9000 --model_name=test2 --model_base_path=/tmp/test
7.客户端调用
#编辑客户端调用脚本test_client.py
# -*- coding: utf-8 -*-
from grpc.beta import implementations
import numpy as np
import tensorflow as tf
import os
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
tf.reset_default_graph()
tf.app.flags.DEFINE_string('server', 'localhost:9000',
'PredictionService host:port')
FLAGS = tf.app.flags.FLAGS
N = 200
x = np.linspace(-1, 1, N)
#y = 2.0*x + np.random.standard_normal(x.shape)*0.3+0.5
x = x.reshape([N, 1])
#y = y.reshape([N, 1])
host, port = FLAGS.server.split(':')
channel = implementations.insecure_channel(host, int(port))
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = 'test2'
request.model_spec.signature_name = 'predict_images'
request.inputs['inputx'].CopyFrom(tf.contrib.util.make_tensor_proto(x, shape=[200, 1], dtype=tf.float32))
#request.outputs['inputy'].CopyFrom(tf.contrib.util.make_tensor_proto(y, shape=[200, 1], dtype=tf.float32))
#.SerializeToString()
result = stub.Predict(request, 10.0) # 10 secs timeout
print (result)
执行python3 test_client.py,得到预测结果。