如何使用 Coral Edge TPU 在 Raspberry Pi 上运行 Ultralytics YOLO26#
Raspberry Pi 是一个省电且经济实惠的平台,适合在边缘端运行计算机 vision,但即使使用 ONNX 或 OpenVINO 等优化格式,设备上的推理速度仍然很慢。将 Pi 与 Coral Edge TPU 协处理器搭配使用可将推理卸载到专用硬件,从而大幅提高运行速度。本指南将向你展示如何安装运行时、将 Ultralytics YOLO26 模型导出为 Edge TPU 格式,以及运行加速推理。
Watch: How to Run Inference on Raspberry Pi using Google Coral Edge TPU
为什么要使用 Coral Edge TPU?#
Coral Edge TPU 是一款紧凑型设备,通过为你的系统增加一个 Edge TPU 协处理器,能够为 TensorFlow Lite 模型实现低功耗、高性能的机器学习推理。它非常适合那些仅靠 CPU 无法满足需求的嵌入式和移动端部署场景:
- 更快的推理速度 — Edge TPU 对量化模型的加速效果远超 Raspberry Pi CPU 自身的能力。
- 低功耗 — 它能提供极高的单位功率吞吐量,非常适合电池或太阳能供电的部署环境。
- 即插即用 — USB Accelerator 通过 USB 3.0 连接,因此无需进行额外的硬件集成。
官方 Coral 指南已经过时:原有的 Coral 运行时构建已无法与当前的 TensorFlow Lite 运行时版本兼容,且该项目在 2021 年至 2025 年期间没有任何更新。本指南使用维护中的 Edge TPU 运行时和最新的 tflite-runtime,确保加速器能够在当前的 Raspberry Pi OS 安装上正常工作。
前提条件#
- Raspberry Pi 4B(建议 2GB 或更高)或 Raspberry Pi 5(推荐)
- 带桌面的 Raspberry Pi OS Bullseye/Bookworm (64-bit)(推荐)
- Coral USB Accelerator
- 用于导出模型的非 ARM 平台(Google Colab、x86_64 Linux 机器或 Ultralytics Docker 容器),因为 Edge TPU 编译器在 ARM 上不可用
本指南假定你已经成功安装了带有 ultralytics 及其依赖项的 Raspberry Pi OS。如果尚未安装,请先参考快速入门指南。
在准备好先决条件后,工作流包含三个步骤:在 Pi 上安装 Edge TPU 运行时、在非 ARM 机器上导出模型,以及在 Pi 上运行推理。
安装 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 |
从此处下载最新版本,然后安装 .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,请将模型转换为兼容格式。由于 Edge TPU 编译器在 ARM 上不可用,因此请在非 ARM 平台(Google Colab、x86_64 Linux 机器、官方 Ultralytics Docker 容器或 Ultralytics Platform)上运行导出。可用参数请参阅导出模式。
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 会将其作为普通的 TensorFlow Lite 模型加载,而不会检测到 Edge TPU,从而无法使用加速器。
在 Edge TPU 上运行推理#
在运行模型之前,请在 Raspberry Pi 上安装正确的库。如果已经安装了 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,可以使用 device 参数选择特定的一个。
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 测得的。这些数据仅显示推理时间(不包括预处理/后处理),并作为 Edge TPU 在不同 Pi 模型和模式下提供加速的相对参考。
这些基准测试记录使用的是 YOLOv8 模型。绝对推理时间会因模型版本和图像大小而异,但 Pi 模型和时钟模式之间的相对速度提升是有效的。
| 图像大小 | 模型 | 标准推理时间 (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%。
结论#
Coral Edge TPU 可将 Raspberry Pi 变成一个能够为 Ultralytics YOLO26 提供支持的高效低功耗推理设备。在非 ARM 机器上导出你的模型,保留 _edgetpu.tflite 后缀,并在 Pi 上使用 tflite-runtime 运行它以获得加速边缘推理。有关更多部署选项,请参阅 Raspberry Pi 指南。
常见问题解答#
什么是 Coral Edge TPU?它如何通过 Ultralytics YOLO26 提升 Raspberry Pi 的性能?#
Coral Edge TPU 是一款紧凑型设备,为你的系统添加了 Edge TPU 协处理器。该协处理器实现了低功耗、高性能的机器学习推理,特别针对 TensorFlow Lite 模型进行了优化。在 Raspberry Pi 上,它能将推理速度提升至远超 CPU 单独处理的水平,从而显著增强 Ultralytics YOLO26 模型的性能。
如何在 Raspberry Pi 上安装 Coral Edge TPU 运行时?#
从此链接下载适合你的 Raspberry Pi OS 版本的 .deb 软件包,然后进行安装:
sudo dpkg -i path/to/package.deb请务必按照安装 Edge TPU 运行时部分中的步骤卸载任何以前的 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,该怎么办?#
如果你安装了 TensorFlow 并且需要切换到 tflite-runtime,请先卸载 TensorFlow:
pip uninstall tensorflow tensorflow-aarch64然后安装或更新 tflite-runtime:
pip install -U tflite-runtime有关详细说明,请参考在 Edge TPU 上运行推理部分。
如何使用 Coral Edge TPU 在 Raspberry Pi 上运行导出的 YOLO26 模型推理?#
将 YOLO26 模型导出为与 Edge TPU 兼容的格式后,使用以下代码片段运行推理。模型文件必须保留 _edgetpu.tflite 后缀,以便 Ultralytics 能够在 Edge 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")有关预测模式的综合详细信息,请参见预测页面。