跳至内容

Coral EdgeTPU on a Raspberry Pi withUltralytics YOLOv8 🚀

配备 USB EdgeTPU 加速器的 Raspberry Pi 单板计算机

什么是珊瑚边TPU ?

Coral EdgeTPU 是一款结构紧凑的设备,可为系统添加一个 EdgeTPU 协处理器。它可以为TensorFlow Lite 模型提供低功耗、高性能的 ML 推断。更多信息,请访问Coral EdgeTPU 主页

利用 Coral Edge 提升树莓派模型性能TPU

许多人希望在 Raspberry Pi 等嵌入式或移动设备上运行他们的模型,因为这些设备非常省电,可以用于许多不同的应用。然而,这些设备上的推理性能通常很差,即使使用像 onnxopenvino.Coral EdgeTPU 可以很好地解决这个问题,因为它可以与 Raspberry Pi 一起使用,大大提高推理性能。

利用TensorFlow Lite 在 Raspberry Pi 上实现边缘TPU (新)⭐

Coral现有的关于如何在 Raspberry Pi 上使用 EdgeTPU 的指南已经过时,而且当前的 Coral EdgeTPU 运行时构建已无法与当前的TensorFlow Lite 运行时版本配合使用。此外,谷歌似乎已经完全放弃了 Coral 项目,而且在 2021 年至 2024 年期间没有任何更新。本指南将向您介绍如何在 Raspberry Pi 单板计算机(SBC)上使用最新版本的TensorFlow Lite 运行时和更新的 Coral EdgeTPU 运行时运行 EdgeTPU 。

先决条件

安装攻略

本指南假定您已经安装了可正常工作的 Raspberry Pi 操作系统,并且已经安装了 ultralytics 和所有依赖项。获取 ultralytics 安装,请访问 快速入门指南 进行设置后再继续。

安装 EdgeTPU 运行时

首先,我们需要安装 EdgeTPU 运行时。目前有许多不同的版本,因此需要根据操作系统选择合适的版本。

树莓派操作系统 高频模式 下载版本
牛眼 32 位 没有 libedgetpu1-std_ ... .bullseye_armhf.deb
牛眼 64 位 没有 libedgetpu1-std_ ... .bullseye_arm64.deb
牛眼 32 位 libedgetpu1-max_ ... .bullseye_armhf.deb
牛眼 64 位 libedgetpu1-max_ ... .bullseye_arm64.deb
书虫 32 位 没有 libedgetpu1-std_ ... .bookworm_armhf.deb
书虫 64bit 没有 libedgetpu1-std_ ... .bookworm_arm64.deb
书虫 32 位 libedgetpu1-max_ ... .bookworm_armhf.deb
书虫 64bit libedgetpu1-max_ ... .bookworm_arm64.deb

从这里下载最新版本

下载文件后,可以使用以下命令进行安装:

sudo dpkg -i path/to/package.deb

安装运行时后,您需要将 Coral EdgeTPU 插入 Raspberry Pi 的 USB 3.0 端口。这是因为,根据官方指南,一个新的 udev 规则需要在安装后生效。

重要

如果已经安装了 Coral EdgeTPU 运行时,请使用以下命令卸载它。

# If you installed the standard version
sudo apt remove libedgetpu1-std 

# If you installed the high frequency version
sudo apt remove libedgetpu1-max 

将模型导出为与 EdgeTPU 兼容的模型

要使用 EdgeTPU ,您需要将模型转换为兼容格式。建议在 Google Colab、x86_64 Linux 机器上使用官方Ultralytics Docker 容器或使用Ultralytics HUB 运行 export,因为 Edge TPU 编译器在 ARM 上不可用。有关可用参数,请参阅 "导出模式"。

出口

from ultralytics import YOLO

# Load a model
model = YOLO('path/to/model.pt')  # Load an official model or custom model

# Export the model
model.export(format='edgetpu')
yolo export model=path/to/model.pt format=edgetpu  # Export an official model or custom model

导出的模型将保存在 <model_name>_saved_model/ 文件夹,其名称为 <model_name>_full_integer_quant_edgetpu.tflite.

运行模型

导出模型后,您可以使用以下代码运行推理:

运行

from ultralytics import YOLO

# Load a model
model = YOLO('path/to/edgetpu_model.tflite')  # Load an official model or custom model

# Run Prediction
model.predict("path/to/source.png")
yolo predict model=path/to/edgetpu_model.tflite source=path/to/source.png  # Load an official model or custom model

有关预测模式的详细信息,请访问 "预测 "页面。

重要

您应该使用 tflite-runtime 而不是 tensorflow. 如果 tensorflow 时,请使用以下命令卸载tensorflow :

pip uninstall tensorflow tensorflow-aarch64

然后安装/更新 tflite-runtime:

pip install -U tflite-runtime

如果您想要 tflite-runtime 用于 tensorflow 2.15.0 下载地址 这里 并使用 pip 或您选择的软件包管理器。



创建于 2024-02-12,更新于 2024-04-27
作者:glenn-jocher(3),Skillnoob(1)

评论