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. poi 生成图片到excel

    try { InputStream iss = new FileInputStream("D:\\test.xlsx"); XSSFWorkbook wb = new XSSFWo ...

  2. 尾递归与Continuation(转载)

    递归与尾递归 关于递归操作,相信大家都已经不陌生.简单地说,一个函数直接或间接地调用自身,是为直接或间接递归.例如,我们可以使用递归来计算一个单向链表的长度: public class Node { ...

  3. SAP 创建 component

    1: 进入x3c 系统,输入 T-CODE     BSP_WD_CMPWB 2: 输入以Z开头的组件名. 点击create using wizard 3:  输入应用属性 4: 定义 bol mod ...

  4. 请求&注解

    @RequestMapping("/{page}") //获取路径中的page参数值 public String showPage(@PathVariable String pag ...

  5. yum 安装报错 File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:

    原因: 这是因为yum采用python作为命令解释器,这可以从/usr/bin/yum文件中第一行#!/usr/bin/python发现.而python版本之间兼容性不太好,使得2.X版本与3.0版本 ...

  6. [LeetCode] 872. Leaf-Similar Trees_Easy tag: DFS

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

  7. [LeetCode] 661. Image Smoother_Easy

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  8. VirtualBox 在Win10上的蓝屏问题

    今天也是第一次使用VirtualBox ,因为比VM更轻量,当然主要还是版权,结果装完虚拟机后,每次打开虚拟机就蓝屏,系统报错. 这是出现在WIN10上的问题啊. 解决办法: 找到Control Pa ...

  9. 安装python sklearn经验总结

    1. 需要安装 numpy, scipy, 和sklearn和ipython,Cython sklearn,ipython, Cython都可以通过pip来安装.但scipy,可能还有numpy必须通 ...

  10. linux 编译 'aclocal-1.14' is missing on your system

    centos编译出现:类似情况: $tar -xvf libpcap-1.0.0.tar.gz      $cd libpcap-1.0.0.tar.gz      $./configure      ...