Skip to content

Export to ONNX and cross-platform command-line tool to script ML.NET training and inference #248

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 29 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1b78603
Export to ONNX and Maml cross-platform executable
codemzs May 28, 2018
1508cd4
misc.
codemzs May 29, 2018
c6763f3
PR feedback.
codemzs May 30, 2018
b04ef49
PR feedback.
codemzs May 30, 2018
eaa8e3a
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs May 30, 2018
de02e1e
resolve merge issues.
codemzs May 30, 2018
6f4434e
cleanup.
codemzs May 30, 2018
6c97416
cleanup.
codemzs May 30, 2018
534fcd1
PR feedback.
codemzs May 30, 2018
caebed0
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs May 30, 2018
692b526
update test baselines.
codemzs May 30, 2018
6c92033
cleanup.
codemzs May 30, 2018
e466157
cleanup.
codemzs May 30, 2018
5a04795
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs May 31, 2018
8ef9b3f
PR feedback.
codemzs May 31, 2018
64cdb80
PR feedback.
codemzs May 31, 2018
3276dd3
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs Jun 1, 2018
c6bc1c6
PR feedback.
codemzs Jun 1, 2018
17a738a
update baselines and regenerate csharp APIs.
codemzs Jun 1, 2018
5090d24
Add link to the commit in ONNX MD file.
codemzs Jun 1, 2018
4cfed38
PR feedback.
codemzs Jun 1, 2018
0a13ad7
cleanup.
codemzs Jun 1, 2018
5bea824
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs Jun 4, 2018
2ab729f
Add missing attributes to ONNX model.
codemzs Jun 4, 2018
faf528c
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs Jun 5, 2018
3dfd81f
cleanup.
codemzs Jun 5, 2018
1865825
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs Jun 6, 2018
3562331
add more commands.
codemzs Jun 6, 2018
d20f1a4
cleanup.
codemzs Jun 6, 2018
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
Prev Previous commit
Next Next commit
PR feedback.
  • Loading branch information
codemzs committed May 30, 2018
commit 534fcd1a1e9d40b84ff473a0c017f1ab05b25002
2 changes: 1 addition & 1 deletion Microsoft.ML.sln
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.ML.Parquet", "Mic
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.Benchmarks", "test\Microsoft.ML.Benchmarks\Microsoft.ML.Benchmarks.csproj", "{7A9DB75F-2CA5-4184-9EF5-1F17EB39483F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.Commands", "src\Microsoft.ML.Commands\Microsoft.ML.Commands.csproj", "{C5EB2982-739C-4D42-8DA5-0FB5F4223B6D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.Maml", "src\Microsoft.ML.Commands\Microsoft.ML.Maml.csproj", "{C5EB2982-739C-4D42-8DA5-0FB5F4223B6D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
File renamed without changes.
10 changes: 0 additions & 10 deletions src/Microsoft.ML.Core/Data/ITransformModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ public interface ITransformModel
/// </summary>
ISchema OutputSchema { get; }

/// <summary>
/// This contains the transforms to save instantiated on an IDataView with
/// appropriate initial schema. Note that the "root" of this is typically either
/// an empty IDataView or a BinaryLoader with no rows. However, other root
/// types are possible, since we don't insist on this when loading a model
/// from a zip file. However, whenever we save, we force a BinaryLoader to
/// be serialized for the root.
/// </summary>
IDataView View { get; }

/// <summary>
/// Apply the transform(s) in the model to the given input data.
/// </summary>
Expand Down
12 changes: 0 additions & 12 deletions src/Microsoft.ML.Data/EntryPoints/PredictorModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ public PredictorModel(IHostEnvironment env, RoleMappedData trainingData, IDataVi
_predictor = predictor;
}

//REVIEW: I'm not sure this is the right thing to do because we are setting predictor to null
//when this class is supposed to contain a predictor. TransformModel may or may not
//contain a predictor. Here we are just using this class as a wrapper for TransformModel
//so that we can use a single class to accept TransformModel and PredictorModel has inputs.
public PredictorModel(IHostEnvironment env, ITransformModel transformModel)
{
Contracts.CheckValue(env, nameof(env));
env.CheckValue(transformModel, nameof(transformModel));

_transformModel = transformModel;
}

public PredictorModel(IHostEnvironment env, Stream stream)
{
Contracts.CheckValue(env, nameof(env));
Expand Down
10 changes: 0 additions & 10 deletions src/Microsoft.ML.Data/EntryPoints/TransformModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ public sealed class TransformModel : ITransformModel
/// </summary>
public ISchema OutputSchema => _chain.Schema;

/// <summary>
/// This contains the transforms to save instantiated on an IDataView with
/// appropriate initial schema. Note that the "root" of this is typically either
/// an empty IDataView or a BinaryLoader with no rows. However, other root
/// types are possible, since we don't insist on this when loading a model
/// from a zip file. However, whenever we save, we force a BinaryLoader to
/// be serialized for the root.
/// </summary>
public IDataView View => _chain;

/// <summary>
/// Create a TransformModel containing the transforms from "result" back to "input".
/// </summary>
Expand Down
51 changes: 16 additions & 35 deletions src/Microsoft.ML.Data/Model/Onnx/SaveOnnxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
[assembly: LoadableClass(SaveOnnxCommand.Summary, typeof(SaveOnnxCommand), typeof(SaveOnnxCommand.Arguments), typeof(SignatureCommand),
"Save ONNX", "SaveOnnx", DocName = "command/SaveOnnx.md")]

[assembly: LoadableClass(typeof(void), typeof(SaveOnnxCommand), null, typeof(SignatureEntryPointModule), "SaveOnnxCommand")]
[assembly: LoadableClass(typeof(void), typeof(SaveOnnxCommand), null, typeof(SignatureEntryPointModule), "SaveOnnx")]

namespace Microsoft.ML.Runtime.Model.Onnx
{
Expand All @@ -41,24 +41,24 @@ public sealed class Arguments : DataCommand.ArgumentsBase
[Argument(ArgumentType.AtMostOnce, HelpText = "The 'domain' property in the output ONNX.", NullName = "<Auto>", SortOrder = 4)]
public string Domain;

[Argument(ArgumentType.AtMostOnce, HelpText = "Comma delimited list of input column names to drop", ShortName = "idrop", SortOrder = 5)]
[Argument(ArgumentType.AtMostOnce, Visibility = ArgumentAttribute.VisibilityType.CmdLineOnly, HelpText = "Comma delimited list of input column names to drop", ShortName = "idrop", SortOrder = 5)]
public string InputsToDrop;
Copy link
Contributor

@TomFinley TomFinley May 30, 2018

Choose a reason for hiding this comment

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

Set visibility on this. #Closed


[Argument(ArgumentType.AtMostOnce, Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly, HelpText = "Array of input column names to drop", SortOrder = 6)]
[Argument(ArgumentType.AtMostOnce, Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly, HelpText = "Array of input column names to drop", Name = nameof(InputsToDrop), SortOrder = 6)]
public string[] InputsToDropArray;
Copy link
Contributor

@TomFinley TomFinley May 30, 2018

Choose a reason for hiding this comment

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

InputsToDropArray [](start = 28, length = 17)

Awkward name to use. Please use Name = nameof(InputsToDrop) instead. #Closed


[Argument(ArgumentType.AtMostOnce, HelpText = "Comma delimited list of output column names to drop", ShortName = "odrop", SortOrder = 7)]
[Argument(ArgumentType.AtMostOnce, Visibility = ArgumentAttribute.VisibilityType.CmdLineOnly, HelpText = "Comma delimited list of output column names to drop", ShortName = "odrop", SortOrder = 7)]
public string OutputsToDrop;

[Argument(ArgumentType.AtMostOnce, Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly, HelpText = "Array of output column names to drop", SortOrder = 8)]
[Argument(ArgumentType.AtMostOnce, Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly, HelpText = "Array of output column names to drop", Name = nameof(OutputsToDrop), SortOrder = 8)]
public string[] OutputsToDropArray;
Copy link
Contributor

@TomFinley TomFinley May 30, 2018

Choose a reason for hiding this comment

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

OutputsToDropArray [](start = 28, length = 18)

Same comments on inputs apply to outputs. #Closed


[Argument(ArgumentType.AtMostOnce, HelpText = "Whether we should attempt to load the predictor and attach the scorer to the pipeline if one is present.", ShortName = "pred", SortOrder = 9)]
[Argument(ArgumentType.AtMostOnce, Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly, HelpText = "Whether we should attempt to load the predictor and attach the scorer to the pipeline if one is present.", ShortName = "pred", SortOrder = 9)]
public bool? LoadPredictor;
Copy link
Contributor

@TomFinley TomFinley May 30, 2018

Choose a reason for hiding this comment

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

Is there a reason to have this in entry-point land? #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

Not if we are passing in ITransformModel that contains the predictor.


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


[Argument(ArgumentType.Required, HelpText = "Model that needs to be converted to ONNX format.", SortOrder = 10)]
[Argument(ArgumentType.Required, Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly, HelpText = "Model that needs to be converted to ONNX format.", SortOrder = 10)]

public IPredictorModel Model;
public ITransformModel Model;
Copy link
Contributor

@TomFinley TomFinley May 31, 2018

Choose a reason for hiding this comment

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

There appears to be a newline gap here. We tend to not put whitespace between the attribute and the field, as we see elsewhere here. #Closed

}

private readonly string _outputModelPath;
Expand All @@ -68,7 +68,7 @@ public sealed class Arguments : DataCommand.ArgumentsBase
private readonly bool? _loadPredictor;
private readonly HashSet<string> _inputsToDrop;
private readonly HashSet<string> _outputsToDrop;
private readonly IPredictorModel _model;
private readonly ITransformModel _model;

public SaveOnnxCommand(IHostEnvironment env, Arguments args)
: base(env, args, LoadName)
Expand All @@ -83,20 +83,12 @@ public SaveOnnxCommand(IHostEnvironment env, Arguments args)
_name = args.Name;

_loadPredictor = args.LoadPredictor;
_inputsToDrop = args.InputsToDropArray != null ? CreateDropMap(args.InputsToDropArray) : CreateDropMap(args.InputsToDrop);
_outputsToDrop = args.OutputsToDropArray != null ? CreateDropMap(args.OutputsToDropArray) : CreateDropMap(args.OutputsToDrop);
_inputsToDrop = CreateDropMap(args.InputsToDropArray ?? args.InputsToDrop?.Split(','));
_outputsToDrop = CreateDropMap(args.OutputsToDropArray ?? args.OutputsToDrop?.Split(','));
_domain = args.Domain;
_model = args.Model;
}

private static HashSet<string> CreateDropMap(string toDrop)
{
if (string.IsNullOrWhiteSpace(toDrop))
return new HashSet<string>();

return new HashSet<string>(toDrop.Split(','));
}

private static HashSet<string> CreateDropMap(string[] toDrop)
{
if (toDrop == null)
Expand Down Expand Up @@ -140,8 +132,8 @@ private void GetPipe(IChannel ch, IDataView end, out IDataView source, out IData

private void Run(IChannel ch)
{
IDataLoader loader = null; ;
IPredictor rawPred;
IDataLoader loader = null;
IPredictor rawPred = null;
IDataView view;
RoleMappedSchema trainSchema = null;

Expand All @@ -161,12 +153,7 @@ private void Run(IChannel ch)
view = loader;
}
else
{
view = _model.TransformModel.View;
rawPred = _model?.Predictor;
if (rawPred != null)
trainSchema = _model.GetTrainingSchema(Host);
}
view = _model.Apply(Host, new EmptyDataView(Host, _model.InputSchema));

// Get the transform chain.
IDataView source;
Expand Down Expand Up @@ -276,7 +263,6 @@ private void Run(IChannel ch)

public sealed class Output
{
//REVIEW: Would be nice to include ONNX protobuf model here but code generator needs an upgrade.
}

//REVIEW: Ideally there is no need to define this input class and just reuse the Argument class from SaveONNX command
Expand All @@ -302,12 +288,8 @@ public sealed class Input
[Argument(ArgumentType.AtMostOnce, HelpText = "Array of output column names to drop", SortOrder = 6)]
public string[] OutputsToDrop;

[Argument(ArgumentType.AtMostOnce, HelpText = "Whether we should attempt to load the predictor and attach the scorer to the pipeline if one is present.", ShortName = "pred", SortOrder = 7)]
public bool? LoadPredictor;

[Argument(ArgumentType.Required, HelpText = "Model that needs to be converted to ONNX format.", SortOrder = 8)]

public IPredictorModel Model;
[Argument(ArgumentType.Required, HelpText = "Model that needs to be converted to ONNX format.", SortOrder = 7)]
public ITransformModel Model;
}


Expand All @@ -321,7 +303,6 @@ public static Output Apply(IHostEnvironment env, Input input)
args.Domain = input.Domain;
args.InputsToDropArray = input.InputsToDrop;
args.OutputsToDropArray = input.OutputsToDrop;
args.LoadPredictor = input.LoadPredictor;
args.Model = input.Model;

var cmd = new SaveOnnxCommand(env, args);
Expand Down
21 changes: 21 additions & 0 deletions src/Microsoft.ML.Maml/Console.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Microsoft.ML.Runtime.Tools.Console
{
public static class Console
{
public static int Main(string[] args)
{
string all = string.Join(" ", args);
return Maml.MainAll(all);
}

public static unsafe int MainRaw(char* psz)
{
string args = new string(psz);
return Maml.MainAll(args);
}
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.ML.Maml/Microsoft.ML.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<IncludeInPackage>Microsoft.ML</IncludeInPackage>
<TargetFramework>netcoreapp2.0</TargetFramework>
<OutputType>Exe</OutputType>
<StartupObject>Microsoft.ML.Runtime.Tools.Maml</StartupObject>
<StartupObject>Microsoft.ML.Runtime.Tools.Console.Console</StartupObject>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.ML.Commands\Microsoft.ML.Commands.csproj" />
<ProjectReference Include="..\Microsoft.ML.Commands\Microsoft.ML.Maml.csproj" />
<ProjectReference Include="..\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.ML.Commands\Microsoft.ML.Commands.csproj" />
<ProjectReference Include="..\Microsoft.ML.Commands\Microsoft.ML.Maml.csproj" />
<ProjectReference Include="..\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
<ProjectReference Include="..\Microsoft.ML.CpuMath\Microsoft.ML.CpuMath.csproj" />
<ProjectReference Include="..\Microsoft.ML.Data\Microsoft.ML.Data.csproj" />
Expand Down
7 changes: 1 addition & 6 deletions src/Microsoft.ML/CSharpApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2789,15 +2789,10 @@ public sealed partial class OnnxConverter
/// </summary>
public string[] OutputsToDrop { get; set; }

/// <summary>
/// Whether we should attempt to load the predictor and attach the scorer to the pipeline if one is present.
/// </summary>
public bool? LoadPredictor { get; set; }

/// <summary>
/// Model that needs to be converted to ONNX format.
/// </summary>
public Var<Microsoft.ML.Runtime.EntryPoints.IPredictorModel> Model { get; set; } = new Var<Microsoft.ML.Runtime.EntryPoints.IPredictorModel>();
public Var<Microsoft.ML.Runtime.EntryPoints.ITransformModel> Model { get; set; } = new Var<Microsoft.ML.Runtime.EntryPoints.ITransformModel>();


public sealed class Output
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML/Microsoft.ML.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<ItemGroup>
<ProjectReference Include="..\Microsoft.ML.Api\Microsoft.ML.Api.csproj" />
<ProjectReference Include="..\Microsoft.ML.Commands\Microsoft.ML.Commands.csproj" />
<ProjectReference Include="..\Microsoft.ML.Commands\Microsoft.ML.Maml.csproj" />
<ProjectReference Include="..\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
<ProjectReference Include="..\Microsoft.ML.Data\Microsoft.ML.Data.csproj" />
<ProjectReference Include="..\Microsoft.ML.Transforms\Microsoft.ML.Transforms.csproj" />
Expand Down
9 changes: 6 additions & 3 deletions src/Microsoft.ML/Models/OnnxConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Microsoft.ML.Runtime;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Runtime.EntryPoints;

namespace Microsoft.ML.Models
{
Expand All @@ -19,7 +22,7 @@ public void Convert(PredictionModel model)
Experiment experiment = environment.CreateExperiment();
experiment.Add(this);
experiment.Compile();
experiment.SetInput(Model, new PredictorModel(environment, model.PredictorModel));
experiment.SetInput(Model, model.PredictorModel);
experiment.Run();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.ML.Api\Microsoft.ML.Api.csproj" />
<ProjectReference Include="..\..\src\Microsoft.ML.Commands\Microsoft.ML.Maml.csproj" />
<ProjectReference Include="..\..\src\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
<ProjectReference Include="..\..\src\Microsoft.ML.Data\Microsoft.ML.Data.csproj" />
<ProjectReference Include="..\..\src\Microsoft.ML.FastTree\Microsoft.ML.FastTree.csproj" />
<ProjectReference Include="..\..\src\Microsoft.ML.Maml\Microsoft.ML.Console.csproj" />
<ProjectReference Include="..\..\src\Microsoft.ML.ResultProcessor\Microsoft.ML.ResultProcessor.csproj" />
<ProjectReference Include="..\..\src\Microsoft.ML\Microsoft.ML.csproj" />
</ItemGroup>
Expand Down