ONNX and Azure Machine Learning

Learn how use of the Open Neural Network Exchange (ONNX) can help to optimize the inference of your machine learning model. Inference or model scoring, is the process of using a deployed model to generate predictions on production data.

Optimizing machine learning models for inference requires you to tune the model and the inference library to make the most of the hardware capabilities. This task becomes complex if you want to get optimal performance on different kinds of platforms such as cloud or edge, CPU or GPU, and so on, since each platform has different capabilities and characteristics. The complexity increases if you have models from various frameworks that need to run on different platforms. It can be time-consuming to optimize all the different combinations of frameworks and hardware. Therefore, a useful solution is to train your model once in your preferred framework and then run it anywhere on the cloud or edge—this solution is where ONNX comes in.

What is ONNX?

Microsoft and a community of partners created ONNX as an open standard for representing machine learning models. Models from many frameworks including TensorFlow, PyTorch, scikit-learn, Keras, Chainer, MXNet, and MATLAB can be exported or converted to the standard ONNX format. Once the models are in the ONNX format, they can be run on various platforms and devices.

ONNX Runtime is a high-performance inference engine for deploying ONNX models to production. It's optimized for both cloud and edge and works on Linux, Windows, and Mac. While ONNX is written in C++, it also has C, Python, C#, Java, and JavaScript (Node.js) APIs for usage in many environments. ONNX Runtime supports both deep neural networks (DNN) and traditional machine learning models, and it integrates with accelerators on different hardware such as TensorRT on Nvidia GPUs, OpenVINO on Intel processors, and DirectML on Windows. By using ONNX Runtime, you can benefit from the extensive production-grade optimizations, testing, and ongoing improvements.

ONNX Runtime is used in high-scale Microsoft services such as Bing, Office, and Azure AI. Although performance gains depend on many factors, these Microsoft services report an average 2x performance gain on CPU. In addition to Azure Machine Learning services, ONNX Runtime also runs in other products that support Machine Learning workloads, including:

ONNX flow diagram showing training, converters, and deployment.

How to obtain ONNX models

You can obtain ONNX models in several ways:

Many models, including image classification, object detection, and text processing models can be represented as ONNX models. If you run into an issue with a model that can't be converted successfully, file a GitHub issue in the repository of the converter that you used. You can continue using your existing model format until the issue is addressed.

ONNX model deployment in Azure

With Azure Machine Learning, you can deploy, manage, and monitor your ONNX models. Using the standard MLOps deployment workflow and ONNX Runtime, you can create a REST endpoint hosted in the cloud. For hands-on examples, see these Jupyter notebooks.

Installation and use of ONNX Runtime with Python

Python packages for ONNX Runtime are available on PyPi.org (CPU and GPU). Be sure to review the system requirements before installation.

To install ONNX Runtime for Python, use one of the following commands:

pip install onnxruntime          # CPU build
pip install onnxruntime-gpu   # GPU build

To call ONNX Runtime in your Python script, use:

import onnxruntime
session = onnxruntime.InferenceSession("path to model")

The documentation accompanying the model usually tells you the inputs and outputs for using the model. You can also use a visualization tool such as Netron to view the model. ONNX Runtime also lets you query the model metadata, inputs, and outputs as follows:

first_input_name = session.get_inputs()[0].name
first_output_name = session.get_outputs()[0].name

To perform inferencing on your model, use run and pass in the list of outputs you want returned (or leave the list empty if you want all of them) and a map of the input values. The result is a list of the outputs.

results = session.run(["output1", "output2"], {
                      "input1": indata1, "input2": indata2})
results = session.run([], {"input1": indata1, "input2": indata2})

For the complete Python API reference, see the ONNX Runtime reference docs.


Learn more about ONNX or contribute to the project:

Learn more about ONNX Runtime or contribute to the project: