コンテンツへスキップ

YOLO11 Model Export to TorchScript for Quick Deployment

Deploying computer vision models across different environments, including embedded systems, web browsers, or platforms with limited Python support, requires a flexible and portable solution. TorchScript focuses on portability and the ability to run models in environments where the entire Python framework is unavailable. This makes it ideal for scenarios where you need to deploy your computer vision capabilities across various devices or platforms.

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

なぜTorchScript 。

Torchscript 概要

Developed by the creators of PyTorch, TorchScript is a powerful tool for optimizing and deploying PyTorch models across a variety of platforms. Exporting YOLO11 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.

The process involves two techniques: tracing and scripting. Tracing records operations during model execution, while scripting allows for the definition of models using a subset of Python. These techniques ensure that models like YOLO11 can still work their magic even outside their usual Python environment.

TorchScript スクリプトとトレース

TorchScript モデルは、演算子融合やメモリ使用量の改良などの技術によって最適化され、効率的な実行を保証することもできます。TorchScript にエクスポートするもう一つの利点は、様々なハードウェアプラットフォームでモデルの実行を高速化できる可能性があることです。C++環境、組み込みシステム、またはウェブやモバイルアプリケーションに統合できる、PyTorch モデルのスタンドアロンで量産可能な表現が作成されます。

TorchScript モデルの主な特徴

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

TorchScript 特徴

ここでは、TorchScript を開発者にとって価値あるツールにしている主な機能を紹介する:

  • 静的グラフ実行:TorchScript は、モデルの計算を静的グラフで表現しており、PyTorch の動的グラフ実行とは異なる。静的グラフ実行では、計算グラフは実際の実行の前に一度定義され、コンパイルされる。

  • モデルのシリアライズ:TorchScript では、PyTorch のモデルをプラットフォームに依存しない形式にシリアライズすることができます。シリアライズされたモデルは、オリジナルのPython 。

  • JITコンパイル:TorchScript は、PyTorch のモデルを最適化された中間表現に変換するために、ジャスト・イン・タイム(JIT)コンパイルを使用します。JITはモデルの計算グラフをコンパイルし、ターゲット・デバイス上での効率的な実行を可能にする。

  • 言語間の統合:TorchScript を使えば、PyTorch のモデルを、C++、Java、JavaScript などの他の言語にエクスポートすることができる。これにより、PyTorch のモデルを、異なる言語で書かれた既存のソフトウェア・システムに統合することが容易になります。

  • 段階的な変換:TorchScript は段階的な変換アプローチを提供し、PyTorch モデルの一部を段階的にTorchScript に変換することができます。この柔軟性は、複雑なモデルを扱う場合や、コードの特定の部分を最適化したい場合に特に役立ちます。

配備オプションTorchScript

Before we look at the code for exporting YOLO11 models to the TorchScript format, let's understand where TorchScript models are normally used.

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

  • C++ API:TorchScript の最も一般的な使用例は、その C++ API である。この API を使用すると、最適化されたTorchScript モデルを C++ アプリケーション内で直接ロードして実行することができる。これは、Python が適していなかったり、利用できなかったりする本番環境に最適です。C++ APIは、TorchScript モデルの低オーバーヘッドかつ効率的な実行を提供し、潜在的なパフォーマンスを最大限に引き出します。

  • Mobile Deployment: TorchScript offers tools for converting models into formats readily deployable on mobile devices. PyTorch Mobile provides a runtime for executing these models within iOS and Android apps. This enables low-latency, offline inference capabilities, enhancing user experience and data privacy.

  • クラウドデプロイメント:TorchScript のモデルは、TorchServeのようなソリューションを使ってクラウドベースのサーバーにデプロイできる。TorchServeは、モデルのバージョン管理、バッチ処理、本番環境でのスケーラブルなデプロイのためのメトリクス監視などの機能を提供します。TorchScript を使ったクラウドデプロイメントにより、APIやその他のウェブサービス経由でモデルにアクセスできるようになります。

Export to TorchScript: Converting Your YOLO11 Model

Exporting YOLO11 models to TorchScript makes it easier to use them in different places and helps them run faster and more efficiently. This is great for anyone looking to use deep learning models more effectively in real-world applications.

インストール

必要なパッケージをインストールするには、以下を実行する:

インストール

# Install the required package for YOLO11
pip install ultralytics

For detailed instructions and best practices related to the installation process, check our Ultralytics Installation guide. While installing the required packages for YOLO11, if you encounter any difficulties, consult our Common Issues guide for solutions and tips.

使用方法

Before diving into the usage instructions, it's important to note that while all Ultralytics YOLO11 models are available for exporting, you can ensure that the model you select supports export functionality here.

使用方法

from ultralytics import YOLO

# Load the YOLO11 model
model = YOLO("yolo11n.pt")

# Export the model to TorchScript format
model.export(format="torchscript")  # creates 'yolo11n.torchscript'

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

# Run inference
results = torchscript_model("https://ultralytics.com/images/bus.jpg")
# Export a YOLO11n PyTorch model to TorchScript format
yolo export model=yolo11n.pt format=torchscript  # creates 'yolo11n.torchscript'

# Run inference with the exported model
yolo predict model=yolo11n.torchscript source='https://ultralytics.com/images/bus.jpg'

エクスポートプロセスの詳細については、Ultralytics ドキュメントのエクスポートに関するページをご覧ください。

Deploying Exported YOLO11 TorchScript Models

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

  • Explore Mobile Deployment: The PyTorch Mobile Documentation provides comprehensive guidelines for deploying models on mobile devices, ensuring your applications are efficient and responsive.

  • マスターサーバー側の展開:スケーラブルで効率的なモデル配信のためのステップバイステップのチュートリアルを提供します。

  • C++の導入:C++ でのTorchScript モデルのロードに関するチュートリアルをご覧ください。C++ アプリケーションへのTorchScript モデルの統合が容易になり、パフォーマンスと汎用性が向上します。

概要

In this guide, we explored the process of exporting Ultralytics YOLO11 models to the TorchScript format. By following the provided instructions, you can optimize YOLO11 models for performance and gain the flexibility to deploy them across various platforms and environments.

使い方の詳細については、TorchScript の公式ドキュメントをご覧ください。

Also, if you'd like to know more about other Ultralytics YOLO11 integrations, visit our integration guide page. You'll find plenty of useful resources and insights there.

よくあるご質問

What is Ultralytics YOLO11 model export to TorchScript?

Exporting an Ultralytics YOLO11 model to TorchScript allows for flexible, cross-platform deployment. TorchScript, a part of the PyTorch ecosystem, facilitates the serialization of models, which can then be executed in environments that lack Python support. This makes it ideal for deploying models on embedded systems, C++ environments, mobile applications, and even web browsers. Exporting to TorchScript enables efficient performance and wider applicability of your YOLO11 models across diverse platforms.

How can I export my YOLO11 model to TorchScript using Ultralytics?

To export a YOLO11 model to TorchScript, you can use the following example code:

使用方法

from ultralytics import YOLO

# Load the YOLO11 model
model = YOLO("yolo11n.pt")

# Export the model to TorchScript format
model.export(format="torchscript")  # creates 'yolo11n.torchscript'

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

# Run inference
results = torchscript_model("https://ultralytics.com/images/bus.jpg")
# Export a YOLO11n PyTorch model to TorchScript format
yolo export model=yolo11n.pt format=torchscript  # creates 'yolo11n.torchscript'

# Run inference with the exported model
yolo predict model=yolo11n.torchscript source='https://ultralytics.com/images/bus.jpg'

エクスポート処理の詳細については、Ultralytics のエクスポートに関するドキュメントを参照してください。

Why should I use TorchScript for deploying YOLO11 models?

Using TorchScript for deploying YOLO11 models offers several advantages:

  • 移植性:エクスポートされたモデルは、C++アプリケーション、組み込みシステム、モバイル機器など、Python を必要としない環境で実行できます。
  • 最適化:TorchScript は、静的グラフ実行とJIT(Just-In-Time)コンパイルをサポートしており、モデルのパフォーマンスを最適化することができます。
  • 言語間の統合:TorchScript のモデルは他のプログラミング言語に統合することができ、柔軟性と拡張性を高めます。
  • シリアライズ:モデルをシリアライズすることで、プラットフォームに依存しないロードと推論が可能になる。

配備に関する詳細については、PyTorch Mobile DocumentationTorchServe DocumentationC++ Deployment Guideをご覧ください。

What are the installation steps for exporting YOLO11 models to TorchScript?

To install the required package for exporting YOLO11 models, use the following command:

インストール

# Install the required package for YOLO11
pip install ultralytics

詳しい手順については、Ultralytics インストールガイドをご覧ください。インストール中に問題が発生した場合は、「よくある問題」ガイドを参照してください。

How do I deploy my exported TorchScript YOLO11 models?

After exporting YOLO11 models to the TorchScript format, you can deploy them across a variety of platforms:

  • C++ API:低オーバーヘッドで高効率な生産環境に最適。
  • モバイル展開:iOS およびAndroid アプリケーションにはPyTorch Mobileを使用する。
  • クラウド展開:スケーラブルなサーバーサイドデプロイメントにTorchServeのようなサービスを活用する。

TorchScript の機能を最大限に活用するため、これらの環境におけるモデル展開の包括的なガイドラインを探る。

📅 Created 7 months ago ✏️ Updated 22 days ago

コメント