.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/core/plot_onnxruntime_inference.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_core_plot_onnxruntime_inference.py: .. _l-plot-onnxruntime-inference: Running inference with onnxruntime =================================== This example shows how to build a small ONNX model and run it with :mod:`onnxruntime`. The model is a two-layer MLP created with the :func:`~yaourt.doc.demo_mlp_model` helper. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import numpy as np import onnxruntime from yaourt.doc import demo_mlp_model .. GENERATED FROM PYTHON SOURCE LINES 17-24 1. Build the model ------------------ :func:`demo_mlp_model ` returns an ``onnx.ModelProto`` representing a small fully-connected network: ``x (3×10) → MatMul → Add → Relu → MatMul → Add → output (3×1)``. The input shape is fixed to a batch size of 3. .. GENERATED FROM PYTHON SOURCE LINES 24-30 .. code-block:: Python model = demo_mlp_model("") print("Opset:", model.opset_import[0].version) print("Inputs :", [i.name for i in model.graph.input]) print("Outputs:", [o.name for o in model.graph.output]) .. rst-class:: sphx-glr-script-out .. code-block:: none Opset: 18 Inputs : ['x'] Outputs: ['output_0'] .. GENERATED FROM PYTHON SOURCE LINES 31-36 2. Run inference ---------------- We create a random input tensor matching the model's fixed batch size and call :meth:`~onnxruntime.InferenceSession.run`. .. GENERATED FROM PYTHON SOURCE LINES 36-46 .. code-block:: Python x = np.random.randn(3, 10).astype(np.float32) sess = onnxruntime.InferenceSession(model.SerializeToString(), providers=["CPUExecutionProvider"]) (output,) = sess.run(None, {"x": x}) print("Input shape:", x.shape) print("Output shape:", output.shape) assert output.shape == (3, 1), f"Unexpected output shape: {output.shape}" .. rst-class:: sphx-glr-script-out .. code-block:: none Input shape: (3, 10) Output shape: (3, 1) .. GENERATED FROM PYTHON SOURCE LINES 47-51 3. Run multiple times --------------------- The same session can be reused for multiple calls with different data. .. GENERATED FROM PYTHON SOURCE LINES 51-59 .. code-block:: Python for i in range(3): xi = np.random.randn(3, 10).astype(np.float32) (yi,) = sess.run(None, {"x": xi}) print(f" run={i} output shape: {yi.shape}") assert yi.shape == (3, 1) print("All runs passed.") .. rst-class:: sphx-glr-script-out .. code-block:: none run=0 output shape: (3, 1) run=1 output shape: (3, 1) run=2 output shape: (3, 1) All runs passed. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.248 seconds) .. _sphx_glr_download_auto_examples_core_plot_onnxruntime_inference.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_onnxruntime_inference.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_onnxruntime_inference.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_onnxruntime_inference.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_