onnxruntime/python/sources/auto_examples/plot_load_and_predict.rst.txt
2021-03-09 10:21:38 -08:00

206 lines
4.4 KiB
ReStructuredText

.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/plot_load_and_predict.py"
.. LINE NUMBERS ARE GIVEN BELOW.
.. 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.
.. GENERATED FROM PYTHON SOURCE LINES 14-19
.. code-block:: default
import onnxruntime as rt
import numpy
from onnxruntime.datasets import get_example
.. GENERATED FROM PYTHON SOURCE LINES 20-22
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>`_.
.. GENERATED FROM PYTHON SOURCE LINES 22-26
.. code-block:: default
example1 = get_example("sigmoid.onnx")
sess = rt.InferenceSession(example1)
.. GENERATED FROM PYTHON SOURCE LINES 27-28
Let's see the input name and shape.
.. GENERATED FROM PYTHON SOURCE LINES 28-36
.. 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)
.. GENERATED FROM PYTHON SOURCE LINES 37-38
Let's see the output name and shape.
.. GENERATED FROM PYTHON SOURCE LINES 38-46
.. 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)
.. GENERATED FROM PYTHON SOURCE LINES 47-48
Let's compute its outputs (or predictions if it is a machine learned model).
.. GENERATED FROM PYTHON SOURCE LINES 48-54
.. 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.56617785, 0.551158 , 0.57431483, 0.62868774, 0.5294609 ],
[0.6545371 , 0.64250827, 0.6819708 , 0.5105157 , 0.5584753 ],
[0.66830933, 0.7094791 , 0.70664704, 0.6744693 , 0.7030401 ],
[0.5395019 , 0.7210481 , 0.5845876 , 0.59664494, 0.6563896 ]],
[[0.71235013, 0.6528918 , 0.5907483 , 0.66855776, 0.61100346],
[0.51468205, 0.60125333, 0.5410304 , 0.57149607, 0.56778824],
[0.5155948 , 0.54921585, 0.5138594 , 0.7051111 , 0.62632954],
[0.5651827 , 0.55247986, 0.6941072 , 0.50415695, 0.7062323 ]],
[[0.51758766, 0.67160237, 0.59442437, 0.5007695 , 0.56175166],
[0.72844744, 0.5150477 , 0.5052765 , 0.5447472 , 0.7088654 ],
[0.596162 , 0.5197903 , 0.6099661 , 0.724396 , 0.5885481 ],
[0.6910895 , 0.53817046, 0.596786 , 0.6119356 , 0.5707261 ]]],
dtype=float32)]
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.012 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>`_