首先安裝tfserving曹铃,此處選擇比較簡單的方式,你也可以選擇Bazel編譯方式安裝捧杉。(注安裝環(huán)境為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的發(fā)行URI添加為package源
# 把Serving的發(fā)行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命令調(diào)用
sudo apt-get update && sudo apt-get install tensorflow-model-server
以后可以通過以下方式把ModelServer升級到指定版本:
sudo apt-get upgrade tensorflow-model-server
5.模型的訓(xùn)練和導(dǎo)出
#編寫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 # 樣本點(diǎn)數(shù)目
x = np.linspace(-1, 1, N)
y = 2.0*x + np.random.standard_normal(x.shape)*0.3+0.5 # 生成線性數(shù)據(jù)
x = x.reshape([N, 1]) # 轉(zhuǎn)換一下格式,準(zhǔn)備feed進(jìn)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))
# 優(yōu)化目標(biāo)函數(shù)
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!')
執(zhí)行python3 train_model.py訓(xùn)練并導(dǎo)出模型评甜,導(dǎo)出模型路徑下會生成saved_model.pb variables兩個文件灰粮。
6.開啟Serving服務(wù)
#mode_name是模型的名字,model_base_path為模型導(dǎo)出路徑
tensorflow_model_server --port=9000 --model_name=test2 --model_base_path=/tmp/test
7.客戶端調(diào)用
#編輯客戶端調(diào)用腳本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)
執(zhí)行python3 test_client.py忍坷,得到預(yù)測結(jié)果粘舟。