|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +""" |
| 5 | +.. _l-example-simple-usage: |
| 6 | +
|
| 7 | +Common errors with onnxruntime |
| 8 | +============================== |
| 9 | +
|
| 10 | +This example looks into several common situations |
| 11 | +in which *onnxruntime* does not return the model |
| 12 | +prediction but raises an exception instead. |
| 13 | +It starts by loading the model trained in example |
| 14 | +:ref:`l-logreg-example` which produced a logistic regression |
| 15 | +trained on *Iris* datasets. The model takes |
| 16 | +a vector of dimension 2 and returns a class among three. |
| 17 | +""" |
| 18 | +import onnxruntime as rt |
| 19 | +import numpy |
| 20 | +from onnxruntime.datasets import get_example |
| 21 | + |
| 22 | +example2 = get_example("logreg_iris.onnx") |
| 23 | +sess = rt.InferenceSession(example2) |
| 24 | + |
| 25 | +input_name = sess.get_inputs()[0].name |
| 26 | +output_name = sess.get_outputs()[0].name |
| 27 | + |
| 28 | +############################# |
| 29 | +# The first example fails due to *bad types*. |
| 30 | +# *onnxruntime* only expects single floats (4 bytes) |
| 31 | +# and cannot handle any other kind of floats. |
| 32 | + |
| 33 | +try: |
| 34 | + x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float64) |
| 35 | + sess.run([output_name], {input_name: x}) |
| 36 | +except Exception as e: |
| 37 | + print("Unexpected type") |
| 38 | + print("{0}: {1}".format(type(e), e)) |
| 39 | + |
| 40 | +######################### |
| 41 | +# The model fails to return an output if the name |
| 42 | +# is misspelled. |
| 43 | + |
| 44 | +try: |
| 45 | + x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32) |
| 46 | + sess.run(["misspelled"], {input_name: x}) |
| 47 | +except Exception as e: |
| 48 | + print("Misspelled output name") |
| 49 | + print("{0}: {1}".format(type(e), e)) |
| 50 | + |
| 51 | +########################### |
| 52 | +# The output name is optional, it can be replaced by *None* |
| 53 | +# and *onnxruntime* will then return all the outputs. |
| 54 | + |
| 55 | +x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32) |
| 56 | +res = sess.run(None, {input_name: x}) |
| 57 | +print("All outputs") |
| 58 | +print(res) |
| 59 | + |
| 60 | +######################### |
| 61 | +# The same goes if the input name is misspelled. |
| 62 | + |
| 63 | +try: |
| 64 | + x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32) |
| 65 | + sess.run([output_name], {"misspelled": x}) |
| 66 | +except Exception as e: |
| 67 | + print("Misspelled input name") |
| 68 | + print("{0}: {1}".format(type(e), e)) |
| 69 | + |
| 70 | +######################### |
| 71 | +# *onnxruntime* does not necessarily fail if the input |
| 72 | +# dimension is a multiple of the expected input dimension. |
| 73 | + |
| 74 | +for x in [ |
| 75 | + numpy.array([1.0, 2.0, 3.0, 4.0], dtype=numpy.float32), |
| 76 | + numpy.array([[1.0, 2.0, 3.0, 4.0]], dtype=numpy.float32), |
| 77 | + numpy.array([[1.0, 2.0], [3.0, 4.0]], dtype=numpy.float32), |
| 78 | + numpy.array([1.0, 2.0, 3.0], dtype=numpy.float32), |
| 79 | + numpy.array([[1.0, 2.0, 3.0]], dtype=numpy.float32), |
| 80 | + ]: |
| 81 | + r = sess.run([output_name], {input_name: x}) |
| 82 | + print("Shape={0} and predicted labels={1}".format(x.shape, r)) |
| 83 | + |
| 84 | +for x in [ |
| 85 | + numpy.array([1.0, 2.0, 3.0, 4.0], dtype=numpy.float32), |
| 86 | + numpy.array([[1.0, 2.0, 3.0, 4.0]], dtype=numpy.float32), |
| 87 | + numpy.array([[1.0, 2.0], [3.0, 4.0]], dtype=numpy.float32), |
| 88 | + numpy.array([1.0, 2.0, 3.0], dtype=numpy.float32), |
| 89 | + numpy.array([[1.0, 2.0, 3.0]], dtype=numpy.float32), |
| 90 | + ]: |
| 91 | + r = sess.run(None, {input_name: x}) |
| 92 | + print("Shape={0} and predicted probabilities={1}".format(x.shape, r[1])) |
| 93 | + |
| 94 | +######################### |
| 95 | +# It does not fail either if the number of dimension |
| 96 | +# is higher than expects but produces a warning. |
| 97 | + |
| 98 | +for x in [ |
| 99 | + numpy.array([[[1.0, 2.0], [3.0, 4.0]]], dtype=numpy.float32), |
| 100 | + numpy.array([[[1.0, 2.0, 3.0]]], dtype=numpy.float32), |
| 101 | + numpy.array([[[1.0, 2.0]], [[3.0, 4.0]]], dtype=numpy.float32), |
| 102 | + ]: |
| 103 | + r = sess.run([output_name], {input_name: x}) |
| 104 | + print("Shape={0} and predicted labels={1}".format(x.shape, r)) |
0 commit comments