C#加载tensorflow keras训练的onnx模型

b09cbbtk  于 2023-10-19  发布在  C#
关注(0)|答案(1)|浏览(206)

我正在尝试使用onnx将input(1,37)float[]数组输入到tensorflow keras训练模型中。模型的输入形状应该是3D(1,1,37),所以我用下面的代码重塑了它。但是,在session.Run(inputs);得到了这个错误,
Microsoft.ML.OnnxRuntime.OnnxRuntimeException:'[ErrorCode:InvalidArgument]输入的维度无效:float_input用于以下索引:1 Got:1 Expected:37 index:2 Got:37预期: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);

在这种情况下,如何正确地重塑输入数组?

fruv7luv

fruv7luv1#

你找到解决办法了吗
如果可以帮助,在python中,你必须这样做:

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

相关问题