diff --git a/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs b/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs
index dc4cb551d0..c4fa3d71a2 100644
--- a/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs
+++ b/src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs
@@ -182,12 +182,25 @@ public ITransformer Load(Stream stream, out DataViewSchema inputSchema)
}
///
- /// Load the model and its input schema from the stream.
+ /// Load the model and its input schema from the file.
+ ///
+ /// Path to model.
+ /// Will contain the input schema for the model. If the model was saved using older APIs
+ /// it may not contain an input schema, in this case will be null.
+ /// The loaded model.
+ public ITransformer Load(string modelPath, out DataViewSchema inputSchema)
+ {
+ using (var stream = File.OpenRead(modelPath))
+ return Load(stream, out inputSchema);
+ }
+
+ ///
+ /// Load the model from the stream.
///
/// A readable, seekable stream to load from.
/// A model of type containing the loader
/// and the transformer chain.
- public IDataLoader Load(Stream stream)
+ public IDataLoader LoadDataLoader(Stream stream)
{
_env.CheckValue(stream, nameof(stream));
@@ -205,6 +218,18 @@ public IDataLoader Load(Stream stream)
}
}
+ ///
+ /// Load the model from the file.
+ ///
+ /// Path to model.
+ /// A model of type containing the loader
+ /// and the transformer chain.
+ public IDataLoader LoadDataLoader(string modelPath)
+ {
+ using (var stream = File.OpenRead(modelPath))
+ return LoadDataLoader(stream);
+ }
+
///
/// Load a transformer model and a data loader model from the stream.
///
@@ -215,7 +240,7 @@ public ITransformer LoadWithDataLoader(Stream stream, out IDataLoader composite)
{
loader = composite.Loader;
@@ -224,6 +249,18 @@ public ITransformer LoadWithDataLoader(Stream stream, out IDataLoader();
}
+ ///
+ /// Load a transformer model and a data loader model from the file.
+ ///
+ /// Path to model.
+ /// The data loader from the model stream.
+ /// The transformer model from the model stream.
+ public ITransformer LoadWithDataLoader(string modelPath, out IDataLoader loader)
+ {
+ using (var stream = File.OpenRead(modelPath))
+ return LoadWithDataLoader(stream, out loader);
+ }
+
///
/// Create a prediction engine for one-time prediction.
///
diff --git a/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs b/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs
index 0a36b2af62..656ae47840 100644
--- a/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs
+++ b/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs
@@ -5643,10 +5643,7 @@ public void LoadEntryPointModel()
{
var modelPath = GetDataPath($"backcompat/ep_model{i}.zip");
ITransformer loadedModel;
- using (var stream = File.OpenRead(modelPath))
- {
- loadedModel = ml.Model.Load(stream, out var inputSchema);
- }
+ loadedModel = ml.Model.Load(modelPath, out var inputSchema);
}
}
}
diff --git a/test/Microsoft.ML.Functional.Tests/ModelLoading.cs b/test/Microsoft.ML.Functional.Tests/ModelLoading.cs
index 193ddedad5..25d78c2236 100644
--- a/test/Microsoft.ML.Functional.Tests/ModelLoading.cs
+++ b/test/Microsoft.ML.Functional.Tests/ModelLoading.cs
@@ -68,24 +68,19 @@ public void LoadModelAndExtractPredictor()
ITransformer loadedTransformerModel;
IDataLoader loadedCompositeLoader;
ITransformer loadedTransformerModel1;
- using (var fs = File.OpenRead(modelAndSchemaPath))
- loadedTransformerModel = _ml.Model.Load(fs, out var loadedSchema);
- using (var fs = File.OpenRead(compositeLoaderModelPath))
- {
- // This model can be loaded either as a composite data loader,
- // a transformer model + an input schema, or a transformer model + a data loader.
- var t = _ml.Model.LoadWithDataLoader(fs, out IDataLoader l);
- var t1 = _ml.Model.Load(fs, out var s);
- loadedCompositeLoader = _ml.Model.Load(fs);
- }
- using (var fs = File.OpenRead(loaderAndTransformerModelPath))
- {
- // This model can be loaded either as a composite data loader,
- // a transformer model + an input schema, or a transformer model + a data loader.
- var t = _ml.Model.Load(fs, out var s);
- var c = _ml.Model.Load(fs);
- loadedTransformerModel1 = _ml.Model.LoadWithDataLoader(fs, out IDataLoader l);
- }
+ loadedTransformerModel = _ml.Model.Load(modelAndSchemaPath, out var loadedSchema);
+
+ // This model can be loaded either as a composite data loader,
+ // a transformer model + an input schema, or a transformer model + a data loader.
+ var t = _ml.Model.LoadWithDataLoader(compositeLoaderModelPath, out IDataLoader l);
+ var t1 = _ml.Model.Load(compositeLoaderModelPath, out var s);
+ loadedCompositeLoader = _ml.Model.LoadDataLoader(compositeLoaderModelPath);
+
+ // This model can be loaded either as a composite data loader,
+ // a transformer model + an input schema, or a transformer model + a data loader.
+ var tt = _ml.Model.Load(loaderAndTransformerModelPath, out var ss);
+ var c = _ml.Model.LoadDataLoader(loaderAndTransformerModelPath);
+ loadedTransformerModel1 = _ml.Model.LoadWithDataLoader(loaderAndTransformerModelPath, out IDataLoader ll);
var gam = ((loadedTransformerModel as ISingleFeaturePredictionTransformer