使用Google Coral EdgeTPU技术,构建自己预算的TensorFlow Lite AI云系统

本文将学习如何创建预算人工智能云,提供由Google的Coral EdgeTPU技术实现的高速性能对象检测REST API。

在创建后端服务器应用时,通常需要从性能的角度考虑。性能可以通过高度优化的软件来实现,但很多情况下瓶颈是由于硬件功能的限制所造成的。在这里我们添加了AI。我们正在谈论的是在多异构核处理器上使用高功耗的Megatron GPU。

设想一下,只使用一个插即用的USB硬件,您就可以搭建一个独特的高性能AI云服务器。它能够提供最大运行容量为约900mA的峰值电流。这得益于谷歌的Coral EdgeTPU技术。在本教程中,我们将使用Coral USB加速器来验证这项硬件。根据您的想象力,让我们来构建您独有的AI云解决方案吧!

无论是Coral的哪个产品,都可以在这个项目中使用。但请注意,仅在这个教程的即插即用目的下使用加速器。

重要的文件或事情。

珊瑚 USB 加速器

为了构建代码,可以使用基于Debian的x86_64主机(我在家用台式机上运行Debian10,在笔记本上运行Ubuntu 18.04,但在其他机器上尝试构建却失败了)。
执行二进制文件的服务器可能是arm64或x86_64,并且可能与构建机器相同。
建议从Coral的网页上阅读起步指南(还需要根据正在运行代码的指南安装libedgetpu软件包)。
本指南假设读者具备AI、TensorFlow、Linux、后端应用程序、REST API和Git的基本知识。

介绍项目修复

restor是我个人的项目,通过EdgeTPU平台提供了针对对象检测的REST API,实现了高速化(虽然我觉得名字有点奇怪)。您可以通过以下链接访问该项目:https://github.com/namburger/restor。
restor是用最新的C++编写,以进一步提升性能,并依赖于Google的EdgeTPU对象检测引擎。目前,restor有四个端点,但这将会扩展。

alt

POST方法用于客户端向服务器发送图像数据。在JSON字符串的”data”字段中,数据应该是从.bmp图像转换成Base64编码的数据,服务器会对这些数据进行处理以检测对象。不用担心,我会给你一些客户端应用程序的示例。

让我们来看看上周在山上拍摄的这幅美丽的照片,照片中有我的猫Maxium以及我和几位朋友。

alt

准备:

# Let's First install some deps
$ sudo apt update
$ sudo apt install wget curl jq imagemagick
# Secondly you can download the same image
$ wget https://srv-file5.gofile.io/download/2rC4jJ/maximum.jpg
# Then this image needs to be reformatted to a .bmp file instead of .jpg
$ mogrify -format bmp maximum.jpg
# at this point, a maximum.bmp file should be generated.
# Next, turn this image data into a base64 encoded json string and save it as a tmp file:
$ echo "{\"data\":\"`cat maximum.bmp|base64 -w0`\"}" > /tmp/maximum.json

目送:

# send it to the server (you need to set the server up first)
$ 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
  }
}

等一下,剛剛你說了「貓」嗎?!?Restor認為Maximum的答案應該是狗o_0。在這種情況下,Restor也有可能是正確的¯\(ツ)/¯。

设置恢复

首先,为了构建项目,让我们安装构建必需的软件包。
# If you are running the server on the same machine as your build machine, install this:
$ sudo apt-get install -y build-essential
# If you are running the server on another machine that does not have the same CPU architecture, install this:
$ sudo apt-get install -y crossbuild-essential-armhf crossbuild-essential-arm64
接下来,让我们设定项目。
# clone the project:
$ git clone https://github.com/namburger/restor && cd restor
# download some dependencies:
$ python3 install_thirdparty.py
接下来,进行项目构建。

# 如果在同一台机器上运行服务器:
$ make
# 如果在不同的机器上运行服务器,则需要指定运行该机器的CPU架构。例如:
$ make CPU=aarch64
**当前版本支持amd64的CPU=k8,arm64的CPU=aarch64,可以使用CPU=armv7a来构建用于arm32位的二进制文件,但由于libedgetpu不再支持32位架构,因此需要安装旧版本的libedgetpu.so。

当所有内容都成功构建后,将在项目的根目录下的”out/{k8 || aarch64 || armv7a}/restore”目录中显示恢复二进制文件。

我們運行伺服器。

构成

restor可以通过CLI参数解析或yaml配置文件来执行。在config/restor.yaml文件中有一个配置示例:

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

每个字段的名称都非常直观,因此不需要额外解释。但是,对于modelFile和labelFile字段,建议提供完整路径,以便在不同目录中执行二进制文件时使用。
**注意:此示例使用的模型直接从google-coral的edgetpu存储库中获取,可以检测到90种对象。

执行还原服务器

要将服务器本地化执行,需要指定config_path。下面是在上面的配置中执行的示例:

$ ./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] 找到1个TPU
I0106 14:13:02.806658 53679 main.cc:35] 配置:config/restor.yaml
I0106 14:13:05.524688 53679 server.cc:38] 引擎已初始化
I0106 14:13:05.524776 53679 server.cc:39] 模型:test_data/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite
I0106 14:13:05.524821 53679 server.cc:40] 标签:test_data/coco_labels.txt
I0106 14:13:05.524849 53679 server.cc:41] 结果数:5
I0106 14:13:05.524873 53679 server.cc:42] 线程数:4
I0106 14:13:05.524894 53679 server.cc:43] 输入张量形状:[1,300,300,3]
I0106 14:13:05.524971 53679 server.cc:58] 终点已注册
I0106 14:13:05.525341 53679 server.cc:60] 服务在端口上运行:8888

要在另一台机器上执行,需将模型、标签文件、配置和二进制文件复制到执行机器上,然后执行。这就是全部。您已经构建了一个独特的AI云,可以处理目标检测任务。虽然类似的产品很多,但这是属于您的独特之处 🙂

您可以在上面的预览部分中循环测试服务器。如果您只对个人AI云设置感兴趣,教程到此结束,但也包括了一些客户端程序和/指标监控端点。如果您想要查看它们,请继续阅读奖励部分!

指标监控

针对更高级用户,可能需要某种形式的监控系统。收集指标是了解服务器如何处理各种压力的一种方式。为此我们将Prometheus/metrics端点集成进来。通过运行恢复服务器,该端点将立即可用作默认选项。

$ 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

为了更清晰地显示这些指标,您可以设置Prometheus Docker镜像。在这里,我们提供了prometheus.yaml配置文件和Dockerfile。请将prometheus.yaml中的IP地址值更改为与服务器的IP地址匹配。然后:

$ cd config
# modify the prometheus.yaml here
$ docker build -t prometheus . 
$ docker run -p 9090:9090 prometheus

如果一切顺利,在Web浏览器中访问localhost:9090/graph,应该会显示类似以下的内容:

alt
# go to it
$ cd example_client/cpp_cli_client
# install some deps
$ make install
... not showing this output ...
# build
$ make
g++ -Iinclude -std=c++17 -Wall -Wextra -pthread -o restor_client main.cc
# should see all these files
$ ls
include  install_dependencies.sh  main.cc  Makefile  restor_client
# example run
$ ./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
  }
}

Restor客户端可以查看其他的端点,但是否理解则取决于你自己:)

一個Python客戶端的例子,用於從獲取圖像並發送到伺服器。
這個Python客戶端的示例使用OpenCV來捕捉照片並將其發送給恢復器以進行檢測。

# go to it
$ cd example_client/cv_client
# run it
$ python3 cv_client.py --host localhost --port 8888
alt

使用了这个,就完成了一切。使用即插即用设备成功构建了自己的AI云平台。(作者:Nam Vu)
如果对Google Coral产品以及批量销售或大宗销售(量价折扣)的详细信息感兴趣,欢迎访问Coral海外代理商Gravitylink的网站:https://store.gravitylink.com/global。

alt
广告
将在 10 秒后关闭
bannerAds