콘텐츠로 건너뛰기

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 은 JIT(Just-In-Time) 컴파일을 사용하여 PyTorch 모델을 최적화된 중간 표현으로 변환합니다. 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 의 가장 일반적인 사용 사례는 최적화된 TorchScript 모델을 C++ 애플리케이션 내에서 직접 로드하고 실행할 수 있는 C++ API입니다. 이는 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와 같은 솔루션을 사용하여 클라우드 기반 서버에 배포할 수 있습니다. 프로덕션 환경에서 확장 가능한 배포를 위해 모델 버전 관리, 배치 및 메트릭 모니터링과 같은 기능을 제공합니다. 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.

  • 마스터 서버 측 배포: 확장 가능하고 효율적인 모델 제공을 위한 단계별 튜토리얼을 제공하는 TorchServe를 사용하여 서버 측에 모델을 배포하는 방법을 알아보세요.

  • C++ 배포 구현: TorchScript 모델을 C++로 로드하는 튜토리얼을 통해 TorchScript 모델을 C++ 애플리케이션에 통합하여 향상된 성능과 다양한 기능을 활용할 수 있습니다.

요약

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 모바일 문서, TorchServe 문서C++ 배포 가이드를 참조하세요.

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 모바일을 사용하세요.
  • 클라우드 배포: 확장 가능한 서버 측 배포를 위해 TorchServe와 같은 서비스를 활용하세요.

이러한 설정에서 모델을 배포하기 위한 포괄적인 가이드라인을 살펴보고 TorchScript 의 기능을 최대한 활용하세요.

📅 Created 7 months ago ✏️ Updated 22 days ago

댓글