1.安装tensorflow serving

1.1确保当前环境已经安装并可运行tensorflow

从github上下载源码

git clone --recurse-submodules https://github.com/tensorflow/serving

进入到serving目录下的tensorflow运行./configure,并安装步骤完成(需将 2问题解决的的步骤全操作完后执行安装步骤)

1.2.编译example代码

bazel build tensorflow_serving/example/...

1.3.运行mnist例子导出model到/tmp/mnist_export目录下,目录下会根据export_version创建一个目录名为 /tmp/mnist_export/00000001

rm -rf /tmp/mnist_export/(第一次执行不存在,不必操作)
 
bazel-bin/tensorflow_serving/example/mnist_export --training_iteration=10000 --export_version=1 /tmp/mnist_export
 
Training model...
('Extracting''/tmp/train-images-idx3-ubyte.gz')
('Extracting''/tmp/train-labels-idx1-ubyte.gz')
('Extracting''/tmp/t10k-images-idx3-ubyte.gz')
('Extracting''/tmp/t10k-labels-idx1-ubyte.gz')
training accuracy 0.9219
Done training!
Exporting trained model to /tmp/mnist_export
Done exporting!

1.4 执行inference开启服务,端口9000,目录指向之前导出的目录

bazel-bin/tensorflow_serving/example/mnist_inference --port=9000 /tmp/mnist_export/00000001
I tensorflow_serving/session_bundle/session_bundle.cc:130] Attempting to load a SessionBundle from: /tmp/mnist_export/00000001
I tensorflow_serving/session_bundle/session_bundle.cc:107] Running restore op for SessionBundle
I tensorflow_serving/session_bundle/session_bundle.cc:178] Done loading SessionBundle
I tensorflow_serving/example/mnist_inference.cc:163] Running...

1.6 执行测试client

bazel-bin/tensorflow_serving/example/mnist_client --num_tests=1000 --server=localhost:9000
 
('Extracting''/tmp/train-images-idx3-ubyte.gz')
('Extracting''/tmp/train-labels-idx1-ubyte.gz')
('Extracting''/tmp/t10k-images-idx3-ubyte.gz')
('Extracting''/tmp/t10k-labels-idx1-ubyte.gz')
........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Inference error rate: 9.2%

2.问题解决

no such package '@boringssl_git//': Error cloning repository:https://boringssl.googlesource.com/boringssl: cannot open git-upload-pack and referenced by '//external:libssl'.

由于GFW把google的很多地址给墙了,所以无法下载相关的内容,修改seving目录下的tensorflow/tensorflow/workspace.bzl 文件相关的repository

git_repository(
name = "boringssl_git",
#commit = "436432d849b83ab90f18773e4ae1c7a8f148f48d",
commit = "db0729054d5964feab9e60089ba2d06a181e78b1",
init_submodules = True,
)

https://github.com/tensorflow/serving/issues/6

运行mnist_client时报错

Traceback (most recent call last):
File "/root/tensorflow-serving/bazel-bin/tensorflow_serving/example/mnist_client.runfiles/__main__/tensorflow_serving/example/mnist_client.py", line 34, in <module>
from grpc.beta import implementations
ImportError: No module named grpc.beta

使用pip安装grpcio模块

pip install grpcio

https://github.com/grpc/grpc/tree/master/src/python/grpcio

export过程报错缺少 manifest_pb2.py 的解决方法:

首先编译serving下的example目录得到

bazel-bin/tensorflow_serving/example/mnist_export.runfiles/org_tensorflow/tensorflow/contrib/session_bundle/manifest_pb2.py

随后copy到python主目录下的lib目录 例如:/usr/lib/python2.7/site-packages/

如果还报相同错误

修改 /usr/lib/python2.7/site-packages/tensorflow/contrib/session_bundle/目录下的exporter.py文件

删除 from tensorflow.contrib.session_bundle import manifest_pb2

增加 import manifest_pb2

解决思路:主要就是PYTHONPATH中缺少manifest_pb2.py,所以需要设置,设置路径可以找到manifest_pb2.py即可

manifest_pb2.py为tensorflow/contrib/session_bundle/manifest.proto 的protobuf生成文件,若有条件可手动生成

3.参考地址

tensorflow serving github : https://github.com/tensorflow/serving

https://github.com/tensorflow/serving/blob/master/tensorflow_serving/g3doc/setup.md

bazel http://www.bazel.io/ (需FQ)

4.Serving Framework

4.1.Train

训练模型的过程

4.2.exporter

负责将训练好的模型导出

4.3.Sever

负责存储操作,例如将对象存储到磁盘

4.4.Server

提供grpc server,组织request调用Module,将结果response client

4.5.ModuleManager

负责加载训练好的模型

4.6.Scheduler

负责请求的调度,例如BatchScheduler(buffer 某一批数据才发给Service)

4.7.client

负责发送Request请求接收Response

5.如何编写Serving

5.1 export model

模型训练完成后,需要export model

1) 需要确定 signature : (classification_signature,regression_signature,generic_signature)

classification_signature: input , classes , scores

regression_signature: input , output

generic_signature: map<string,tensor_name>

signature规定了输入和输出的tensor_name, 这个tensor_name应该对应到graph里的tensor

例如 mnist 为classification模型 训练输入了 x 训练出 y 则在export的时候使用:

signature = exporter.classification_signature(input_tensor=x, scores_tensor=y)

5.2 确定输出的路径

导出model需要一个可存储的路径,这个路径会在inference程序读取时使用

5.3 编写inference

inference主要流程:

1)获得    SessionBundle

std::unique_ptr<SessionBundleFactory> bundle_factory;
 
TF_QCHECK_OK(
 
SessionBundleFactory::Create(session_bundle_config, &bundle_factory));
 
std::unique_ptr<SessionBundle> bundle(new SessionBundle);
 
TF_QCHECK_OK(bundle_factory->CreateSessionBundle(bundle_path, &bundle));

2)  提供输入与输出的tensor

Tensor input(tensorflow::DT_FLOAT, {1, kImageDataSize});
 
    std::copy_n(request->image_data().begin(), kImageDataSize,
 
    input.flat<float>().data());
 
std::vector<Tensor> outputs;

3)  通过signatrue传入input,output到session_bundle并执行

const tensorflow::Status status = bundle_->session->Run(
 
        {{signature_.input().tensor_name(), input}},
 
        {signature_.scores().tensor_name()}, {}, &outputs);
 

tensorflow serving的更多相关文章

  1. Tensorflow serving的编译

    Tensorflow serving提供了部署tensorflow生成的模型给线上服务的方法,包括模型的export,load等等. 安装参考这个 https://github.com/tensorf ...

  2. 谷歌发布 TensorFlow Serving

    TensorFlow服务是一个灵活的,高性能的机器学习模型的服务系统,专为生产环境而设计. TensorFlow服务可以轻松部署新的算法和实验,同时保持相同的服务器体系结构和API. TensorFl ...

  3. 学习笔记TF067:TensorFlow Serving、Flod、计算加速,机器学习评测体系,公开数据集

    TensorFlow Serving https://tensorflow.github.io/serving/ . 生产环境灵活.高性能机器学习模型服务系统.适合基于实际数据大规模运行,产生多个模型 ...

  4. tensorflow 模型保存与加载 和TensorFlow serving + grpc + docker项目部署

    TensorFlow 模型保存与加载 TensorFlow中总共有两种保存和加载模型的方法.第一种是利用 tf.train.Saver() 来保存,第二种就是利用 SavedModel 来保存模型,接 ...

  5. 通过Docker构建TensorFlow Serving

    最近在用Docker搭建TensorFlow Serving, 在查阅了官方资料后,发现其文档内有不少冗余的步骤,便一步步排查,终于找到了更简单的Docker镜像构建方法.这里有两种方式: 版本一: ...

  6. tensorflow serving 之minist_saved_model.py解读

    最近在学习tensorflow serving,但是就这样平淡看代码可能觉得不能真正思考,就想着写个文章看看,自己写给自己的,就像自己对着镜子演讲一样,写个文章也像自己给自己讲课,这样思考的比较深,学 ...

  7. tensorflow serving 中 No module named tensorflow_serving.apis,找不到predict_pb2问题

    最近在学习tensorflow serving,但是运行官网例子,不使用bazel时,发现运行mnist_client.py的时候出错, 在api文件中也没找到predict_pb2,因此,后面在网上 ...

  8. Tensorflow Serving 模型部署和服务

    http://blog.csdn.net/wangjian1204/article/details/68928656 本文转载自:https://zhuanlan.zhihu.com/p/233614 ...

  9. tensorflow serving 编写配置文件platform_config_file的方法

    1.安装grpc gRPC 的安装: $ pip install grpcio 安装 ProtoBuf 相关的 python 依赖库: $ pip install protobuf 安装 python ...

随机推荐

  1. what's the python之模块

    正则表达式 首先,我们引入了正则表达式的知识.所谓正则表达式,就是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对 ...

  2. dxRibbonRadialMenu控件使用

    设计视图 双击dxRibbonRadialMenu1增加项目 增改显示文字 增加图标列表 代码很简单,从当前鼠标位置打开,屏蔽系统右键 procedure TForm1.cxMemo1ContextP ...

  3. (转)Mysql LIMIT如何正确对其进行优化

    以下的文章主要是对Mysql LIMIT简单介绍,我们大家都知道LIMIT子句一般是用来限制SELECT语句返回的实际行数.LIMIT取1个或是2个数字参数,如果给定的是2个参数,第一个指定要返回的第 ...

  4. 2018-2019-1 20189221《Linux内核原理与分析》第三周作业

    2018-2019-1 20189221<Linux内核原理与分析>第三周作业 实验二 完成一个简单的时间片轮转多道程序内核代码 实验过程 在实验楼中编译内核 编写mymain.c函数和m ...

  5. PHP获取访客ip、系统、浏览器等信息[转]

    1.获取访客操作系统信息   <?php   function GetOs() {   if (!empty($_SERVER['HTTP_USER_AGENT'])) {   $OS = $_ ...

  6. SVM、LR、决策树的对比

    一.LR LR,DT,SVM都有自身的特性,首先来看一下LR,工业界最受青睐的机器学习算法,训练.预测的高效性能以及算法容易实现使其能轻松适应工业界的需求.LR还有个非常方便实用的额外功能就是它并不会 ...

  7. linux shell脚本检测硬盘磁盘空间 邮件报警

    使用 http://www.weiruoyu.cn/?p=368 shell脚本监控硬盘空间剩余空间 邮件报警 1.先观察一下磁盘,和如何使用脚本 [root@localhost ~]# df -h ...

  8. cocos2dx 3.x(绘制线条)

    // //  MainScene.hpp //  helloworld // //  Created by apple on 16/9/19. // // #ifndef MainScene_hpp ...

  9. node.js初识06

    node中的fs文件系统 var http = require("http"); var fs = require("fs"); var server = ht ...

  10. python基础-abstractmethod、__属性、property、setter、deleter、classmethod、staticmethod

    python基础-abstractmethod.__属性.property.setter.deleter.classmethod.staticmethod