mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-17 18:40:28 +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
144 lines
No EOL
4.6 KiB
Text
144 lines
No EOL
4.6 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"%matplotlib inline"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n\n# ONNX Runtime for Keras\n\nThe following demonstrates how to compute the predictions\nof a pretrained deep learning model obtained from \n`keras <https://keras.io/>`_\nwith *onnxruntime*. The conversion requires\n`keras <https://keras.io/>`_,\n`tensorflow <https://www.tensorflow.org/>`_,\n`keras-onnx <https://github.com/onnx/keras-onnx/>`_,\n`onnxmltools <https://pypi.org/project/onnxmltools/>`_\nbut then only *onnxruntime* is required\nto compute the predictions.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\nif not os.path.exists('dense121.onnx'):\n from keras.applications.densenet import DenseNet121\n model = DenseNet121(include_top=True, weights='imagenet')\n\n from keras2onnx import convert_keras\n onx = convert_keras(model, 'dense121.onnx')\n with open(\"dense121.onnx\", \"wb\") as f:\n f.write(onx.SerializeToString())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Let's load an image (source: wikipedia).\n\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from keras.preprocessing.image import array_to_img, img_to_array, load_img\nimg = load_img('Sannosawa1.jpg')\nximg = img_to_array(img)\n\nimport matplotlib.pyplot as plt\nplt.imshow(ximg / 255)\nplt.axis('off')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Let's load the model with onnxruntime.\n\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import onnxruntime as rt\nfrom onnxruntime.capi.onnxruntime_pybind11_state import InvalidGraph\n\ntry:\n sess = rt.InferenceSession('dense121.onnx')\n ok = True\nexcept (InvalidGraph, TypeError, RuntimeError) as e:\n # Probably a mismatch between onnxruntime and onnx version.\n print(e)\n ok = False\n\nif ok:\n print(\"The model expects input shape:\", sess.get_inputs()[0].shape)\n print(\"image shape:\", ximg.shape)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Let's resize the image.\n\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"if ok:\n from skimage.transform import resize\n import numpy\n\n ximg224 = resize(ximg / 255, (224, 224, 3), anti_aliasing=True)\n ximg = ximg224[numpy.newaxis, :, :, :]\n ximg = ximg.astype(numpy.float32)\n\n print(\"new shape:\", ximg.shape)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Let's compute the output.\n\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"if ok:\n input_name = sess.get_inputs()[0].name\n res = sess.run(None, {input_name: ximg})\n prob = res[0]\n print(prob.ravel()[:10]) # Too big to be displayed."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Let's get more comprehensive results.\n\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"if ok:\n from keras.applications.densenet import decode_predictions\n decoded = decode_predictions(prob)\n\n import pandas\n df = pandas.DataFrame(decoded[0], columns=[\"class_id\", \"name\", \"P\"])\n print(df)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.7"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
} |