跳至内容

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 运行时版本配合使用。此外,Google 似乎已经完全放弃了 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 或您选择的软件包管理器。

常见问题

什么是 Coral EdgeTPU ,它如何通过Ultralytics YOLOv8 增强 Raspberry Pi 的性能?

Coral EdgeTPU 是一款结构紧凑的设备,旨在为系统添加一个 EdgeTPU 协处理器。该协处理器可实现低功耗、高性能的机器学习推理,尤其针对TensorFlow Lite 模型进行了优化。使用 Raspberry Pi 时,EdgeTPU 可加速 ML 模型推理,显著提高性能,尤其是对于Ultralytics YOLOv8 模型。有关 Coral EdgeTPU 的更多信息,请访问其主页

如何在 Raspberry Pi 上安装 Coral EdgeTPU 运行时?

要在 Raspberry Pi 上安装 Coral EdgeTPU 运行时,请下载相应的 .deb 为您的 Raspberry Pi 操作系统版本从 此链接.下载后,使用以下命令进行安装:

sudo dpkg -i path/to/package.deb

请确保按照 "安装攻略"部分所述步骤卸载之前的任何 Coral EdgeTPU 运行时版本。

我可以导出Ultralytics YOLOv8 模型,使其与 Coral EdgeTPU 兼容吗?

是的,您可以导出Ultralytics YOLOv8 模型,使其与 Coral EdgeTPU 兼容。建议在Google Colab、x86_64 Linux 机器上或使用Ultralytics Docker 容器进行导出。您也可以使用Ultralytics HUB 进行导出。以下是使用Python 和CLI 导出模型的方法:

出口

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

有关详细信息,请参阅导出模式文档。

如果 Raspberry Pi 上已经安装了TensorFlow ,但我想用 tflite-runtime 代替,该怎么办?

如果您已经在 Raspberry Pi 上安装了TensorFlow ,并需要切换到 tflite-runtime您需要先卸载TensorFlow :

pip uninstall tensorflow tensorflow-aarch64

然后,安装或更新 tflite-runtime 使用以下命令:

pip install -U tflite-runtime

对于特定的轮子,例如TensorFlow 2.15.0 tflite-runtime您可以从 此链接 并使用 pip.详细说明见运行模型部分 运行模型.

如何在 Raspberry Pi 上使用 Coral EdgeTPU 对导出的YOLOv8 模型进行推理?

将YOLOv8 模型导出为与 EdgeTPU 兼容的格式后,可以使用以下代码片段运行推理:

运行

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

有关预测模式全部功能的详细信息,请参阅 "预测 "页面



创建于 2024-02-12,更新于 2024-07-05
作者:glenn-jocher(7),Skillnoob(1)

评论