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. api-gateway-engine知识点(2)

    GroupVersion实现engine本地缓存 package com.inspur.cloud.apigw.engine.cache; import java.util.Map;import ja ...

  2. (转)从拜占庭将军问题谈谈为什么pow是目前最好的共识机制

    我们知道基于区块链技术现在有很多的共识机制,包括不限于POW,POS,DPOS,PBFT……,我先不说为什么我最认可POW,我们先来看看著名的拜占庭将军问题: 拜占庭帝国即中世纪的土耳其,拥有巨大的财 ...

  3. Python 全栈开发九 日志模块

    日志是一种可以追踪某些软件运行时所发生事件的方法.软件开发人员可以向他们的代码中调用日志记录相关的方法来表明发生了某些事情.一个事件可以用一个可包含可选变量数据的消息来描述.此外,事件也有重要性的概念 ...

  4. Python 初始函数

    python之路——初识函数   阅读目录 为什么要用函数 函数的定义与调用 函数的返回值 函数的参数 本章小结 为什么要用函数 我们就想啊,要是我们能像使用len一样使用我们这一大段“计算长度”的代 ...

  5. Sql注入基础原理介绍

    说明:文章所有内容均截选自实验楼教程[Sql注入基础原理介绍]~ 实验原理 Sql 注入攻击是通过将恶意的 Sql 查询或添加语句插入到应用的输入参数中,再在后台 Sql 服务器上解析执行进行的攻击, ...

  6. 要求根据RandomStr.java:使用类型转换生成六位验证字符串,示例程序每次运 行时,都会生成不同的字符串。

    1.程序设计思想验证码 ①定义一个字符串变量来保存随机生成的. ②利用循环产生六位随机数,在产生每一位时将其转换为char类型并写在字符串后面. ③利用对话框显示生成的验证码,并提示用户输入验证码. ...

  7. iOS 开发笔记 cocoapods 配置遇到的问题

    当使用svn的时候,每次使用pods update,都会出现一个问题,原来所有的第三方类库里面的.svn都被删除了.提交上svn服务器时,会要求提交全部.如果只是提交如MJExtension这个类库, ...

  8. StackExchange.Redis在net中使用

    redis 官网https://redis.io redis 下载  进入下载页面  https://redis.io/download https://github.com/MicrosoftArc ...

  9. Visual Studio 2015 开发Android Cordova出现unsupported major minor version 52.0错误的解决方法

    JDK版本的问题,需要JDK1.8版本,安装!VS2015做如下设置, 工具->选项->用于Apache Cordoba的工具->环境变量替代->JAVA_HOME设为1.8:

  10. 实验:记录一则删除GI的过程

    环境: RHEL 6.5 + Oracle GI 11.2.0.4 (2 nodes) 参考MOS文档 How to Deconfigure/Reconfigure(Rebuild OCR) or D ...