0

I have deep learning model trained in matlab using trainNetwork command.I want to use that model in python for predicting, so i exported the network to onnx format in matlab using "exportONNXNetwork" coomand.I imported the onnx model in python using the following code: sess = onnxruntime.InferenceSession("Alma.onnx")

The model accepts image of size(224,224,3).so i resized the image using cv2.resize. When i try run the model using sess.run command, i am getting a error as RuntimeError: Input 'data' must not be empty. Where 'data' is input_name.The command used for prediction is res = sess.run([output_name], {input_name: x}) I am not able to figure out where i am going wrong.I am sharing the full code.

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)

The error i am getting is:

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


1 Answer 1

1

x (input to sess.run) should be an np array. For example:

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})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.