{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Common errors with onnxruntime\n\nThis example looks into several common situations\nin which *onnxruntime* does not return the model \nprediction but raises an exception instead.\nIt starts by loading the model trained in example\n`l-logreg-example` which produced a logistic regression\ntrained on *Iris* datasets. The model takes\na vector of dimension 2 and returns a class among three.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import onnxruntime as rt\nfrom onnxruntime.capi.onnxruntime_pybind11_state import InvalidArgument\nimport numpy\nfrom onnxruntime.datasets import get_example\n\nexample2 = get_example(\"logreg_iris.onnx\")\nsess = rt.InferenceSession(example2, providers=rt.get_available_providers())\n\ninput_name = sess.get_inputs()[0].name\noutput_name = sess.get_outputs()[0].name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first example fails due to *bad types*.\n*onnxruntime* only expects single floats (4 bytes)\nand cannot handle any other kind of floats.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "try:\n x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float64)\n sess.run([output_name], {input_name: x})\nexcept Exception as e:\n print(\"Unexpected type\")\n print(\"{0}: {1}\".format(type(e), e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The model fails to return an output if the name\nis misspelled.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "try:\n x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32)\n sess.run([\"misspelled\"], {input_name: x})\nexcept Exception as e:\n print(\"Misspelled output name\")\n print(\"{0}: {1}\".format(type(e), e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output name is optional, it can be replaced by *None*\nand *onnxruntime* will then return all the outputs.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32)\ntry:\n res = sess.run(None, {input_name: x})\n print(\"All outputs\")\n print(res)\nexcept (RuntimeError, InvalidArgument) as e:\n print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The same goes if the input name is misspelled.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "try:\n x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32)\n sess.run([output_name], {\"misspelled\": x})\nexcept Exception as e:\n print(\"Misspelled input name\")\n print(\"{0}: {1}\".format(type(e), e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*onnxruntime* does not necessarily fail if the input\ndimension is a multiple of the expected input dimension.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for x in [\n numpy.array([1.0, 2.0, 3.0, 4.0], dtype=numpy.float32),\n numpy.array([[1.0, 2.0, 3.0, 4.0]], dtype=numpy.float32),\n numpy.array([[1.0, 2.0], [3.0, 4.0]], dtype=numpy.float32),\n numpy.array([1.0, 2.0, 3.0], dtype=numpy.float32),\n numpy.array([[1.0, 2.0, 3.0]], dtype=numpy.float32),\n ]:\n try:\n r = sess.run([output_name], {input_name: x})\n print(\"Shape={0} and predicted labels={1}\".format(x.shape, r))\n except (RuntimeError, InvalidArgument) as e:\n print(\"ERROR with Shape={0} - {1}\".format(x.shape, e))\n\nfor x in [\n numpy.array([1.0, 2.0, 3.0, 4.0], dtype=numpy.float32),\n numpy.array([[1.0, 2.0, 3.0, 4.0]], dtype=numpy.float32),\n numpy.array([[1.0, 2.0], [3.0, 4.0]], dtype=numpy.float32),\n numpy.array([1.0, 2.0, 3.0], dtype=numpy.float32),\n numpy.array([[1.0, 2.0, 3.0]], dtype=numpy.float32),\n ]:\n try:\n r = sess.run(None, {input_name: x})\n print(\"Shape={0} and predicted probabilities={1}\".format(x.shape, r[1]))\n except (RuntimeError, InvalidArgument) as e:\n print(\"ERROR with Shape={0} - {1}\".format(x.shape, e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It does not fail either if the number of dimension\nis higher than expects but produces a warning.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for x in [\n numpy.array([[[1.0, 2.0], [3.0, 4.0]]], dtype=numpy.float32),\n numpy.array([[[1.0, 2.0, 3.0]]], dtype=numpy.float32),\n numpy.array([[[1.0, 2.0]], [[3.0, 4.0]]], dtype=numpy.float32),\n ]:\n try:\n r = sess.run([output_name], {input_name: x})\n print(\"Shape={0} and predicted labels={1}\".format(x.shape, r))\n except (RuntimeError, InvalidArgument) as e:\n print(\"ERROR with Shape={0} - {1}\".format(x.shape, e))" ] } ], "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 }