Note
Go to the end to download the full example code
ONNX Runtime Backend for ONNX#
ONNX Runtime extends the onnx backend API to run predictions using this runtime. Let’s use the API to compute the prediction of a simple logistic regression model.
import numpy as np
from onnx import load
import onnxruntime.backend as backend
The device depends on how the package was compiled, GPU or CPU.
from onnxruntime import datasets, get_device
from onnxruntime.capi.onnxruntime_pybind11_state import InvalidArgument
device = get_device()
name = datasets.get_example("logreg_iris.onnx")
model = load(name)
rep = backend.prepare(model, device)
x = np.array([[-1.0, -2.0]], dtype=np.float32)
try:
label, proba = rep.run(x)
print("label={}".format(label))
print("probabilities={}".format(proba))
except (RuntimeError, InvalidArgument) as e:
print(e)
[ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: float_input for the following indices
index: 0 Got: 1 Expected: 3
Please fix either the inputs or the model.
The backend can also directly load the model without using onnx.
rep = backend.prepare(name, device)
x = np.array([[-1.0, -2.0]], dtype=np.float32)
try:
label, proba = rep.run(x)
print("label={}".format(label))
print("probabilities={}".format(proba))
except (RuntimeError, InvalidArgument) as e:
print(e)
[ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: float_input for the following indices
index: 0 Got: 1 Expected: 3
Please fix either the inputs or the model.
The backend API is implemented by other frameworks and makes it easier to switch between multiple runtimes with the same API.
Total running time of the script: ( 0 minutes 0.069 seconds)