Tensorflow Serving
機器學(xué)習(xí)技術(shù)支撐著許多 Google 產(chǎn)品的功能,比如:Google 應(yīng)用中的語音識別,收件箱的智能回復(fù),以及Google 照片搜索,等等。盡管軟件行業(yè)幾十年中積累起的無數(shù)經(jīng)驗促成了很多用于構(gòu)建并支持產(chǎn)品的最佳實踐,但基于機器學(xué)習(xí)的服務(wù)卻還是帶來了一些新穎而有趣的挑戰(zhàn)。
TensorFlow Serving 系統(tǒng)非常適用于大規(guī)模運行能夠基于真實情況的數(shù)據(jù)并會發(fā)生動態(tài)改變的多重模型。它給出了一個把模型應(yīng)用到實際生產(chǎn)的解決方案。
它能夠?qū)崿F(xiàn):
模型生命周期管理。
使用多重算法進(jìn)行試驗。
GPU 資源的有效使用。?
TensorFlow Serving 能夠簡化并加速從模型到生產(chǎn)的過程。它能實現(xiàn)在服務(wù)器架構(gòu)和 API 保持不變的情況下,安全地部署新模型并運行試驗。除了原生集成 TensorFlow,還可以擴展服務(wù)其他類型的模型。下圖顯示了簡化的監(jiān)督學(xué)習(xí)過程,向 learner 輸入訓(xùn)練數(shù)據(jù),然后輸出模型:

TensorFlow Serving 使用(之前訓(xùn)練的)模型來實施推理——基于客戶端呈現(xiàn)數(shù)據(jù)的預(yù)測。因為客戶端通常會使用遠(yuǎn)程過程調(diào)用(RPC)接口來與服務(wù)系統(tǒng)通信,TensorFlow Serving 提供了一種基于gRPC的參考型前端實現(xiàn),這是一種 Google 開發(fā)的高性能開源 RPC 架構(gòu)。當(dāng)新數(shù)據(jù)可用或改進(jìn)模型時,加載并迭代模型是很常見的。事實上,在谷歌,許多管線經(jīng)常運行,一旦當(dāng)新數(shù)據(jù)可用時,就會產(chǎn)生新版本的模型。

文章目錄
TensorFlow Serving 日志
一、安裝步驟
1. 使用anconda 安裝python2環(huán)境
2. 安裝Tensorflow環(huán)境
3. 安裝GRCP框架
4. 安裝glags
5. 配置編譯文件
6. 構(gòu)建tensorflow Serving(Build)
7. 運行Tesnroflow serving
8. 測試:
9. 下載tensorflow serving 源代碼
二.遇到的問題以及解決方案(ISSUE)
1. Python Configuration Warning
解決方法
三、Tensorflow serving 基本用法—以MNIST手寫體數(shù)字為例
三、深入理解
1. 模型導(dǎo)出的流程
三、擴展閱讀
一、安裝步驟
在Python3.6編譯tensorflow Serving 的過程中出現(xiàn)錯誤, 經(jīng)過在github 上調(diào)查得知 Tensorflow Seving 目前不支持python3(詳情參見Installing TensorFlow Serving with Python 3 support )。
請結(jié)合?https://www.tensorflow.org/serving/setup
1. 使用anconda 安裝python2環(huán)境
conda create -n python2 python=2.7
source activate python2 # 進(jìn)入環(huán)境
source deactivate python2 # 退出環(huán)境
2. 安裝Tensorflow環(huán)境
pip install tensorflow-gpu==1.3.0 # 之前使用的是1.4 發(fā)現(xiàn)1.4對cudann6不兼容
3. 安裝GRCP框架
python -m pip install grpcio# 安裝grpcio 1.7
4. 安裝glags
這個組件在tensorflow serving 安裝文檔中沒有指明,但是也是必須的。關(guān)于gflags可以參看https://pypi.python.org/pypi/python-gflags
5. 配置編譯文件
cd tensorflow
./configure
cd
6. 構(gòu)建tensorflow Serving(Build)
在編譯之前,務(wù)必指定python path, 否則會導(dǎo)致測試失敗。
bazel build -c opt tensorflow_serving/...
7. 運行Tesnroflow serving
bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server
8. 測試:
bazel test -c opt tensorflow_serving/...
PS:如果測試失敗,請認(rèn)真看錯誤日志文件。如果是cannot import XX package,請檢查你的文件。
9. 下載tensorflow serving 源代碼
git clone --recurse-submodules https://github.com/tensorflow/serving #/home/master/testServing
二.遇到的問題以及解決方案(ISSUE)
1. Python Configuration Warning
這個一定不能忽略,如果你忽略了。會在編譯的過程中遇到以下錯誤:
https://github.com/tensorflow/tensorflow/issues/9866
或者是
(python2) master@ubuntu:~/testServing/serving$ bazel build -c opt tensorflow_serving/...
......................
WARNING: /home/master/.cache/bazel/_bazel_master/181dd246736c24d622c89ae092af50b3/external/bazel_tools/tools/build_defs/pkg/pkg.bzl:196:9: pkg_tar: renaming non-dict `files` attribute to `srcs`.
WARNING: /home/master/.cache/bazel/_bazel_master/181dd246736c24d622c89ae092af50b3/external/org_tensorflow/third_party/py/python_configure.bzl:30:3: Python Configuration Warning: 'PYTHON_LIB_PATH' environment variable is not set, using '/usr/local/lib/python2.7/dist-packages' as default.
INFO: Downloading https://mirror.bazel.build/ufpr.dl.sourceforge.net/project/swig/swig/swig-3.0.8/swig-3.0.8.tar.gz: 728,119 bytes
解決方法
由于是PYTHON_LIB_PATH 沒有設(shè)置。設(shè)置為:
/home/master/.conda/envs/python2/bin/python
export PYTHON_LIB_PATH=/home/master/.conda/envs/python2/lib/python2.7/site-packages
2 其他問題的解決方法:
https://github.com/tensorflow/tensorflow/issues/9866
https://github.com/tensorflow/tensorflow/issues/14218
https://github.com/tensorflow/tensorflow/issues/14182
https://github.com/tensorflow/tensorflow/issues/14273
https://github.com/tensorflow/tensorflow/issues/14182#issuecomment-342927115
三、Tensorflow serving 基本用法—以MNIST手寫體數(shù)字為例
Tensorflow 提供有相應(yīng)的程序片段用于訓(xùn)練并導(dǎo)出模型。
源文件下載:
https://github.com/tensorflow/serving/blob/master/tensorflow_serving/example/mnist_saved_model.py
訓(xùn)練并且導(dǎo)出模型
python tensorflow_serving/example/mnist_saved_model.py /tmp/mnist_model
運行服務(wù):
tensorflow_model_server --port=9000 --model_name=mnist --model_base_path=/tmp/mnist_model/
運行客戶端
python tensorflow_serving/example/mnist_client.py --num_tests=1000 --server=localhost:9000
三、深入理解
1. 模型導(dǎo)出的流程
創(chuàng)建builder對象,并制定保存的路徑
SavedModelBuilder采用了設(shè)計模型的 生成器/建造者模式的思想。提供了一種創(chuàng)建對象的最佳方式。 Builder 類會一步一步構(gòu)造最終的對象。該 Builder 類是獨立于其他對象的。
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
構(gòu)造TensorInfo proto
將模型的名稱、類別、分?jǐn)?shù)統(tǒng)統(tǒng)用tensorInfo封裝。
classification_inputs = tf.saved_model.utils.build_tensor_info(
serialized_tf_example)
其中serialized_tf_example是一個palceholder ,value='tf_example'
依次構(gòu)造classification_outputs_classes、classification_outputs_scores
并使用
tf.saved_model.signature_def_utils.build_signature_def()將模型信息封裝起來。
PS:
Protocol Buffer 是谷歌開發(fā)的一種語言無關(guān)、平臺無關(guān)、可擴展的序列化結(jié)構(gòu)數(shù)據(jù)格式,在 tensorflow 中可以理解為處理結(jié)構(gòu)化數(shù)據(jù)的工具,用于序列化結(jié)構(gòu)化數(shù)據(jù)(訓(xùn)練好的DNN)。
關(guān)于Protocol Buffer的介紹:https://developers.google.com/protocol-buffers/docs/overview
Protocol Buffer的編碼方式:https://developers.google.com/protocol-buffers/docs/encoding
Protocol Bufferdf 定義數(shù)據(jù)格式的文件一般保存在.proto文件中。每一個 message 代表了一類結(jié)構(gòu)化數(shù)據(jù),message 里面定義了每一個屬性的類型和名字,Protocol Buffer 里屬性的類型可以是像布爾型、整數(shù)型、實數(shù)型、字符型這些基本類型,也可以是另一個 message??捎?required(可選的)、required(必須的)、repeated(可重復(fù)的)說明屬性是否可選的、必須的或者可重復(fù)的。
作用與json類似。給出相關(guān)實踐手冊http://blog.csdn.net/tz_zs/article/details/76637667
將構(gòu)造出的模型添加builder中
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:
classification_signature,
},
legacy_init_op=legacy_init_op)
保存模型
builder.save()
總結(jié)
創(chuàng)建builder的是class SaveModelBuilder; 其功能是用來添加SaverModel
protocol buffer 并保存變量和資源,SaverModelBuilder類提供了創(chuàng)建
SaverModel protocol buffer 的函數(shù)方法。
說的在樸素一點:
SaverModel protocol buffer(tf.saved_model.utils.build_tensor_info)是用來構(gòu)造模型的接口的。 add_meta_graph_and_variable 是用來把構(gòu)造出的接口添加到模型中的。
以上是定義接口并導(dǎo)出模型的方式。如下圖所示:
客戶端調(diào)用接口與使用
https://github.com/tensorflow/serving/blob/master/tensorflow_serving/example/mnist_client.py
以上文件給出 Client 來調(diào)用部署好的 Mode 的實例。
調(diào)用過程中需要用到 TensorFlow Serving 的 Predict API 和 gRPC 的 implementations.insecure_channel 來construct 一個 request。特別要注意的是 input 的 signature 和數(shù)據(jù)必須和之前 export 的模型匹配。本例中為 名稱為x1, float32類型,大小為 [28,28] 的 Tensor。模型的輸出只要與之前的output匹配即可。
擴展閱讀
Can python client be independent of tensorflow ?
How to make predictions on TensorFlow's Wide and Deep model loaded in TensorFlow Serving
https://www.oreilly.com/ideas/object-detection-with-tensorflow
https://www.tensorflow.org/serving/serving_advanced
https://zhuanlan.zhihu.com/p/23361413
http://www.cnblogs.com/YouXiangLiThon/p/7435825.html#undefined
如果MNIST 的實例或許有點難以理解,不妨看看這個:
https://www.bittiger.io/blog/post/JpcAqsSP3n39gzFbm
http://blog.csdn.net/tz_zs/article/details/76637667
http://blog.innodatalabs.com/the-newb-guide-to-google-cloud-machine-learning-engine-episode-one/
已經(jīng)部署好的項目:
https://github.com/hetaoaoao/tensorflow_web_deploy
https://github.com/AxelAli/Tensorflow-Image-Classifier-Web-Demo
By 2017年11月17日星期五