我使用 trainNetwork 命令在matlab中训练了深度学习模型。我想在python中使用该模型进行预测,因此我在matlab中将网络导出为onnx格式使用“ exportONNXNetwork”库曼。我使用以下代码在python中导入了onnx模型:
sess = onnxruntime.InferenceSession("Alma.onnx")
该模型接受大小为(224,224,3)的图像。因此我使用cv2.resize调整了图像的大小。 当我尝试使用 sess.run 命令运行模型时,由于出现 RuntimeError:输入'数据'不能为空。 其中“数据” 为输入名称。用于预测的命令为 res = sess.run([输出名称],{输入名称: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)```
答案 0 :(得分:0)
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})