onnxruntime/docs/api/python/downloads/290d1103c4874727a37c05b400ffb83c/plot_load_and_predict.ipynb
github-actions[bot] 8bde251409
Update Python API docs to commit 9765949 (#10181)
Co-authored-by: natke <natke@users.noreply.github.com>
2022-01-04 09:32:40 -08:00

126 lines
No EOL
3.4 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n# Load and predict with ONNX Runtime and a very simple model\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"
]
},
{
"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, providers=rt.get_available_providers())"
]
},
{
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}