Skip to content

Updating the ApplyOnnxModel transform to meet the API parameter ordering standards #3086

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

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/samples/Microsoft.ML.Samples/Dynamic/OnnxTransform.cs
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ public static void Example()
var mlContext = new MLContext();
var data = GetTensorData();
var idv = mlContext.Data.LoadFromEnumerable(data);
var pipeline = mlContext.Transforms.ApplyOnnxModel(modelPath, new[] { outputInfo.Key }, new[] { inputInfo.Key });
var pipeline = mlContext.Transforms.ApplyOnnxModel(new[] { outputInfo.Key }, new[] { inputInfo.Key }, modelPath);
Copy link
Contributor

@zeahmed zeahmed Mar 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modelPath [](start = 114, length = 9)

I see at line 20 where session is loaded. Does the ApplyOnnxModel reload the model? If it does then there needs to be a way to reuse the session object created above. #WontFix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good point. Can you file an issue on this? We can do this as we start to fix outstanding issues in the ONNX implementation.


In reply to: 268890248 [](ancestors = 268890248)


// Run the pipeline and get the transformed values
var transformedValues = pipeline.Fit(idv).Transform(idv);
8 changes: 4 additions & 4 deletions src/Microsoft.ML.OnnxTransformer/OnnxCatalog.cs
Original file line number Diff line number Diff line change
@@ -33,15 +33,15 @@ public static OnnxScoringEstimator ApplyOnnxModel(this TransformsCatalog catalog
/// Applies a pre-trained Onnx model.
/// </summary>
/// <param name="catalog">The transform's catalog.</param>
/// <param name="modelFile">The path of the file containing the ONNX model.</param>
/// <param name="outputColumnName">The output column resulting from the transformation.</param>
/// <param name="inputColumnName">The input column.</param>
/// <param name="modelFile">The path of the file containing the ONNX model.</param>
/// <param name="gpuDeviceId">Optional GPU device ID to run execution on, <see langword="null" /> to run on CPU.</param>
/// <param name="fallbackToCpu">If GPU error, raise exception or fallback to CPU.</param>
public static OnnxScoringEstimator ApplyOnnxModel(this TransformsCatalog catalog,
string modelFile,
string outputColumnName,
string inputColumnName,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputColumnName [](start = 19, length = 15)

should this default to the outputColumnName like the other inputColumns?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my comment is the same.


In reply to: 268883194 [](ancestors = 268883194)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back and forth on this. I don't think we need to have this as an "in-place" transform because we expect models to create new columns, similar to the TensorFlow transform.

string modelFile,
int? gpuDeviceId = null,
bool fallbackToCpu = false)
=> new OnnxScoringEstimator(CatalogUtils.GetEnvironment(catalog), new[] { outputColumnName }, new[] { inputColumnName }, modelFile, gpuDeviceId, fallbackToCpu);
@@ -50,15 +50,15 @@ public static OnnxScoringEstimator ApplyOnnxModel(this TransformsCatalog catalog
/// Applies a pre-trained Onnx model.
/// </summary>
/// <param name="catalog">The transform's catalog.</param>
/// <param name="modelFile">The path of the file containing the ONNX model.</param>
/// <param name="outputColumnNames">The output columns resulting from the transformation.</param>
/// <param name="inputColumnNames">The input columns.</param>
/// <param name="modelFile">The path of the file containing the ONNX model.</param>
/// <param name="gpuDeviceId">Optional GPU device ID to run execution on, <see langword="null" /> to run on CPU.</param>
/// <param name="fallbackToCpu">If GPU error, raise exception or fallback to CPU.</param>
public static OnnxScoringEstimator ApplyOnnxModel(this TransformsCatalog catalog,
string modelFile,
string[] outputColumnNames,
string[] inputColumnNames,
string modelFile,
int? gpuDeviceId = null,
bool fallbackToCpu = false)
=> new OnnxScoringEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnNames, inputColumnNames, modelFile, gpuDeviceId, fallbackToCpu);
8 changes: 4 additions & 4 deletions test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ void TestSimpleCase()
var xyData = new List<TestDataXY> { new TestDataXY() { A = new float[inputSize] } };
var stringData = new List<TestDataDifferntType> { new TestDataDifferntType() { data_0 = new string[inputSize] } };
var sizeData = new List<TestDataSize> { new TestDataSize() { data_0 = new float[2] } };
var pipe = ML.Transforms.ApplyOnnxModel(modelFile, new[] { "softmaxout_1" }, new[] { "data_0" });
var pipe = ML.Transforms.ApplyOnnxModel(new[] { "softmaxout_1" }, new[] { "data_0" }, modelFile);

var invalidDataWrongNames = ML.Data.LoadFromEnumerable(xyData);
var invalidDataWrongTypes = ML.Data.LoadFromEnumerable(stringData);
@@ -137,7 +137,7 @@ void TestOldSavingAndLoading(int? gpuDeviceId, bool fallbackToCpu)

var inputNames = new[] { "data_0" };
var outputNames = new[] { "softmaxout_1" };
var est = ML.Transforms.ApplyOnnxModel(modelFile, outputNames, inputNames, gpuDeviceId, fallbackToCpu);
var est = ML.Transforms.ApplyOnnxModel(outputNames, inputNames, modelFile, gpuDeviceId, fallbackToCpu);
var transformer = est.Fit(dataView);
var result = transformer.Transform(dataView);
var resultRoles = new RoleMappedData(result);
@@ -241,7 +241,7 @@ public void OnnxModelScenario()
}
});

var onnx = ML.Transforms.ApplyOnnxModel(modelFile, "softmaxout_1", "data_0").Fit(dataView).Transform(dataView);
var onnx = ML.Transforms.ApplyOnnxModel("softmaxout_1", "data_0", modelFile).Fit(dataView).Transform(dataView);
var scoreCol = onnx.Schema["softmaxout_1"];

using (var curs = onnx.GetRowCursor(scoreCol))
@@ -271,7 +271,7 @@ public void OnnxModelMultiInput()
inb = new float[] {1,2,3,4,5}
}
});
var onnx = ML.Transforms.ApplyOnnxModel(modelFile, new[] { "outa", "outb" }, new[] { "ina", "inb" }).Fit(dataView).Transform(dataView);
var onnx = ML.Transforms.ApplyOnnxModel(new[] { "outa", "outb" }, new[] { "ina", "inb" }, modelFile).Fit(dataView).Transform(dataView);

var outaCol = onnx.Schema["outa"];
var outbCol = onnx.Schema["outb"];
4 changes: 2 additions & 2 deletions test/Microsoft.ML.Tests/OnnxConversionTest.cs
Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ public void SimpleEndToEndOnnxConversionTest()
// Step 3: Evaluate the saved ONNX model using the data used to train the ML.NET pipeline.
string[] inputNames = onnxModel.Graph.Input.Select(valueInfoProto => valueInfoProto.Name).ToArray();
string[] outputNames = onnxModel.Graph.Output.Select(valueInfoProto => valueInfoProto.Name).ToArray();
var onnxEstimator = mlContext.Transforms.ApplyOnnxModel(onnxModelPath, outputNames, inputNames);
var onnxEstimator = mlContext.Transforms.ApplyOnnxModel(outputNames, inputNames, onnxModelPath);
var onnxTransformer = onnxEstimator.Fit(data);
var onnxResult = onnxTransformer.Transform(data);

@@ -162,7 +162,7 @@ public void KmeansOnnxConversionTest()
// Evaluate the saved ONNX model using the data used to train the ML.NET pipeline.
string[] inputNames = onnxModel.Graph.Input.Select(valueInfoProto => valueInfoProto.Name).ToArray();
string[] outputNames = onnxModel.Graph.Output.Select(valueInfoProto => valueInfoProto.Name).ToArray();
var onnxEstimator = mlContext.Transforms.ApplyOnnxModel(onnxModelPath, outputNames, inputNames);
var onnxEstimator = mlContext.Transforms.ApplyOnnxModel(outputNames, inputNames, onnxModelPath);
var onnxTransformer = onnxEstimator.Fit(data);
var onnxResult = onnxTransformer.Transform(data);
CompareSelectedR4VectorColumns("Score", "Score0", transformedData, onnxResult, 3);