2019-12-21 01:11:46 +00:00
|
|
|
.. 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
|
|
|
|
|
|
2020-04-06 22:52:48 +00:00
|
|
|
[array([[[0.5143179 , 0.6716157 , 0.6144865 , 0.6863301 , 0.64653844],
|
|
|
|
|
[0.6305008 , 0.5775663 , 0.6964146 , 0.5280421 , 0.61725914],
|
|
|
|
|
[0.7180781 , 0.61795104, 0.6727085 , 0.5730073 , 0.7306371 ],
|
|
|
|
|
[0.68829596, 0.72810763, 0.6397303 , 0.6951108 , 0.6797523 ]],
|
|
|
|
|
|
|
|
|
|
[[0.70466846, 0.5554103 , 0.5995578 , 0.6315835 , 0.66704905],
|
|
|
|
|
[0.6936102 , 0.50828934, 0.51689506, 0.5749565 , 0.5596123 ],
|
|
|
|
|
[0.596185 , 0.61397564, 0.63528085, 0.71865064, 0.5692966 ],
|
|
|
|
|
[0.6515152 , 0.69646513, 0.5182993 , 0.52175283, 0.5565996 ]],
|
|
|
|
|
|
|
|
|
|
[[0.5074223 , 0.7233809 , 0.52564526, 0.52229357, 0.677563 ],
|
|
|
|
|
[0.5302158 , 0.5307286 , 0.67780304, 0.5051534 , 0.580091 ],
|
|
|
|
|
[0.72392386, 0.55004114, 0.5165563 , 0.6645192 , 0.7159723 ],
|
|
|
|
|
[0.6326351 , 0.6996319 , 0.67441595, 0.6697645 , 0.55308944]]],
|
2019-12-21 01:11:46 +00:00
|
|
|
dtype=float32)]
|
|
|
|
|
|
|
|
|
|
|
2020-04-06 22:52:48 +00:00
|
|
|
**Total running time of the script:** ( 0 minutes 0.009 seconds)
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
.. _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>`_
|