onnxruntime/python/_downloads/plot_load_and_predict.ipynb

126 lines
3.5 KiB
Text
Raw Normal View History

2019-12-20 21:35:58 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\nLoad and predict with ONNX Runtime and a very simple model\n==========================================================\n\nThis example demonstrates how to load a model and compute\nthe output for an input vector. It also shows how to\nretrieve the definition of its inputs and outputs.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import onnxruntime as rt\nimport numpy\nfrom onnxruntime.datasets import get_example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's load a very simple model.\nThe model is available on github `onnx...test_sigmoid <https://github.com/onnx/onnx/tree/master/onnx/backend/test/data/node/test_sigmoid>`_.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"example1 = get_example(\"sigmoid.onnx\")\nsess = rt.InferenceSession(example1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see the input name and shape.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"input_name = sess.get_inputs()[0].name\nprint(\"input name\", input_name)\ninput_shape = sess.get_inputs()[0].shape\nprint(\"input shape\", input_shape)\ninput_type = sess.get_inputs()[0].type\nprint(\"input type\", input_type)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see the output name and shape.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"output_name = sess.get_outputs()[0].name\nprint(\"output name\", output_name) \noutput_shape = sess.get_outputs()[0].shape\nprint(\"output shape\", output_shape)\noutput_type = sess.get_outputs()[0].type\nprint(\"output type\", output_type)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's compute its outputs (or predictions if it is a machine learned model).\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy.random\nx = numpy.random.random((3,4,5))\nx = x.astype(numpy.float32)\nres = sess.run([output_name], {input_name: x})\nprint(res)"
]
}
],
"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
}