From fd5877acb4d961de6dfa99bc8e195cfe449888d8 Mon Sep 17 00:00:00 2001 From: J W Date: Fri, 15 Mar 2019 19:47:07 -0400 Subject: [PATCH 1/2] Initial add of loading model by path --- src/Microsoft.ML.Data/DataLoadSave/TransformerChain.cs | 8 ++++++++ src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs | 2 ++ .../Api/CookbookSamples/CookbookSamplesDynamicApi.cs | 4 +--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.ML.Data/DataLoadSave/TransformerChain.cs b/src/Microsoft.ML.Data/DataLoadSave/TransformerChain.cs index a20bc5113c..8402cf643a 100644 --- a/src/Microsoft.ML.Data/DataLoadSave/TransformerChain.cs +++ b/src/Microsoft.ML.Data/DataLoadSave/TransformerChain.cs @@ -300,5 +300,13 @@ public static TransformerChain LoadFrom(IHostEnvironment env, Stre } } } + + public static TransformerChain LoadFromPath(IHostEnvironment env, string modelPath) + { + using (var stream = File.OpenRead(modelPath)) + { + return LoadFrom(env, stream); + } + } } } diff --git a/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs b/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs index 0443240e1d..3ee384154a 100644 --- a/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs +++ b/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs @@ -40,6 +40,8 @@ internal ModelOperationsCatalog(IHostEnvironment env) /// The loaded model. public ITransformer Load(Stream stream) => TransformerChain.LoadFrom(_env, stream); + public ITransformer Load(string modelPath) => TransformerChain.LoadFromPath(_env, modelPath); + /// /// The catalog of model explainability operations. /// diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/CookbookSamples/CookbookSamplesDynamicApi.cs b/test/Microsoft.ML.Tests/Scenarios/Api/CookbookSamples/CookbookSamplesDynamicApi.cs index cebf20503e..413c54f83d 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/CookbookSamples/CookbookSamplesDynamicApi.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/CookbookSamples/CookbookSamplesDynamicApi.cs @@ -130,9 +130,7 @@ private void TrainRegression(string trainDataPath, string testDataPath, string m // Potentially, the lines below can be in a different process altogether. // When you load the model, it's a 'dynamic' transformer. - ITransformer loadedModel; - using (var stream = File.OpenRead(modelPath)) - loadedModel = mlContext.Model.Load(stream); + ITransformer loadedModel = mlContext.Model.Load(modelPath); } [Fact] From b2da9bd6e74ce06c9d24c6d31871989545fdd887 Mon Sep 17 00:00:00 2001 From: J W Date: Fri, 15 Mar 2019 19:52:10 -0400 Subject: [PATCH 2/2] Add XML doc --- src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs b/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs index 3ee384154a..8e9bd0df28 100644 --- a/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs +++ b/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs @@ -40,6 +40,11 @@ internal ModelOperationsCatalog(IHostEnvironment env) /// The loaded model. public ITransformer Load(Stream stream) => TransformerChain.LoadFrom(_env, stream); + /// + /// Load the model from a filePath of the model. + /// + /// The path of the model to load./> + /// The loaded model. public ITransformer Load(string modelPath) => TransformerChain.LoadFromPath(_env, modelPath); ///