教程帖:深度学习模型的部署

2018 年 1 月 20 日 论智 Bing
来源:the Hive
编译:Bing


编者按:训练好一个机器学习模型后,下一步该做什么?数据科学公司Hive又在他们的博客上发布了一篇教程,一步步教你部署深度学习模型。以下是论智对原文的编译。

如果你成功地用TensorFlow或Caffe训练了一个机器学习模型,并且喜欢更简单的解决方案,那就请读下去吧!本文将教你如何将训练好的机器学习模型部署到实际工作中去。

  • 检查TensorFlow的安装

    地址:https://github.com/hiveml/simple-ml-serving/blob/master/test/test_tensorflow.sh

  • 从stdin进行在线分类

    地址:https://github.com/hiveml/simple-ml-serving/blob/master/test/test_label_image.sh

  • 在localhost进行在线分类

    地址:https://github.com/hiveml/simple-ml-serving/blob/master/test/test_tf_classify_server.sh

  • 将分类器放置在硬编码代理器之后(hardcoded proxy)

    地址:https://github.com/hiveml/simple-ml-serving/blob/master/test/test_basic_proxy.sh

  • 将分类器放在带有服务发现的代理器之后

    地址:https://github.com/hiveml/simple-ml-serving/blob/master/test/test_seaport_proxy.sh

  • 用伪DNS运行分类器

    地址:https://github.com/hiveml/simple-ml-serving/blob/master/test/test_p2p_proxy.sh

实际中的机器学习

Hive第一次进入机器学习领域时,我们就已经有了百万张带有标准标签的图片,能让我们用不到一周的时间从零开始训练最先进的深度卷积图片分类模型。不过,一般的机器学习案例通常只需数百幅图像,为此我们建议微调现有的模型。例如,www.tensorflow.org/tutorials/image_retraining网站有关于如何微调ImageNet模型(在120万张图片中训练,共有1000个类别),以分类花的样本数据集(3647张图片,5个类别)。

安装好TensorFlow和框架后,你将要运行以下代码。创建时间大约要花30分钟,训练需要5分钟:

  
    
    
    
  1. (

  2. cd "$HOME" && \

  3. curl -O http://download.tensorflow.org/example_images/flower_photos.tgz && \

  4. tar xzf flower_photos.tgz ;

  5. ) && \

  6. bazel build tensorflow/examples/image_retraining:retrain \

  7.          tensorflow/examples/image_retraining:label_image \

  8. && \

  9. bazel-bin/tensorflow/examples/image_retraining/retrain \

  10.  --image_dir "$HOME"/flower_photos \

  11.  --how_many_training_steps=200

  12. && \

  13. bazel-bin/tensorflow/examples/image_retraining/label_image \

  14.  --graph=/tmp/output_graph.pb \

  15.  --labels=/tmp/output_labels.txt \

  16.  --output_layer=final_result:0 \

  17.  --image=$HOME/flower_photos/daisy/21652746_cc379e0eea_m.jpg

或者,如果你有已经安装好的Docker,可以使用以下预构建的Docker镜像:

  
    
    
    
  1. sudo docker run -it --net=host liubowei/simple-ml-serving:latest /bin/bash

  2. >>> cat test.sh && bash test.sh

这会将你带到容器内的交互式shell中,并运行前面的命令;如果你愿意的话,可以在容器内完成本文剩下的部分。

现在,TensorFlow已经将模型信息保存到/tmp/output_graph.pb/tmp/output_labels.txt中了,这些作为命令参数传递给labelimage.py脚本。谷歌的image_recognition教程也连接到另一个推理脚本,但是现在我们仍然用label_image.py。

将一次性推断转换成在线推断(TensorFlow)

如果我们只是想从标准输入中接收文件名,每行一个,我们可以用“在线”推断简单实现:

  
    
    
    
  1. while read line ; do

  2. bazel-bin/tensorflow/examples/image_retraining/label_image \

  3. --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \

  4. --output_layer=final_result:0 \

  5. --image="$line" ;

  6. done

但从性能角度看,这真是非常糟糕——每输入一个样本就要重新加载神经网络、权重、整个TensorFlow的框架和Python本身!

但实际上还有更好的办法。让我们来编辑label_image.py脚本。我的脚本位于bazel-bin/tensorflow/examples/image_retraining/label_image.runfiles/org_tensorflow/tensorflow/examples/image_retraining/label_image.py中。

让我们替换以下行

  
    
    
    
  1. 141:  run_graph(image_data, labels, FLAGS.input_layer, FLAGS.output_layer,

  2. 142:        FLAGS.num_top_predictions)

  
    
    
    
  1. 141:  for line in sys.stdin:

  2. 142:    run_graph(load_image(line), labels, FLAGS.input_layer, FLAGS.output_layer,

  3. 142:        FLAGS.num_top_predictions)

确实速度变快了很多,但还不是最快!

原因是第100行有with tf.Session( ) as sess结构。实际上TensorFlow每次调用run_graph时将所有计算加载到内存中,一旦你开始尝试在GPU上进行推理,你就可以看到,随着TensorFlow从GPU中加载和卸载模型参数,GPU内存也会跟着变化。据我所知,Caffe或PyTorch等机器学习框架中不存在这种结构。

最后的解决方案是提出with语句,并传递一个sess变量到run_graph

  
    
    
    
  1. def run_graph(image_data, labels, input_layer_name, output_layer_name,

  2.              num_top_predictions, sess):

  3.    # Feed the image_data as input to the graph.

  4.    #   predictions will contain a two-dimensional array, where one

  5.    #   dimension represents the input image count, and the other has

  6.    #   predictions per class

  7.    softmax_tensor = sess.graph.get_tensor_by_name(output_layer_name)

  8.    predictions, = sess.run(softmax_tensor, {input_layer_name: image_data})

  9.    # Sort to show labels in order of confidence

  10.    top_k = predictions.argsort()[-num_top_predictions:][::-1]

  11.    for node_id in top_k:

  12.      human_string = labels[node_id]

  13.      score = predictions[node_id]

  14.      print('%s (score = %.5f)' % (human_string, score))

  15.    return [ (labels[node_id], predictions[node_id].item()) for node_id in top_k ] # numpy floats are not json serializable, have to run item

  16. ...

  17.  with tf.Session() as sess:

  18.    for line in sys.stdin:

  19.      run_graph(load_image(line), labels, FLAGS.input_layer, FLAGS.output_layer,

  20.          FLAGS.num_top_predictions, sess)

代码地址:github.com/hiveml/simple-ml-serving/blob/master/label_image.py

如果运行了上面这个,每张图像只需要0.1秒,比在线使用更快。

将一次性推断转换成在线推断(其他ML框架)

Caffe使用net.forward代码,这个代码很容易放入可调用的框架中。可参阅:nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb

Mxnet有随时可用的公开的推理服务器代码:github.com/awslabs/mxnet-model-server

未来将有更多细节!

部署

我们计划将代码封装成一个Flask应用。Flask是一个轻量级Python web框架,它允许你以最少的工作启动一个http api服务器。

这是一个接受多部分表单数据的POST请求的Flask应用:

  
    
    
    
  1. #!/usr/bin/env python

  2. # usage: python echo.py to launch the server ; and then in another session, do

  3. # curl -v -XPOST 127.0.0.1:12480 -F "data=@./image.jpg"

  4. from flask import Flask, request

  5. app = Flask(__name__)

  6. @app.route('/', methods=['POST'])

  7. def classify():

  8.    try:

  9.        data = request.files.get('data').read()

  10.        print repr(data)[:1000]

  11.        return data, 200

  12.    except Exception as e:

  13.        return repr(e), 500

  14. app.run(host='127.0.0.1',port=12480)

这是相应的搭配上文run_graph的Flask应用:

  
    
    
    
  1. #!/usr/bin/env python

  2. # usage: bash tf_classify_server.sh

  3. from flask import Flask, request

  4. import tensorflow as tf

  5. import label_image as tf_classify

  6. import json

  7. app = Flask(__name__)

  8. FLAGS, unparsed = tf_classify.parser.parse_known_args()

  9. labels = tf_classify.load_labels(FLAGS.labels)

  10. tf_classify.load_graph(FLAGS.graph)

  11. sess = tf.Session()

  12. @app.route('/', methods=['POST'])

  13. def classify():

  14.    try:

  15.        data = request.files.get('data').read()

  16.        result = tf_classify.run_graph(data, labels, FLAGS.input_layer, FLAGS.output_layer, FLAGS.num_top_predictions, sess)

  17.        return json.dumps(result), 200

  18.    except Exception as e:

  19.        return repr(e), 500

  20. app.run(host='127.0.0.1',port=12480)

这看起来很不错,除了flask和tensorflow都是完全同步的。flask按照接收的顺序一次处理一个请求,tensorflow在进行图像分类时完全占用线程。

正如写出的那样,在实际计算中仍存在速度限制,所以升级flask包装代码没有多大意义。也许现在这个代码足以处理你的负载。

有两种方法可以扩大请求的吞吐量:通过增加worker的数量横向扩大(接下来会介绍),或者通过使用GPU和批量处理纵向扩展。后者需要一个能够一次处理多个请求的web服务器,并决定是否继续等待更大批量的处理或是将其发送到TensorFlow图形线程进行分类,这对于Flask是非常不合适的。另外两种方法是使用Twisted+Klein来保存Python代码,或者Node.js+ZeroMQ,如果你更喜欢顶尖的时间循环支持,并能够连接到非Python ML框架(如PyTorch)。

扩展:负载平衡和发现服务

现在我们已经有了一个服务器,但是它太慢了,或者因为负载太高,所以我们想要启动更多的服务器——如何将请求分配到各服务器呢?

一般的方法是添加一个代理层,也许是haproxy或nginx,它可以平衡后端服务器之间的负载,同时提供一个统一的到客户端的接口。以下是运行基本Node.js负载均衡器http代理的代码示例:

  
    
    
    
  1. // Usage : node basic_proxy.js WORKER_PORT_0,WORKER_PORT_1,...

  2. const worker_ports = process.argv[2].split(',')

  3. if (worker_ports.length === 0) { console.err('missing worker ports') ; process.exit(1) }

  4. const proxy = require('http-proxy').createProxyServer({})

  5. proxy.on('error', () => console.log('proxy error'))

  6. let i = 0

  7. require('http').createServer((req, res) => {

  8.  proxy.web(req,res, {target: 'http://localhost:' + worker_ports[ (i++) % worker_ports.length ]})

  9. }).listen(12480)

  10. console.log(`Proxying localhost:${12480} to [${worker_ports.toString()}]`)

  11. // spin up the ML workers

  12. const { exec } = require('child_process')

  13. worker_ports.map(port => exec(`/bin/bash ./tf_classify_server.sh ${port}`))

为了自动检测后端服务器的数量和位置,人们通常使用“服务发现”工具,该工具可能与负载平衡器绑定,也有可能是分开的。一些知名的工具有Consul和Zookeeper。本文不讨论如何设置并学习使用这些工具,所以我使用node.js服务发现包seaport进行非常基础的代理。

代理代码:

  
    
    
    
  1. // Usage : node seaport_proxy.js

  2. const seaportServer = require('seaport').createServer()

  3. seaportServer.listen(12481)

  4. const proxy = require('http-proxy').createProxyServer({})

  5. proxy.on('error', () => console.log('proxy error'))

  6. let i = 0

  7. require('http').createServer((req, res) => {

  8.  seaportServer.get('tf_classify_server', worker_ports => {

  9.    const this_port = worker_ports[ (i++) % worker_ports.length ].port

  10.    proxy.web(req,res, {target: 'http://localhost:' + this_port })

  11.  })

  12. }).listen(12480)

  13. console.log(`Seaport proxy listening on ${12480} to '${'tf_classify_server'}' servers registered to ${12481}`)

worker代码:

  
    
    
    
  1. // Usage : node tf_classify_server.js

  2. const port = require('seaport').connect(12481).register('tf_classify_server')

  3. console.log(`Launching tf classify worker on ${port}`)

  4. require('child_process').exec(`/bin/bash ./tf_classify_server.sh ${port}`)

然而,当应用到机器学习上时,这一配置会遇到带宽的问题。

每秒处理几十到几百张图像,系统就会阻碍网络带宽。在目前的设置中,所有数据都要经过seaport主站,这是提供给客户端的单个端点。

为了解决这个问题,我们需要客户端不要只访问单个端点:http://127.0.0.1:12480,而是要在后端服务器之间轮流访问。如果你懂一些网络,这听起来就像DNS的工作!

但是,本文不讨论设置自定义的DNS服务器。相反,通过更改客户端,遵循“手动DNS”的协议,我们可以重新使用基本的seaport代理来实现客户端直接连接到其服务器的“点对点”协议:

代理代码(worker代码与其相同):

  
    
    
    
  1. // Usage : node p2p_proxy.js

  2. const seaportServer = require('seaport').createServer()

  3. seaportServer.listen(12481)

  4. let i = 0

  5. require('http').createServer((req, res) => {

  6.  seaportServer.get('tf_classify_server', worker_ports => {

  7.    const this_port = worker_ports[ (i++) % worker_ports.length ].port

  8.    res.end(`${this_port}

  9. `)

  10.  })

  11. }).listen(12480)

  12. console.log(`P2P seaport proxy listening on ${12480} to 'tf_classify_server' servers registered to ${12481}`)

客户端代码:

  
    
    
    
  1. curl -v -XPOST localhost:`curl localhost:12480` -F"data=@$HOME/flower_photos/daisy/21652746_cc379e0eea_m.jpg"

RPC安装

即将完成!一个将上文的Flask替换为ZeroMQ的版本。

结语

看到这,你应该能将模型部署到工作中了,但学会本教程并非一劳永逸了。其中还有几处未涉及的要点:

自动部署和设置新的硬件:

  • 如果在自己的硬件上,推荐Openstack或者VMware,安装Docker和处理网络路由则需要Chef或Puppet。推荐使用Docker安装TensorFlow、Python和其他框架;

  • 如果用云,则推荐Kubernetes或者Marathon/Mesos。

模型版本管理

  • 刚开始手工操作并不是很困难;

  • TensorFlow Serving在处理、批量处理和统一部署方面都非常强大。但缺点是有些难设置,编写客户端代码有点难。另外不支持Caffe和PyTorch。

如何从Matlab中移植你的ML代码

  • 不要在生产环境中运行matlab。

CPU驱动程序Cuda,CUDNN

  • 使用nvidia-docker并尝试在线查找一些Docker文件。

后处理图层

  • 一旦在工作中得到了不同的ML模型,你可能开始想要将他们混合匹配不同的案例——如果模型B没有结果就运行模型A,在Caffe中运行模型C并将结果传递给TensorFlow中的模型D等等。

原文地址:thehive.ai/blog/simple-ml-serving

登录查看更多
8

相关内容

深度强化学习策略梯度教程,53页ppt
专知会员服务
175+阅读 · 2020年2月1日
一网打尽!100+深度学习模型TensorFlow与Pytorch代码实现集合
【课程】伯克利2019全栈深度学习课程(附下载)
专知会员服务
53+阅读 · 2019年10月29日
强化学习最新教程,17页pdf
专知会员服务
166+阅读 · 2019年10月11日
如何用TF Serving部署TensorFlow模型
AI研习社
26+阅读 · 2019年3月27日
ML通用指南:文本分类详细教程(上)
论智
19+阅读 · 2018年7月29日
教程 | 用Scikit-Learn实现多类别文本分类
七月在线实验室
7+阅读 · 2018年5月14日
手把手教你如何部署深度学习模型
全球人工智能
14+阅读 · 2018年2月5日
8个深度学习方面的最佳实践
CSDN
3+阅读 · 2018年1月28日
深度学习入门篇--手把手教你用 TensorFlow 训练模型
全球人工智能
4+阅读 · 2017年10月21日
A Survey on Bayesian Deep Learning
Arxiv
59+阅读 · 2020年7月2日
Arxiv
5+阅读 · 2020年3月17日
Arxiv
26+阅读 · 2019年3月5日
Arxiv
3+阅读 · 2018年12月18日
Arxiv
15+阅读 · 2018年4月3日
Arxiv
25+阅读 · 2017年12月6日
VIP会员
相关资讯
如何用TF Serving部署TensorFlow模型
AI研习社
26+阅读 · 2019年3月27日
ML通用指南:文本分类详细教程(上)
论智
19+阅读 · 2018年7月29日
教程 | 用Scikit-Learn实现多类别文本分类
七月在线实验室
7+阅读 · 2018年5月14日
手把手教你如何部署深度学习模型
全球人工智能
14+阅读 · 2018年2月5日
8个深度学习方面的最佳实践
CSDN
3+阅读 · 2018年1月28日
深度学习入门篇--手把手教你用 TensorFlow 训练模型
全球人工智能
4+阅读 · 2017年10月21日
相关论文
A Survey on Bayesian Deep Learning
Arxiv
59+阅读 · 2020年7月2日
Arxiv
5+阅读 · 2020年3月17日
Arxiv
26+阅读 · 2019年3月5日
Arxiv
3+阅读 · 2018年12月18日
Arxiv
15+阅读 · 2018年4月3日
Arxiv
25+阅读 · 2017年12月6日
Top
微信扫码咨询专知VIP会员