使用 Ultralytics YOLO26 在 Raspberry Pi 上运行 Coral Edge TPU 🚀
什么是 Coral Edge TPU?
Coral Edge TPU 是一款紧凑型设备,可为你的系统添加一个 Edge TPU 协处理器。它能够为 TensorFlow Lite 模型提供低功耗、高性能的 ML 推理能力。更多信息请查看 Coral Edge TPU 主页。
Watch: How to Run Inference on Raspberry Pi using Google Coral Edge TPU
使用 Coral Edge TPU 提升 Raspberry Pi 模型性能
许多人希望在 Raspberry Pi 等嵌入式或移动设备上运行模型,因为它们功耗非常高效,可用于各种不同的应用场景。然而,即便使用 ONNX 或 OpenVINO 等格式,这些设备上的推理性能通常也不尽如人意。Coral Edge TPU 是解决此问题的绝佳方案,因为它可与 Raspberry Pi 配合使用,并大幅提升推理性能。
在 Raspberry Pi 上使用 TensorFlow Lite 运行 Edge TPU(新)⭐
Coral 提供的关于如何在 Raspberry Pi 上使用 Edge TPU 的 现有指南 已经过时,目前的 Coral Edge TPU 运行时构建版本已无法与当前的 TensorFlow Lite 运行时版本兼容。此外,Google 似乎已经完全放弃了 Coral 项目,从 2021 年到 2025 年间没有任何更新。本指南将向你展示如何在 Raspberry Pi 单板计算机 (SBC) 上,使用最新版本的 TensorFlow Lite 运行时和更新后的 Coral Edge TPU 运行时来运行 Edge TPU。
先决条件
- Raspberry Pi 4B(建议 2GB 或以上)或 Raspberry Pi 5(推荐)
- Raspberry Pi OS Bullseye/Bookworm (64-bit) 桌面版(推荐)
- Coral USB Accelerator
- 一个非 ARM 架构的平台,用于导出 Ultralytics PyTorch 模型
安装流程指南
本指南假设你已经安装好 Raspberry Pi OS,并已安装 ultralytics 及所有相关依赖。如需安装 ultralytics,请访问 快速入门指南 进行配置,然后再继续后续步骤。
安装 Edge TPU 运行时
首先,我们需要安装 Edge TPU 运行时。由于有许多不同的版本,你需要为你的操作系统选择正确的版本。 高频版本以更高的时钟频率运行 Edge TPU,从而提高性能。但是,这可能会导致 Edge TPU 热节流,因此建议配备某种散热装置。
| Raspberry Pi OS | 高频模式 | 下载版本 |
|---|---|---|
| Bullseye 32bit | 否 | libedgetpu1-std_ ... .bullseye_armhf.deb |
| Bullseye 64bit | 否 | libedgetpu1-std_ ... .bullseye_arm64.deb |
| Bullseye 32bit | 是 | libedgetpu1-max_ ... .bullseye_armhf.deb |
| Bullseye 64bit | 是 | libedgetpu1-max_ ... .bullseye_arm64.deb |
| Bookworm 32bit | 否 | libedgetpu1-std_ ... .bookworm_armhf.deb |
| Bookworm 64bit | 否 | libedgetpu1-std_ ... .bookworm_arm64.deb |
| Bookworm 32bit | 是 | libedgetpu1-max_ ... .bookworm_armhf.deb |
| Bookworm 64bit | 是 | libedgetpu1-max_ ... .bookworm_arm64.deb |
下载文件后,你可以使用以下命令进行安装:
sudo dpkg -i path/to/package.deb安装运行时后,将你的 Coral Edge TPU 插入 Raspberry Pi 的 USB 3.0 端口,以便新的 udev 规则生效。
重要提示
如果你已经安装了 Coral Edge TPU 运行时,请使用以下命令卸载它。
# If you installed the standard version
sudo apt remove libedgetpu1-std
# If you installed the high-frequency version
sudo apt remove libedgetpu1-max导出到 Edge TPU
要使用 Edge TPU,你需要将模型转换为兼容格式。建议在 Google Colab、x86_64 Linux 机器上,使用官方 Ultralytics Docker 容器 或使用 Ultralytics Platform 执行导出,因为 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")导出的模型将保存在 <model_name>_saved_model/ 文件夹中,文件名为 <model_name>_full_integer_quant_edgetpu.tflite。确保文件名以 _edgetpu.tflite 后缀结尾;否则,Ultralytics 将无法检测到你正在使用 Edge TPU 模型。
运行模型
在真正运行模型之前,你需要安装正确的库。
如果你已经安装了 TensorFlow,请使用以下命令卸载它:
pip uninstall tensorflow tensorflow-aarch64然后安装或更新 tflite-runtime:
pip install -U tflite-runtime现在,你可以使用以下代码运行推理:
from ultralytics import YOLO
# Load a model
model = YOLO("path/to/<model_name>_full_integer_quant_edgetpu.tflite") # Load an official model or custom model
# Run Prediction
model.predict("path/to/source.png")在 预测 页面上查找关于预测模式的完整详细信息。
如果你有多个 Edge TPU,可以使用以下代码选择特定的 TPU。
from ultralytics import YOLO
# Load a model
model = YOLO("path/to/<model_name>_full_integer_quant_edgetpu.tflite") # Load an official model or custom model
# Run Prediction
model.predict("path/to/source.png") # Inference defaults to the first TPU
model.predict("path/to/source.png", device="tpu:0") # Select the first TPU
model.predict("path/to/source.png", device="tpu:1") # Select the second TPU基准测试
已在 Raspberry Pi OS Bookworm 64-bit 和 USB Coral Edge TPU 上经过测试。
图中显示的是推理时间,不包含预处理/后处理时间。
| 图像尺寸 | 模型 | 标准推理时间 (ms) | 高频推理时间 (ms) |
|---|---|---|---|
| 320 | YOLOv8n | 32.2 | 26.7 |
| 320 | YOLOv8s | 47.1 | 39.8 |
| 512 | YOLOv8n | 73.5 | 60.7 |
| 512 | YOLOv8s | 149.6 | 125.3 |
平均而言:
- Raspberry Pi 5 在标准模式下比 Raspberry Pi 4B 快 22%。
- Raspberry Pi 5 在高频模式下比 Raspberry Pi 4B 快 30.2%。
- 高频模式比标准模式快 28.4%。
常见问题 (FAQ)
什么是 Coral Edge TPU,它如何提升 Raspberry Pi 使用 Ultralytics YOLO26 的性能?
Coral Edge TPU 是一款紧凑型设备,旨在为你的系统添加一个 Edge TPU 协处理器。该协处理器能够实现低功耗、高性能的 机器学习 推理,并特别针对 TensorFlow Lite 模型进行了优化。在使用 Raspberry Pi 时,Edge TPU 可以加速 ML 模型推理,从而显著提升性能,尤其是对于 Ultralytics YOLO26 模型。你可以在他们的 主页 上阅读有关 Coral Edge TPU 的更多信息。
如何在 Raspberry Pi 上安装 Coral Edge TPU 运行时?
To install the Coral Edge TPU runtime on your Raspberry Pi, download the appropriate .deb package for your Raspberry Pi OS version from this link. Once downloaded, use the following command to install it:
sudo dpkg -i path/to/package.deb请务必按照 安装流程指南 部分概述的步骤卸载任何以前的 Coral Edge TPU 运行时版本。
我可以导出我的 Ultralytics YOLO26 模型使其与 Coral Edge TPU 兼容吗?
可以,你可以导出 Ultralytics YOLO26 模型以使其与 Coral Edge TPU 兼容。建议在 Google Colab、x86_64 Linux 机器上执行导出,或使用 Ultralytics Docker 容器。你也可以使用 Ultralytics Platform 进行导出。以下是如何使用 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")有关更多信息,请参阅 导出模式 文档。
如果我的 Raspberry Pi 上已经安装了 TensorFlow,但我现在想改用 tflite-runtime,该怎么办?
如果你的 Raspberry Pi 上安装了 TensorFlow 并且需要切换到 tflite-runtime,你需要首先使用以下命令卸载 TensorFlow:
pip uninstall tensorflow tensorflow-aarch64然后,使用以下命令安装或更新 tflite-runtime:
pip install -U tflite-runtime有关详细说明,请参阅 运行模型 部分。
如何使用 Coral Edge TPU 在 Raspberry Pi 上通过导出的 YOLO26 模型运行推理?
将 YOLO26 模型导出为 Edge TPU 兼容格式后,你可以使用以下代码片段运行推理:
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")关于完整预测模式功能的详细说明,请参考 预测页面。