使用 onnx 模型进行预测时如何解决运行时错误?

how to solve runtime error while predicting using onnx model?

我使用 trainNetwork 在 matlab 中训练了深度学习模型 command.I 想在 python 用于预测,所以我使用 "exportONNXNetwork" 在 matlab 中将网络导出为 onnx 格式 coomand.I 使用以下代码在 python 中导入 onnx 模型: sess = onnxruntime.InferenceSession("Alma.onnx")

该模型接受大小为 (224,224,3) 的图像。因此我使用 cv2.resize 调整了图像的大小。 当我使用 sess.run 命令尝试 运行 模型时,出现 RuntimeError: Input 'data'不能为空。 其中 'data' 是 input_name.The 用于预测的命令是 res = sess.run([output_name], {input_name: x}) 我不知道我要去哪里 wrong.I 我正在分享完整的代码。

import numpy
import cv2
import tensorflow as tf
sess = onnxruntime.InferenceSession("Alma.onnx")
im = cv2.imread("1.jpg")
img = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
x = tf.convert_to_tensor(img)





input_name = sess.get_inputs()[0].name
print("input name", input_name)
input_shape = sess.get_inputs()[0].shape
print("input shape", input_shape)
input_type = sess.get_inputs()[0].type
print("input type", input_type)


output_name = sess.get_outputs()[0].name
print("output name", output_name)
output_shape = sess.get_outputs()[0].shape
print("output shape", output_shape)
output_type = sess.get_outputs()[0].type
print("output type", output_type)

res = sess.run([output_name], {input_name: x})
print(res)

我得到的错误是:

  File "C:/Users/Hanamanth/PycharmProjects/cocoon/onnx.py", line 29, in <module>
    res = sess.run([output_name], {input_name: x})
  File "C:\Users\Hanamanth\PycharmProjects\cocoon\venv\lib\site-packages\onnxruntime\capi\session.py", line 72, in run
    return self._sess.run(output_names, input_feed, run_options)
RuntimeError: Input 'data' must not be empty.
input name data
input shape [1, 3, 224, 224]
input type tensor(float)
output name prob
output shape [1, 2]
output type tensor(float)```


x(sess.run 的输入)应该是一个 np 数组。 例如:

img = cv2.resize(img, (width, height))
# convert image to numpy
x = numpy.asarray(img).astype(<right_type>).reshape(<right_shape>)
res = sess.run([output_name], {input_name: x})