-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Allow user to save PredictorTransform in file and then convert it to … #3986
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| using System.Text.RegularExpressions; | ||
| using Google.Protobuf; | ||
| using Microsoft.ML.Data; | ||
| using Microsoft.ML.EntryPoints; | ||
| using Microsoft.ML.Model.OnnxConverter; | ||
| using Microsoft.ML.RunTests; | ||
| using Microsoft.ML.Runtime; | ||
|
|
@@ -186,7 +187,7 @@ void CommandLineOnnxConversionTest() | |
| string modelPath = GetOutputPath("ModelWithLessIO.zip"); | ||
| var trainingPathArgs = $"data={dataPath} out={modelPath}"; | ||
| var trainingArgs = " loader=text{col=Label:BL:0 col=F1:R4:1-8 col=F2:TX:9} xf=Cat{col=F2} xf=Concat{col=Features:F1,F2} tr=ft{numberOfThreads=1 numberOfLeaves=8 numberOfTrees=3} seed=1"; | ||
| Assert.Equal(0, Maml.Main(new[] { "train " + trainingPathArgs + trainingArgs})); | ||
| Assert.Equal(0, Maml.Main(new[] { "train " + trainingPathArgs + trainingArgs })); | ||
|
|
||
| var subDir = Path.Combine("..", "..", "BaselineOutput", "Common", "Onnx", "BinaryClassification", "BreastCancer"); | ||
| var onnxTextName = "ModelWithLessIO.txt"; | ||
|
|
@@ -403,6 +404,130 @@ public void MulticlassLogisticRegressionOnnxConversionTest() | |
| Done(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LoadingPredictorModelAndOnnxConversionTest() | ||
| { | ||
| string dataPath = GetDataPath("iris.txt"); | ||
| string modelPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".model.bin"; | ||
| string onnxPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".model.onnx"; | ||
| string onnxJsonPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".model.onnx.json"; | ||
|
|
||
| string inputGraph = string.Format(@" | ||
| {{ | ||
| 'Inputs': {{ | ||
| 'inputFile': '{0}' | ||
| }}, | ||
| 'Nodes': [ | ||
| {{ | ||
| 'Name': 'Data.TextLoader', | ||
| 'Inputs': | ||
| {{ | ||
| 'InputFile': '$inputFile', | ||
| 'Arguments': | ||
| {{ | ||
| 'UseThreads': true, | ||
| 'HeaderFile': null, | ||
| 'MaxRows': null, | ||
| 'AllowQuoting': true, | ||
| 'AllowSparse': true, | ||
| 'InputSize': null, | ||
| 'TrimWhitespace': false, | ||
| 'HasHeader': false, | ||
| 'Column': | ||
| [ | ||
| {{'Name':'Sepal_Width','Type':null,'Source':[{{'Min':2,'Max':2,'AutoEnd':false,'VariableEnd':false,'AllOther':false,'ForceVector':false}}],'KeyCount':null}}, | ||
| {{'Name':'Petal_Length','Type':null,'Source':[{{'Min':3,'Max':3,'AutoEnd':false,'VariableEnd':false,'AllOther':false,'ForceVector':false}}],'KeyCount':null}}, | ||
| {{'Name':'Petal_Width','Type':null,'Source':[{{'Min':4,'Max':4,'AutoEnd':false,'VariableEnd':false,'AllOther':false,'ForceVector':false}}],'KeyCount':null}}, | ||
| ] | ||
| }} | ||
| }}, | ||
| 'Outputs': | ||
| {{ | ||
| 'Data': '$optional_data' | ||
| }} | ||
| }}, | ||
| {{ | ||
| 'Inputs': {{ | ||
| 'Data': '$optional_data', | ||
| 'Features': [ | ||
| 'Petal_Length', | ||
| 'Petal_Width', | ||
| ] | ||
| }}, | ||
| 'Name': 'Transforms.FeatureCombiner', | ||
| 'Outputs': {{ | ||
| 'OutputData': '$output_data' | ||
| }} | ||
| }}, | ||
| {{ | ||
| 'Inputs': {{ | ||
| 'FeatureColumnName': 'Features', | ||
| 'LabelColumnName': 'Sepal_Width', | ||
| 'TrainingData': '$output_data', | ||
| }}, | ||
| 'Name': 'Trainers.StochasticDualCoordinateAscentRegressor', | ||
| 'Outputs': {{ | ||
| 'PredictorModel': '$output_model' | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is typed to |
||
| }} | ||
| }} | ||
| ], | ||
| 'Outputs': {{ | ||
| 'output_model': '{1}' | ||
| }} | ||
| }}", dataPath.Replace("\\", "\\\\"), modelPath.Replace("\\", "\\\\")); | ||
|
|
||
| // Write entry point graph into file so that it can be invoke by graph runner below. | ||
| var jsonPath = DeleteOutputPath("graph.json"); | ||
| File.WriteAllLines(jsonPath, new[] { inputGraph }); | ||
|
|
||
| // Execute the saved entry point graph to produce a predictive model. | ||
| var args = new ExecuteGraphCommand.Arguments() { GraphPath = jsonPath }; | ||
| var cmd = new ExecuteGraphCommand(Env, args); | ||
| cmd.Run(); | ||
|
|
||
| // Make entry point graph to conduct ONNX conversion. | ||
| inputGraph = string.Format(@" | ||
| {{ | ||
| 'Inputs': {{ | ||
| 'model': '{0}' | ||
| }}, | ||
| 'Nodes': [ | ||
| {{ | ||
| 'Inputs': {{ | ||
| 'Domain': 'com.microsoft.models', | ||
| 'Json': '{1}', | ||
| 'Model': '$model', | ||
| 'Onnx': '{2}', | ||
| 'OnnxVersion': 'Experimental' | ||
| }}, | ||
| 'Name': 'Models.OnnxConverter', | ||
| 'Outputs': {{}} | ||
| }} | ||
| ], | ||
| 'Outputs': {{}} | ||
| }} | ||
| ", modelPath.Replace("\\", "\\\\"), onnxJsonPath.Replace("\\", "\\\\"), onnxPath.Replace("\\", "\\\\")); | ||
|
|
||
| // Write entry point graph for ONNX conversion into file so that it can be invoke by graph runner below. | ||
| jsonPath = DeleteOutputPath("graph.json"); | ||
| File.WriteAllLines(jsonPath, new[] { inputGraph }); | ||
|
|
||
| // Onnx converter's assembly is not loaded by default, so we need to register it before calling it. | ||
| Env.ComponentCatalog.RegisterAssembly(typeof(OnnxExportExtensions).Assembly); | ||
|
|
||
| // Execute the saved entry point graph to convert the saved model. | ||
| args = new ExecuteGraphCommand.Arguments() { GraphPath = jsonPath }; | ||
| cmd = new ExecuteGraphCommand(Env, args); | ||
| cmd.Run(); | ||
|
|
||
| File.Delete(modelPath); | ||
| File.Delete(onnxPath); | ||
| File.Delete(onnxJsonPath); | ||
|
|
||
| Done(); | ||
| } | ||
|
|
||
|
|
||
| [Fact] | ||
| public void RemoveVariablesInPipelineTest() | ||
| { | ||
|
|
@@ -451,7 +576,7 @@ public void RemoveVariablesInPipelineTest() | |
|
|
||
| private class SmallSentimentExample | ||
| { | ||
| [LoadColumn(0,3), VectorType(4)] | ||
| [LoadColumn(0, 3), VectorType(4)] | ||
| public string[] Tokens; | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can it be named PredictorModel ?