|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
5 | 5 | using System.IO;
|
| 6 | +using Microsoft.ML.Data; |
6 | 7 | using Microsoft.ML.Functional.Tests.Datasets;
|
7 | 8 | using Microsoft.ML.RunTests;
|
8 | 9 | using Microsoft.ML.TestFramework;
|
@@ -48,18 +49,21 @@ public void SaveOnnxModelLoadAndScoreFastTree()
|
48 | 49 | mlContext.Model.ConvertToOnnx(model, data, file);
|
49 | 50 |
|
50 | 51 | // Load the model as a transform.
|
51 |
| - var onnxEstimator = mlContext.Transforms.ApplyOnnxModel(modelPath); |
| 52 | + // Note that when saving an ML.NET model as an ONNX model, the column types and column names will |
| 53 | + // change. The name changes as ONNX doesn't not allow the same name for an input and output within the ONNX model. |
| 54 | + // Therefore names maintained but have a number appended to the end of the name. In this case, Score0 is the output |
| 55 | + // of the ONNX model. We are renaming Score0 to Score using Copy Columns. |
| 56 | + // ONNX also uses tensors and will return an output of a tensor with the dimension of [1,1] for a single float. |
| 57 | + // Therefore the VectorScoreColumn class (which contains a float [] field called Score) is used for the return |
| 58 | + // type on the Prediction engine. |
| 59 | + // See #2980 and #2981 for more information. |
| 60 | + var onnxEstimator = mlContext.Transforms.ApplyOnnxModel(modelPath) |
| 61 | + .Append(mlContext.Transforms.CopyColumns("Score", "Score0")); |
52 | 62 | var onnxModel = onnxEstimator.Fit(data);
|
53 | 63 |
|
54 |
| - // TODO #2980: ONNX outputs don't match the outputs of the model, so we must hand-correct this for now. |
55 |
| - // TODO #2981: ONNX models cannot be fit as part of a pipeline, so we must use a workaround like this. |
56 |
| - var onnxWorkaroundPipeline = onnxModel.Append( |
57 |
| - mlContext.Transforms.CopyColumns("Score", "Score0").Fit(onnxModel.Transform(data))); |
58 |
| - |
59 | 64 | // Create prediction engine and test predictions.
|
60 | 65 | var originalPredictionEngine = mlContext.Model.CreatePredictionEngine<HousingRegression, ScoreColumn>(model);
|
61 |
| - // TODO #2982: ONNX produces vector types and not the original output type. |
62 |
| - var onnxPredictionEngine = mlContext.Model.CreatePredictionEngine<HousingRegression, VectorScoreColumn>(onnxWorkaroundPipeline); |
| 66 | + var onnxPredictionEngine = mlContext.Model.CreatePredictionEngine<HousingRegression, VectorScoreColumn>(onnxModel); |
63 | 67 |
|
64 | 68 | // Take a handful of examples out of the dataset and compute predictions.
|
65 | 69 | var dataEnumerator = mlContext.Data.CreateEnumerable<HousingRegression>(mlContext.Data.TakeRows(data, 5), false);
|
|
0 commit comments