Skip to content

Load TransformerChain model file in ModelFileUtils #4110

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 3 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
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 src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.ML
/// </summary>
public sealed class ModelOperationsCatalog : IInternalCatalog
{
private const string SchemaEntryName = "Schema";
internal const string SchemaEntryName = "Schema";

IHostEnvironment IInternalCatalog.Environment => _env;
private readonly IHostEnvironment _env;
Expand Down
11 changes: 11 additions & 0 deletions src/Microsoft.ML.Data/Utilities/ModelFileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal static class ModelFileUtils
{
public const string DirPredictor = "Predictor";
public const string DirDataLoaderModel = "DataLoaderModel";
public const string DirTransformerChain = TransformerChain.LoaderSignature;
public const string SchemaEntryName = ModelOperationsCatalog.SchemaEntryName;
// ResultsProcessor needs access to this constant.
public const string DirTrainingInfo = "TrainingInfo";

Expand Down Expand Up @@ -64,6 +66,15 @@ public static IDataView LoadPipeline(IHostEnvironment env, RepositoryReader rep,
Contracts.CheckValue(env, nameof(env));
env.CheckValue(rep, nameof(rep));
env.CheckValue(files, nameof(files));

var entry = rep.OpenEntryOrNull(SchemaEntryName);
if (entry != null)
{
var loader = new BinaryLoader(env, new BinaryLoader.Arguments(), entry.Stream);
ModelLoadContext.LoadModel<ITransformer, SignatureLoadModel>(env, out var transformerChain, rep, DirTransformerChain);
return transformerChain.Transform(loader);
}

using (var ent = rep.OpenEntry(DirDataLoaderModel, ModelLoadContext.ModelStreamName))
{
ILegacyDataLoader loader;
Expand Down
94 changes: 94 additions & 0 deletions test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,100 @@ public void EntryPointExecGraphCommand()
cmd.Run();
}

[Fact]
public void ScoreTransformerChainModel()
{
var dataPath = GetDataPath("wikipedia-detox-250-line-data.tsv");
var modelPath = DeleteOutputPath("model.zip");
var outputDataPath = DeleteOutputPath("scored.idv");

var mlContext = new MLContext();

var data = new TextLoader(mlContext,
new TextLoader.Options()
{
AllowQuoting = true,
Separator = "\t",
HasHeader = true,
Columns = new[]
{
new TextLoader.Column("Label", DataKind.Boolean, 0),
new TextLoader.Column("SentimentText", DataKind.String, 1)
}
}).Load(dataPath);

var pipeline = mlContext.Transforms.Text.FeaturizeText("Features", "SentimentText")
.Append(mlContext.BinaryClassification.Trainers.AveragedPerceptron());

var model = pipeline.Fit(data);

mlContext.Model.Save(model, data.Schema, modelPath);

string inputGraph = string.Format(@"
{{
'Inputs': {{
'file': '{0}',
'transform_model': '{1}'
}},
'Nodes': [
{{
'Name': 'Data.CustomTextLoader',
'Inputs': {{
'CustomSchema': 'col=Sentiment:I8:0 col=SentimentText:TX:1 quote+ header=+ sep=tab',
'InputFile': '$file'
}},
'Outputs': {{
'Data': '$data'
}}
}},
{{
'Name': 'Transforms.DatasetTransformScorer',
'Inputs': {{
'Data': '$data',
'TransformModel': '$transform_model'
}},
'Outputs': {{
'ScoredData': '$scoredVectorData'
}}
}},
{{
'Inputs': {{
'Data': '$scoredVectorData'
}},
'Name': 'Transforms.ScoreColumnSelector',
'Outputs': {{
'OutputData': '$scoreColumnsOnlyData'
}}
}},
{{
'Inputs': {{
'Data': '$scoreColumnsOnlyData',
'PredictedLabelColumn': 'PredictedLabel'
}},
'Name': 'Transforms.PredictedLabelColumnOriginalValueConverter',
'Outputs': {{
'OutputData': '$output_data'
}}
}}
],
'Outputs': {{
'output_data': '{2}'
}}
}}", EscapePath(dataPath), EscapePath(modelPath), EscapePath(outputDataPath));

var jsonPath = DeleteOutputPath("graph.json");
File.WriteAllLines(jsonPath, new[] { inputGraph });

var args = new ExecuteGraphCommand.Arguments() { GraphPath = jsonPath };
var cmd = new ExecuteGraphCommand(Env, args);
cmd.Run();

var loadedData = mlContext.Data.LoadFromBinary(outputDataPath);

Assert.NotNull(loadedData.Schema.GetColumnOrNull("PredictedLabel"));
Assert.NotNull(loadedData.Schema.GetColumnOrNull("Score"));
}

//[Fact]
//public void EntryPointArrayOfVariables()
//{
Expand Down