Get started with ONNX Runtime in Python
Below is a quick guide to get the packages installed to use ONNX for model serialization and inference with ORT.
Contents
- Install ONNX Runtime
- Install ONNX for model export
- Quickstart Examples for PyTorch, TensorFlow, and SciKit Learn
- Python API Reference Docs
- Builds
- Learn More
Install ONNX Runtime
There are two Python packages for ONNX Runtime. Only one of these packages should be installed at a time in any one environment. The GPU package encompasses most of the CPU functionality.
Install ONNX Runtime CPU
Use the CPU package if you are running on Arm®-based CPUs and/or macOS.
Install ONNX Runtime GPU (CUDA 12.x)
The default CUDA version for ORT is 12.x.
Install ONNX Runtime GPU (CUDA 11.8)
For Cuda 11.8, please use the following instructions to install from ORT Azure Devops Feed
Install ONNX for model export
Quickstart Examples for PyTorch, TensorFlow, and SciKit Learn
Train a model using your favorite framework, export to ONNX format and inference in any supported ONNX Runtime language!
PyTorch CV
In this example we will go over how to export a PyTorch CV model into ONNX format and then inference with ORT. The code to create the model is from the PyTorch Fundamentals learning path on Microsoft Learn.
- Export the model using torch.onnx.export
- Load the onnx model with onnx.load import onnx onnx_model = onnx.load("fashion_mnist_model.onnx") onnx.checker.check_model(onnx_model)
- Create inference session using ort.InferenceSession
PyTorch NLP
In this example we will go over how to export a PyTorch NLP model into ONNX format and then inference with ORT. The code to create the AG News model is from this PyTorch tutorial.
- Process text and create the sample data input and offsets for export. import torch text = "Text from the news article" text = torch.tensor(text_pipeline(text)) offsets = torch.tensor([0])
- Export Model # Export the model torch.onnx.export(model, # model being run (text, offsets), # model input (or a tuple for multiple inputs) "ag_news_model.onnx", # where to save the model (can be a file or file-like object) export_params=True, # store the trained parameter weights inside the model file opset_version=10, # the ONNX version to export the model to do_constant_folding=True, # whether to execute constant folding for optimization input_names = ['input', 'offsets'], # the model's input names output_names = ['output'], # the model's output names dynamic_axes={'input' : {0 : 'batch_size'}, # variable length axes 'output' : {0 : 'batch_size'}})
- Load the model using onnx.load import onnx onnx_model = onnx.load("ag_news_model.onnx") onnx.checker.check_model(onnx_model)
- Create inference session with ort.InferenceSession import onnxruntime as ort import numpy as np ort_sess = ort.InferenceSession('ag_news_model.onnx') outputs = ort_sess.run(None, {'input': text.numpy(), 'offsets': torch.tensor([0]).numpy()}) # Print Result result = outputs[0].argmax(axis=1)+1 print("This is a %s news" %ag_news_label[result[0]])
TensorFlow CV
In this example we will go over how to export a TensorFlow CV model into ONNX format and then inference with ORT. The model used is from this GitHub Notebook for Keras resnet50.
- Get the pretrained model
- Convert the model to onnx and export
- Create inference session with rt.InferenceSession
SciKit Learn CV
In this example we will go over how to export a SciKit Learn CV model into ONNX format and then inference with ORT. We’ll use the famous iris datasets.
- Convert or export the model into ONNX format
- Load and run the model using ONNX Runtime We will use ONNX Runtime to compute the predictions for this machine learning model.
- Get predicted class
The code can be changed to get one specific output by specifying its name into a list.
Python API Reference Docs
Builds
If using pip, run pip install --upgrade pip prior to downloading.
| onnxruntime | CPU (Release) | Windows (x64), Linux (x64, ARM64), Mac (X64), |
| nightly | CPU (Dev) | Same as above |
| onnxruntime-gpu | GPU (Release) | Windows (x64), Linux (x64, ARM64) |
| onnxruntime-gpu for CUDA 11.* | GPU (Dev) | Windows (x64), Linux (x64, ARM64) |
| onnxruntime-gpu for CUDA 12.* | GPU (Dev) | Windows (x64), Linux (x64, ARM64) |
Example to install onnxruntime-gpu for CUDA 11.*:
Example to install onnxruntime-gpu for CUDA 12.*:
For Python compiler version notes, see this page
Learn More
- Python Tutorials
- TensorFlow with ONNX Runtime
- PyTorch with ONNX Runtime
- scikit-learn with ONNX Runtime
For documentation questions, please file an issue.