I have created a Tensoflow model in Python and exported it to an ONNX model to use in C#. The problem is that I don't know how to interpret the input and output parameters of the model in C#. I've seen some posts for specific situations, but for my (pretty simple) model, I can't find out what to do.
My model input and output is as follows: enter image description here
In my C# project I create an input class:
public class OnnxInput
{
[VectorType(1)]
[ColumnName("input_3")]
public float[]? Temperaturen { get; set; }
}
and an output class:
public class OnnxOutput
{
[VectorType(1)]
[ColumnName("dense_5")]
public float[]? PredictedTemp { get; set; }
}
My C# code:
private void button1_Click(object sender, EventArgs e)
{
MLContext mlContext = new MLContext();
var onnxPredictionPipeline = GetPredictionPipeline(mlContext);
var onnxPredictionEngine = mlContext.Model.CreatePredictionEngine<OnnxInput, OnnxOutput>(onnxPredictionPipeline);
var testInput = new OnnxInput
{
Temperaturen = new float[] { 1f, 2f, 3f, 4f, 5f }
};
var prediction = onnxPredictionEngine.Predict(testInput);
MessageBox.Show($"Predicted Fare: {prediction.PredictedTemp}");
}
static ITransformer GetPredictionPipeline(MLContext mlContext)
{
var inputColumns = new string[] {"input_3"};
var outputColumns = new string[] { "dense_5" };
var onnxPredictionPipeline =
mlContext
.Transforms
.ApplyOnnxModel(
outputColumnNames: outputColumns,
inputColumnNames: inputColumns,
ONNX_MODEL_PATH);
var emptyDv = mlContext.Data.LoadFromEnumerable(new OnnxInput[] { });
return onnxPredictionPipeline.Fit(emptyDv);
}
}
The error I get is: System.InvalidOperationException: 'Input shape mismatch: Input 'input_3' has shape 1,5,1, but input data is of length 1.'
Can anyone help me on this input/output? Is there maybe somewhere a ONNX2C# converter or something? ;-)
Thanks in advance!
- Bender