microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
14.4k stars 2.89k forks source link

[ErrorCode:InvalidArgument] Invalid Feed Input Name:image #15692

Open amar-mustaqim opened 1 year ago

amar-mustaqim commented 1 year ago

Hi, I am new to onnx and still trying to understand it. I try to detect the image using the pretrained yolov3 onnx model, and did some modification on the code provided in this repository but I'm using opencvsharp instead of imagesharp. However, I'm getting this Microsoft.ML.OnnxRuntime.OnnxRuntimeException: '[ErrorCode:InvalidArgument] Invalid Feed Input Name:image' error every time I run the code.

onnx_error

Why does this happen and where can I amend it? Here is the full code.

using OnnxTest; using Microsoft.ML.OnnxRuntime; using Microsoft.ML.OnnxRuntime.Tensors; using System; using System.Collections.Generic; using System.Linq; using OpenCvSharp;

namespace OnnxTest {

class Program
{
    static void Main(string[] args)
    {
        string modelFilePath = "C:/Users/Amar-TTV/Downloads/yolov3-10/yolov3/yolov3.onnx";
        string imageFilePath = "C:/Users/Amar-TTV/Downloads/puppy.jpg";

        // Read the image
        Console.WriteLine("Reading image: " + imageFilePath);
        //Image<Rgb24> image = Image.Load<Rgb24>(imageFilePath, out IImageFormat format);

        //Mat mat = new Mat();
        Mat image = new Mat(imageFilePath,ImreadModes.Color);
        int width = image.Width;
        int height = image.Height;

        // Convert the image to a tensor
        Tensor<byte> input = new DenseTensor<byte>(new[] { height, width, 3 });

        var indexer = image.GetGenericIndexer<Vec3b>();
        for (int y = 0; y < image.Height; y++)
        {
            for (int x = 0; x < image.Width; x++)
            {
                Vec3b color = indexer[y];
                //byte temp = color.Item0;
                //color.Item0 = color.Item2; // B <- R
                //color.Item2 = temp;        // R <- B
                //indexer[y, x] = color;
                input[y, x, 0] = color.Item0;
                input[y, x, 1] = color.Item1;
                input[y, x, 2] = color.Item2;
            }
        }

        var inputs = new List<NamedOnnxValue> {
            NamedOnnxValue.CreateFromTensor("image", input)
        };

        using var session = new InferenceSession(modelFilePath);

        foreach (var r in session.Run(inputs))
        {
            Console.WriteLine("Output for {0}", r.Name);
            if (r.Name == "top_classes")
            {
                Console.WriteLine(r.AsTensor<Int64>().GetArrayString());
            }
            else
            {
                Console.WriteLine(r.AsTensor<float>().GetArrayString());
            }
        }

    }
}

}

Thank you in advance for your help 😄

xadupre commented 1 year ago

There may be a mismatch between your input and what the model expects: NamedOnnxValue.CreateFromTensor("image", input). You can use a tool like netron to see the input name ("image" in your case) and input shape and type. Maybe the model expects floats and you give it bytes.

amar-mustaqim commented 1 year ago

This is what I got when I run the model in netron.

netron

So meaning that my input name should be input_1 with float type, and I should amend to those lines right?

// Convert the image to a tensor Tensor<float> input = new DenseTensor<float>(new[] { height, width, 3 }); . . . NamedOnnxValue.CreateFromTensor("input_1", input)

But I got another error when I made those changes. What does it means?

onnx_error_1