mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-15 18:23:41 +00:00
184 lines
3.8 KiB
ReStructuredText
184 lines
3.8 KiB
ReStructuredText
.. only:: html
|
|
|
|
.. 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:: default
|
|
|
|
|
|
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:: default
|
|
|
|
|
|
example1 = get_example("sigmoid.onnx")
|
|
sess = rt.InferenceSession(example1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Let's see the input name and shape.
|
|
|
|
|
|
.. code-block:: default
|
|
|
|
|
|
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:: default
|
|
|
|
|
|
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:: default
|
|
|
|
|
|
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.51158583, 0.71759593, 0.58291364, 0.7120014 , 0.6782218 ],
|
|
[0.6614846 , 0.666275 , 0.5892761 , 0.6977681 , 0.62876207],
|
|
[0.6731731 , 0.504596 , 0.68911326, 0.64759874, 0.55347925],
|
|
[0.68378323, 0.6788985 , 0.70521903, 0.7012743 , 0.6849997 ]],
|
|
|
|
[[0.69679904, 0.6268262 , 0.635523 , 0.705201 , 0.6575402 ],
|
|
[0.5726959 , 0.585034 , 0.61183447, 0.6313906 , 0.5761279 ],
|
|
[0.5608245 , 0.6542765 , 0.62418705, 0.68640214, 0.6531157 ],
|
|
[0.60396266, 0.51947266, 0.64402723, 0.7152903 , 0.67965746]],
|
|
|
|
[[0.5545499 , 0.6871018 , 0.69852173, 0.53383857, 0.5307777 ],
|
|
[0.5215983 , 0.7091785 , 0.50127536, 0.6032124 , 0.6328076 ],
|
|
[0.6385152 , 0.6556017 , 0.56685936, 0.5005025 , 0.56599593],
|
|
[0.56378585, 0.6028171 , 0.54956913, 0.65903753, 0.64652383]]],
|
|
dtype=float32)]
|
|
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-timing
|
|
|
|
**Total running time of the script:** ( 0 minutes 0.083 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 sphx-glr-download-python
|
|
|
|
:download:`Download Python source code: plot_load_and_predict.py <plot_load_and_predict.py>`
|
|
|
|
|
|
|
|
.. container:: sphx-glr-download sphx-glr-download-jupyter
|
|
|
|
: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.github.io>`_
|