Tensorflow serving部署模型總結(jié)

首先安裝tfserving,此處選擇比較簡(jiǎn)單的方式,你也可以選擇Bazel編譯方式安裝。(注安裝環(huán)境為Ubuntu,安裝部分參考了此篇文章https://blog.csdn.net/koibiki/article/details/84584975)

1.全局安裝grpcio

sudo pip3 install grpcio

2.安裝依賴庫(kù)

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升級(jí)到指定版本:

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)出模型路徑下會(huì)生成saved_model.pb variables兩個(gè)文件。

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ù)測(cè)結(jié)果。

總結(jié)此篇只是一個(gè)簡(jiǎn)單的參考教程,真正生產(chǎn)環(huán)境中會(huì)有更多考慮,以后有機(jī)會(huì)補(bǔ)充一個(gè)完整的部署。懇請(qǐng)大家批評(píng)指正,也可評(píng)論區(qū)討論更多部署問題。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容