mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-18 18:52:16 +00:00
140 lines
2.6 KiB
ReStructuredText
140 lines
2.6 KiB
ReStructuredText
.. note::
|
|
:class: sphx-glr-download-link-note
|
|
|
|
Click :ref:`here <sphx_glr_download_auto_examples_plot_backend.py>` to download the full example code
|
|
.. rst-class:: sphx-glr-example-title
|
|
|
|
.. _sphx_glr_auto_examples_plot_backend.py:
|
|
|
|
|
|
|
|
.. _l-example-backend-api:
|
|
|
|
ONNX Runtime Backend for ONNX
|
|
=============================
|
|
|
|
*ONNX Runtime* extends the
|
|
`onnx backend API <https://github.com/onnx/onnx/blob/master/docs/ImplementingAnOnnxBackend.md>`_
|
|
to run predictions using this runtime.
|
|
Let's use the API to compute the prediction
|
|
of a simple logistic regression model.
|
|
|
|
|
|
.. code-block:: default
|
|
|
|
import numpy as np
|
|
from onnxruntime import datasets
|
|
import onnxruntime.backend as backend
|
|
from onnx import load
|
|
|
|
name = datasets.get_example("logreg_iris.onnx")
|
|
model = load(name)
|
|
|
|
rep = backend.prepare(model, 'CPU')
|
|
x = np.array([[-1.0, -2.0]], dtype=np.float32)
|
|
label, proba = rep.run(x)
|
|
print("label={}".format(label))
|
|
print("probabilities={}".format(proba))
|
|
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-script-out
|
|
|
|
Out:
|
|
|
|
.. code-block:: none
|
|
|
|
label=[1]
|
|
probabilities=[{0: 0.02731134556233883, 1: 0.5175684094429016, 2: 0.4551202654838562}]
|
|
|
|
|
|
|
|
The device depends on how the package was compiled,
|
|
GPU or CPU.
|
|
|
|
|
|
.. code-block:: default
|
|
|
|
from onnxruntime import get_device
|
|
print(get_device())
|
|
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-script-out
|
|
|
|
Out:
|
|
|
|
.. code-block:: none
|
|
|
|
CPU
|
|
|
|
|
|
|
|
The backend can also directly load the model
|
|
without using *onnx*.
|
|
|
|
|
|
.. code-block:: default
|
|
|
|
|
|
rep = backend.prepare(name, 'CPU')
|
|
x = np.array([[-1.0, -2.0]], dtype=np.float32)
|
|
label, proba = rep.run(x)
|
|
print("label={}".format(label))
|
|
print("probabilities={}".format(proba))
|
|
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-script-out
|
|
|
|
Out:
|
|
|
|
.. code-block:: none
|
|
|
|
label=[1]
|
|
probabilities=[{0: 0.02731134556233883, 1: 0.5175684094429016, 2: 0.4551202654838562}]
|
|
|
|
|
|
|
|
The backend API is implemented by other frameworks
|
|
and makes it easier to switch between multiple runtimes
|
|
with the same API.
|
|
|
|
|
|
.. rst-class:: sphx-glr-timing
|
|
|
|
**Total running time of the script:** ( 0 minutes 0.078 seconds)
|
|
|
|
|
|
.. _sphx_glr_download_auto_examples_plot_backend.py:
|
|
|
|
|
|
.. only :: html
|
|
|
|
.. container:: sphx-glr-footer
|
|
:class: sphx-glr-footer-example
|
|
|
|
|
|
|
|
.. container:: sphx-glr-download
|
|
|
|
:download:`Download Python source code: plot_backend.py <plot_backend.py>`
|
|
|
|
|
|
|
|
.. container:: sphx-glr-download
|
|
|
|
:download:`Download Jupyter notebook: plot_backend.ipynb <plot_backend.ipynb>`
|
|
|
|
|
|
.. only:: html
|
|
|
|
.. rst-class:: sphx-glr-signature
|
|
|
|
`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
|