Skip to content
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

Load a model by path #2988

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 8 additions & 0 deletions src/Microsoft.ML.Data/DataLoadSave/TransformerChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,13 @@ public static TransformerChain<ITransformer> LoadFrom(IHostEnvironment env, Stre
}
}
}

public static TransformerChain<ITransformer> LoadFromPath(IHostEnvironment env, string modelPath)
{
using (var stream = File.OpenRead(modelPath))
{
return LoadFrom(env, stream);
}
}
}
}
7 changes: 7 additions & 0 deletions src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ internal ModelOperationsCatalog(IHostEnvironment env)
/// <returns>The loaded model.</returns>
public ITransformer Load(Stream stream) => TransformerChain.LoadFrom(_env, stream);

/// <summary>
/// Load the model from a filePath of the model.
/// </summary>
/// <param name="modelPath">The path of the model to load./></param>
/// <returns>The loaded model.</returns>
public ITransformer Load(string modelPath) => TransformerChain.LoadFromPath(_env, modelPath);

/// <summary>
/// The catalog of model explainability operations.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just did this as a quick test. Should we add this as a separate test and include the option within the cookbook?

}

[Fact]
Expand Down