使用谷歌Coral EdgeTPU技术打造自己的廉价Tensorflow-lite AI云

TL,DR; 在这篇文章里,你将会学到如何使用谷歌Coral EdgeTPU技术,创建带有高效物体检测 REST API的廉价版私有化AI云。看到文章最后,挑战一下自己吧!

Ahhhh,云…

使用谷歌Coral EdgeTPU技术打造自己的廉价Tensorflow-lite AI云

图片摘自 www.teeshirtpalace.com

当创建一个后端服务器应用的时候,我们通常想到的就是性能。就算经过优化后的软件能够满足性能的时候,有限的硬件水平又制造出了瓶颈。加入AI后,将高耗能的mega-tron GPU(s)放在你的多极核心处理器上,就是我们所要讨论的。

现在,让我们想象下,仅用一块即插即用的USB硬件,就能够安装你自己的个人高效AI Cloud服务器且最大运行能力为900mA峰值流。好了,我们可以不用再做白日梦了,因为现在使用谷歌的Coral EdgeTPU技术就能够梦想成真。这篇文章会用到的就是一块硬件:Coral USB Accelerator。那么让我们顺着我们的想象,打造属于自己的廉价版AI Cloud!!!

使用谷歌Coral EdgeTPU技术打造自己的廉价Tensorflow-lite AI云

Coral USB Accelerator (图片摘自 coral.ai)

** 注意:所有的Coral产品都可以用于此项目,但是这篇文章只涉及USB加速器的即插即用功能。

使用谷歌Coral EdgeTPU技术打造自己的廉价Tensorflow-lite AI云

谷歌的Edge TPU基准

要求

  • The Coral USB Accelerator
  • 一台Debian操作系统的x86_64的主机,你可以使用这台机器构建代码(我家的台式电脑是Debian10系统,我的笔记本是Ubunta 18.04系统。我没有在其他的电脑机器上尝试过构建代码)
  • 一台拥有arm64或者x86_64的电脑,也可以是和你的运行服务器CPU架构相同电脑,这样你可以在这台电脑运行可执行文件。
  • 我建议浏览一下Coral网站上的入门指南Getting Started Guide from Coral’s Webpage (你同样需要安装libedgetpu 安装包,它会指导你在哪里运行代码)
  • 这个指南需要读者掌握AI, TensorFlow,linux,backend application, REST APIs, 以及git的入门知识。

Restor项目介绍

restor是我个人开发的一个项目,它有一个用于物体检测的REST API,EdgeTPU 平台加速了物体检测的速度 (我认为Restor这个名字有点一语双关的含义)。你可以访问这个网址,了解这个项目:https://github.com/namburger/restoris

为了更好的性能提升,Restor是使用现代C++模式编写的,而且是基于谷歌的EdgeTPU物体检测引擎 EdgeTPU Object Detection Engine. 目前,restor有4个终端,但是以后会增加:

Restor的终端截至2020年1月6日可用

POST 方法将会用于客户端发送图片数据给服务器。在json字符串的“data”字段中,数据应该是一张.bmp格式图片转化得到的base64编码的json字符串。restor服务器会处理该数据然后进行物体检测。不用担心,我也会举几个示例客户端应用。:)

  • 下面是restor的功能预览

先看看这张漂亮的照片,我的爱猫马克西蒙和我以及我的一些朋友们都在峭壁旁。

使用谷歌Coral EdgeTPU技术打造自己的廉价Tensorflow-lite AI云

maxium.jpg

  • 准备工作
<code># 首先安装一些deps
$ sudo apt update
$ sudo apt install wget curl jq imagemagick# 第二步,你可以下载相同的图片
$ wget https://srv-file5.gofile.io/download/2rC4jJ/maximum.jpg# 然后将这张图片的格式转成.bmp,而不是.jpg
$ mogrify -format bmp maximum.jpg
# 这时,一张名称为maximum.bmp的文件就生成了。接下来了,把这张图片的数据转化成一张base64编码的json字符串,并将它保存为tmp文件:
$ echo "{\"data\":\"`cat maximum.bmp|base64 -w0`\"}" > /tmp/maximum.json
/<code>
  • 发送
<code># 将这份文件发送给服务器(你首先需要准备好服务器) 
$ curl -d@/tmp/maximum.json -H "Content-Type: application/json" -X POST http://localhost:8888/detects | jq
{
"req_id": 627,
"result1": {
"candidate": "dog",
"score": 0.91015625
},
"result2": {
"candidate": "person",
"score": 0.66015625
},
"result3": {
"candidate": "person",
"score": 0.41796875
}
}
/<code>

等等,我刚说的是“cat”吗?因为Restor绝对相信Maximum是一只狗。如果这样的话,我认为restor可能不会搞错。 ¯_(ツ)_/¯


设置restor

  • 首先,我们安装build-essential安装包,来进行项目构建
<code># 如果你的服务器以及构建机器是同一台机器,安装下面这个:
$ sudo apt-get install -y build-essential# 如果你的服务器在另外一台机器上,属于不同的CPU构架,安装下面这个:
$ sudo apt-get install -y crossbuild-essential-armhf crossbuild-essential-arm64
/<code>
  • 第二步,让我们创建项目
<code># 复制下面的项目:
$ git clone https://github.com/namburger/restor && cd restor# 下载一些核心依赖:
$ python3 install_thirdparty.py
/<code>
  • 接下来,构建项目
<code># 如果服务器和构建机器是同一台:
$ make# 如果你在另一台机器上运行服务器,那么你将需要明确这台服务器的CPU构架,例如:

$ make CPU=aarch64** currently we have CPU=k8 for amd64 and CPU=aarch64 for arm64, the binary can also be built for arm32 bits with CPU=armv7a, however the libedgetpu no longer support 32 bits architecture, so you may have to install an older version of libedgetpu.so
/<code>

如果一切进展顺利,你将会在“out/{k8||aarch64||armv7a}/restor”目录里,看到RESTOR二进制出现在项目的ROOT部分。

运行服务器

  • 配置

restor既可以通过CLI argument parsing运行,也可以通过 yaml config运行。这里有一个示例配置 config/restor.yaml:

<code>restor:
modelFile: test_data/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite
labelFile: test_data/coco_labels.txt
numResults: 5
numThreads: 4
port: 8888
/<code>

每个域名都很清晰明了,我也不多做赘述。尽管都是modelFile和labelFile文件,但是我建议给出完整路径,以防你在不同的目录里运行二进制。

** 注意:例子里用的模型是直接从google-coral’s edgetpu repo 借鉴过来的,这已经能够检测90种不同的物体。

  • 运行Restor服务器

为了能够在本机运行服务器,只要加一个config_path,下面是一个带有config的例子:

<code>$ ./out/k8/restor --config_path config/restor.yaml
I0106 14:13:02.776759 53679 main.cc:27] RESTOR
I0106 14:13:02.806629 53679 main.cc:34] found 1 TPU(s)
I0106 14:13:02.806658 53679 main.cc:35] config: config/restor.yaml
I0106 14:13:05.524688 53679 server.cc:38] Engine initialized
I0106 14:13:05.524776 53679 server.cc:39] model: test_data/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite
I0106 14:13:05.524821 53679 server.cc:40] label: test_data/coco_labels.txt
I0106 14:13:05.524849 53679 server.cc:41] num_results: 5
I0106 14:13:05.524873 53679 server.cc:42] num_threads: 4
I0106 14:13:05.524894 53679 server.cc:43] input_tensor_shape: [1,300,300,3]
I0106 14:13:05.524971 53679 server.cc:58] End points registered
I0106 14:13:05.525341 53679 server.cc:60] Serving on port: 8888
/<code>

如果在另外一台机器上运行,只要直接复制模型,标注文件名,config以及二进制到你想要运行的机器上。


各位,就是这样,你已经打造好了你自己的私人AI Cloud,能够完成物体检测的任务。有很多的相似的AI Cloud,但是这个是属于你自己的:)。

使用谷歌Coral EdgeTPU技术打造自己的廉价Tensorflow-lite AI云

你现在可以回到上面预览部分,来测试下服务器。教程到此就为止了,然后如果你对构建个人AI Cloud有疑问的话,我也有两三份服务器程序,以及一个度量检测终端。如果你想进一步了解的话,继续阅读下面的福利部分。


福利

  • 度量检测

对于越来越多的高级用户,你们想必需要一些检测系统。收集度量是找出服务器出现何种类型故障的一种办法。为此,我建了一个运维监控 prometheus/度量终端。通过运行restor服务器,该终端能够检测出故障:

<code>$ curl localhost:8888/metrics
# HELP server_request_total Number of total http requests handled
# TYPE server_request_total counter
server_request_total{endpoint="metrics",method="GET"} 63.000000
server_request_total{endpoint="version",method="GET"} 84.000000
server_request_total{endpoint="detects",method="POST"} 84.000000
# HELP process_open_fds Number of open file descriptors
# TYPE process_open_fds gauge
process_open_fds 18.000000
# HELP process_resident_memory_bytes Process resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 441085952.000000
# HELP process_virtual_memory_bytes Process virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 704843776.000000

# HELP process_virtual_memory_max_bytes Process peak virtual memory size in bytes.
# TYPE process_virtual_memory_max_bytes gauge
process_virtual_memory_max_bytes 911925248.000000
/<code>

想要更好的预览这些度量,你可以创建一个运维监控镜像。我在这提供了一个prometheus.yaml config 以及一个Dockerfile(点击:here)。请在prometheus.yam中更换能够匹配服务器ip地址的ip地址值。然后

<code>$ cd config
# 在这修改prometheus.yaml
$ docker build -t prometheus .
$ docker run -p 9090:9090 prometheus
/<code>

如果一切顺利的话,你就能够用你的浏览器浏览localhost:9090/graph,像这样:

使用谷歌Coral EdgeTPU技术打造自己的廉价Tensorflow-lite AI云

metrics.png

  • 示例cpp客户端给服务器发送任意图像

我也编过一个示例客户端,这个客户端能够连接所有restor的终端:点击cpp_cli_client。快速上手:

<code># 干起来
$ cd example_client/cpp_cli_client# 安装一些deps
$ make install
... not showing this output ...# 构建
$ make
g++ -Iinclude -std=c++17 -Wall -Wextra -pthread -o restor_client main.cc# 应该能够看见所有文件
$ ls
include install_dependencies.sh main.cc Makefile restor_client# 示例运行
$ ./restor_client --host localhost --port 8888 --method post --endpoint /detects --image /tmp/maximum.bmp
Sending POST @data={"data": base64_encode(/tmp/maximum.bmp)} to localhost:8888/detects
{
"req_id": 1,
"result1": {
"candidate": "dog",
"score": 0.91015625
},
"result2": {
"candidate": "person",
"score": 0.66015625
},
"result3": {
"candidate": "person",
"score": 0.41796875
}
}
/<code>

** Restor客户端可以看见其他终端,但是我想让你自己发现。:)

  • 示例Python客户端采集图片并发送给服务器

Python客户端的例子取决于开放源代码计算机视觉类库 opencv,通过这个视觉类库来拍摄照片,发送给restor进行监测。

<code># 开始
$ cd example_client/cv_client# 运行
$ python3 cv_client.py --host localhost --port 8888
/<code>
使用谷歌Coral EdgeTPU技术打造自己的廉价Tensorflow-lite AI云

就是这些,你先可以成功地使用一个即插即用的设备打造自己的AI Cloud。我的 Github Repo上有整个源代码。可以发送一些特性请求,问题以及PRs….

最后,我们还有一个挑战: 我带着你使用USB Accelerator一步一步完成了你机器的创建,那么你能不能在 Dev Board Dev Board?!?上创建restor呢?

原文:Coral is Google’s quiet initiative to enable AI without the cloud https://www.theverge.com/2020/1/14/21065141/google-coral-ai-edge-computing-products-applications-cloud (By James Vincent)

转载请注明:文章转载自 Coral中文网[https://coral.org.cn/]

本文标题:使用谷歌Coral EdgeTPU技术打造属于自己的廉价版Tensorflow-lite AI云

本文地址:https://coral.org.cn/post/own-budget-Tensorflow-lite-AI-Cloud/


分享到:


相關文章: