Python 如何解决使用onnx模型进行预测时的运行时错误?
我使用trainNetwork命令在matlab中对深度学习模型进行了培训。我想在python中使用该模型进行预测,因此我使用exportONNXNetwork coomand将网络导出到matlab中的onnx格式。我使用以下代码在python中导入onnx模型: sess=onnxruntime.推断sessionalma.onnx 该模型接受大小为24224,3的图像。因此,我使用cv2.resize调整了图像的大小。 当我尝试使用sess.run命令运行模型时,我得到一个错误作为RuntimeError:输入“data”不能为空。 其中'data'是input_name。用于预测的命令是res=sess。运行[output_name],{input_name:x} 我无法找出哪里出了问题。我正在共享完整的代码Python 如何解决使用onnx模型进行预测时的运行时错误?,python,matlab,onnx,onnxruntime,Python,Matlab,Onnx,Onnxruntime,我使用trainNetwork命令在matlab中对深度学习模型进行了培训。我想在python中使用该模型进行预测,因此我使用exportONNXNetwork coomand将网络导出到matlab中的onnx格式。我使用以下代码在python中导入onnx模型: sess=onnxruntime.推断sessionalma.onnx 该模型接受大小为24224,3的图像。因此,我使用cv2.resize调整了图像的大小。 当我尝试使用sess.run命令运行模型时,我得到一个错误作为Runt
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)
我得到的错误是:
sess.run的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})
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})