Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot fit an Onnx Transform as part of a pipeline #2981

Closed
rogancarr opened this issue Mar 15, 2019 · 3 comments · Fixed by #3253 or #4849
Closed

Cannot fit an Onnx Transform as part of a pipeline #2981

rogancarr opened this issue Mar 15, 2019 · 3 comments · Fixed by #3253 or #4849
Assignees
Labels
bug Something isn't working

Comments

@rogancarr
Copy link
Contributor

It is possible to fit an ONNX model by itself:

var onnxEstimator = mlContext.Transforms.ApplyOnnxModel(modelPath)
var onnxModel = onnxEstimator.Fit(data);

But it throws when it is part of a pipeline:

var onnxEstimator = mlContext.Transforms.ApplyOnnxModel(modelPath)
    // TODO #2980: ONNX outputs don't match the outputs of the model, so we must hand-correct this for now.
    .Append(mlContext.Transforms.CopyColumns("Score", "Score0"));
    .Append(mlContext.Transforms.CopyColumns("Label", "Fable"));
    .Append(mlContext.Transforms.NormalizeLpNorm("Features2", "Features", LpNormNormalizingEstimatorBase.NormFunction.L2));
var onnxModel = onnxEstimator.Fit(data);

Any of these Append statements cause a throw. They do not affect the ONNX model at all, and use either the result of the calculation (Score0), rows unused by the model (Label) or rows also used by the transform (Features).

In this case, the error message is:
System.ArgumentOutOfRangeException : Schema mismatch for input column 'Label': expected vector, got R4 Parameter name: inputSchema

@rogancarr rogancarr added the bug Something isn't working label Mar 15, 2019
@rogancarr
Copy link
Contributor Author

Must be related to #2982, that the type changes when passed through ONNX.

@singlis
Copy link
Member

singlis commented Apr 9, 2019

Based on #2982 and #2980, I was able to get this to work by specifying a custom score class:

    internal sealed class OnnxScoreColumn
    {
        [ColumnName("Score0")]
        public float[] Score { get; set; }
    }
...

var onnxPredictionEngine = mlContext.Model.CreatePredictionEngine<HousingRegression, OnnxScoreColumn>(onnxModel);

@rogancarr - I will post a PR with the changes and then close these three issues.

singlis added a commit to singlis/machinelearning that referenced this issue Apr 9, 2019
Score column being named "Score0". The ONNX model will rename the output
columns by design, therefore a different class with the ColumnName of
"Score0" is needed. This fixes the test pipeline to address this issue.

Fixes dotnet#2981
@singlis
Copy link
Member

singlis commented Apr 9, 2019

@rogancarr - sorry I misunderstood the bug. I see what you are describing and will post a fix.

singlis added a commit that referenced this issue Apr 18, 2019
Fixes #2981

* When adding an ONNX transform to an ML.NET pipeline, an exception would
occur if the input type was not a variable vector or vector type. This
is not needed as we do support converting basic types to equivalent ONNX
tensor type. Therefore the check was modified to throw if the type is a variable vector.
@ghost ghost locked as resolved and limited conversation to collaborators Mar 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants