onnxruntime/python/_downloads/plot_dl_keras.ipynb
2019-12-20 13:35:58 -08:00

144 lines
No EOL
4.7 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n\nONNX Runtime for Keras\n======================\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\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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
}