如何在 Raspberry Pi 上使用 Coral Edge TPU 运行 Ultralytics YOLO26#
Raspberry Pi 是一个节能且经济实惠的平台,适合在边缘运行计算机视觉任务,但即使使用 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 模型提供低功耗、高性能的 ML 推理。它非常适合嵌入式和移动部署,在这些场景中,单靠 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 位)(推荐)
- Coral USB Accelerator
- 用于导出模型的非 ARM 平台(Google Colab、x86_64 Linux 计算机或 Ultralytics Docker 容器),因为 Edge TPU 编译器不适用于 ARM
本指南假设你已经安装了可正常运行的 Raspberry Pi OS,并安装了 ultralytics 及其依赖项。如果没有,请先按照快速入门指南操作。
准备好前置条件后,工作流程分为三个步骤:在 Pi 上安装 Edge TPU 运行时,在非 ARM 计算机上导出模型,然后在 Pi 上运行推理。
安装 Edge TPU 运行时#
该运行时提供多个构建版本,因此请选择与你的操作系统匹配的版本。高频构建版本会以更高的时钟频率运行 Edge TPU,从而获得更好的性能,但可能导致热降频;如果选择该版本,请使用某种散热方式。
| Raspberry Pi OS | 高频模式 | 要下载的版本 |
|---|---|---|
| Bullseye 32 位 | 否 | libedgetpu1-std_ ... .bullseye_armhf.deb |
| Bullseye 64 位 | 否 | libedgetpu1-std_ ... .bullseye_arm64.deb |
| Bullseye 32 位 | 是 | libedgetpu1-max_ ... .bullseye_armhf.deb |
| Bullseye 64 位 | 是 | libedgetpu1-max_ ... .bullseye_arm64.deb |
| Bookworm 32 位 | 否 | libedgetpu1-std_ ... .bookworm_armhf.deb |
| Bookworm 64 位 | 否 | libedgetpu1-std_ ... .bookworm_arm64.deb |
| Bookworm 32 位 | 是 | libedgetpu1-max_ ... .bookworm_armhf.deb |
| Bookworm 64 位 | 是 | 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,请将模型转换为兼容格式。在非 ARM 平台上执行导出操作,例如 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 会将其作为普通 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/yolo26n_full_integer_quant_edgetpu.tflite") # Load an official model or custom model
# Run Prediction
model.predict("path/to/source.png")请在 Predict 页面查看完整的预测模式详细信息。
如果你有多个 Edge TPU,可以通过 device 参数选择特定的设备。
from ultralytics import YOLO
# Load a model
model = YOLO("path/to/yolo26n_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 位和 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 是一款紧凑型设备,可为你的系统添加 Edge TPU 协处理器。该协处理器支持低功耗、高性能的 ML 推理,尤其针对 TensorFlow Lite 模型进行了优化。在 Raspberry Pi 上,它能够将推理速度提升到远超 CPU 单独运行时的水平,从而显著提升 Ultralytics YOLO26 模型的性能。
从此链接下载适用于你的 Raspberry Pi OS 版本的
.deb软件包,然后进行安装:sudo dpkg -i path/to/package.deb请按照安装 Edge TPU 运行时部分中的步骤,确保卸载所有以前版本的 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")如需了解更多信息,请参阅导出模式文档。
如果你已安装 TensorFlow 并需要切换到
tflite-runtime,请先卸载 TensorFlow:pip uninstall tensorflow tensorflow-aarch64然后安装或更新
tflite-runtime:pip install -U tflite-runtime如需详细说明,请参阅在 Edge TPU 上运行推理部分。
将 YOLO26 模型导出为 Edge TPU 兼容格式后,使用以下代码片段运行推理。模型文件必须保留
_edgetpu.tflite后缀,以便 Ultralytics 将其加载到 Edge TPU 上:运行模型from ultralytics import YOLO # Load a model model = YOLO("path/to/yolo26n_full_integer_quant_edgetpu.tflite") # Load an official model or custom model # Run Prediction model.predict("path/to/source.png")有关预测模式的完整详细信息,请参阅 Predict 页面。