Link to this section使用 ExecuTorch 在移动和边缘设备上部署 YOLO26#
在智能手机、平板电脑和嵌入式系统等边缘设备上部署计算机视觉模型,需要一个能够平衡性能与资源限制的优化运行时。ExecuTorch 作为 PyTorch 的边缘计算解决方案,支持为 Ultralytics YOLO 模型提供高效的端侧推理。
本指南概述了如何将 Ultralytics YOLO 模型导出为 ExecuTorch 格式,助你在移动和边缘设备上部署性能优化的模型。
Link to this section为什么要导出到 ExecuTorch?#
ExecuTorch 是 PyTorch 的端到端解决方案,旨在实现移动和边缘设备上的端侧推理能力。ExecuTorch 以便携和高效为目标构建,可用于在各种计算平台上运行 PyTorch 程序。
Link to this sectionExecuTorch 的主要功能#
ExecuTorch 为在边缘设备上部署 Ultralytics YOLO 模型提供了几项强大的功能:
-
便携式模型格式:ExecuTorch 使用
.pte(PyTorch ExecuTorch) 格式,该格式针对资源受限设备上的尺寸和加载速度进行了优化。 -
XNNPACK 后端:默认与 XNNPACK 集成,可在移动 CPU 上提供高度优化的推理,无需专用硬件即可实现出色的性能。
-
支持量化:ExecuTorch 生态系统支持量化技术,以减小模型体积并提高推理速度;Ultralytics 目前通过 XNNPACK 后端导出 FP32 模型。
-
内存效率:优化的内存管理减少了运行时内存占用,非常适合 RAM 有限的设备。
-
模型元数据:导出的模型包含一个单独的 YAML 文件,其中列出了元数据(图像尺寸、类别名称等),以便轻松集成。
Link to this section使用 ExecuTorch 的部署选项#
ExecuTorch 模型可以部署在各种边缘和移动平台上:
-
移动应用:在 iOS 和 Android 应用上实现原生性能部署,支持在移动 App 中进行实时目标检测。
-
嵌入式系统:在 Raspberry Pi、NVIDIA Jetson 等嵌入式 Linux 设备及其他 ARM 架构系统上以优化性能运行。
-
边缘 AI 设备:使用自定义委托(delegates)在专用边缘 AI 硬件上进行加速推理部署。
-
IoT 设备:集成到 IoT 设备中,实现无需云连接的端侧推理。
Link to this section将 Ultralytics YOLO26 模型导出到 ExecuTorch#
将 Ultralytics YOLO26 模型转换为 ExecuTorch 格式,可实现在移动和边缘设备上的高效部署。
Link to this section安装#
ExecuTorch 导出需要 Python 3.10-3.13 和 PyTorch >= 2.9.0,以及 executorch 软件包:
# Install Ultralytics package
pip install ultralytics有关安装过程的详细说明和最佳实践,请查看我们的 YOLO26 安装指南。在安装 YOLO26 所需软件包时,如果遇到任何困难,请参考我们的 常见问题指南 获取解决方案和建议。
Link to this section用法#
将 YOLO26 模型导出到 ExecuTorch 非常简单:
ExecuTorch 格式支持 导出、预测 和 验证 模式。导出模型后,即可加载导出的模型进行推理或验证其精度。
from ultralytics import YOLO
# Load a YOLO26 model
model = YOLO("yolo26n.pt")
# Export the model to ExecuTorch format
model.export(format="executorch") # creates 'yolo26n_executorch_model'from ultralytics import YOLO
# Load the exported ExecuTorch model
model = YOLO("yolo26n_executorch_model")
# Run inference
results = model("https://ultralytics.com/images/bus.jpg")from ultralytics import YOLO
# Load the exported ExecuTorch model
model = YOLO("yolo26n_executorch_model")
# Validate accuracy on the COCO8 dataset
metrics = model.val(data="coco8.yaml")ExecuTorch 导出过程会生成一个包含 .pte 文件和元数据的目录。在你的移动或嵌入式应用程序中使用 ExecuTorch 运行时来加载 .pte 模型并执行推理。
Link to this section导出参数#
导出到 ExecuTorch 格式时,你可以指定以下参数:
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
format | str | 'executorch' | 导出模型的目标格式,定义了与各种部署环境的兼容性。 |
imgsz | int 或 tuple | 640 | 模型输入的期望图像尺寸。可以是一个用于正方形图像的整数,或者是一个用于特定尺寸的元组 (height, width)。 |
batch | int | 1 | 指定导出模型的推理批次大小,或导出模型在 predict 模式下并发处理的最大图像数量。 |
device | str | None | 指定导出所用的设备:GPU (device=0)、CPU (device=cpu) 或 Apple 芯片的 MPS (device=mps)。 |
Link to this section输出结构#
ExecuTorch 导出将创建一个包含模型和元数据的目录:
yolo26n_executorch_model/
├── model.pte # ExecuTorch model file
└── metadata.yaml # Model metadata (classes, image size, etc.)Link to this section使用导出的 ExecuTorch 模型#
导出模型后,你需要使用 ExecuTorch 运行时将其集成到目标应用程序中。
Link to this section移动端集成#
对于移动应用程序 (iOS/Android),你需要:
- 添加 ExecuTorch 运行时:将 ExecuTorch 运行时库包含在你的移动项目中
- 加载模型:在应用程序中加载
.pte文件 - 运行推理:处理图像并获取预测结果
iOS 集成示例 (Objective-C/C++):
// iOS uses C++ APIs for model loading and inference
// See https://pytorch.org/executorch/stable/using-executorch-ios.html for complete examples
#include <executorch/extension/module/module.h>
using namespace ::executorch::extension;
// Load the model
Module module("/path/to/model.pte");
// Create input tensor
float input[1 * 3 * 640 * 640];
auto tensor = from_blob(input, {1, 3, 640, 640});
// Run inference
const auto result = module.forward(tensor);Android 集成示例 (Kotlin):
import org.pytorch.executorch.EValue
import org.pytorch.executorch.Module
import org.pytorch.executorch.Tensor
// Load the model
val module = Module.load("/path/to/model.pte")
// Prepare input tensor
val inputTensor = Tensor.fromBlob(floatData, longArrayOf(1, 3, 640, 640))
val inputEValue = EValue.from(inputTensor)
// Run inference
val outputs = module.forward(inputEValue)
val scores = outputs[0].toTensor().dataAsFloatArrayLink to this section嵌入式 Linux#
对于嵌入式 Linux 系统,请使用 ExecuTorch C++ API:
#include <executorch/extension/module/module.h>
#include <executorch/extension/tensor/tensor.h>
using namespace ::executorch::extension;
// Load model
Module module("model.pte");
// Prepare input
std::vector<float> input_data = preprocessImage(image);
auto input_tensor = from_blob(input_data.data(), {1, 3, 640, 640});
// Run inference
const auto outputs = module.forward(input_tensor);有关将 ExecuTorch 集成到你的应用程序中的更多详细信息,请访问 ExecuTorch 文档。
Link to this section性能优化#
Link to this section模型尺寸优化#
要减小模型体积以进行部署:
- 使用更小的模型:从 YOLO26n (nano) 开始,以获得最小的内存占用
- 降低输入分辨率:使用较小的图像尺寸(例如
imgsz=320或imgsz=416) - 量化:应用量化技术(未来 ExecuTorch 版本将支持)
Link to this section推理速度优化#
为了获得更快的推理速度:
- XNNPACK 后端:默认的 XNNPACK 后端提供优化的 CPU 推理
- 硬件加速:使用特定平台的委托(例如 iOS 的 CoreML)
- 批处理:尽可能同时处理多张图像
Link to this section基准测试#
Ultralytics 团队对 YOLO26 模型进行了基准测试,比较了 PyTorch 与 ExecuTorch 之间的速度和精度。
| 模型 | 格式 | 状态 | 大小 (MB) | 指标/mAP50-95(B) | 推理时间 (ms/im) |
|---|---|---|---|---|---|
| YOLO26n | PyTorch | ✅ | 5.3 | 0.4790 | 314.80 |
| YOLO26n | ExecuTorch | ✅ | 9.4 | 0.4800 | 142 |
| YOLO26s | PyTorch | ✅ | 19.5 | 0.5730 | 930.90 |
| YOLO26s | ExecuTorch | ✅ | 36.5 | 0.5780 | 376.1 |
推理时间不包括预处理/后处理。
Link to this section故障排除#
Link to this section常见问题#
问题:Python version error
解决方案:ExecuTorch 需要 Python 3.10 或更高版本。请升级你的 Python 安装:
# Using conda
conda create -n executorch python=3.10
conda activate executorch问题:Export fails during first run
解决方案:确保安装了最新的预构建 executorch wheel:
pip install --upgrade executorch问题:Import errors for ExecuTorch modules
解决方案:确保已正确安装 ExecuTorch:
pip install executorch --force-reinstall如需更多故障排除帮助,请访问 Ultralytics GitHub Issues 或 ExecuTorch 文档。
Link to this section总结#
将 YOLO26 模型导出到 ExecuTorch 格式可实现移动和边缘设备上的高效部署。凭借 PyTorch 原生集成、跨平台支持和优化性能,ExecuTorch 是边缘 AI 应用的绝佳选择。
关键要点:
- ExecuTorch 提供具有出色性能的 PyTorch 原生边缘部署
- 通过
format='executorch'参数即可简单导出 - 模型通过 XNNPACK 后端为移动 CPU 进行优化
- 支持 iOS、Android 和嵌入式 Linux 平台
- 需要 Python 3.10-3.13 和 PyTorch >= 2.9.0
Link to this section常见问题解答#
Link to this section如何将 YOLO26 模型导出为 ExecuTorch 格式?#
使用 Python 或 CLI 将 YOLO26 模型导出到 ExecuTorch:
from ultralytics import YOLO
model = YOLO("yolo26n.pt")
model.export(format="executorch")或
yolo export model=yolo26n.pt format=executorchLink to this sectionExecuTorch 导出的系统要求是什么?#
ExecuTorch 导出要求:
- Python 3.10 或更高版本
executorch软件包 (通过pip install executorch安装)- PyTorch (随 ultralytics 自动安装)
注意:executorch 软件包附带预构建的 wheel(包含 XNNPACK 后端),因此导出期间无需额外的编译步骤。
Link to this section我可以直接在 Python 中使用 ExecuTorch 模型运行推理吗?#
ExecuTorch 模型可以直接通过 YOLO() 加载,以便在 Python 中进行推理和验证(请参阅上面的“预测/验证”示例),它们也可以使用 ExecuTorch 运行时库部署在移动和边缘设备上。
Link to this sectionExecuTorch 支持哪些平台?#
ExecuTorch 支持:
- 移动端:iOS 和 Android
- 嵌入式 Linux:Raspberry Pi、NVIDIA Jetson 以及其他 ARM 设备
- 桌面端:Linux、macOS 和 Windows(用于开发)
Link to this sectionExecuTorch 在移动端部署方面与 TFLite 相比如何?#
ExecuTorch 和 TFLite 都是优秀的移动端部署方案:
- ExecuTorch:更好的 PyTorch 集成,原生的 PyTorch 工作流,生态系统正在不断发展
- TFLite:更成熟,硬件支持更广泛,部署示例更多
如果你已经在用 PyTorch 并希望获得原生部署路径,请选择 ExecuTorch。如果追求最大兼容性和成熟的工具链,请选择 TFLite。
Link to this section我可以在 ExecuTorch 模型中使用 GPU 加速吗?#
可以!ExecuTorch 通过各种后端支持硬件加速:
- 移动 GPU:通过 Vulkan、Metal 或 OpenCL 代理
- NPU/DSP:通过特定于平台的代理
- 默认:用于优化 CPU 推理的 XNNPACK
请参考 ExecuTorch Documentation 获取特定于后端的设置信息。