Back to Blogs

ONNX Standardized Format: The Universal Translator for AI Models

August 15, 2024
5 mins
blog image

Modern artificial intelligence (AI) is moving beyond the traditional machine learning (ML) models involving straightforward statistical calculations. With the emergence of advanced computational resources and big data, AI frameworks are now more sophisticated, performing complex inferences on extensive datasets.

However, as model complexity increases, so does the need for interoperability as developers begin using multiple frameworks to build, test, and deploy AI systems. Also, using AI-based solutions with legacy infrastructure calls for solutions that allow businesses to seamlessly integrate AI tools with existing tech stack. This lack of interoperability often results in time-consuming and error-prone conversion processes, creating significant obstacles to the smooth deployment of AI solutions.

Enter the Open Neural Network Exchange (ONNX) framework. ONNX addresses this interoperability challenge by offering a standardized, open-source format for representing AI models. With ONNX, developers can build, share, and run models across various platforms without worrying about compatibility issues, thereby streamlining the entire model development and deployment lifecycle.

In this article, we will discuss in detail what ONNX is and its key features, benefits, challenges, and best practices. It will help you understand how to use ONNX optimally to streamline your model development lifecycle. 

What is ONNX?

ONNX is a unified open-source format designed to enable interoperability between different AI and ML algorithms. A standard format allows users to execute models between multiple frameworks without implementing complex conversion pipelines.

Originally developed by Facebook and Microsoft in 2017, ONNX has gained support from numerous tech giants, including IBM, Intel, and Qualcomm. 

Traditionally, developers used the HD5 format to save a model in Keras, the SavedModel format to store the model in TensorFlow, and Pickle for Scikit-Learn. These formats are framework-specific, and their support in other development environments is limited.

ONNX lets you overcome these limitations through the following key features:

  • Open-source: ONNX is an open-source project on GitHub, with a large and active community that contributed to the development and enhancement of the framework’s ecosystem.
  • Standardized Format: Standardization allows developers to use an ONNX-based model with any framework to provide smooth cross-platform integrations.
  • Conversion Tools: ONNX includes extensive tools and APIs that enhance the ML lifecycle. For instance, it supports multiple libraries that enable the conversions of models built in popular frameworks such as TensorFlow, Keras, and PyTorch to ONNX.
  • Visualization and Optimization Libraries: ONNX offers tools to visualize models and provides optimization solutions to remove redundant nodes and improve performance. Users can also deploy ONNX models using runtime libraries that support multiple hardware such as CPUs, GPUs, and accelerators.
  • Interoperability: ONNX enables seamless import and export models across multiple ML frameworks. The ability enables developers to leverage the strengths of a particular framework during model development, convert the model into ONNX, and export it to a suitable lightweight and low-latency runtime environment.
  • Focus on Inference: ONNX Runtime is a tool for efficiently deploying machine learning models in production with faster inferencing and broad compatibility with different hardware platforms.
  • Format Flexibility: The ONNX standard supports traditional and state-of-the-art (SOTA) deep learning models such as complex computer vision (CV) and natural language processing (NLP) architectures.
  • Performance Optimizations: ONNX Runtime supports multiple performance-enhancing graph optimizations through node elimination and fusion techniques, which help improve model execution efficiency.

Popular Frameworks Compatible with ONNX

ONNX supports multiple frameworks that let developers build a wide range of deep learning and traditional machine learning models more flexibly and efficiently.

The following list highlights a few of the popular frameworks that are compatible with ONNX.

  • PyTorch: Meta's PyTorch is a Python-based library that offers robust GPU-accelerated tensor computation to build complex CV and NLP models. PyTorch is particularly favored for its dynamic computational graph (also known as reverse-mode auto-differentiation), which allows developers to modify the graph on the fly.
  • TensorFlow: Google’s TensorFlow is an end-to-end ML framework that offers intuitive APIs to build AI applications. It offers tools for developing and deploying models across various environments, including edge devices, the web, and mobile platforms. TensorFlow also includes utilities for creating input pipelines for data preprocessing.
  • Scikit-Learn: Scikit-Learn is a Python-based platform for building traditional ML models for classification, regression, and clustering. It also offers tools for dimensionality reduction, model selection, and data preprocessing, making it a comprehensive framework for standard ML tasks.
  • Keras: Keras is a high-level API for developing ML-powered apps with straightforward code that is quick to debug, deploy, and maintain.
  • Microsoft Cognitive Toolkit (CNTK): CNTK is an open-source deep-learning library representing neural networks through directed computational graphs. The feature makes CNTK suitable for building architectures involving feed-forward, convolutional, and recurrent neural nets.

Converting Models to ONNX

ONNX offers libraries to convert models in different frameworks to ONNX format. The format consists of an ONNX graph that describes the ML model through mathematical operations. The operations transform input features to generate relevant predictions.

For instance, a developer may create a linear regression model in Python and convert it to an ONNX graph. The model is a function of three variables, an addition, and a multiplication operation.

blog_image_8960

Linear Regression Model

Converting it to ONNX means using ONNX operators to represent the model in a standard graph that the developer can run and execute on any platform. The conversion involves writing the linear regression model in the ONNX language to declare the variables, define nodes, create the graph, and add relevant metadata.

blog_image_9534

ONNX Graph for Linear Regression Model

Although developers can manually write models in the ONNX language, a more convenient alternative is using pre-built ONNX conversion libraries. These libraries automatically convert Python-based models in supported frameworks to ONNX format.

The following is a brief list of conversion libraries:

  • sklearn-onnx: Helps convert scikit-learn models
  • tf2onnx: Enables developers to transform TensorFlow, Keras, TensorFlow.js, and Tflite models to ONNX.
  • onnx-coreml: Facilitates the conversion of ONNX models to CoreML format.
  • torch.onnx: Supports converting Pytorch-based models to ONNX.

YOLOv8 to ONNX Example

YOLOv8 is an open-source PyTorch-based CV model by Ultralytics. It helps you with object detection and tracking, image classification, pose estimation, and instance segmentation tasks.

blog_image_11621

CV Tasks in YOLOv8

Converting the model to ONNX format is straightforward. The following code snippet from Ultralytics demonstrates you can quickly export and run YOLO-v8 in ONNX.

from ultralytics import YOLO

# Load the YOLOv8 model
model = YOLO("yolov8n.pt")

# Export the model to ONNX formatmodel.export(format="onnx") # creates 'yolov8n.onnx'

# Load the exported ONNX model
onnx_model = YOLO("yolov8n.onnx")

# Run inference
results = onnx_model("https://ultralytics.com/images/bus.jpg")

Curious how YOLO works? Learn more about the algorithm in our detailed guide on YOLO Object Detection

Pre-Trained Models in ONNX

While you can convert models to ONNX, the ONNX Model Zoo is a GitHub repository that offers multiple pretrained CV, NLP, Generative AI, and Graph ML models in ONNX format. The source of the models includes open-source repositories such as transformers, torchvision, timm, and torch_hub.

Vision models include frameworks for image classification, object detection, image segmentation, pose estimation, and image manipulation tasks. Language models include machine translation and comprehension algorithms. Lastly, it offers models for speech recognition and visual question answering (VQA) tasks.

Optimizing ONNX Models

Developers can optimize ONNX models for better performance using ONNX Optimizer - an open-source library based on C++. The framework helps developers perform arbitrary optimizations that require custom backend information.

In addition, ONNX Runtime is the official ONNX production engine, which lets you tailor ONNX-based deployments to specific hardware across multiple platforms. The framework applies relevant optimizations to run models efficiently on CPUs, GPUs, and accelerators.

Developers can use ONNX Runtime to deploy models on web, mobile, edge, and cloud-based environments. The library also allows them to boost training speed and accuracy for large language models (LLMs) and perform on-device model learning to protect user privacy.

Applications of ONNX Models in Computer Vision

ONNX models are versatile and flexible frameworks that help you build and deploy models for multiple use cases. Below is a list of ONNX-based CV applications in various domains.

  • ONNX for Image Classification: ONNX models can perform complex image classification tasks, such as classifying medical images to diagnose diseases.

blog_image_15596

Medical Image Classification

  • ONNX for Object Detection: CV applications often require object detection models with high inference speeds. For instance, models for self-driving cars must recognize objects in real-time without delay. Developers can achieve such performance by deploying ONNX models tailored to specific hardware limitations.

blog_image_16306

Object Detection in self-driving cars

  • ONNX for Segmentation: Authorities can use ONNX models to perform segmentation tasks for urban planning. These models may require deployment in satellites and drones, where ONNX can optimize inferencing performance through on-device processing.

blog_image_16891

a) Urban Planning Map, b) Semantic Segmentation of Map

  • ONNX for Facial Recognition: Robust security systems require powerful facial recognition algorithms to verify identities and restrict access. ONNX models can help developers optimize model deployment on edge devices such as cameras and sensors.

blog_image_17627

Facial Recognition


Benefits and Challenges of ONNX Models

Through a unified standard, ONNX offers an efficient cross-platform framework to deploy ML models. However, despite its advantages, ONNX has a few challenges.

Users should understand these benefits, challenges, and mitigation strategies to use ONNX to its full potential.

ONNX Benefits

  • Interoperability Across Frameworks: The key advantage of using ONNX is that it allows developers to build, execute, and run models across various platforms.
  • Flexibility in Deployment: The framework facilitates quick deployment and inference across different hardware types.
  • Optimization For Performance: ONNX models can be deployed across a wide range of platforms, including cloud, edge, mobile, and on-premises environments. This flexibility makes it easier to integrate AI models into diverse production settings.
  • Hardware Agnostic: Developers can run ONNX models on multiple hardware, including CPUs, GPUs, and accelerators. Relevant libraries tailor ONNX models to specific hardware requirements for streamlined development.
  • No Vendor Lock-in: Dependency on a single vendor’s ecosystem limits the functionalities a model can perform. ONNX frees developers from these restrictions and allows them to use the most suitable platform for a specific use case.

ONNX Challenges

  • Model Conversion Complexity: While ONNX offers multiple libraries for model conversion, transforming complex architectures into graphs can still be challenging. Certain features or layers in a model might not be fully supported, leading to potential loss of fidelity or requiring additional manual adjustments.
  • Performance Degradation: Converting models to ONNX may result in performance loss when compared to models natively built and run in their original frameworks. This can be particularly noticeable in highly optimized environments or when using specialized hardware. However, using the ONNX-Runtime library to optimize hardware-specific performance can address performance issues.
  • Difficulty in Debugging and Troubleshooting: Due to limited support and expertise, debugging in native and legacy frameworks can be relatively easier than in the ONNX language. However, visualization tools with detailed logging can help developers find issues more effectively.
  • Dependency on Third-Party Tools: ONNX relies on various third-party tools and libraries for conversion and optimization. Compatibility issues between these tools, or lack of support for specific model features, can create additional hurdles for developers.
  • Evolving Community: As the ONNX platform evolves, instability and backward incompatibility may be frequent issues. Developers must keep track of the latest developments and follow community forums to stay updated regarding new releases.
  • Model Size: After converting to ONNX, the model's size may increase. Model compression and regularization techniques can help reduce model sizes.

Best Practices for Deploying ONNX Models

Effective ONNX model deployment requires experts to use appropriate strategies to maximize the benefits of ONNX frameworks. Below are a few best practices that will help you streamline ONNX deployments for more efficient results.

  • Select the Right Runtime: Use the ONNX Runtime library to optimize model’s performance before deployment through quantization, pruning, and fusion techniques. Consider hardware-specific runtimes like TensorRT or OpenVINO for additional optimizations.
  • Performance Tuning: Use ONNX Optimizer to streamline your model by removing redundant nodes and applying performance-enhancing techniques. Regularly profile and benchmark the model to ensure it meets performance goals.
  • Testing and Validation: Perform thorough unit testing and check relevant metrics such as latency, throughput, and resource utilization. Compare these against benchmarks and previous model versions to identify potential issues before deployment.
  • Deploying on Different Platforms: Ensure the deployment environment matches model requirements. Use cloud-based resources to scale operations or use edge devices for fast inferencing while protecting user privacy.
  • Monitoring and Maintenance: Build continuous monitoring pipelines with real-time alerts that send instant notifications when performance metrics fall behind targets. Also, conduct regular updates and check for the latest ONNX updates to use the best model resources.
  • Ensure Security and Compliance: Secure your deployment environment and ensure compliance with relevant data protection regulations. For edge deployments, optimize model size and efficiency, and test on target devices.
  • Documentation: Maintaining comprehensive documentation regarding model structure, version updates, and metadata can streamline ONNX implementation and help new members get up to speed without much hassle.

ONNX Model: Key Takeaways

Organizations with AI models running on multiple platforms can significantly benefit from ONNX interoperability. The framework enables users to streamline model development and deployment workflows through cross-platform support.

Below are a few critical points regarding ONNX.

  1. Standard Format: ONNX is an open-source format for storing and executing models across different platforms.
  2. Compatibility: ONNX supports popular ML frameworks such as PyTorch, Keras, and TensorFlow with pre-built libraries to convert models in these environments to ONNX.
  3. ONN Optimization: ONNX lets you optimize models and leverage hardware-specific capabilities through the ONNX Runtime library.

encord logo

Power your AI models with the right data

Automate your data curation, annotation and label validation workflows.

Get started
Written by
author-avatar-url

Alexandre Bonnet

View more posts
Frequently asked questions
  • Use tf2onnx to convert TensorFlow models to ONNX, and use the torch.onnx module for PyTorch models.
  • ONNX supports multiple frameworks, including PyTorch, TensorFlow, Keras, Scikit-Learn, and Microsoft Cognitive Toolkit (CNTK)
  • You can follow the steps below to optimize ONNX models. First, convert the model to ONNX. Then, use the ONNX Runtime library to tune performance. Lastly, apply techniques such as quantization and graph optimization.
  • ONNX Runtime offers a hardware-agnostic accelerator across multiple platforms and languages through different APIs.
  • Yes. ONNX Runtime optimizes models for speed, throughput, and resource utilization. There are no standard benchmarks, as they depend on specific use cases, development environments, and model architecture.
  • You can convert the model to ONNX and use ONNX Runtime to optimize it according to the device’s specifications. Next, you can deploy the model using the device’s APIs.
  • ONNX Runtime and ONNX Optimizer can help improve model performance for computer vision tasks.
  • ONNX Runtime Mobile can help you integrate your models with Android and iOS devices.
  • Yes. The ONNX Model Zoo repository on GitHub offers pre-trained object detection models in ONNX format.
  • You can convert a model to ONNX using pre-built conversion libraries or use a pre-trained classification model in ONNX format from the ONNX Model Zoo repository.

Explore our products