如何解决在预测时使用ONNX模型时的运行时错误?

2024-06-01 03:41:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我用trainNetwork命令在matlab中训练了深度学习模型。我想用python中的模型进行预测,所以我用“exportONNXNetwork”命令将网络导出到matlab中的onnx格式。我用python中的以下代码导入onnx模型: sess = onnxruntime.InferenceSession("Alma.onnx")

模型接受大小为(224224,3)的图像,因此我使用cv2.resize调整了图像的大小。 当我尝试使用运行模型时赛斯·润命令,我得到一个错误,即运行时错误:输入'data'不能为空。 其中输入“数据”_名字。那个用于预测的命令是res=赛斯·润([output\u name],{input\u name:x}) 我无法找出哪里出了问题。我正在共享完整的代码。你知道吗

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)```



Tags: runname模型命令inputoutputgettype
1条回答
网友
1楼 · 发布于 2024-06-01 03:41:54

x(输入到赛斯·润)应该是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})

相关问题 更多 >