mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-28 20:11:22 +00:00
177 lines
3.7 KiB
ReStructuredText
177 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:: 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.5360762 , 0.666669 , 0.6136168 , 0.6098715 , 0.6330898 ],
|
|
[0.70909226, 0.67855203, 0.5512253 , 0.6054007 , 0.5453689 ],
|
|
[0.67941666, 0.60376704, 0.59458643, 0.56611687, 0.567247 ],
|
|
[0.6243701 , 0.6456822 , 0.5711165 , 0.5364119 , 0.50267375]],
|
|
|
|
[[0.72189057, 0.51031893, 0.508917 , 0.724934 , 0.6013869 ],
|
|
[0.5751356 , 0.63135314, 0.70504206, 0.66305155, 0.53833747],
|
|
[0.64060616, 0.5622595 , 0.6350931 , 0.64188236, 0.5740597 ],
|
|
[0.56608844, 0.65403676, 0.5818875 , 0.5041134 , 0.7170976 ]],
|
|
|
|
[[0.61101925, 0.51149344, 0.7126139 , 0.64984477, 0.5554885 ],
|
|
[0.6957284 , 0.6517055 , 0.726743 , 0.66872954, 0.58586377],
|
|
[0.57124126, 0.505744 , 0.5355146 , 0.62422305, 0.72279936],
|
|
[0.6734552 , 0.70127356, 0.5607188 , 0.5609607 , 0.53647304]]],
|
|
dtype=float32)]
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-timing
|
|
|
|
**Total running time of the script:** ( 0 minutes 0.059 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.github.io>`_
|