YOLO Vision 2026:

将 YOLO26 模型导出为 TorchScript 以实现快速部署#

PyTorch 正在淘汰 TorchScript

PyTorch has deprecated TorchScript and is gradually removing its features. For new mobile and edge deployments, use the supported ExecuTorch integration. Ultralytics retains regular TorchScript export for legacy C++ compatibility.

Deploying computer vision models in C++ environments without Python requires a portable serialized representation. TorchScript provides that compatibility for legacy LibTorch applications.

Export to TorchScript to serialize your Ultralytics YOLO26 models for cross-platform compatibility and streamlined deployment. In this guide, we'll show you how to export your YOLO26 models to the TorchScript format, making it easier for you to use them across a wider range of applications.

为什么要导出到 TorchScript?#

TorchScript model serialization and deployment workflow overview

Developed by the creators of PyTorch, TorchScript is a powerful tool for optimizing and deploying PyTorch models across a variety of platforms. Exporting YOLO26 models to TorchScript is crucial for moving from research to real-world applications. TorchScript, part of the PyTorch framework, helps make this transition smoother by allowing PyTorch models to be used in environments that don't support Python.

该过程涉及两种技术:追踪 (tracing) 和脚本编写 (scripting)。追踪记录模型执行期间的操作,而脚本编写允许使用 Python 的子集来定义模型。这些技术确保了像 YOLO26 这样的模型即使在它们通常的 Python 环境之外也能发挥其威力。

TorchScript scripting vs tracing comparison

TorchScript 模型还可以通过算子融合和内存使用优化等技术进行优化,从而确保高效执行。导出到 TorchScript 的另一个优势是它能够加速模型在各种硬件平台上的执行。它为你的 PyTorch 模型创建了一个可直接用于生产环境的独立表示形式,并可集成到 C++ 环境中。

TorchScript 模型的主要功能#

TorchScript, a key part of the PyTorch ecosystem, provides powerful features for optimizing and deploying deep learning models.

TorchScript key features overview

以下是使 TorchScript 成为开发者宝贵工具的关键功能:

  • 静态图执行:TorchScript 使用模型计算的静态图表示,这与 PyTorch 的动态图执行不同。在静态图执行中,计算图在实际执行前被定义和编译一次,从而提高了推理性能。

  • 模型序列化:TorchScript 允许你将 PyTorch 模型序列化为平台无关的格式。序列化后的模型无需原始 Python 代码即可加载,从而实现在不同运行时环境中的部署。

  • JIT 编译:TorchScript 使用即时 (JIT) 编译将 PyTorch 模型转换为优化的中间表示。JIT 编译模型的计算图,从而在目标设备上实现高效执行。

  • 逐步转换:TorchScript 提供了一种逐步转换的方法,允许你将 PyTorch 模型的部分内容增量转换为 TorchScript。这种灵活性在处理复杂模型或想要优化特定代码段时特别有用。

TorchScript 中的部署选项#

在查看将 YOLO26 模型导出为 TorchScript 格式的代码之前,让我们先了解 TorchScript 模型通常在何处使用。

TorchScript offers various deployment options for machine learning models, such as:

  • C++ API: The most common use case for TorchScript is its LibTorch C++ API, which allows you to load and execute optimized TorchScript models directly within C++ applications. This is ideal for production environments where Python may not be suitable or available. The C++ API offers low-overhead and efficient execution of TorchScript models, maximizing performance potential.

  • For low-latency, offline inference and data privacy on mobile devices, use ExecuTorch, PyTorch's replacement for TorchScript Mobile.

  • 云端部署:可以使用 TorchServe 等解决方案将 TorchScript 模型部署到基于云的服务器上。它提供了模型版本控制、批处理和指标监控等功能,可用于生产环境中的可扩展部署。使用 TorchScript 进行云端部署可以使你的模型通过 API 或其他 Web 服务被访问。

支持的任务#

TorchScript 导出支持所有七个 Ultralytics 任务。语义分割和深度估计仅在 YOLO26(唯一搭载这些头部网络的系列)中可用。

任务YOLOv8YOLO11YOLO26
检测
分割
语义
深度
分类
Pose
OBB

导出到 TorchScript:转换你的 YOLO26 模型#

将 YOLO26 模型导出到 TorchScript 可以更轻松地在不同地方使用它们,并帮助它们运行得更快、更高效。对于任何希望在实际应用中更有效地使用深度学习模型的人来说,这都很棒。

安装#

要安装所需的软件包,请运行:

安装
# Install the required package for YOLO26
pip install ultralytics

有关安装过程的详细说明和最佳实践,请查阅我们的 Ultralytics Installation guide。在安装 YOLO26 所需的软件包时,如果遇到任何困难,请参考我们的 Common Issues guide 获取解决方案和提示。

用法#

所有Ultralytics YOLO26 模型都旨在开箱即用支持导出,这使得将它们集成到你首选的部署工作流中变得轻而易举。你可以查看支持的导出格式和配置选项完整列表,为你的应用选择最佳设置。

The TorchScript format supports the Export, Predict, and Validate modes. Export your model, then load the exported model to run inference or validate its accuracy.

导出
from ultralytics import YOLO

# Load a YOLO26 model
model = YOLO("yolo26n.pt")

# Export the model to TorchScript format
model.export(format="torchscript")  # creates 'yolo26n.torchscript'
预测
from ultralytics import YOLO

# Load the exported TorchScript model
model = YOLO("yolo26n.torchscript")

# Run inference
results = model("https://ultralytics.com/images/bus.jpg")
验证
from ultralytics import YOLO

# Load the exported TorchScript model
model = YOLO("yolo26n.torchscript")

# Validate accuracy on the COCO8 dataset
metrics = model.val(data="coco8.yaml")

导出参数#

参数类型默认值描述
formatstr'torchscript'导出模型的目标格式,定义了与各种部署环境的兼容性。
imgszinttuple640模型输入的所需图像大小。对于正方形图像可以是整数,或者对于特定尺寸可以是元组 (height, width)
dynamicboolFalse允许动态输入尺寸,增强处理不同图像尺寸时的灵活性。
quantizeintstrNoneQuantization precision: 16 (FP16) requires GPU export with device=0; 32/unset is FP32. Replaces the deprecated half flag.
nmsboolFalse添加非极大值抑制 (NMS),这是准确高效的检测后处理所必需的。
batchint1指定导出模型的批处理推理大小,或导出的模型在 predict 模式下同时处理的最大图像数量。
devicestrNone指定用于导出的设备:GPU(device=0)、CPU(device=cpu)、适用于 Apple 硅芯片的 MPS(device=mps)。

有关导出过程的更多详细信息,请访问 Ultralytics 关于导出的文档页面

部署已导出的 YOLO26 TorchScript 模型#

After successfully exporting your Ultralytics YOLO26 models to TorchScript format, you can now deploy them. The primary and recommended first step for running a TorchScript model is to use the YOLO("model.torchscript") method, as outlined in the previous usage code snippet. For in-depth instructions on deploying your TorchScript models in other settings, take a look at the following resources:

  • Explore Mobile Deployment: Use ExecuTorch's separate torch.export().pte pipeline for current PyTorch mobile deployment.

  • Master Server-Side Deployment: Learn how to deploy models server-side with TorchServe, offering a step-by-step tutorial for scalable, efficient model serving.

  • Implement C++ Deployment: Dive into the Tutorial on Loading a TorchScript Model in C++, facilitating the integration of your TorchScript models into C++ applications for enhanced performance and versatility.

总结#

在本指南中,我们探索了将 Ultralytics YOLO26 模型导出为 TorchScript 格式的过程。通过遵循提供的说明,你可以优化 YOLO26 模型的性能,并获得在各种平台和环境中部署它们的灵活性。

For further details on usage, visit TorchScript's official documentation.

此外,如果你想了解有关其他Ultralytics YOLO26集成的更多信息,请访问我们的integration guide page。你将在那里找到大量有用的资源和见解。

常见问题解答#

  • 将 Ultralytics YOLO26 模型导出为 TorchScript 可以实现灵活的跨平台部署。TorchScript 是 PyTorch 生态系统的一部分,它促进了模型的序列化,随后可以在缺乏 Python 支持的环境中执行。这使得它在 C++ 环境中部署模型非常有用。

  • 要将 YOLO26 模型导出为 TorchScript,你可以使用以下示例代码:

    用法
    from ultralytics import YOLO
    
    # Load a YOLO26 model
    model = YOLO("yolo26n.pt")
    
    # Export the model to TorchScript format
    model.export(format="torchscript")  # creates 'yolo26n.torchscript'
    
    # Load the exported TorchScript model
    torchscript_model = YOLO("yolo26n.torchscript")
    
    # Run inference
    results = torchscript_model("https://ultralytics.com/images/bus.jpg")

    For more details about the export process, refer to the Ultralytics documentation on exporting.

  • 使用 TorchScript 部署 YOLO26 模型有几个优点:

    • 可移植性:导出的模型可以在没有 Python 的 C++ 应用程序中运行。
    • 优化:TorchScript 支持静态图执行和即时 (JIT) 编译,这可以优化模型性能。
    • 跨语言集成:TorchScript 模型可以集成到其他编程语言中,从而增强灵活性和可扩展性。
    • 序列化:模型可以被序列化,从而允许与平台无关的加载和推理。

    For more insights into deployment, visit the TorchServe Documentation and the C++ Deployment Guide. For on-device mobile deployment, PyTorch now recommends ExecuTorch, which uses its own separate torch.export().pte pipeline rather than TorchScript.

  • 要安装导出 YOLO26 模型所需的包,请使用以下命令:

    安装
    # Install the required package for YOLO26
    pip install ultralytics

    For detailed instructions, visit the Ultralytics Installation guide. If any issues arise during installation, consult the Common Issues guide.

  • 在将 YOLO26 模型导出为 TorchScript 格式后,你可以将它们部署到各种平台上:

    • C++ API: Use LibTorch for low-overhead, highly efficient production environments.
    • Mobile Deployment: Use ExecuTorch, PyTorch's supported replacement with a separate .pte export pipeline.
    • Cloud Deployment: Utilize services like TorchServe for scalable server-side deployment.

    探索在这些设置中部署模型的综合指南,以充分利用 TorchScript 的功能。

评论