Kubeflow实战系列:利用TensorFlow Serving进行模型预测

2018 年 6 月 24 日 云栖社区

云栖君导读:本系列将介绍如何在阿里云容器服务上运行Kubeflow, 本文介绍如何使用TensorFlow Serving加载训练模型并且进行模型预测。


第一篇:阿里云上使用JupyterHub

http://click.aliyun.com/m/1000003285/


第二篇:阿里云上小试TFJob

http://click.aliyun.com/m/1000003286/


第三篇:利用TFJob运行分布式TensorFlow

http://click.aliyun.com/m/1000003287/


第四篇:利用TFJob导出分布式TensorFlow模型

http://click.aliyun.com/m/1000003288/


第五篇:利用TensorFlow Serving进行模型预测(本文)


TensorFlow Serving简介


TensorFlow Serving是Google开源的一个灵活的、高性能的机器学习模型服务系统,能够简化并加速从模型到生产应用的过程。它除了原生支持TensorFlow模型,还可以扩展支持其他类型的机器学习模型。



在前面的文章中,已经介绍了如何进行单机和分布式的模型训练,并且可以将训练的导出模型放置到分布式存储上。在本文中,会介绍模型如何被发布到TensorFlow Serving系统服务器端。并通过gRPC客户端提交请求,由服务端返回预测结果。



在分布式存储查看训练的模型


在前一篇文章中,我们已经将训练的模型导出到NAS上,可以先查看一下导出的模型。在serving的文件夹指定了模型的,即mnist名称;而mnist的下一层是模型的版本。



在模型导出的章节中,已经在这个NAS存储上创建了对应的pv: tf-serving-pv和pvc: tf-serving-pvc, 而TensorFlow Serving将从pvc中加载模型。



利用Kubeflow启动TensorFlow Serving



部署完成后可以通过kubectl get deploy查询到TensorFlow Serving运行状态



查看TensorFlow Serving运行日志,发现模型已经加载



2018-06-19 06:50:19.185785: 

external/org_tensorflow/tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA


2018-06-19 06:50:19.202907: 

I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:161] Restoring SavedModel bundle.


2018-06-19 06:50:19.418625: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:196] Running LegacyInitOp on SavedModel bundle.


2018-06-19 06:50:19.425357:

 I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:291] SavedModel load for tags { serve }; Status: success. Took 550707 microseconds.


2018-06-19 06:50:19.430435:

 I tensorflow_serving/core/loader_harness.cc:86] Successfully loaded servable version {name: mnist version: 1}


以及对外的暴露的服务ip和端口



这里可以看到gRPC对外服务ip为xx.xx.xx.xx,对外服务的端口为9000



使用gRPC客户端访问TensorFlow Serving


通过kubectl run运行gRPC客户端, 并且点击回车,登录到Pod里


kubectl run -i --tty mnist-client --image=registry.cn-hangzhou.aliyuncs.com/tensorflow-samples/tf-mnist-client-demo --restart=Never --command -- /bin/bash
If you don't see a command prompt, try pressing enter.


运行客户端python代码:


# export TF_MNIST_IMAGE_PATH=1.png

# export TF_MODEL_SERVER_HOST=172.19.4.241

# python mnist_client.py

/usr/local/lib/python2.7/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
 

from ._conv import register_converters as _register_converters


这样我们训练导出的模型,就可以直接通过gRPC的客户端访问了,从而实现在线预测。结合前面的文章,我们已经介绍了从深度学习的模型训练,模型导出到模型部署上线的全路径通路。


删除TensorFlow Serving



总结


这个例子介绍了如何通过Kubeflow部署TensorFlow Serving, 并且加载阿里云NAS上存储的模型,并且提供模型预测服务。


Kubeflow部署机器学习应用非常简单,但是只有应用层的简便是不够的;云端基础设施的自动化集成也是非常重要的,比如GPU/NAS/OSS以及负载均衡的无缝调用,而这方面使用阿里云Kubernetes容器服务可以大幅度降低数据科学家的模型交付难度。


登录查看更多
4

相关内容

高性能TensorFlow模型托管服务器
专知会员服务
78+阅读 · 2020年6月20日
【Google】利用AUTOML实现加速感知神经网络设计
专知会员服务
28+阅读 · 2020年3月5日
TensorFlow Lite指南实战《TensorFlow Lite A primer》,附48页PPT
专知会员服务
68+阅读 · 2020年1月17日
TensorFlow 2.0 学习资源汇总
专知会员服务
66+阅读 · 2019年10月9日
【电子书推荐】Data Science with Python and Dask
专知会员服务
42+阅读 · 2019年6月1日
如何用TF Serving部署TensorFlow模型
AI研习社
26+阅读 · 2019年3月27日
从零开始深度学习第8讲:利用Tensorflow搭建神经网络
基于TensorFlow的深度学习实战
七月在线实验室
9+阅读 · 2018年4月25日
手把手教TensorFlow(附代码)
深度学习世界
15+阅读 · 2017年10月17日
如何用TensorFlow预测时间序列:TFTS库详细教程
人工智能头条
9+阅读 · 2017年8月30日
TensorFlow学习笔记2:构建CNN模型
黑龙江大学自然语言处理实验室
3+阅读 · 2016年6月14日
Arxiv
6+阅读 · 2018年11月1日
A Survey on Deep Transfer Learning
Arxiv
11+阅读 · 2018年8月6日
Bidirectional Attention for SQL Generation
Arxiv
4+阅读 · 2018年6月21日
Arxiv
3+阅读 · 2018年3月2日
Arxiv
3+阅读 · 2015年5月16日
VIP会员
相关VIP内容
相关资讯
如何用TF Serving部署TensorFlow模型
AI研习社
26+阅读 · 2019年3月27日
从零开始深度学习第8讲:利用Tensorflow搭建神经网络
基于TensorFlow的深度学习实战
七月在线实验室
9+阅读 · 2018年4月25日
手把手教TensorFlow(附代码)
深度学习世界
15+阅读 · 2017年10月17日
如何用TensorFlow预测时间序列:TFTS库详细教程
人工智能头条
9+阅读 · 2017年8月30日
TensorFlow学习笔记2:构建CNN模型
黑龙江大学自然语言处理实验室
3+阅读 · 2016年6月14日
相关论文
Arxiv
6+阅读 · 2018年11月1日
A Survey on Deep Transfer Learning
Arxiv
11+阅读 · 2018年8月6日
Bidirectional Attention for SQL Generation
Arxiv
4+阅读 · 2018年6月21日
Arxiv
3+阅读 · 2018年3月2日
Arxiv
3+阅读 · 2015年5月16日
Top
微信扫码咨询专知VIP会员