Note
Click here to download the full example code
Load and predict with ONNX Runtime and a very simple model¶
This example demonstrates how to load a model and compute the output for an input vector. It also shows how to retrieve the definition of its inputs and outputs.
import onnxruntime as rt
import numpy
from onnxruntime.datasets import get_example
Let’s load a very simple model. The model is available on github onnx…test_sigmoid.
example1 = get_example("sigmoid.onnx")
sess = rt.InferenceSession(example1, providers=rt.get_available_providers())
Let’s see the input name and shape.
input_name = sess.get_inputs()[0].name
print("input name", input_name)
input_shape = sess.get_inputs()[0].shape
print("input shape", input_shape)
input_type = sess.get_inputs()[0].type
print("input type", input_type)
Out:
input name x
input shape [3, 4, 5]
input type tensor(float)
Let’s see the output name and shape.
output_name = sess.get_outputs()[0].name
print("output name", output_name)
output_shape = sess.get_outputs()[0].shape
print("output shape", output_shape)
output_type = sess.get_outputs()[0].type
print("output type", output_type)
Out:
output name y
output shape [3, 4, 5]
output type tensor(float)
Let’s compute its outputs (or predictions if it is a machine learned model).
import numpy.random
x = numpy.random.random((3,4,5))
x = x.astype(numpy.float32)
res = sess.run([output_name], {input_name: x})
print(res)
Out:
[array([[[0.6423601 , 0.65232253, 0.6620137 , 0.708999 , 0.65169865],
[0.548968 , 0.59544575, 0.7161434 , 0.525905 , 0.7210646 ],
[0.5178277 , 0.5842683 , 0.5627599 , 0.6324704 , 0.5833795 ],
[0.69634616, 0.60848683, 0.6746977 , 0.50677085, 0.5549751 ]],
[[0.5097179 , 0.59407187, 0.56360227, 0.7223234 , 0.5392329 ],
[0.5398089 , 0.5622808 , 0.5369593 , 0.5819309 , 0.5735331 ],
[0.5688669 , 0.71247685, 0.63964766, 0.63349843, 0.63380575],
[0.64378905, 0.60552883, 0.5184905 , 0.6312441 , 0.5047166 ]],
[[0.63900065, 0.6108959 , 0.5249817 , 0.5055595 , 0.55390376],
[0.62443805, 0.550723 , 0.5320551 , 0.5522731 , 0.68858314],
[0.69650024, 0.54673976, 0.56964463, 0.58536506, 0.5743989 ],
[0.6382853 , 0.5826889 , 0.53635114, 0.52279866, 0.7300966 ]]],
dtype=float32)]
Total running time of the script: ( 0 minutes 0.013 seconds)