jad
jad

Reputation: 77

C# loads tensorflow keras trained onnx model

I'm trying to feed input (1, 37) float[] array to tensorflow keras trained model with onnx. The input shape of model should be 3D (1, 1, 37) so I reshaped it with the following code. But, at session.Run(inputs); got this error,

Microsoft.ML.OnnxRuntime.OnnxRuntimeException: '[ErrorCode:InvalidArgument] Got invalid dimensions for input: float_input for the following indices index: 1 Got: 1 Expected: 37 index: 2 Got: 37 Expected: 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
            
var model_file = "C:/keras_model.onnx";
var session = new InferenceSession(model_file);
    Console.WriteLine("Hellof, World!");
    // Get the names of the input tensors
    var inputNames = session.InputMetadata.Values;

    // Print the names of the input tensors
    foreach (var inputName in inputNames)
    {
        Console.WriteLine("Input name: " + inputName);
    }

    var inputData1 = new float[] { 1.10E+01f,4.00f,1.00f,3.00f,3.00f,8.00f,5.00f,7.00f,2.00f,7.00f,7.00f,1.00f,4.00f,3.00f,3.00f,1.00f,5.00f,0.00f,1.00f,2.30E+01f,-1.70E-01f,2.08E-01f,2.02E-01f,-1.06f,7.71E-02f,2.29E-01f,4.94E-02f,4.04E-02f,1.10E-01f,1.08E-01f,-2.29E-01f,-4.22E-01f,1.34E-02f,3.38E-01f,-3.79E-02f,2.91E+02f,2.91E+02f };
    //var inputTensor1 = new DenseTensor<float>(inputData1, new int[] { 1, 37 });
    var inputTensor = new DenseTensor<float>(inputData1, new int[] { 1, 37 }); // Input shape: (1, 37)
    var reshapedInput = inputTensor.Reshape(new int[] { 1, 1, 37 }); // New shape: (1, 1, 37)

var inputs = new List<NamedOnnxValue>() {
        NamedOnnxValue.CreateFromTensor<float>("float_input",reshapedInput),
    };
    
    //var results = session.Run(inputs);
    var results = session.Run(inputs);

How to correctly reshape the input array in this case?

Upvotes: 0

Views: 484

Answers (1)

david
david

Reputation: 212

Do you found a solution ?

If can help, in python, you have to do like this:

new_data = {
    "pm_bin00": [value00],
    "pm_bin01": [value01],
    # ...
    "pm_bin14": [value14],
    "pm_bin15": [value15]
}
# Convert to DataFrame
df_new = pd.DataFrame(new_data)

Upvotes: 0

Related Questions