注意
跳转到底部 以下载完整示例代码。
onnxruntime 常见错误#
此示例探讨了 onnxruntime 不返回模型预测而是引发异常的几种常见情况。它首先加载在示例 步骤 1:使用您喜欢的框架训练模型 中训练的模型,该模型在 Iris 数据集上训练了一个逻辑回归模型。该模型接受一个维度为 2 的向量,并返回三个类别中的一个。
import numpy
import onnxruntime as rt
from onnxruntime.capi.onnxruntime_pybind11_state import InvalidArgument
from onnxruntime.datasets import get_example
example2 = get_example("logreg_iris.onnx")
sess = rt.InferenceSession(example2, providers=rt.get_available_providers())
input_name = sess.get_inputs()[0].name
output_name = sess.get_outputs()[0].name
第一个示例由于 错误的类型 而失败。onnxruntime 只接受单精度浮点数(4 字节),不能处理任何其他类型的浮点数。
try:
x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float64)
sess.run([output_name], {input_name: x})
except Exception as e:
print("Unexpected type")
print(f"{type(e)}: {e}")
Unexpected type
<class 'onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument'>: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Unexpected input data type. Actual: (tensor(double)) , expected: (tensor(float))
如果名称拼写错误,模型将无法返回输出。
try:
x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32)
sess.run(["misspelled"], {input_name: x})
except Exception as e:
print("Misspelled output name")
print(f"{type(e)}: {e}")
Misspelled output name
<class 'onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument'>: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid output name:misspelled
输出名称是可选的,可以用 None 替换,此时 onnxruntime 将返回所有输出。
x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32)
try:
res = sess.run(None, {input_name: x})
print("All outputs")
print(res)
except (RuntimeError, InvalidArgument) as e:
print(e)
All outputs
[array([0, 0, 0], dtype=int64), [{0: 0.9505997896194458, 1: 0.027834143489599228, 2: 0.021566055715084076}, {0: 0.9974970817565918, 1: 5.6270167988259345e-05, 2: 0.0024466365575790405}, {0: 0.9997311234474182, 1: 1.787709464906584e-07, 2: 0.0002686927327886224}]]
如果输入名称拼写错误,情况也相同。
try:
x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32)
sess.run([output_name], {"misspelled": x})
except Exception as e:
print("Misspelled input name")
print(f"{type(e)}: {e}")
Misspelled input name
<class 'ValueError'>: Required inputs (['float_input']) are missing from input feed (['misspelled']).
如果输入维度是预期输入维度的倍数,onnxruntime 不一定会失败。
for x in [
numpy.array([1.0, 2.0, 3.0, 4.0], dtype=numpy.float32),
numpy.array([[1.0, 2.0, 3.0, 4.0]], dtype=numpy.float32),
numpy.array([[1.0, 2.0], [3.0, 4.0]], dtype=numpy.float32),
numpy.array([1.0, 2.0, 3.0], dtype=numpy.float32),
numpy.array([[1.0, 2.0, 3.0]], dtype=numpy.float32),
]:
try:
r = sess.run([output_name], {input_name: x})
print(f"Shape={x.shape} and predicted labels={r}")
except (RuntimeError, InvalidArgument) as e:
print(f"ERROR with Shape={x.shape} - {e}")
for x in [
numpy.array([1.0, 2.0, 3.0, 4.0], dtype=numpy.float32),
numpy.array([[1.0, 2.0, 3.0, 4.0]], dtype=numpy.float32),
numpy.array([[1.0, 2.0], [3.0, 4.0]], dtype=numpy.float32),
numpy.array([1.0, 2.0, 3.0], dtype=numpy.float32),
numpy.array([[1.0, 2.0, 3.0]], dtype=numpy.float32),
]:
try:
r = sess.run(None, {input_name: x})
print(f"Shape={x.shape} and predicted probabilities={r[1]}")
except (RuntimeError, InvalidArgument) as e:
print(f"ERROR with Shape={x.shape} - {e}")
ERROR with Shape=(4,) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: float_input Got: 1 Expected: 2 Please fix either the inputs/outputs or the model.
ERROR with Shape=(1, 4) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: float_input for the following indices
index: 0 Got: 1 Expected: 3
index: 1 Got: 4 Expected: 2
Please fix either the inputs/outputs or the model.
ERROR with Shape=(2, 2) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: float_input for the following indices
index: 0 Got: 2 Expected: 3
Please fix either the inputs/outputs or the model.
ERROR with Shape=(3,) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: float_input Got: 1 Expected: 2 Please fix either the inputs/outputs or the model.
ERROR with Shape=(1, 3) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: float_input for the following indices
index: 0 Got: 1 Expected: 3
index: 1 Got: 3 Expected: 2
Please fix either the inputs/outputs or the model.
ERROR with Shape=(4,) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: float_input Got: 1 Expected: 2 Please fix either the inputs/outputs or the model.
ERROR with Shape=(1, 4) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: float_input for the following indices
index: 0 Got: 1 Expected: 3
index: 1 Got: 4 Expected: 2
Please fix either the inputs/outputs or the model.
ERROR with Shape=(2, 2) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: float_input for the following indices
index: 0 Got: 2 Expected: 3
Please fix either the inputs/outputs or the model.
ERROR with Shape=(3,) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: float_input Got: 1 Expected: 2 Please fix either the inputs/outputs or the model.
ERROR with Shape=(1, 3) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: float_input for the following indices
index: 0 Got: 1 Expected: 3
index: 1 Got: 3 Expected: 2
Please fix either the inputs/outputs or the model.
如果维度数量高于预期,它也不会失败,但会产生警告。
for x in [
numpy.array([[[1.0, 2.0], [3.0, 4.0]]], dtype=numpy.float32),
numpy.array([[[1.0, 2.0, 3.0]]], dtype=numpy.float32),
numpy.array([[[1.0, 2.0]], [[3.0, 4.0]]], dtype=numpy.float32),
]:
try:
r = sess.run([output_name], {input_name: x})
print(f"Shape={x.shape} and predicted labels={r}")
except (RuntimeError, InvalidArgument) as e:
print(f"ERROR with Shape={x.shape} - {e}")
ERROR with Shape=(1, 2, 2) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: float_input Got: 3 Expected: 2 Please fix either the inputs/outputs or the model.
ERROR with Shape=(1, 1, 3) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: float_input Got: 3 Expected: 2 Please fix either the inputs/outputs or the model.
ERROR with Shape=(2, 1, 2) - [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: float_input Got: 3 Expected: 2 Please fix either the inputs/outputs or the model.
脚本总运行时间:(0 分 0.009 秒)