mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-15 18:23:41 +00:00
176 lines
3.7 KiB
ReStructuredText
176 lines
3.7 KiB
ReStructuredText
.. note::
|
|
:class: sphx-glr-download-link-note
|
|
|
|
Click :ref:`here <sphx_glr_download_auto_examples_plot_load_and_predict.py>` to download the full example code
|
|
.. rst-class:: sphx-glr-example-title
|
|
|
|
.. _sphx_glr_auto_examples_plot_load_and_predict.py:
|
|
|
|
|
|
.. _l-example-simple-usage:
|
|
|
|
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.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
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 <https://github.com/onnx/onnx/tree/master/onnx/backend/test/data/node/test_sigmoid>`_.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
example1 = get_example("sigmoid.onnx")
|
|
sess = rt.InferenceSession(example1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Let's see the input name and shape.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-script-out
|
|
|
|
Out:
|
|
|
|
.. code-block:: none
|
|
|
|
input name x
|
|
input shape [3, 4, 5]
|
|
input type tensor(float)
|
|
|
|
|
|
Let's see the output name and shape.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-script-out
|
|
|
|
Out:
|
|
|
|
.. code-block:: none
|
|
|
|
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).
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-script-out
|
|
|
|
Out:
|
|
|
|
.. code-block:: none
|
|
|
|
[array([[[0.61738354, 0.5889719 , 0.6793853 , 0.50794476, 0.6481656 ],
|
|
[0.63874 , 0.6554151 , 0.70349395, 0.7234402 , 0.5136753 ],
|
|
[0.5245013 , 0.6156528 , 0.5979552 , 0.50427085, 0.6905146 ],
|
|
[0.50659853, 0.5972322 , 0.63199735, 0.52700824, 0.5550221 ]],
|
|
|
|
[[0.6085912 , 0.60911506, 0.5874832 , 0.62220854, 0.52513546],
|
|
[0.6714244 , 0.554621 , 0.54446864, 0.50728863, 0.58585966],
|
|
[0.69436765, 0.6819202 , 0.5424466 , 0.63762194, 0.7102783 ],
|
|
[0.5940473 , 0.6690069 , 0.6540941 , 0.5415039 , 0.5430267 ]],
|
|
|
|
[[0.70212066, 0.60011494, 0.613671 , 0.6573008 , 0.6949564 ],
|
|
[0.5501859 , 0.65843284, 0.56367683, 0.5267073 , 0.50210917],
|
|
[0.5226443 , 0.6559813 , 0.62244976, 0.690172 , 0.58052164],
|
|
[0.55922556, 0.70860493, 0.72129 , 0.5805169 , 0.5123959 ]]],
|
|
dtype=float32)]
|
|
|
|
|
|
**Total running time of the script:** ( 0 minutes 0.035 seconds)
|
|
|
|
|
|
.. _sphx_glr_download_auto_examples_plot_load_and_predict.py:
|
|
|
|
|
|
.. only :: html
|
|
|
|
.. container:: sphx-glr-footer
|
|
:class: sphx-glr-footer-example
|
|
|
|
|
|
|
|
.. container:: sphx-glr-download
|
|
|
|
:download:`Download Python source code: plot_load_and_predict.py <plot_load_and_predict.py>`
|
|
|
|
|
|
|
|
.. container:: sphx-glr-download
|
|
|
|
:download:`Download Jupyter notebook: plot_load_and_predict.ipynb <plot_load_and_predict.ipynb>`
|
|
|
|
|
|
.. only:: html
|
|
|
|
.. rst-class:: sphx-glr-signature
|
|
|
|
`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
|