2021-03-09 18:21:38 +00:00
{
"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": [
2022-08-03 17:24:42 +00:00
"import numpy\n\nimport onnxruntime as rt\nfrom onnxruntime.datasets import get_example"
2021-03-09 18:21:38 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2022-12-09 23:55:42 +00:00
"Let's load a very simple model.\nThe model is available on github [onnx...test_sigmoid](https://github.com/onnx/onnx/blob/main/onnx/backend/test/data/node/test_sigmoid).\n\n"
2021-03-09 18:21:38 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
2022-01-04 17:32:40 +00:00
"example1 = get_example(\"sigmoid.onnx\")\nsess = rt.InferenceSession(example1, providers=rt.get_available_providers())"
2021-03-09 18:21:38 +00:00
]
},
{
"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": [
2022-08-03 17:24:42 +00:00
"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)"
2021-03-09 18:21:38 +00:00
]
},
{
"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": [
2022-08-03 17:24:42 +00:00
"import numpy.random\n\nx = numpy.random.random((3, 4, 5))\nx = x.astype(numpy.float32)\nres = sess.run([output_name], {input_name: x})\nprint(res)"
2021-03-09 18:21:38 +00:00
]
}
],
"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",
2022-12-09 23:55:42 +00:00
"version": "3.10.6"
2021-03-09 18:21:38 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 0
}