搜索
您的当前位置:首页正文

TensorFlow Serving 日志

来源:二三四教育网

Tensorflow Serving

TensorFlow Serving 系统非常适用于大规模运行能够基于真实情况的数据并会发生动态改变的多重模型。它给出了一个把模型应用到实际生产的解决方案

它能够实现:

模型生命周期管理。

使用多重算法进行试验。

GPU 资源的有效使用。 

文章目录

TensorFlow Serving 日志

一、安装步骤

1. 使用anconda 安装python2环境

2. 安装Tensorflow环境

3. 安装GRCP框架

4. 安装glags

5. 配置编译文件

6. 构建tensorflow Serving(Build)

7. 运行Tesnroflow serving

8. 测试:

9. 下载tensorflow serving 源代码

二.遇到的问题以及解决方案(ISSUE)

1. Python Configuration Warning

解决方法

三、Tensorflow serving 基本用法—以MNIST手写体数字为例

三、深入理解

1. 模型导出的流程

三、扩展阅读

一、安装步骤

1. 使用anconda 安装python2环境

conda create -n python2 python=2.7

source activate python2 # 进入环境

source deactivate python2 # 退出环境

2. 安装Tensorflow环境

pip install tensorflow-gpu==1.3.0 # 之前使用的是1.4 发现1.4对cudann6不兼容

3. 安装GRCP框架

python -m pip install grpcio# 安装grpcio 1.7

4. 安装glags

5. 配置编译文件

cd tensorflow

./configure

cd

6. 构建tensorflow Serving(Build)

在编译之前,务必指定python path, 否则会导致测试失败。

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:如果测试失败,请认真看错误日志文件。如果是cannot import XX package,请检查你的文件。

9. 下载tensorflow serving 源代码

二.遇到的问题以及解决方案(ISSUE)

1. Python Configuration Warning

这个一定不能忽略,如果你忽略了。会在编译的过程中遇到以下错误:

或者是

(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.

解决方法

由于是PYTHON_LIB_PATH 没有设置。设置为:

/home/master/.conda/envs/python2/bin/python

export PYTHON_LIB_PATH=/home/master/.conda/envs/python2/lib/python2.7/site-packages

2 其他问题的解决方法:

三、Tensorflow serving 基本用法—以MNIST手写体数字为例

Tensorflow 提供有相应的程序片段用于训练并导出模型。

源文件下载:

训练并且导出模型

python tensorflow_serving/example/mnist_saved_model.py /tmp/mnist_model

运行服务:

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. 模型导出的流程

创建builder对象,并制定保存的路径

SavedModelBuilder采用了设计模型的 生成器/建造者模式的思想。提供了一种创建对象的最佳方式。 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。

builder = tf.saved_model.builder.SavedModelBuilder(export_path)

构造TensorInfo proto

将模型的名称、类别、分数统统用tensorInfo封装。

classification_inputs = tf.saved_model.utils.build_tensor_info(

serialized_tf_example)

其中serialized_tf_example是一个palceholder ,value='tf_example'

依次构造classification_outputs_classes、classification_outputs_scores

并使用

tf.saved_model.signature_def_utils.build_signature_def()将模型信息封装起来。

PS:

Protocol Buffer 是谷歌开发的一种语言无关、平台无关、可扩展的序列化结构数据格式,在 tensorflow 中可以理解为处理结构化数据的工具,用于序列化结构化数据(训练好的DNN)。

Protocol Bufferdf 定义数据格式的文件一般保存在.proto文件中。每一个 message 代表了一类结构化数据,message 里面定义了每一个属性的类型和名字,Protocol Buffer 里属性的类型可以是像布尔型、整数型、实数型、字符型这些基本类型,也可以是另一个 message。可用 required(可选的)、required(必须的)、repeated(可重复的)说明属性是否可选的、必须的或者可重复的。

将构造出的模型添加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()

总结

创建builder的是class SaveModelBuilder; 其功能是用来添加SaverModel

protocol buffer 并保存变量和资源,SaverModelBuilder类提供了创建

SaverModel protocol buffer 的函数方法。

说的在朴素一点:

SaverModel protocol buffer(tf.saved_model.utils.build_tensor_info)是用来构造模型的接口的。 add_meta_graph_and_variable 是用来把构造出的接口添加到模型中的。

以上是定义接口并导出模型的方式。如下图所示:

客户端调用接口与使用

以上文件给出 Client 来调用部署好的 Mode 的实例。

调用过程中需要用到 TensorFlow Serving 的 Predict API 和 gRPC 的 implementations.insecure_channel 来construct 一个 request。特别要注意的是 input 的 signature 和数据必须和之前 export 的模型匹配。本例中为 名称为x1, float32类型,大小为 [28,28] 的 Tensor。模型的输出只要与之前的output匹配即可。

扩展阅读

如果MNIST 的实例或许有点难以理解,不妨看看这个:

已经部署好的项目:

By 2017年11月17日星期五

Top