Yolo Vision Shenzhen
Шэньчжэнь
Присоединиться сейчас
Глоссарий

ONNX (Открытая биржа нейронных сетей)

Узнайте, как ONNX улучшает переносимость и совместимость моделей ИИ, обеспечивая беспрепятственное развертывание моделей Ultralytics YOLO на различных платформах.

ONNX (Open Neural Network Exchange) is an open-source format designed to represent machine learning models, allowing interoperability between various AI frameworks and tools. It serves as a universal translator for deep learning, enabling developers to build models in one framework—such as PyTorch, TensorFlow, or Scikit-learn—and seamlessly deploy them in another environment optimized for inference. By defining a common set of operators and a standard file format, ONNX eliminates the need for complex, custom conversion scripts that were historically required to move models from research to production. This flexibility is crucial for modern AI workflows, where training might occur on powerful cloud GPUs while deployment targets diverse hardware like edge devices, mobile phones, or web browsers.

The Role of ONNX in Modern AI

In the rapidly evolving landscape of artificial intelligence, researchers and engineers often use different tools for different stages of the development lifecycle. A data scientist might prefer the flexibility of PyTorch for experimentation and training, while a production engineer needs the optimized performance of TensorRT or OpenVINO for deployment. Without a standard exchange format, moving a model between these ecosystems is difficult and error-prone.

ONNX bridges this gap by providing a shared definition of the computation graph. When a model is exported to ONNX, it is serialized into a format that captures the network structure (layers, connections) and parameters (weights, biases) in a framework-agnostic way. This allows inference engines specifically tuned for hardware acceleration—such as the ONNX Runtime—to execute the model efficiently across multiple platforms, including Linux, Windows, macOS, Android, and iOS.

Key Benefits of Using ONNX

Adopting the Open Neural Network Exchange format offers several strategic advantages for AI projects:

  • Framework Interoperability: Developers can switch between frameworks without getting locked into a single ecosystem. You can train a model using the user-friendly Ultralytics Python API and export it for use in a C++ application or a web-based JavaScript environment.
  • Hardware Optimization: Many hardware manufacturers provide specialized execution providers that interface with ONNX. This means a single .onnx file can be accelerated on NVIDIA GPUs, Intel CPUs, or mobile NPUs (Neural Processing Units) using tools like OpenVINO or CoreML.
  • Faster Inference: The ONNX Runtime applies graph optimizations—such as node fusion and constant folding—that can significantly reduce inference latency. This is essential for real-time applications like autonomous vehicles or high-speed manufacturing lines.
  • Simplified Deployment: Instead of maintaining separate deployment pipelines for every training framework, engineering teams can standardize on ONNX as the delivery format, streamlining ModelOps processes.

Применение в реальном мире

The versatility of ONNX makes it a staple in diverse industries. Here are two concrete examples of its application:

1. Edge AI on Mobile Devices

Consider a mobile application designed for real-time crop health monitoring. The model might be trained on a powerful cloud server using a large dataset of plant images. However, the app needs to run offline on a farmer's smartphone. By exporting the trained model to ONNX, developers can integrate it into the mobile app using ONNX Runtime Mobile. This allows the phone's processor to run object detection locally, identifying pests or diseases instantly without needing an internet connection.

2. Cross-Platform Web Inference

In e-commerce, a "virtual try-on" feature might use pose estimation to overlay clothing on a user's webcam feed. Training this model might happen in Python, but the deployment target is a web browser. Using ONNX, the model can be converted and run directly in the user's browser via ONNX Runtime Web. This utilizes the client's device capabilities (WebGL or WebAssembly) to perform computer vision tasks, ensuring a smooth, privacy-preserving experience since video data never leaves the user's computer.

Сравнение со связанными терминами

It is helpful to distinguish ONNX from other model formats and tools:

  • vs. TensorRT: While ONNX is an exchange format, TensorRT is an inference engine and optimizer specifically for NVIDIA GPUs. A common workflow involves exporting a model to ONNX first, and then parsing that ONNX file into TensorRT to achieve maximum throughput on NVIDIA hardware.
  • vs. TensorFlow SavedModel: SavedModel is the native serialization format for TensorFlow. While robust within the Google ecosystem, it is less universally compatible than ONNX. Tools often exist to convert SavedModels into ONNX to gain broader platform support.
  • vs. CoreML: CoreML is Apple's framework for on-device machine learning. While distinct, models are frequently converted from PyTorch to ONNX, and then from ONNX to CoreML (or directly) to run efficiently on iPhones and iPads.

Экспорт в ONNX Ultralytics

The Ultralytics ecosystem simplifies the process of converting state-of-the-art models like YOLO26 into the ONNX format. The export functionality is built directly into the library, handling the complex graph traversal and operator mapping automatically.

The following example demonstrates how to export a pre-trained YOLO26 model to ONNX format for deployment:

from ultralytics import YOLO

# Load the YOLO26n model (Nano version recommended for edge deployment)
model = YOLO("yolo26n.pt")

# Export the model to ONNX format
# The 'dynamic' argument enables variable input sizes
path = model.export(format="onnx", dynamic=True)

print(f"Model exported successfully to: {path}")

Once exported, this .onnx file can be utilized in the Платформа Ultralytics for management or deployed directly to edge devices using the ONNX Runtime, making high-performance computer vision accessible in virtually any environment.

Присоединяйтесь к сообществу Ultralytics

Присоединяйтесь к будущему ИИ. Общайтесь, сотрудничайте и развивайтесь вместе с мировыми новаторами

Присоединиться сейчас