参考https://blog.csdn.net/u010175803/article/details/81333583

1.全局安装grpcio

sudo pip3 install grpcio

2.安装依赖库

sudo apt-get update && sudo apt-get install -y automake build-essential curl libcurl3-dev git libtool libfreetype6-dev libpng12-dev libzmq3-dev pkg-config python3-dev python3-numpy python3-pip software-properties-common swig zip zlib1g-dev

3.安装tensorflow-serving-api,

无需Bazel即可运行Python客户端:

pip3 install tensorflow-serving-api

对于Serving,可以安装二进制文件,也可以从源码安装。此处选择前者。 
TensorFlow Serving ModelServer有两个版本,即tensorflow-model-server和tensorflow-model-server-universal,其中前者针对SSE4和AVX之类的指令集进行了优化,但对老机器支持不好。前者不可行,即处理器不支持AVX指令集,则安装后者。

# 把Serving的发行URI添加为package源

# 把Serving的发行URI添加为package源
echo "deb [arch=amd64] http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" | sudo tee /etc/apt/sources.list.d/tensorflow-serving.list
curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | sudo apt-key add -
# 安装更新,之后即可通过tensorflow_model_server命令调用
sudo apt-get update && sudo apt-get install tensorflow-model-server

以后可以通过以下方式把ModelServer升级到指定版本:

sudo apt-get upgrade tensorflow-model-server

4.训练部署模型

从https://github.com/tensorflow/serving下载tensorflow serving的源码,复制 serving/tensorflow_serving/example 文件夹到另一处,运行 mnist_saved_model.py 获得测试用模型

python mnist_saved_model.py --training_iteration=1000 --model_version=1 ./test_model
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================# ! /usr/bin/env python
r"""Train and export a simple Softmax Regression TensorFlow model.The model is from the TensorFlow "MNIST For ML Beginner" tutorial. This program
simply follows all its training instructions, and uses TensorFlow SavedModel to
export the trained model with proper signatures that can be loaded by standard
tensorflow_model_server.Usage: mnist_saved_model.py [--training_iteration=x] [--model_version=y] \export_dir
"""from __future__ import print_functionimport os
import sys# This is a placeholder for a Google-internal import.import tensorflow as tfimport mnist_input_datatf.app.flags.DEFINE_integer('training_iteration', 1000,'number of training iterations.')
tf.app.flags.DEFINE_integer('model_version', 1, 'version number of the model.')
tf.app.flags.DEFINE_string('work_dir', './mnist_data', 'Working directory.')
FLAGS = tf.app.flags.FLAGSdef main(_):if len(sys.argv) < 2 or sys.argv[-1].startswith('-'):print('Usage: mnist_saved_model.py [--training_iteration=x] ''[--model_version=y] export_dir')sys.exit(-1)if FLAGS.training_iteration <= 0:print('Please specify a positive value for training iteration.')sys.exit(-1)if FLAGS.model_version <= 0:print('Please specify a positive value for version number.')sys.exit(-1)# Train modelprint('Training model...')mnist = mnist_input_data.read_data_sets(FLAGS.work_dir, one_hot=True)sess = tf.InteractiveSession()x = tf.placeholder('float', shape=[None, 784])y_ = tf.placeholder('float', shape=[None, 10])w = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))sess.run(tf.global_variables_initializer())y = tf.nn.softmax(tf.matmul(x, w) + b, name='y')cross_entropy = -tf.reduce_sum(y_ * tf.log(y))train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)for _ in range(FLAGS.training_iteration):batch = mnist.train.next_batch(50)train_step.run(feed_dict={x: batch[0], y_: batch[1]})correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))print('training accuracy %g' % sess.run(accuracy, feed_dict={x: mnist.test.images,y_: mnist.test.labels}))print('Done training!')# Export model# WARNING(break-tutorial-inline-code): The following code snippet is# in-lined in tutorials, please update tutorial documents accordingly# whenever code changes.export_path_base = sys.argv[-1]export_path = os.path.join(tf.compat.as_bytes(export_path_base),tf.compat.as_bytes(str(FLAGS.model_version)))print('Exporting trained model to', export_path)builder = tf.saved_model.builder.SavedModelBuilder(export_path)# 生成 输入tensor x 和 输出tensor y的 tensor infotensor_info_x = tf.saved_model.utils.build_tensor_info(x)tensor_info_y = tf.saved_model.utils.build_tensor_info(y)# 生成prediction_signatureprediction_signature = (tf.saved_model.signature_def_utils.build_signature_def(# 客户端request的时候 输入的key 要与设置的相同inputs={'images': tensor_info_x},# 获得的response 结构体中 通过 设置的key('score')字段获得结果outputs={'scores': tensor_info_y},method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))# 定义 metabuilder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.SERVING],signature_def_map={# 用于客户端request时 的 signature_name'predict_images': prediction_signature,},main_op=tf.tables_initializer(),# 向上兼容strip_default_attrs=True)builder.save()print('Done exporting!')if __name__ == '__main__':tf.app.run()

最后一个参数为训练模型保存的地址

运行后 ./test_model 文件夹下目录格式

test_model/
└── 1            ├── saved_model.pb└── variables├── variables.data-00000-of-00001└── variables.index

1是模型的model_version,save_model.pb是一个保存了graph和权重的模型文件

将文件夹 1 复制到 Tensorflow Serving的工作目录  /home/xxx/Serving/model/mnist

开启Serving服务

tensorflow_model_server --port=9000 --model_name=mnist --model_base_path=/home/xxx/Serving/model/mnist

mode_name是模型的名字,调用请求时需要保证request的 ip地址,端口,model_name 与开启的Serving服务一致。

当部署多个模型时,可以让不同模型监听不同端口。

5.调用客户端

运行例子中的 mnist_client.py

python mnist_client.py --num_tests=100 --server=localhost:9000
request = predict_pb2.PredictRequest()
# serving 开启时定义的 model_name
request.model_spec.name = 'mnist'
# model 训练时定义的 signature_def_map
request.model_spec.signature_name = 'predict_images'
image, label = test_data_set.next_batch(1)request.inputs['images'].CopyFrom(tf.contrib.util.make_tensor_proto(image[0], shape=[1, image[0].size]))
result_counter.throttle()
result_future = stub.Predict.future(request, 5.0)  # 5 seconds
result_future.add_done_callback(_create_rpc_callback(label[0], result_counter))

在Ubuntu上安装Tensorflow Serving相关推荐

  1. Ubuntu上安装TensorFlow(python2.7版)

    笔记内容:Ubuntu上安装TensorFlow(python2.7版) 笔记日期:2018-01-31 我的系统环境: Ubuntu 16.04 LTS python 2.7 python 3.5 ...

  2. 通过Anaconda在Ubuntu16.04上安装 TensorFlow(GPU版本)

    一. 安装环境 Ubuntu16.04.3 LST GPU: GeForce GTX1070 Python: 3.5 CUDA Toolkit 8.0 GA1 (Sept 2016) cuDNN v6 ...

  3. 在Ubuntu上安装Keras深度学习框架

    目录 1)安装pip 2)安装Python科学套件 3)安装TensorFlow 4)安装keras 5)安装Jupyter Notebook 6)运行Keras 本文介绍如何在Ubuntu上安装Ke ...

  4. 虚拟机VMware的Ubuntu下安装tensorflow详解

    这里笔者已经安装好了虚拟机并安装了Ubuntu系统,并且虚拟机可以联网,因此就直接开始介绍下面的步骤.网上博文很乱,特意整理,以供参考. 虚拟机VMware的Ubuntu下安装tensorflow详解 ...

  5. linux python2.7 protobuf_在ubuntulinux上安装tensorflow,protobuf版本issu

    在Ubuntu(GCloud VM)上安装tensorflow时,我得到的警告是-pip install tensorflow apachebeam0.6.0要求protobuf==3.2.0,但是p ...

  6. [网摘学习]在Ubuntu上安装和配置OpenStack Nova之二

    再收藏一份Openstack的文章,这两天的操作与此相同.但其中出现的问题还需要查找原因.待个人继续学习研究. 原文参考:http://www.linuxde.net/2011/11/1599.htm ...

  7. 在 Ubuntu 上安装最新版本的 Erlang方法介绍

    Erlang 是一种用于构建大规模可扩展实时系统的函数式编程语言.Erlang 最初是由 爱立信 创建的专有软件,后来被开源. Erlang 在 Ubuntu 的 Universe 仓库 中可用.启用 ...

  8. JetsonTX2上安装tensorflow的心酸史

    JetsonTX2上安装tensorflow的心酸史 还是那句话,做事情得有耐心,有耐心-耐心-.心--感觉像是给自己的一个心理暗示- -.-||| tensorflow安装 常见问题总结 验证 te ...

  9. 在Ubuntu上安装Odoo 11(企业版)

    2019独角兽企业重金招聘Python工程师标准>>> 在Ubuntu上安装Odoo 11(企业版) 2017年10月8日YENTHE666 在本教程中,我将学习如何在Ubuntu ...

  10. 在 Mac OS X 上安装 TensorFlow

    在 Mac OS X 上安装 TensorFlow 这个文档说明了如何在 Mac OS X 上安装 TensorFlow. 注意:从 1.2 版本开始,在 Mac OS X 上 TensorFlow ...

最新文章

  1. 2022-2028年中国阻尼涂料市场研究及前瞻分析报告
  2. tensorflow 协同过滤_基于django和协同过滤/cnn的电影推荐系统
  3. 一文看懂MYCAT数据库服务器!
  4. android studio 怎么做屏幕适配?
  5. SQLServer 实现rownum 的功能
  6. Microsoft.Office.Interop.Excel的用法
  7. 标签内超出的文字显示省略号
  8. 线性代数相关知识点回顾
  9. mysql下载安装及配置_mysql的下载,安装和配置
  10. 学习手记(2019/7/05~2019/8/31)——快乐暑假
  11. 基于BISS0001构成的热释电红外延时照明控制器电路图
  12. 转:Delphi2010新发现-类的构造和析构函数功能
  13. 阿里巴巴fastjson @JSONField 注解说明
  14. TensorFlow入门(1)
  15. eclipse 导入appcompat-v7 包
  16. SHELL下把一个文件附加到另外一个文件,注意编码问题
  17. 学php应该怎么学习数学,数学难学,数学到底该怎么学?
  18. 解决Ubuntu 20.04挂载NTFS分区不能写入(只读权限)的问题
  19. 开发一个标题为Flipflop的游戏应用程序
  20. Ubuntu装系统后重启卡死解决办法(超详细)

热门文章

  1. rust+mysql+prep+exec_Rust + Flutter 高性能的跨端尝试
  2. Java基于JSP+Servlet的校友论坛管理系统
  3. 对抗攻击(Adversarial Attack)
  4. js 手机虚拟键盘控制
  5. java连连看开题报告_JAVA连连看
  6. 纯java写2D格斗游戏(一)——界面背景设置及人物的简单设置
  7. Chia命令行P图工具
  8. 反问疑问_反问、疑问还是设问?
  9. 《工业控制系统信息安全防护指南》产品措施匹配表
  10. 即将毕业大学生的第一个五年计划