【问题标题】:how to solve runtime error while predicting using onnx model?如何在使用 onnx 模型进行预测时解决运行时错误?
【发布时间】:2023-03-12 17:09:01
【问题描述】:

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

模型接受大小为 (224,224,3) 的图像。所以我使用 cv2.resize 调整了图像的大小。 当我尝试使用 sess.run 命令运行模型时,我收到一个错误,因为 RuntimeError: Input 'data' must not be empty. 其中'data'为input_name。用于预测的命令为res = sess.run([output_name], {input_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)```


【问题讨论】:

    标签: python matlab onnx onnxruntime


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-24
      • 2014-01-03
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多