mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-15 18:23:41 +00:00
* initial setup and rename "how to" to "setup" * move API to main nav * move api to main nav * add get starated, rework nav order * rename to install move mds out of install section * update api nav and home page * add install docs and python qs updates * python get started work * remove c and obj c for now * move java, python, and obj-c docs under api folder * move java api html to iframe (ugh) * remove api docs w/o details, move api text getstar * remove api docs wo detail updates get started * remvoe iframes * move eco system to main nav * fix api buttons * added more examples moved intro to ORT * fix links * fix get started titles * fix get started titles * fix more links * fix more links * more link fixes * fix nav remove inferencing and training subnav * fix top nav remove inference and training nav * fix title * fix tutorials nav hierarchy * fix python api button * add tenorflow keras example * fix quickstart toc * add imports fix spacing * fix links * update nav and python get started page * move ort training example, add coming soon for iot * update C# get started * fix spacing on quantization * Add some js get started content * fix formatting * fix typo * removed onnx-pytorch and onnx-tf * updated pip install torch and added links iot page * added pytorch tutorial heirarchy * updated web to docs soon added release blog link * add web link
198 lines
4.9 KiB
ReStructuredText
198 lines
4.9 KiB
ReStructuredText
|
|
.. DO NOT EDIT.
|
|
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
|
|
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
|
|
.. "auto_examples\plot_dl_keras.py"
|
|
.. LINE NUMBERS ARE GIVEN BELOW.
|
|
|
|
.. only:: html
|
|
|
|
.. note::
|
|
:class: sphx-glr-download-link-note
|
|
|
|
Click :ref:`here <sphx_glr_download_auto_examples_plot_dl_keras.py>`
|
|
to download the full example code
|
|
|
|
.. rst-class:: sphx-glr-example-title
|
|
|
|
.. _sphx_glr_auto_examples_plot_dl_keras.py:
|
|
|
|
|
|
.. _l-example-backend-api-tensorflow:
|
|
|
|
ONNX Runtime for Keras
|
|
======================
|
|
|
|
The following demonstrates how to compute the predictions
|
|
of a pretrained deep learning model obtained from
|
|
`keras <https://keras.io/>`_
|
|
with *onnxruntime*. The conversion requires
|
|
`keras <https://keras.io/>`_,
|
|
`tensorflow <https://www.tensorflow.org/>`_,
|
|
`keras-onnx <https://github.com/onnx/keras-onnx/>`_,
|
|
`onnxmltools <https://pypi.org/project/onnxmltools/>`_
|
|
but then only *onnxruntime* is required
|
|
to compute the predictions.
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 22-32
|
|
|
|
.. code-block:: default
|
|
|
|
import os
|
|
if not os.path.exists('dense121.onnx'):
|
|
from keras.applications.densenet import DenseNet121
|
|
model = DenseNet121(include_top=True, weights='imagenet')
|
|
|
|
from keras2onnx import convert_keras
|
|
onx = convert_keras(model, 'dense121.onnx')
|
|
with open("dense121.onnx", "wb") as f:
|
|
f.write(onx.SerializeToString())
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-script-out
|
|
|
|
.. code-block:: pytb
|
|
|
|
Traceback (most recent call last):
|
|
File "C:\xadupre\microsoft_xadupre\onnxruntime\docs\python\examples\plot_dl_keras.py", line 28, in <module>
|
|
onx = convert_keras(model, 'dense121.onnx')
|
|
File "C:\xadupre\microsoft_xadupre\keras-onnx\keras2onnx\main.py", line 82, in convert_keras
|
|
tf_graph = build_layer_output_from_model(model, output_dict, input_names,
|
|
File "C:\xadupre\microsoft_xadupre\keras-onnx\keras2onnx\_parser_tf.py", line 308, in build_layer_output_from_model
|
|
graph = model.outputs[0].graph
|
|
AttributeError: 'KerasTensor' object has no attribute 'graph'
|
|
|
|
|
|
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 33-34
|
|
|
|
Let's load an image (source: wikipedia).
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 34-43
|
|
|
|
.. code-block:: default
|
|
|
|
|
|
from keras.preprocessing.image import array_to_img, img_to_array, load_img
|
|
img = load_img('Sannosawa1.jpg')
|
|
ximg = img_to_array(img)
|
|
|
|
import matplotlib.pyplot as plt
|
|
plt.imshow(ximg / 255)
|
|
plt.axis('off')
|
|
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 44-45
|
|
|
|
Let's load the model with onnxruntime.
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 45-60
|
|
|
|
.. code-block:: default
|
|
|
|
import onnxruntime as rt
|
|
from onnxruntime.capi.onnxruntime_pybind11_state import InvalidGraph
|
|
|
|
try:
|
|
sess = rt.InferenceSession('dense121.onnx')
|
|
ok = True
|
|
except (InvalidGraph, TypeError, RuntimeError) as e:
|
|
# Probably a mismatch between onnxruntime and onnx version.
|
|
print(e)
|
|
ok = False
|
|
|
|
if ok:
|
|
print("The model expects input shape:", sess.get_inputs()[0].shape)
|
|
print("image shape:", ximg.shape)
|
|
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 61-62
|
|
|
|
Let's resize the image.
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 62-73
|
|
|
|
.. code-block:: default
|
|
|
|
|
|
if ok:
|
|
from skimage.transform import resize
|
|
import numpy
|
|
|
|
ximg224 = resize(ximg / 255, (224, 224, 3), anti_aliasing=True)
|
|
ximg = ximg224[numpy.newaxis, :, :, :]
|
|
ximg = ximg.astype(numpy.float32)
|
|
|
|
print("new shape:", ximg.shape)
|
|
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 74-75
|
|
|
|
Let's compute the output.
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 75-83
|
|
|
|
.. code-block:: default
|
|
|
|
|
|
if ok:
|
|
input_name = sess.get_inputs()[0].name
|
|
res = sess.run(None, {input_name: ximg})
|
|
prob = res[0]
|
|
print(prob.ravel()[:10]) # Too big to be displayed.
|
|
|
|
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 84-85
|
|
|
|
Let's get more comprehensive results.
|
|
|
|
.. GENERATED FROM PYTHON SOURCE LINES 85-95
|
|
|
|
.. code-block:: default
|
|
|
|
|
|
if ok:
|
|
from keras.applications.densenet import decode_predictions
|
|
decoded = decode_predictions(prob)
|
|
|
|
import pandas
|
|
df = pandas.DataFrame(decoded[0], columns=["class_id", "name", "P"])
|
|
print(df)
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-timing
|
|
|
|
**Total running time of the script:** ( 0 minutes 6.417 seconds)
|
|
|
|
|
|
.. _sphx_glr_download_auto_examples_plot_dl_keras.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_dl_keras.py <plot_dl_keras.py>`
|
|
|
|
|
|
|
|
.. container:: sphx-glr-download sphx-glr-download-jupyter
|
|
|
|
:download:`Download Jupyter notebook: plot_dl_keras.ipynb <plot_dl_keras.ipynb>`
|
|
|
|
|
|
.. only:: html
|
|
|
|
.. rst-class:: sphx-glr-signature
|
|
|
|
`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
|