From 73da7d2511640e933e73effe8a99bbad27d125a0 Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Tue, 27 Apr 2021 19:12:38 -0700 Subject: [PATCH 1/9] fixed onnx temp model deleting --- .../OnnxTransform.cs | 2 +- src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs | 29 +++++++++---------- .../OnnxTransformTests.cs | 11 ++++--- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs b/src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs index df94857931..007dfc40af 100644 --- a/src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs +++ b/src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs @@ -304,7 +304,7 @@ private protected override void SaveModel(ModelSaveContext ctx) ctx.CheckAtModel(); ctx.SetVersionInfo(GetVersionInfo()); - ctx.SaveBinaryStream("OnnxModel", w => { w.WriteByteArray(File.ReadAllBytes(Model.ModelFile)); }); + ctx.SaveBinaryStream("OnnxModel", w => { w.WriteByteArray(File.ReadAllBytes(Model.ModelStream.Name)); }); Host.CheckNonEmpty(Inputs, nameof(Inputs)); ctx.Writer.Write(Inputs.Length); diff --git a/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs b/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs index cdba65a4b1..96bdf76741 100644 --- a/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs +++ b/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs @@ -143,14 +143,9 @@ public OnnxVariableInfo(string name, OnnxShape shape, Type typeInOnnxRuntime, Da /// private readonly InferenceSession _session; /// - /// Indicates if is a temporal file created by - /// or . If , should delete . - /// - private bool _ownModelFile; - /// /// The location where the used ONNX model loaded from. /// - internal string ModelFile { get; } + internal FileStream ModelStream { get; } /// /// The ONNX model's information from ONNXRuntime's perspective. ML.NET can change the input and output of that model in some ways. /// For example, ML.NET can shuffle the inputs so that the i-th ONNX input becomes the j-th input column of . @@ -172,9 +167,7 @@ public OnnxVariableInfo(string name, OnnxShape shape, Type typeInOnnxRuntime, Da public OnnxModel(string modelFile, int? gpuDeviceId = null, bool fallbackToCpu = false, bool ownModelFile=false, IDictionary shapeDictionary = null) { - ModelFile = modelFile; // If we don't own the model file, _disposed should be false to prevent deleting user's file. - _ownModelFile = ownModelFile; _disposed = false; if (gpuDeviceId != null) @@ -202,9 +195,15 @@ public OnnxModel(string modelFile, int? gpuDeviceId = null, bool fallbackToCpu = { // Load ONNX model file and parse its input and output schema. The reason of doing so is that ONNXRuntime // doesn't expose full type information via its C# APIs. - ModelFile = modelFile; var model = new OnnxCSharpToProtoWrapper.ModelProto(); - using (var modelStream = File.OpenRead(modelFile)) + // If we own the model file set the DeleteOnClose flag so it is always deleted. + if (ownModelFile) + ModelStream = new FileStream(modelFile, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.DeleteOnClose); + else + ModelStream = new FileStream(modelFile, FileMode.Open, FileAccess.Read); + + // The CodedInputStream auto closes the stream, and we need to make sure that our main stream stays open, so creating a new one here. + using (var modelStream = new FileStream(modelFile, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.Read)) using (var codedStream = Google.Protobuf.CodedInputStream.CreateWithLimits(modelStream, Int32.MaxValue, 10)) model = OnnxCSharpToProtoWrapper.ModelProto.Parser.ParseFrom(codedStream); @@ -366,7 +365,7 @@ public IDisposableReadOnlyCollection Run(List - /// Flag used to indicate if the unmanaged resources (aka the model file + /// Flag used to indicate if the unmanaged resources (aka the model file handle /// and ) have been deleted. /// private bool _disposed; @@ -378,8 +377,7 @@ public void Dispose() } /// - /// There are two unmanaged resources we can dispose, and - /// if is . + /// There are two unmanaged resources we can dispose, and /// /// private void Dispose(bool disposing) @@ -391,9 +389,8 @@ private void Dispose(bool disposing) { // First, we release the resource token by ONNXRuntime. _session.Dispose(); - // Second, we delete the model file if that file is not created by the user. - if (_ownModelFile && File.Exists(ModelFile)) - File.Delete(ModelFile); + // Second, Dispose of the model file stream. + ModelStream.Dispose(); } _disposed = true; } diff --git a/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs b/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs index 2e3f95693b..72966cbb3b 100644 --- a/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs +++ b/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs @@ -684,13 +684,12 @@ public void TestOnnxModelDisposal() var onnxModel = OnnxModel.CreateFromBytes(modelInBytes); // Check if a temporal file is crated for storing the byte[]. - Assert.True(File.Exists(onnxModel.ModelFile)); + Assert.True(File.Exists(onnxModel.ModelStream.Name)); // Delete the temporal file. onnxModel.Dispose(); - // Make sure the temporal file is deleted. - Assert.False(File.Exists(onnxModel.ModelFile)); + Assert.False(File.Exists(onnxModel.ModelStream.Name)); } [OnnxFact] @@ -703,13 +702,13 @@ public void TestOnnxModelNotDisposal() var onnxModel = new OnnxModel(modelFile); // Check if a temporal file is crated for storing the byte[]. - Assert.True(File.Exists(onnxModel.ModelFile)); + Assert.True(File.Exists(onnxModel.ModelStream.Name)); // Don't delete the temporal file! onnxModel.Dispose(); // Make sure the temporal file still exists. - Assert.True(File.Exists(onnxModel.ModelFile)); + Assert.True(File.Exists(onnxModel.ModelStream.Name)); } private class OnnxMapInput @@ -859,7 +858,7 @@ private void TryModelWithCustomShapesHelper(IDictionary shapeDict } }); - // Define a ONNX transform, trains it, and apply it to the input data. + // Define a ONNX transform, trains it, and apply it to the input data. var pipeline = ML.Transforms.ApplyOnnxModel(new[] { "outa", "outb" }, new[] { "ina", "inb" }, modelFile, shapeDictionary, gpuDeviceId: _gpuDeviceId, fallbackToCpu: _fallbackToCpu); } From 9c553d19a6e54a68e9de63debd866fb0aa68a090 Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Mon, 3 May 2021 09:51:18 -0700 Subject: [PATCH 2/9] random file path fixed --- src/Microsoft.ML.AutoML/API/ExperimentSettings.cs | 2 +- src/Microsoft.ML.Data/Commands/TrainTestCommand.cs | 2 +- src/Microsoft.ML.Data/MLContext.cs | 7 +++++++ src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs | 2 +- src/Microsoft.ML.TensorFlow/TensorflowTransform.cs | 2 +- src/Microsoft.ML.TensorFlow/TensorflowUtils.cs | 2 +- src/Microsoft.ML.Vision/DnnRetrainTransform.cs | 2 +- src/Microsoft.ML.Vision/ImageClassificationTrainer.cs | 2 +- .../Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs | 4 ++-- test/Microsoft.ML.PerformanceTests/FeaturizeTextBench.cs | 3 +-- test/Microsoft.ML.PerformanceTests/TextLoaderBench.cs | 3 +-- test/Microsoft.ML.TestFramework/RemoteExecutor.cs | 2 +- test/Microsoft.ML.Tests/ImagesTests.cs | 2 +- test/Microsoft.ML.Tests/OnnxConversionTest.cs | 6 +++--- .../ScenariosWithDirectInstantiation/TensorflowTests.cs | 6 +++--- .../TrainerEstimators/TreeEnsembleFeaturizerTest.cs | 4 ++-- 16 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs b/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs index 21d08eca30..ca5eba5862 100644 --- a/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs +++ b/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs @@ -66,7 +66,7 @@ public ExperimentSettings() { MaxExperimentTimeInSeconds = 24 * 60 * 60; CancellationToken = default; - CacheDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "Microsoft.ML.AutoML")); + CacheDirectory = new DirectoryInfo(Path.Combine(MLContext.TempFilePath, "Microsoft.ML.AutoML")); CacheBeforeTrainer = CacheBeforeTrainer.Auto; MaxModels = int.MaxValue; } diff --git a/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs b/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs index 58907622cb..66c6e74326 100644 --- a/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs +++ b/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs @@ -188,7 +188,7 @@ private void RunCore(IChannel ch, string cmd) ILegacyDataLoader testPipe; bool hasOutfile = !string.IsNullOrEmpty(ImplOptions.OutputModelFile); - var tempFilePath = hasOutfile ? null : Path.GetTempFileName(); + var tempFilePath = hasOutfile ? null : Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); using (var file = new SimpleFileHandle(ch, hasOutfile ? ImplOptions.OutputModelFile : tempFilePath, true, !hasOutfile)) { diff --git a/src/Microsoft.ML.Data/MLContext.cs b/src/Microsoft.ML.Data/MLContext.cs index ccb708addc..908c95a59e 100644 --- a/src/Microsoft.ML.Data/MLContext.cs +++ b/src/Microsoft.ML.Data/MLContext.cs @@ -79,6 +79,13 @@ public sealed class MLContext : ISeededEnvironment /// public ComponentCatalog ComponentCatalog => _env.ComponentCatalog; + private static string _tempFilePath = System.IO.Path.GetTempPath(); + public static string TempFilePath + { + get { return _tempFilePath; } + set { _tempFilePath = value; } + } + /// /// Create the ML context. /// diff --git a/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs b/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs index 96bdf76741..7132e3651a 100644 --- a/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs +++ b/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs @@ -347,7 +347,7 @@ public static OnnxModel CreateFromBytes(byte[] modelBytes) public static OnnxModel CreateFromBytes(byte[] modelBytes, int? gpuDeviceId = null, bool fallbackToCpu = false, IDictionary shapeDictionary = null) { - var tempModelFile = Path.GetTempFileName(); + var tempModelFile = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); File.WriteAllBytes(tempModelFile, modelBytes); return new OnnxModel(tempModelFile, gpuDeviceId, fallbackToCpu, ownModelFile: true, shapeDictionary: shapeDictionary); diff --git a/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs b/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs index 1537ab10cf..7cd39e8039 100644 --- a/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs +++ b/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs @@ -143,7 +143,7 @@ private static TensorFlowTransformer Create(IHostEnvironment env, ModelLoadConte return new TensorFlowTransformer(env, LoadTFSession(env, modelBytes), outputs, inputs, null, false, addBatchDimensionInput, treatOutputAsBatched: treatOutputAsBatched); } - var tempDirPath = Path.GetFullPath(Path.Combine(Path.GetTempPath(), nameof(TensorFlowTransformer) + "_" + Guid.NewGuid())); + var tempDirPath = Path.GetFullPath(Path.Combine(MLContext.TempFilePath, nameof(TensorFlowTransformer) + "_" + Guid.NewGuid())); CreateFolderWithAclIfNotExists(env, tempDirPath); try { diff --git a/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs b/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs index 8fbbd772a0..4d587d05be 100644 --- a/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs +++ b/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs @@ -632,7 +632,7 @@ public void Dispose() internal static string GetTemporaryDirectory() { - string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + string tempDirectory = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); Directory.CreateDirectory(tempDirectory); return tempDirectory; } diff --git a/src/Microsoft.ML.Vision/DnnRetrainTransform.cs b/src/Microsoft.ML.Vision/DnnRetrainTransform.cs index 240b2773f7..57a0705e55 100644 --- a/src/Microsoft.ML.Vision/DnnRetrainTransform.cs +++ b/src/Microsoft.ML.Vision/DnnRetrainTransform.cs @@ -116,7 +116,7 @@ private static DnnRetrainTransformer Create(IHostEnvironment env, ModelLoadConte null, false, addBatchDimensionInput, 1); } - var tempDirPath = Path.GetFullPath(Path.Combine(Path.GetTempPath(), nameof(DnnRetrainTransformer) + "_" + Guid.NewGuid())); + var tempDirPath = Path.GetFullPath(Path.Combine(MLContext.TempFilePath, nameof(DnnRetrainTransformer) + "_" + Guid.NewGuid())); CreateFolderWithAclIfNotExists(env, tempDirPath); try { diff --git a/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs b/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs index e5fe486ae5..286616ff39 100644 --- a/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs +++ b/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs @@ -487,7 +487,7 @@ public sealed class Options : TrainerInputBaseWithLabel private readonly bool _cleanupWorkspace; private int _classCount; private Graph Graph => _session.graph; - private static readonly string _resourcePath = Path.Combine(Path.GetTempPath(), "MLNET"); + private static readonly string _resourcePath = Path.Combine(MLContext.TempFilePath, "MLNET"); private readonly string _sizeFile; /// diff --git a/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs b/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs index 72966cbb3b..5f660164ef 100644 --- a/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs +++ b/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs @@ -244,7 +244,7 @@ public void OnnxWorkout() var result = model.Transform(data); // save and reload the model - var tempPath = Path.GetTempFileName(); + var tempPath = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); ML.Model.Save(model, data.Schema, tempPath); var loadedModel = ML.Model.Load(tempPath, out DataViewSchema modelSchema); (loadedModel as IDisposable)?.Dispose(); @@ -966,7 +966,7 @@ public void TestOnnxTransformSaveAndLoadWithCustomShapes() // Save the trained ONNX transformer into file and then load it back. ITransformer loadedModel = null; - var tempPath = Path.GetTempFileName(); + var tempPath = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); using (var file = new SimpleFileHandle(Env, tempPath, true, true)) { // Save. diff --git a/test/Microsoft.ML.PerformanceTests/FeaturizeTextBench.cs b/test/Microsoft.ML.PerformanceTests/FeaturizeTextBench.cs index fdb7475a8c..1689ef0e45 100644 --- a/test/Microsoft.ML.PerformanceTests/FeaturizeTextBench.cs +++ b/test/Microsoft.ML.PerformanceTests/FeaturizeTextBench.cs @@ -25,9 +25,8 @@ public class FeaturizeTextBench : BenchmarkBase [GlobalSetup] public void SetupData() { - Path.GetTempFileName(); _mlContext = new MLContext(seed: 1); - var path = Path.GetTempFileName(); + var path = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); Console.WriteLine($"Created dataset in temporary file:\n{path}\n"); path = RandomFile.CreateRandomFile(path, _numRows, _numColumns, _maxWordLength); diff --git a/test/Microsoft.ML.PerformanceTests/TextLoaderBench.cs b/test/Microsoft.ML.PerformanceTests/TextLoaderBench.cs index 73abceed60..9d8a2654c8 100644 --- a/test/Microsoft.ML.PerformanceTests/TextLoaderBench.cs +++ b/test/Microsoft.ML.PerformanceTests/TextLoaderBench.cs @@ -28,9 +28,8 @@ public class TextLoaderBench : BenchmarkBase [GlobalSetup] public void SetupData() { - Path.GetTempFileName(); _mlContext = new MLContext(seed: 1); - var path = Path.GetTempFileName(); + var path = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); Console.WriteLine($"Created dataset in temporary file:\n{path}\n"); path = RandomFile.CreateRandomFile(path, _numRows, _numColumns, _maxWordLength); diff --git a/test/Microsoft.ML.TestFramework/RemoteExecutor.cs b/test/Microsoft.ML.TestFramework/RemoteExecutor.cs index 39775b4be1..20cb5eba76 100644 --- a/test/Microsoft.ML.TestFramework/RemoteExecutor.cs +++ b/test/Microsoft.ML.TestFramework/RemoteExecutor.cs @@ -199,6 +199,6 @@ public RemoteInvokeOptions(Dictionary environmentVariables = nul public bool CheckExitCode { get; set; } = true; public int TimeOut { get; set; } = RemoteExecutor.FailWaitTimeoutMilliseconds; public int ExpectedExitCode { get; set; } = RemoteExecutor.SuccessExitCode; - public string ExceptionFile { get; } = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + public string ExceptionFile { get; } = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); } } diff --git a/test/Microsoft.ML.Tests/ImagesTests.cs b/test/Microsoft.ML.Tests/ImagesTests.cs index 729f81dccd..c866f95915 100644 --- a/test/Microsoft.ML.Tests/ImagesTests.cs +++ b/test/Microsoft.ML.Tests/ImagesTests.cs @@ -77,7 +77,7 @@ public void TestEstimatorSaveLoad() pipe.GetOutputSchema(SchemaShape.Create(data.Schema)); var model = pipe.Fit(data); - var tempPath = Path.GetTempFileName(); + var tempPath = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); using (var file = new SimpleFileHandle(env, tempPath, true, true)) { using (var fs = file.CreateWriteStream()) diff --git a/test/Microsoft.ML.Tests/OnnxConversionTest.cs b/test/Microsoft.ML.Tests/OnnxConversionTest.cs index 5e97a970a6..58ae97eae6 100644 --- a/test/Microsoft.ML.Tests/OnnxConversionTest.cs +++ b/test/Microsoft.ML.Tests/OnnxConversionTest.cs @@ -637,9 +637,9 @@ public void MulticlassLogisticRegressionOnnxConversionTest() public void LoadingPredictorModelAndOnnxConversionTest() { string dataPath = GetDataPath("iris.txt"); - string modelPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".model.bin"; - string onnxPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".model.onnx"; - string onnxJsonPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".model.onnx.json"; + string modelPath = MLContext.TempFilePath + Guid.NewGuid().ToString() + ".model.bin"; + string onnxPath = MLContext.TempFilePath + Guid.NewGuid().ToString() + ".model.onnx"; + string onnxJsonPath = MLContext.TempFilePath + Guid.NewGuid().ToString() + ".model.onnx.json"; string inputGraph = string.Format(@" {{ diff --git a/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs b/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs index 3508cfe481..62c5158a06 100644 --- a/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs +++ b/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs @@ -34,7 +34,7 @@ internal sealed class TensorFlowScenariosTestsFixture : IDisposable public static string assetsPath; internal static void CreateParentWorkspacePathForImageClassification() { - tempFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + tempFolder = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); assetsPath = Path.Combine(tempFolder, "assets"); parentWorkspacePath = Path.Combine(assetsPath, "cached"); // Delete if the workspace path already exists @@ -1678,7 +1678,7 @@ internal void TensorFlowImageClassificationWithLRScheduling(LearningRateSchedule Assert.True(File.Exists(Path.Combine(options.WorkspacePath, options.TrainSetBottleneckCachedValuesFileName))); Assert.True(File.Exists(Path.Combine(options.WorkspacePath, options.ValidationSetBottleneckCachedValuesFileName))); - Assert.True(File.Exists(Path.Combine(Path.GetTempPath(), "MLNET", ImageClassificationTrainer.ModelFileName[options.Arch]))); + Assert.True(File.Exists(Path.Combine(MLContext.TempFilePath, "MLNET", ImageClassificationTrainer.ModelFileName[options.Arch]))); (loadedModel as IDisposable)?.Dispose(); } @@ -1961,7 +1961,7 @@ public class ImagePrediction private static string GetTemporaryDirectory() { - string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + string tempDirectory = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); Directory.CreateDirectory(tempDirectory); return tempDirectory; } diff --git a/test/Microsoft.ML.Tests/TrainerEstimators/TreeEnsembleFeaturizerTest.cs b/test/Microsoft.ML.Tests/TrainerEstimators/TreeEnsembleFeaturizerTest.cs index 68dee8a5b9..2d62b8feb4 100644 --- a/test/Microsoft.ML.Tests/TrainerEstimators/TreeEnsembleFeaturizerTest.cs +++ b/test/Microsoft.ML.Tests/TrainerEstimators/TreeEnsembleFeaturizerTest.cs @@ -611,7 +611,7 @@ public void TestSaveAndLoadTreeFeaturizer() // Save the trained model into file. ITransformer loadedModel = null; - var tempPath = Path.GetTempFileName(); + var tempPath = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); using (var file = new SimpleFileHandle(Env, tempPath, true, true)) { using (var fs = file.CreateWriteStream()) @@ -668,7 +668,7 @@ public void TestSaveAndLoadDoubleTreeFeaturizer() // Save the trained model into file and then load it back. ITransformer loadedModel = null; - var tempPath = Path.GetTempFileName(); + var tempPath = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); using (var file = new SimpleFileHandle(Env, tempPath, true, true)) { using (var fs = file.CreateWriteStream()) From 62521ae942da36add6ebf324039136caf0889a2c Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Tue, 4 May 2021 14:46:26 -0700 Subject: [PATCH 3/9] updates from pr --- .../API/ExperimentSettings.cs | 2 +- src/Microsoft.ML.AutoML/Experiment/Experiment.cs | 2 +- src/Microsoft.ML.Core/Data/IHostEnvironment.cs | 7 ++++++- .../Environment/HostEnvironmentBase.cs | 6 +++++- .../Commands/TrainTestCommand.cs | 2 +- .../DataLoadSave/DataOperationsCatalog.cs | 2 +- src/Microsoft.ML.Data/MLContext.cs | 14 ++++++++------ .../OnnxTransform.cs | 2 +- src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs | 16 +++++++++------- .../TensorflowTransform.cs | 2 +- src/Microsoft.ML.TensorFlow/TensorflowUtils.cs | 4 ++-- src/Microsoft.ML.Vision/DnnRetrainTransform.cs | 2 +- .../ImageClassificationTrainer.cs | 9 +++++---- .../OnnxTransformTests.cs | 6 +++--- .../FeaturizeTextBench.cs | 2 +- .../TextLoaderBench.cs | 2 +- .../Microsoft.ML.TestFramework/RemoteExecutor.cs | 2 +- test/Microsoft.ML.Tests/ImagesTests.cs | 2 +- test/Microsoft.ML.Tests/OnnxConversionTest.cs | 6 +++--- .../TensorflowTests.cs | 6 +++--- .../TreeEnsembleFeaturizerTest.cs | 4 ++-- 21 files changed, 57 insertions(+), 43 deletions(-) diff --git a/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs b/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs index ca5eba5862..21d08eca30 100644 --- a/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs +++ b/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs @@ -66,7 +66,7 @@ public ExperimentSettings() { MaxExperimentTimeInSeconds = 24 * 60 * 60; CancellationToken = default; - CacheDirectory = new DirectoryInfo(Path.Combine(MLContext.TempFilePath, "Microsoft.ML.AutoML")); + CacheDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "Microsoft.ML.AutoML")); CacheBeforeTrainer = CacheBeforeTrainer.Auto; MaxModels = int.MaxValue; } diff --git a/src/Microsoft.ML.AutoML/Experiment/Experiment.cs b/src/Microsoft.ML.AutoML/Experiment/Experiment.cs index 8b8bbe09f3..8792a5351d 100644 --- a/src/Microsoft.ML.AutoML/Experiment/Experiment.cs +++ b/src/Microsoft.ML.AutoML/Experiment/Experiment.cs @@ -140,7 +140,7 @@ public IList Execute() // Pseudo random number generator to result in deterministic runs with the provided main MLContext's seed and to // maintain variability between training iterations. - int? mainContextSeed = ((ISeededEnvironment)_context.Model.GetEnvironment()).Seed; + int? mainContextSeed = ((IHostEnvironmentInternal)_context.Model.GetEnvironment()).Seed; _newContextSeedGenerator = (mainContextSeed.HasValue) ? RandomUtils.Create(mainContextSeed.Value) : null; do diff --git a/src/Microsoft.ML.Core/Data/IHostEnvironment.cs b/src/Microsoft.ML.Core/Data/IHostEnvironment.cs index e010cf335e..1bfd5c3230 100644 --- a/src/Microsoft.ML.Core/Data/IHostEnvironment.cs +++ b/src/Microsoft.ML.Core/Data/IHostEnvironment.cs @@ -83,12 +83,17 @@ internal interface ICancelable } [BestFriend] - internal interface ISeededEnvironment : IHostEnvironment + internal interface IHostEnvironmentInternal : IHostEnvironment { /// /// The seed property that, if assigned, makes components requiring randomness behave deterministically. /// int? Seed { get; } + + /// + /// The location for the temp files created by ML.NET + /// + string TempFilePath { get; set; } } /// diff --git a/src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs b/src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs index 4a3efeec3d..3313ddd238 100644 --- a/src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs +++ b/src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs @@ -93,9 +93,11 @@ internal interface IMessageSource /// query progress. /// [BestFriend] - internal abstract class HostEnvironmentBase : ChannelProviderBase, ISeededEnvironment, IChannelProvider, ICancelable + internal abstract class HostEnvironmentBase : ChannelProviderBase, IHostEnvironmentInternal, IChannelProvider, ICancelable where TEnv : HostEnvironmentBase { + public string TempFilePath { get; set; } + void ICancelable.CancelExecution() { lock (_cancelLock) @@ -360,6 +362,7 @@ protected HostEnvironmentBase(int? seed, bool verbose, string shortName = null, string parentFullName = null) : base(shortName, parentFullName, verbose) { + TempFilePath = Path.GetTempPath(); Seed = seed; _rand = RandomUtils.Create(Seed); ListenerDict = new ConcurrentDictionary(); @@ -377,6 +380,7 @@ protected HostEnvironmentBase(HostEnvironmentBase source, Random rand, boo string shortName = null, string parentFullName = null) : base(shortName, parentFullName, verbose) { + TempFilePath = Path.GetTempPath(); Contracts.CheckValue(source, nameof(source)); Contracts.CheckValueOrNull(rand); _rand = rand ?? RandomUtils.Create(); diff --git a/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs b/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs index 66c6e74326..62f614ba70 100644 --- a/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs +++ b/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs @@ -188,7 +188,7 @@ private void RunCore(IChannel ch, string cmd) ILegacyDataLoader testPipe; bool hasOutfile = !string.IsNullOrEmpty(ImplOptions.OutputModelFile); - var tempFilePath = hasOutfile ? null : Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + var tempFilePath = hasOutfile ? null : Path.Combine((Host as IHostEnvironmentInternal).TempFilePath, Path.GetRandomFileName()); using (var file = new SimpleFileHandle(ch, hasOutfile ? ImplOptions.OutputModelFile : tempFilePath, true, !hasOutfile)) { diff --git a/src/Microsoft.ML.Data/DataLoadSave/DataOperationsCatalog.cs b/src/Microsoft.ML.Data/DataLoadSave/DataOperationsCatalog.cs index 268766ed88..e7dbc556d4 100644 --- a/src/Microsoft.ML.Data/DataLoadSave/DataOperationsCatalog.cs +++ b/src/Microsoft.ML.Data/DataLoadSave/DataOperationsCatalog.cs @@ -534,7 +534,7 @@ internal static string CreateSplitColumn(IHostEnvironment env, ref IDataView dat } else if(fallbackInEnvSeed) { - ISeededEnvironment seededEnv = (ISeededEnvironment)env; + IHostEnvironmentInternal seededEnv = (IHostEnvironmentInternal)env; seedToUse = seededEnv.Seed; } else diff --git a/src/Microsoft.ML.Data/MLContext.cs b/src/Microsoft.ML.Data/MLContext.cs index 908c95a59e..cdf925538a 100644 --- a/src/Microsoft.ML.Data/MLContext.cs +++ b/src/Microsoft.ML.Data/MLContext.cs @@ -14,7 +14,7 @@ namespace Microsoft.ML /// create components for data preparation, feature enginering, training, prediction, model evaluation. /// It also allows logging, execution control, and the ability set repeatable random numbers. /// - public sealed class MLContext : ISeededEnvironment + public sealed class MLContext : IHostEnvironmentInternal { // REVIEW: consider making LocalEnvironment and MLContext the same class instead of encapsulation. private readonly LocalEnvironment _env; @@ -79,11 +79,13 @@ public sealed class MLContext : ISeededEnvironment /// public ComponentCatalog ComponentCatalog => _env.ComponentCatalog; - private static string _tempFilePath = System.IO.Path.GetTempPath(); - public static string TempFilePath + /// + /// The location for the temp files created by ML.NET + /// + public string TempFilePath { - get { return _tempFilePath; } - set { _tempFilePath = value; } + get { return _env.TempFilePath; } + set { _env.TempFilePath = value; } } /// @@ -147,7 +149,7 @@ private void ProcessMessage(IMessageSource source, ChannelMessage message) IChannel IChannelProvider.Start(string name) => _env.Start(name); IPipe IChannelProvider.StartPipe(string name) => _env.StartPipe(name); IProgressChannel IProgressChannelProvider.StartProgressChannel(string name) => _env.StartProgressChannel(name); - int? ISeededEnvironment.Seed => _env.Seed; + int? IHostEnvironmentInternal.Seed => _env.Seed; [BestFriend] internal void CancelExecution() => ((ICancelable)_env).CancelExecution(); diff --git a/src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs b/src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs index 007dfc40af..6a4d98750c 100644 --- a/src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs +++ b/src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs @@ -227,7 +227,7 @@ private OnnxTransformer(IHostEnvironment env, Options options, byte[] modelBytes { // Entering this region means that the byte[] is passed as the model. To feed that byte[] to ONNXRuntime, we need // to create a temporal file to store it and then call ONNXRuntime's API to load that file. - Model = OnnxModel.CreateFromBytes(modelBytes, options.GpuDeviceId, options.FallbackToCpu, shapeDictionary: shapeDictionary); + Model = OnnxModel.CreateFromBytes(modelBytes, env, options.GpuDeviceId, options.FallbackToCpu, shapeDictionary: shapeDictionary); } } catch (OnnxRuntimeException e) diff --git a/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs b/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs index 7132e3651a..f5af9358e2 100644 --- a/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs +++ b/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs @@ -321,33 +321,35 @@ private static bool CheckOnnxShapeCompatibility(IEnumerable left, IEnumerab /// /// Create an OnnxModel from a byte[]. Usually, a ONNX model is consumed by as a file. - /// With and , + /// With and , /// it's possible to use in-memory model (type: byte[]) to create . /// /// Bytes of the serialized model - public static OnnxModel CreateFromBytes(byte[] modelBytes) + /// IHostEnvironment + public static OnnxModel CreateFromBytes(byte[] modelBytes, IHostEnvironment env) { - return CreateFromBytes(modelBytes, null, false); + return CreateFromBytes(modelBytes, env, null, false); } /// /// Create an OnnxModel from a byte[]. Set execution to GPU if required. /// Usually, a ONNX model is consumed by as a file. - /// With and - /// , + /// With and + /// , /// it's possible to use in-memory model (type: byte[]) to create . /// /// Bytes of the serialized model. + /// IHostEnvironment /// GPU device ID to execute on. Null for CPU. /// If true, resumes CPU execution quietly upon GPU error. /// User-provided shapes. If the key "myTensorName" is associated /// with the value [1, 3, 5], the shape of "myTensorName" will be set to [1, 3, 5]. /// The shape loaded from would be overwritten. /// An - public static OnnxModel CreateFromBytes(byte[] modelBytes, int? gpuDeviceId = null, bool fallbackToCpu = false, + public static OnnxModel CreateFromBytes(byte[] modelBytes, IHostEnvironment env, int? gpuDeviceId = null, bool fallbackToCpu = false, IDictionary shapeDictionary = null) { - var tempModelFile = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + var tempModelFile = Path.Combine((env as IHostEnvironmentInternal).TempFilePath, Path.GetRandomFileName()); File.WriteAllBytes(tempModelFile, modelBytes); return new OnnxModel(tempModelFile, gpuDeviceId, fallbackToCpu, ownModelFile: true, shapeDictionary: shapeDictionary); diff --git a/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs b/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs index 7cd39e8039..59f31d9210 100644 --- a/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs +++ b/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs @@ -143,7 +143,7 @@ private static TensorFlowTransformer Create(IHostEnvironment env, ModelLoadConte return new TensorFlowTransformer(env, LoadTFSession(env, modelBytes), outputs, inputs, null, false, addBatchDimensionInput, treatOutputAsBatched: treatOutputAsBatched); } - var tempDirPath = Path.GetFullPath(Path.Combine(MLContext.TempFilePath, nameof(TensorFlowTransformer) + "_" + Guid.NewGuid())); + var tempDirPath = Path.GetFullPath(Path.Combine((env as IHostEnvironmentInternal).TempFilePath, nameof(TensorFlowTransformer) + "_" + Guid.NewGuid())); CreateFolderWithAclIfNotExists(env, tempDirPath); try { diff --git a/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs b/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs index 4d587d05be..75f3eefc34 100644 --- a/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs +++ b/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs @@ -630,9 +630,9 @@ public void Dispose() } } - internal static string GetTemporaryDirectory() + internal static string GetTemporaryDirectory(IHostEnvironment env) { - string tempDirectory = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + string tempDirectory = Path.Combine((env as IHostEnvironmentInternal).TempFilePath, Path.GetRandomFileName()); Directory.CreateDirectory(tempDirectory); return tempDirectory; } diff --git a/src/Microsoft.ML.Vision/DnnRetrainTransform.cs b/src/Microsoft.ML.Vision/DnnRetrainTransform.cs index 57a0705e55..6ebb5900e2 100644 --- a/src/Microsoft.ML.Vision/DnnRetrainTransform.cs +++ b/src/Microsoft.ML.Vision/DnnRetrainTransform.cs @@ -116,7 +116,7 @@ private static DnnRetrainTransformer Create(IHostEnvironment env, ModelLoadConte null, false, addBatchDimensionInput, 1); } - var tempDirPath = Path.GetFullPath(Path.Combine(MLContext.TempFilePath, nameof(DnnRetrainTransformer) + "_" + Guid.NewGuid())); + var tempDirPath = Path.GetFullPath(Path.Combine((env as IHostEnvironmentInternal).TempFilePath, nameof(DnnRetrainTransformer) + "_" + Guid.NewGuid())); CreateFolderWithAclIfNotExists(env, tempDirPath); try { diff --git a/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs b/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs index 286616ff39..29b06ba7ed 100644 --- a/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs +++ b/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs @@ -487,7 +487,7 @@ public sealed class Options : TrainerInputBaseWithLabel private readonly bool _cleanupWorkspace; private int _classCount; private Graph Graph => _session.graph; - private static readonly string _resourcePath = Path.Combine(MLContext.TempFilePath, "MLNET"); + private readonly string _resourcePath; private readonly string _sizeFile; /// @@ -531,10 +531,11 @@ internal ImageClassificationTrainer(IHostEnvironment env, Options options) Host.CheckNonEmpty(options.ScoreColumnName, nameof(options.ScoreColumnName)); Host.CheckNonEmpty(options.PredictedLabelColumnName, nameof(options.PredictedLabelColumnName)); tf.compat.v1.disable_eager_execution(); + _resourcePath = Path.Combine((env as IHostEnvironmentInternal).TempFilePath, "MLNET"); if (string.IsNullOrEmpty(options.WorkspacePath)) { - options.WorkspacePath = GetTemporaryDirectory(); + options.WorkspacePath = GetTemporaryDirectory(env); _cleanupWorkspace = true; } @@ -1322,9 +1323,9 @@ private void AddTransferLearningLayer(string labelColumn, private static TensorFlowSessionWrapper LoadTensorFlowSessionFromMetaGraph(IHostEnvironment env, Architecture arch) { var modelFileName = ModelFileName[arch]; - var modelFilePath = Path.Combine(_resourcePath, modelFileName); + var modelFilePath = Path.Combine((env as IHostEnvironmentInternal).TempFilePath, "MLNET", modelFileName); int timeout = 10 * 60 * 1000; - DownloadIfNeeded(env, @"meta\" + modelFileName, _resourcePath, modelFileName, timeout); + DownloadIfNeeded(env, @"meta\" + modelFileName, Path.Combine((env as IHostEnvironmentInternal).TempFilePath, "MLNET"), modelFileName, timeout); return new TensorFlowSessionWrapper(GetSession(env, modelFilePath, true), modelFilePath); } diff --git a/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs b/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs index 5f660164ef..202b3171c1 100644 --- a/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs +++ b/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs @@ -244,7 +244,7 @@ public void OnnxWorkout() var result = model.Transform(data); // save and reload the model - var tempPath = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + var tempPath = Path.GetTempFileName(); ML.Model.Save(model, data.Schema, tempPath); var loadedModel = ML.Model.Load(tempPath, out DataViewSchema modelSchema); (loadedModel as IDisposable)?.Dispose(); @@ -681,7 +681,7 @@ public void TestOnnxModelDisposal() var modelInBytes = File.ReadAllBytes(modelFile); // Create ONNX model from the byte[]. - var onnxModel = OnnxModel.CreateFromBytes(modelInBytes); + var onnxModel = OnnxModel.CreateFromBytes(modelInBytes, ML); // Check if a temporal file is crated for storing the byte[]. Assert.True(File.Exists(onnxModel.ModelStream.Name)); @@ -966,7 +966,7 @@ public void TestOnnxTransformSaveAndLoadWithCustomShapes() // Save the trained ONNX transformer into file and then load it back. ITransformer loadedModel = null; - var tempPath = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + var tempPath = Path.GetTempFileName(); using (var file = new SimpleFileHandle(Env, tempPath, true, true)) { // Save. diff --git a/test/Microsoft.ML.PerformanceTests/FeaturizeTextBench.cs b/test/Microsoft.ML.PerformanceTests/FeaturizeTextBench.cs index 1689ef0e45..5916de4082 100644 --- a/test/Microsoft.ML.PerformanceTests/FeaturizeTextBench.cs +++ b/test/Microsoft.ML.PerformanceTests/FeaturizeTextBench.cs @@ -26,7 +26,7 @@ public class FeaturizeTextBench : BenchmarkBase public void SetupData() { _mlContext = new MLContext(seed: 1); - var path = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + var path = Path.GetTempFileName(); Console.WriteLine($"Created dataset in temporary file:\n{path}\n"); path = RandomFile.CreateRandomFile(path, _numRows, _numColumns, _maxWordLength); diff --git a/test/Microsoft.ML.PerformanceTests/TextLoaderBench.cs b/test/Microsoft.ML.PerformanceTests/TextLoaderBench.cs index 9d8a2654c8..2aabb2f91b 100644 --- a/test/Microsoft.ML.PerformanceTests/TextLoaderBench.cs +++ b/test/Microsoft.ML.PerformanceTests/TextLoaderBench.cs @@ -29,7 +29,7 @@ public class TextLoaderBench : BenchmarkBase public void SetupData() { _mlContext = new MLContext(seed: 1); - var path = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + var path = Path.GetTempFileName(); Console.WriteLine($"Created dataset in temporary file:\n{path}\n"); path = RandomFile.CreateRandomFile(path, _numRows, _numColumns, _maxWordLength); diff --git a/test/Microsoft.ML.TestFramework/RemoteExecutor.cs b/test/Microsoft.ML.TestFramework/RemoteExecutor.cs index 20cb5eba76..39775b4be1 100644 --- a/test/Microsoft.ML.TestFramework/RemoteExecutor.cs +++ b/test/Microsoft.ML.TestFramework/RemoteExecutor.cs @@ -199,6 +199,6 @@ public RemoteInvokeOptions(Dictionary environmentVariables = nul public bool CheckExitCode { get; set; } = true; public int TimeOut { get; set; } = RemoteExecutor.FailWaitTimeoutMilliseconds; public int ExpectedExitCode { get; set; } = RemoteExecutor.SuccessExitCode; - public string ExceptionFile { get; } = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + public string ExceptionFile { get; } = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); } } diff --git a/test/Microsoft.ML.Tests/ImagesTests.cs b/test/Microsoft.ML.Tests/ImagesTests.cs index c866f95915..729f81dccd 100644 --- a/test/Microsoft.ML.Tests/ImagesTests.cs +++ b/test/Microsoft.ML.Tests/ImagesTests.cs @@ -77,7 +77,7 @@ public void TestEstimatorSaveLoad() pipe.GetOutputSchema(SchemaShape.Create(data.Schema)); var model = pipe.Fit(data); - var tempPath = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + var tempPath = Path.GetTempFileName(); using (var file = new SimpleFileHandle(env, tempPath, true, true)) { using (var fs = file.CreateWriteStream()) diff --git a/test/Microsoft.ML.Tests/OnnxConversionTest.cs b/test/Microsoft.ML.Tests/OnnxConversionTest.cs index 58ae97eae6..5e97a970a6 100644 --- a/test/Microsoft.ML.Tests/OnnxConversionTest.cs +++ b/test/Microsoft.ML.Tests/OnnxConversionTest.cs @@ -637,9 +637,9 @@ public void MulticlassLogisticRegressionOnnxConversionTest() public void LoadingPredictorModelAndOnnxConversionTest() { string dataPath = GetDataPath("iris.txt"); - string modelPath = MLContext.TempFilePath + Guid.NewGuid().ToString() + ".model.bin"; - string onnxPath = MLContext.TempFilePath + Guid.NewGuid().ToString() + ".model.onnx"; - string onnxJsonPath = MLContext.TempFilePath + Guid.NewGuid().ToString() + ".model.onnx.json"; + string modelPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".model.bin"; + string onnxPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".model.onnx"; + string onnxJsonPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".model.onnx.json"; string inputGraph = string.Format(@" {{ diff --git a/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs b/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs index 62c5158a06..cb67fe5244 100644 --- a/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs +++ b/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs @@ -34,7 +34,7 @@ internal sealed class TensorFlowScenariosTestsFixture : IDisposable public static string assetsPath; internal static void CreateParentWorkspacePathForImageClassification() { - tempFolder = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + tempFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); assetsPath = Path.Combine(tempFolder, "assets"); parentWorkspacePath = Path.Combine(assetsPath, "cached"); // Delete if the workspace path already exists @@ -1678,7 +1678,7 @@ internal void TensorFlowImageClassificationWithLRScheduling(LearningRateSchedule Assert.True(File.Exists(Path.Combine(options.WorkspacePath, options.TrainSetBottleneckCachedValuesFileName))); Assert.True(File.Exists(Path.Combine(options.WorkspacePath, options.ValidationSetBottleneckCachedValuesFileName))); - Assert.True(File.Exists(Path.Combine(MLContext.TempFilePath, "MLNET", ImageClassificationTrainer.ModelFileName[options.Arch]))); + Assert.True(File.Exists(Path.Combine(_mlContext.TempFilePath, "MLNET", ImageClassificationTrainer.ModelFileName[options.Arch]))); (loadedModel as IDisposable)?.Dispose(); } @@ -1961,7 +1961,7 @@ public class ImagePrediction private static string GetTemporaryDirectory() { - string tempDirectory = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(tempDirectory); return tempDirectory; } diff --git a/test/Microsoft.ML.Tests/TrainerEstimators/TreeEnsembleFeaturizerTest.cs b/test/Microsoft.ML.Tests/TrainerEstimators/TreeEnsembleFeaturizerTest.cs index 2d62b8feb4..68dee8a5b9 100644 --- a/test/Microsoft.ML.Tests/TrainerEstimators/TreeEnsembleFeaturizerTest.cs +++ b/test/Microsoft.ML.Tests/TrainerEstimators/TreeEnsembleFeaturizerTest.cs @@ -611,7 +611,7 @@ public void TestSaveAndLoadTreeFeaturizer() // Save the trained model into file. ITransformer loadedModel = null; - var tempPath = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + var tempPath = Path.GetTempFileName(); using (var file = new SimpleFileHandle(Env, tempPath, true, true)) { using (var fs = file.CreateWriteStream()) @@ -668,7 +668,7 @@ public void TestSaveAndLoadDoubleTreeFeaturizer() // Save the trained model into file and then load it back. ITransformer loadedModel = null; - var tempPath = Path.Combine(MLContext.TempFilePath, Path.GetRandomFileName()); + var tempPath = Path.GetTempFileName(); using (var file = new SimpleFileHandle(Env, tempPath, true, true)) { using (var fs = file.CreateWriteStream()) From 301b581b60b4bde05d2450db4019ada0855a34c6 Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Tue, 11 May 2021 14:58:39 -0700 Subject: [PATCH 4/9] Changes from PR comments. --- .../RankingExperiment.cs | 2 +- .../RecommendationExperiment.cs | 2 +- src/Microsoft.ML.AutoML/API/AutoCatalog.cs | 10 +++++----- .../API/BinaryClassificationExperiment.cs | 11 +++++++++++ src/Microsoft.ML.AutoML/API/ExperimentSettings.cs | 14 +++++++++++++- .../API/MulticlassClassificationExperiment.cs | 10 ++++++++++ src/Microsoft.ML.AutoML/API/RankingExperiment.cs | 14 ++++++++++++++ .../API/RecommendationExperiment.cs | 10 ++++++++++ .../API/RegressionExperiment.cs | 13 +++++++++++++ .../Environment/HostEnvironmentBase.cs | 8 ++++---- src/Microsoft.ML.Data/Commands/TrainTestCommand.cs | 2 +- src/Microsoft.ML.Data/MLContext.cs | 2 +- src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs | 2 +- test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs | 6 +++--- .../Utils/TaskAgnosticAutoFit.cs | 6 +++--- 15 files changed, 91 insertions(+), 21 deletions(-) diff --git a/docs/samples/Microsoft.ML.AutoML.Samples/RankingExperiment.cs b/docs/samples/Microsoft.ML.AutoML.Samples/RankingExperiment.cs index 8d04914fb4..0fc2133d87 100644 --- a/docs/samples/Microsoft.ML.AutoML.Samples/RankingExperiment.cs +++ b/docs/samples/Microsoft.ML.AutoML.Samples/RankingExperiment.cs @@ -28,7 +28,7 @@ public static void Run() // STEP 2: Run AutoML experiment Console.WriteLine($"Running AutoML recommendation experiment for {ExperimentTime} seconds..."); ExperimentResult experimentResult = mlContext.Auto() - .CreateRankingExperiment(new RankingExperimentSettings() { MaxExperimentTimeInSeconds = ExperimentTime }) + .CreateRankingExperiment(new RankingExperimentSettings(mlContext) { MaxExperimentTimeInSeconds = ExperimentTime }) .Execute(trainDataView, testDataView, new ColumnInformation() { diff --git a/docs/samples/Microsoft.ML.AutoML.Samples/RecommendationExperiment.cs b/docs/samples/Microsoft.ML.AutoML.Samples/RecommendationExperiment.cs index 95f453cc30..ef8a4f993d 100644 --- a/docs/samples/Microsoft.ML.AutoML.Samples/RecommendationExperiment.cs +++ b/docs/samples/Microsoft.ML.AutoML.Samples/RecommendationExperiment.cs @@ -31,7 +31,7 @@ public static void Run() // STEP 2: Run AutoML experiment Console.WriteLine($"Running AutoML recommendation experiment for {ExperimentTime} seconds..."); ExperimentResult experimentResult = mlContext.Auto() - .CreateRecommendationExperiment(new RecommendationExperimentSettings() { MaxExperimentTimeInSeconds = ExperimentTime }) + .CreateRecommendationExperiment(new RecommendationExperimentSettings(mlContext) { MaxExperimentTimeInSeconds = ExperimentTime }) .Execute(trainDataView, testDataView, new ColumnInformation() { diff --git a/src/Microsoft.ML.AutoML/API/AutoCatalog.cs b/src/Microsoft.ML.AutoML/API/AutoCatalog.cs index 71941a7015..6623112ed6 100644 --- a/src/Microsoft.ML.AutoML/API/AutoCatalog.cs +++ b/src/Microsoft.ML.AutoML/API/AutoCatalog.cs @@ -34,7 +34,7 @@ internal AutoCatalog(MLContext context) /// public RegressionExperiment CreateRegressionExperiment(uint maxExperimentTimeInSeconds) { - return new RegressionExperiment(_context, new RegressionExperimentSettings() + return new RegressionExperiment(_context, new RegressionExperimentSettings(_context) { MaxExperimentTimeInSeconds = maxExperimentTimeInSeconds }); @@ -69,7 +69,7 @@ public RegressionExperiment CreateRegressionExperiment(RegressionExperimentSetti /// public BinaryClassificationExperiment CreateBinaryClassificationExperiment(uint maxExperimentTimeInSeconds) { - return new BinaryClassificationExperiment(_context, new BinaryExperimentSettings() + return new BinaryClassificationExperiment(_context, new BinaryExperimentSettings(_context) { MaxExperimentTimeInSeconds = maxExperimentTimeInSeconds }); @@ -104,7 +104,7 @@ public BinaryClassificationExperiment CreateBinaryClassificationExperiment(Binar /// public MulticlassClassificationExperiment CreateMulticlassClassificationExperiment(uint maxExperimentTimeInSeconds) { - return new MulticlassClassificationExperiment(_context, new MulticlassExperimentSettings() + return new MulticlassClassificationExperiment(_context, new MulticlassExperimentSettings(_context) { MaxExperimentTimeInSeconds = maxExperimentTimeInSeconds }); @@ -139,7 +139,7 @@ public MulticlassClassificationExperiment CreateMulticlassClassificationExperime /// public RecommendationExperiment CreateRecommendationExperiment(uint maxExperimentTimeInSeconds) { - return new RecommendationExperiment(_context, new RecommendationExperimentSettings() + return new RecommendationExperiment(_context, new RecommendationExperimentSettings(_context) { MaxExperimentTimeInSeconds = maxExperimentTimeInSeconds }); @@ -174,7 +174,7 @@ public RecommendationExperiment CreateRecommendationExperiment(RecommendationExp /// public RankingExperiment CreateRankingExperiment(uint maxExperimentTimeInSeconds) { - return new RankingExperiment(_context, new RankingExperimentSettings() + return new RankingExperiment(_context, new RankingExperimentSettings(_context) { MaxExperimentTimeInSeconds = maxExperimentTimeInSeconds }); diff --git a/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs b/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs index 84f539c932..9e99540792 100644 --- a/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using Microsoft.ML.Data; +using Microsoft.ML.Runtime; using Microsoft.ML.Trainers; using Microsoft.ML.Trainers.FastTree; using Microsoft.ML.Trainers.LightGbm; @@ -37,6 +38,16 @@ public BinaryExperimentSettings() OptimizingMetric = BinaryClassificationMetric.Accuracy; Trainers = Enum.GetValues(typeof(BinaryClassificationTrainer)).OfType().ToList(); } + + /// + /// Initializes a new instance of using the temp file location provided to the MLContext. + /// + public BinaryExperimentSettings(MLContext context) : + base(context) + { + OptimizingMetric = BinaryClassificationMetric.Accuracy; + Trainers = Enum.GetValues(typeof(BinaryClassificationTrainer)).OfType().ToList(); + } } /// diff --git a/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs b/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs index 21d08eca30..7bbb7b061b 100644 --- a/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs +++ b/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs @@ -60,7 +60,7 @@ public abstract class ExperimentSettings internal int MaxModels; /// - /// Initializes a new instance of . + /// Initializes a new instance of using the default temp file location. /// public ExperimentSettings() { @@ -70,6 +70,18 @@ public ExperimentSettings() CacheBeforeTrainer = CacheBeforeTrainer.Auto; MaxModels = int.MaxValue; } + + /// + /// Initializes a new instance of using the temp file location provided to the MLContext. + /// + public ExperimentSettings(MLContext context) + { + MaxExperimentTimeInSeconds = 24 * 60 * 60; + CancellationToken = default; + CacheDirectory = new DirectoryInfo(Path.Combine(context.TempFilePath, "Microsoft.ML.AutoML")); + CacheBeforeTrainer = CacheBeforeTrainer.Auto; + MaxModels = int.MaxValue; + } } /// diff --git a/src/Microsoft.ML.AutoML/API/MulticlassClassificationExperiment.cs b/src/Microsoft.ML.AutoML/API/MulticlassClassificationExperiment.cs index 6f45e6f54a..07e5ca61a5 100644 --- a/src/Microsoft.ML.AutoML/API/MulticlassClassificationExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/MulticlassClassificationExperiment.cs @@ -39,6 +39,16 @@ public MulticlassExperimentSettings() OptimizingMetric = MulticlassClassificationMetric.MicroAccuracy; Trainers = Enum.GetValues(typeof(MulticlassClassificationTrainer)).OfType().ToList(); } + + /// + /// Initializes a new instances of using the temp file location provided to the MLContext. + /// + public MulticlassExperimentSettings(MLContext context) : + base(context) + { + OptimizingMetric = MulticlassClassificationMetric.MicroAccuracy; + Trainers = Enum.GetValues(typeof(MulticlassClassificationTrainer)).OfType().ToList(); + } } /// diff --git a/src/Microsoft.ML.AutoML/API/RankingExperiment.cs b/src/Microsoft.ML.AutoML/API/RankingExperiment.cs index f6e039c3be..801b7e5816 100644 --- a/src/Microsoft.ML.AutoML/API/RankingExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/RankingExperiment.cs @@ -35,12 +35,26 @@ public sealed class RankingExperimentSettings : ExperimentSettings /// public uint OptimizationMetricTruncationLevel { get; set; } + /// + /// Initializes a new instance of . + /// public RankingExperimentSettings() { OptimizingMetric = RankingMetric.Ndcg; Trainers = Enum.GetValues(typeof(RankingTrainer)).OfType().ToList(); OptimizationMetricTruncationLevel = 10; } + + /// + /// Initializes a new instance of using the temp file location provided to the MLContext. + /// + public RankingExperimentSettings(MLContext context) : + base(context) + { + OptimizingMetric = RankingMetric.Ndcg; + Trainers = Enum.GetValues(typeof(RankingTrainer)).OfType().ToList(); + OptimizationMetricTruncationLevel = 10; + } } public enum RankingMetric { diff --git a/src/Microsoft.ML.AutoML/API/RecommendationExperiment.cs b/src/Microsoft.ML.AutoML/API/RecommendationExperiment.cs index 18f5f9ccf5..8bafc1e8df 100644 --- a/src/Microsoft.ML.AutoML/API/RecommendationExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/RecommendationExperiment.cs @@ -34,6 +34,16 @@ public RecommendationExperimentSettings() OptimizingMetric = RegressionMetric.RSquared; Trainers = Enum.GetValues(typeof(RecommendationTrainer)).OfType().ToList(); } + + /// + /// Initializes a new instance of using the temp file location provided to the MLContext. + /// + public RecommendationExperimentSettings(MLContext context) : + base(context) + { + OptimizingMetric = RegressionMetric.RSquared; + Trainers = Enum.GetValues(typeof(RecommendationTrainer)).OfType().ToList(); + } } /// diff --git a/src/Microsoft.ML.AutoML/API/RegressionExperiment.cs b/src/Microsoft.ML.AutoML/API/RegressionExperiment.cs index 438260a1eb..4be5a8870e 100644 --- a/src/Microsoft.ML.AutoML/API/RegressionExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/RegressionExperiment.cs @@ -31,11 +31,24 @@ public sealed class RegressionExperimentSettings : ExperimentSettings /// public ICollection Trainers { get; } + /// + /// Initializes a new instance of . + /// public RegressionExperimentSettings() { OptimizingMetric = RegressionMetric.RSquared; Trainers = Enum.GetValues(typeof(RegressionTrainer)).OfType().ToList(); } + + /// + /// Initializes a new instance of using the temp file location provided to the MLContext. + /// + public RegressionExperimentSettings(MLContext context) : + base(context) + { + OptimizingMetric = RegressionMetric.RSquared; + Trainers = Enum.GetValues(typeof(RegressionTrainer)).OfType().ToList(); + } } /// diff --git a/src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs b/src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs index 3313ddd238..37eebc4322 100644 --- a/src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs +++ b/src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs @@ -96,8 +96,6 @@ internal interface IMessageSource internal abstract class HostEnvironmentBase : ChannelProviderBase, IHostEnvironmentInternal, IChannelProvider, ICancelable where TEnv : HostEnvironmentBase { - public string TempFilePath { get; set; } - void ICancelable.CancelExecution() { lock (_cancelLock) @@ -328,6 +326,10 @@ public void RemoveListener(Action listenerFunc) } } +#pragma warning disable MSML_NoInstanceInitializers // Need this to have a default value incase the user doesn't set it. + public string TempFilePath { get; set; } = System.IO.Path.GetTempPath(); +#pragma warning restore MSML_NoInstanceInitializers + protected readonly TEnv Root; // This is non-null iff this environment was a fork of another. Disposing a fork // doesn't free temp files. That is handled when the master is disposed. @@ -362,7 +364,6 @@ protected HostEnvironmentBase(int? seed, bool verbose, string shortName = null, string parentFullName = null) : base(shortName, parentFullName, verbose) { - TempFilePath = Path.GetTempPath(); Seed = seed; _rand = RandomUtils.Create(Seed); ListenerDict = new ConcurrentDictionary(); @@ -380,7 +381,6 @@ protected HostEnvironmentBase(HostEnvironmentBase source, Random rand, boo string shortName = null, string parentFullName = null) : base(shortName, parentFullName, verbose) { - TempFilePath = Path.GetTempPath(); Contracts.CheckValue(source, nameof(source)); Contracts.CheckValueOrNull(rand); _rand = rand ?? RandomUtils.Create(); diff --git a/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs b/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs index 62f614ba70..3291a5f4d7 100644 --- a/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs +++ b/src/Microsoft.ML.Data/Commands/TrainTestCommand.cs @@ -188,7 +188,7 @@ private void RunCore(IChannel ch, string cmd) ILegacyDataLoader testPipe; bool hasOutfile = !string.IsNullOrEmpty(ImplOptions.OutputModelFile); - var tempFilePath = hasOutfile ? null : Path.Combine((Host as IHostEnvironmentInternal).TempFilePath, Path.GetRandomFileName()); + var tempFilePath = hasOutfile ? null : Path.Combine(((IHostEnvironmentInternal)Host).TempFilePath, Path.GetRandomFileName()); using (var file = new SimpleFileHandle(ch, hasOutfile ? ImplOptions.OutputModelFile : tempFilePath, true, !hasOutfile)) { diff --git a/src/Microsoft.ML.Data/MLContext.cs b/src/Microsoft.ML.Data/MLContext.cs index cdf925538a..b653f61b2e 100644 --- a/src/Microsoft.ML.Data/MLContext.cs +++ b/src/Microsoft.ML.Data/MLContext.cs @@ -80,7 +80,7 @@ public sealed class MLContext : IHostEnvironmentInternal public ComponentCatalog ComponentCatalog => _env.ComponentCatalog; /// - /// The location for the temp files created by ML.NET + /// Gets or sets the location for the temp files created by ML.NET. /// public string TempFilePath { diff --git a/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs b/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs index f5af9358e2..f602287f73 100644 --- a/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs +++ b/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs @@ -349,7 +349,7 @@ public static OnnxModel CreateFromBytes(byte[] modelBytes, IHostEnvironment env) public static OnnxModel CreateFromBytes(byte[] modelBytes, IHostEnvironment env, int? gpuDeviceId = null, bool fallbackToCpu = false, IDictionary shapeDictionary = null) { - var tempModelFile = Path.Combine((env as IHostEnvironmentInternal).TempFilePath, Path.GetRandomFileName()); + var tempModelFile = Path.Combine(((IHostEnvironmentInternal)env).TempFilePath, Path.GetRandomFileName()); File.WriteAllBytes(tempModelFile, modelBytes); return new OnnxModel(tempModelFile, gpuDeviceId, fallbackToCpu, ownModelFile: true, shapeDictionary: shapeDictionary); diff --git a/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs b/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs index d5af1f1be5..e28148ba7b 100644 --- a/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs +++ b/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs @@ -183,8 +183,9 @@ public void AutoFitRegressionTest(string culture) // Furthermore, these issues might only occur after ~70 // iterations, so setting the internal maxModels parameter. int maxModels = culture == "en-US" ? 1 : 75; + var context = new MLContext(1); - var experimentSettings = new RegressionExperimentSettings { MaxModels = maxModels }; + var experimentSettings = new RegressionExperimentSettings(context) { MaxModels = maxModels }; if (!Environment.Is64BitProcess) { @@ -192,7 +193,6 @@ public void AutoFitRegressionTest(string culture) experimentSettings.Trainers.Remove(RegressionTrainer.LightGbm); } - var context = new MLContext(1); var dataPath = DatasetUtil.GetMlNetGeneratedRegressionDataset(); var columnInference = context.Auto().InferColumns(dataPath, DatasetUtil.MlNetGeneratedRegressionLabel); var textLoader = context.Data.CreateTextLoader(columnInference.TextLoaderOptions); @@ -232,7 +232,7 @@ public void AutoFitRankingTest() trainDataView = mlContext.Data.SkipRows(trainDataView, 500); // STEP 2: Run AutoML experiment - var settings = new RankingExperimentSettings() + var settings = new RankingExperimentSettings(mlContext) { MaxExperimentTimeInSeconds = 5, OptimizationMetricTruncationLevel = 3 diff --git a/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs b/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs index 49e91e4382..c275ed8445 100644 --- a/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs +++ b/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs @@ -51,7 +51,7 @@ internal IEnumerable AutoFit( { case TaskType.Classification: - var mcs = new MulticlassExperimentSettings + var mcs = new MulticlassExperimentSettings(_context) { OptimizingMetric = MulticlassClassificationMetric.MicroAccuracy, @@ -73,7 +73,7 @@ internal IEnumerable AutoFit( case TaskType.Regression: - var rs = new RegressionExperimentSettings + var rs = new RegressionExperimentSettings(_context) { OptimizingMetric = RegressionMetric.RSquared, @@ -95,7 +95,7 @@ internal IEnumerable AutoFit( case TaskType.Recommendation: - var recommendationSettings = new RecommendationExperimentSettings + var recommendationSettings = new RecommendationExperimentSettings (_context) { OptimizingMetric = RegressionMetric.RSquared, From 186ab97131f7c5ec2dc1ed65b93fcf46629564c4 Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Wed, 12 May 2021 12:58:09 -0700 Subject: [PATCH 5/9] Changed how auto ml caches. --- .../RankingExperiment.cs | 2 +- .../RecommendationExperiment.cs | 2 +- src/Microsoft.ML.AutoML/API/AutoCatalog.cs | 10 +++++----- .../API/BinaryClassificationExperiment.cs | 10 ---------- .../API/ExperimentSettings.cs | 19 ++++--------------- .../API/MulticlassClassificationExperiment.cs | 10 ---------- .../API/RankingExperiment.cs | 11 ----------- .../API/RecommendationExperiment.cs | 10 ---------- .../API/RegressionExperiment.cs | 10 ---------- .../Experiment/Experiment.cs | 2 +- .../TensorflowTransform.cs | 2 +- .../TensorflowUtils.cs | 2 +- .../Microsoft.ML.AutoML.Tests/AutoFitTests.cs | 4 ++-- .../Utils/TaskAgnosticAutoFit.cs | 6 +++--- 14 files changed, 19 insertions(+), 81 deletions(-) diff --git a/docs/samples/Microsoft.ML.AutoML.Samples/RankingExperiment.cs b/docs/samples/Microsoft.ML.AutoML.Samples/RankingExperiment.cs index 0fc2133d87..8d04914fb4 100644 --- a/docs/samples/Microsoft.ML.AutoML.Samples/RankingExperiment.cs +++ b/docs/samples/Microsoft.ML.AutoML.Samples/RankingExperiment.cs @@ -28,7 +28,7 @@ public static void Run() // STEP 2: Run AutoML experiment Console.WriteLine($"Running AutoML recommendation experiment for {ExperimentTime} seconds..."); ExperimentResult experimentResult = mlContext.Auto() - .CreateRankingExperiment(new RankingExperimentSettings(mlContext) { MaxExperimentTimeInSeconds = ExperimentTime }) + .CreateRankingExperiment(new RankingExperimentSettings() { MaxExperimentTimeInSeconds = ExperimentTime }) .Execute(trainDataView, testDataView, new ColumnInformation() { diff --git a/docs/samples/Microsoft.ML.AutoML.Samples/RecommendationExperiment.cs b/docs/samples/Microsoft.ML.AutoML.Samples/RecommendationExperiment.cs index ef8a4f993d..95f453cc30 100644 --- a/docs/samples/Microsoft.ML.AutoML.Samples/RecommendationExperiment.cs +++ b/docs/samples/Microsoft.ML.AutoML.Samples/RecommendationExperiment.cs @@ -31,7 +31,7 @@ public static void Run() // STEP 2: Run AutoML experiment Console.WriteLine($"Running AutoML recommendation experiment for {ExperimentTime} seconds..."); ExperimentResult experimentResult = mlContext.Auto() - .CreateRecommendationExperiment(new RecommendationExperimentSettings(mlContext) { MaxExperimentTimeInSeconds = ExperimentTime }) + .CreateRecommendationExperiment(new RecommendationExperimentSettings() { MaxExperimentTimeInSeconds = ExperimentTime }) .Execute(trainDataView, testDataView, new ColumnInformation() { diff --git a/src/Microsoft.ML.AutoML/API/AutoCatalog.cs b/src/Microsoft.ML.AutoML/API/AutoCatalog.cs index 6623112ed6..71941a7015 100644 --- a/src/Microsoft.ML.AutoML/API/AutoCatalog.cs +++ b/src/Microsoft.ML.AutoML/API/AutoCatalog.cs @@ -34,7 +34,7 @@ internal AutoCatalog(MLContext context) /// public RegressionExperiment CreateRegressionExperiment(uint maxExperimentTimeInSeconds) { - return new RegressionExperiment(_context, new RegressionExperimentSettings(_context) + return new RegressionExperiment(_context, new RegressionExperimentSettings() { MaxExperimentTimeInSeconds = maxExperimentTimeInSeconds }); @@ -69,7 +69,7 @@ public RegressionExperiment CreateRegressionExperiment(RegressionExperimentSetti /// public BinaryClassificationExperiment CreateBinaryClassificationExperiment(uint maxExperimentTimeInSeconds) { - return new BinaryClassificationExperiment(_context, new BinaryExperimentSettings(_context) + return new BinaryClassificationExperiment(_context, new BinaryExperimentSettings() { MaxExperimentTimeInSeconds = maxExperimentTimeInSeconds }); @@ -104,7 +104,7 @@ public BinaryClassificationExperiment CreateBinaryClassificationExperiment(Binar /// public MulticlassClassificationExperiment CreateMulticlassClassificationExperiment(uint maxExperimentTimeInSeconds) { - return new MulticlassClassificationExperiment(_context, new MulticlassExperimentSettings(_context) + return new MulticlassClassificationExperiment(_context, new MulticlassExperimentSettings() { MaxExperimentTimeInSeconds = maxExperimentTimeInSeconds }); @@ -139,7 +139,7 @@ public MulticlassClassificationExperiment CreateMulticlassClassificationExperime /// public RecommendationExperiment CreateRecommendationExperiment(uint maxExperimentTimeInSeconds) { - return new RecommendationExperiment(_context, new RecommendationExperimentSettings(_context) + return new RecommendationExperiment(_context, new RecommendationExperimentSettings() { MaxExperimentTimeInSeconds = maxExperimentTimeInSeconds }); @@ -174,7 +174,7 @@ public RecommendationExperiment CreateRecommendationExperiment(RecommendationExp /// public RankingExperiment CreateRankingExperiment(uint maxExperimentTimeInSeconds) { - return new RankingExperiment(_context, new RankingExperimentSettings(_context) + return new RankingExperiment(_context, new RankingExperimentSettings() { MaxExperimentTimeInSeconds = maxExperimentTimeInSeconds }); diff --git a/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs b/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs index 9e99540792..fe527e2ba5 100644 --- a/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs @@ -38,16 +38,6 @@ public BinaryExperimentSettings() OptimizingMetric = BinaryClassificationMetric.Accuracy; Trainers = Enum.GetValues(typeof(BinaryClassificationTrainer)).OfType().ToList(); } - - /// - /// Initializes a new instance of using the temp file location provided to the MLContext. - /// - public BinaryExperimentSettings(MLContext context) : - base(context) - { - OptimizingMetric = BinaryClassificationMetric.Accuracy; - Trainers = Enum.GetValues(typeof(BinaryClassificationTrainer)).OfType().ToList(); - } } /// diff --git a/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs b/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs index 7bbb7b061b..7d981a5462 100644 --- a/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs +++ b/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs @@ -42,13 +42,13 @@ public abstract class ExperimentSettings public CancellationToken CancellationToken { get; set; } /// - /// This is a pointer to a directory where all models trained during the AutoML experiment will be saved. + /// This is the name of the directory where all models trained during the AutoML experiment will be saved. /// If , models will be kept in memory instead of written to disk. /// (Please note: for an experiment with high runtime operating on a large dataset, opting to keep models in /// memory could cause a system to run out of memory.) /// - /// The default value is the directory named "Microsoft.ML.AutoML" in the current user's temporary folder. - public DirectoryInfo CacheDirectory { get; set; } + /// The default value is the directory named "Microsoft.ML.AutoML" in the in the location specified by the . + public string CacheDirectoryName { get; set; } /// /// Whether AutoML should cache before ML.NET trainers. @@ -66,22 +66,11 @@ public ExperimentSettings() { MaxExperimentTimeInSeconds = 24 * 60 * 60; CancellationToken = default; - CacheDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "Microsoft.ML.AutoML")); + CacheDirectoryName = "Microsoft.ML.AutoML"; CacheBeforeTrainer = CacheBeforeTrainer.Auto; MaxModels = int.MaxValue; } - /// - /// Initializes a new instance of using the temp file location provided to the MLContext. - /// - public ExperimentSettings(MLContext context) - { - MaxExperimentTimeInSeconds = 24 * 60 * 60; - CancellationToken = default; - CacheDirectory = new DirectoryInfo(Path.Combine(context.TempFilePath, "Microsoft.ML.AutoML")); - CacheBeforeTrainer = CacheBeforeTrainer.Auto; - MaxModels = int.MaxValue; - } } /// diff --git a/src/Microsoft.ML.AutoML/API/MulticlassClassificationExperiment.cs b/src/Microsoft.ML.AutoML/API/MulticlassClassificationExperiment.cs index 07e5ca61a5..6f45e6f54a 100644 --- a/src/Microsoft.ML.AutoML/API/MulticlassClassificationExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/MulticlassClassificationExperiment.cs @@ -39,16 +39,6 @@ public MulticlassExperimentSettings() OptimizingMetric = MulticlassClassificationMetric.MicroAccuracy; Trainers = Enum.GetValues(typeof(MulticlassClassificationTrainer)).OfType().ToList(); } - - /// - /// Initializes a new instances of using the temp file location provided to the MLContext. - /// - public MulticlassExperimentSettings(MLContext context) : - base(context) - { - OptimizingMetric = MulticlassClassificationMetric.MicroAccuracy; - Trainers = Enum.GetValues(typeof(MulticlassClassificationTrainer)).OfType().ToList(); - } } /// diff --git a/src/Microsoft.ML.AutoML/API/RankingExperiment.cs b/src/Microsoft.ML.AutoML/API/RankingExperiment.cs index 801b7e5816..8709c122b0 100644 --- a/src/Microsoft.ML.AutoML/API/RankingExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/RankingExperiment.cs @@ -44,17 +44,6 @@ public RankingExperimentSettings() Trainers = Enum.GetValues(typeof(RankingTrainer)).OfType().ToList(); OptimizationMetricTruncationLevel = 10; } - - /// - /// Initializes a new instance of using the temp file location provided to the MLContext. - /// - public RankingExperimentSettings(MLContext context) : - base(context) - { - OptimizingMetric = RankingMetric.Ndcg; - Trainers = Enum.GetValues(typeof(RankingTrainer)).OfType().ToList(); - OptimizationMetricTruncationLevel = 10; - } } public enum RankingMetric { diff --git a/src/Microsoft.ML.AutoML/API/RecommendationExperiment.cs b/src/Microsoft.ML.AutoML/API/RecommendationExperiment.cs index 8bafc1e8df..18f5f9ccf5 100644 --- a/src/Microsoft.ML.AutoML/API/RecommendationExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/RecommendationExperiment.cs @@ -34,16 +34,6 @@ public RecommendationExperimentSettings() OptimizingMetric = RegressionMetric.RSquared; Trainers = Enum.GetValues(typeof(RecommendationTrainer)).OfType().ToList(); } - - /// - /// Initializes a new instance of using the temp file location provided to the MLContext. - /// - public RecommendationExperimentSettings(MLContext context) : - base(context) - { - OptimizingMetric = RegressionMetric.RSquared; - Trainers = Enum.GetValues(typeof(RecommendationTrainer)).OfType().ToList(); - } } /// diff --git a/src/Microsoft.ML.AutoML/API/RegressionExperiment.cs b/src/Microsoft.ML.AutoML/API/RegressionExperiment.cs index 4be5a8870e..e86199be62 100644 --- a/src/Microsoft.ML.AutoML/API/RegressionExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/RegressionExperiment.cs @@ -39,16 +39,6 @@ public RegressionExperimentSettings() OptimizingMetric = RegressionMetric.RSquared; Trainers = Enum.GetValues(typeof(RegressionTrainer)).OfType().ToList(); } - - /// - /// Initializes a new instance of using the temp file location provided to the MLContext. - /// - public RegressionExperimentSettings(MLContext context) : - base(context) - { - OptimizingMetric = RegressionMetric.RSquared; - Trainers = Enum.GetValues(typeof(RegressionTrainer)).OfType().ToList(); - } } /// diff --git a/src/Microsoft.ML.AutoML/Experiment/Experiment.cs b/src/Microsoft.ML.AutoML/Experiment/Experiment.cs index 8792a5351d..d1462ac79b 100644 --- a/src/Microsoft.ML.AutoML/Experiment/Experiment.cs +++ b/src/Microsoft.ML.AutoML/Experiment/Experiment.cs @@ -56,7 +56,7 @@ public Experiment(MLContext context, _experimentSettings = experimentSettings; _metricsAgent = metricsAgent; _trainerAllowList = trainerAllowList; - _modelDirectory = GetModelDirectory(_experimentSettings.CacheDirectory); + _modelDirectory = GetModelDirectory(new DirectoryInfo(Path.Combine(_context.TempFilePath,_experimentSettings.CacheDirectoryName))); _datasetColumnInfo = datasetColumnInfo; _runner = runner; _logger = logger; diff --git a/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs b/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs index 59f31d9210..6f88331829 100644 --- a/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs +++ b/src/Microsoft.ML.TensorFlow/TensorflowTransform.cs @@ -143,7 +143,7 @@ private static TensorFlowTransformer Create(IHostEnvironment env, ModelLoadConte return new TensorFlowTransformer(env, LoadTFSession(env, modelBytes), outputs, inputs, null, false, addBatchDimensionInput, treatOutputAsBatched: treatOutputAsBatched); } - var tempDirPath = Path.GetFullPath(Path.Combine((env as IHostEnvironmentInternal).TempFilePath, nameof(TensorFlowTransformer) + "_" + Guid.NewGuid())); + var tempDirPath = Path.GetFullPath(Path.Combine(((IHostEnvironmentInternal)env).TempFilePath, nameof(TensorFlowTransformer) + "_" + Guid.NewGuid())); CreateFolderWithAclIfNotExists(env, tempDirPath); try { diff --git a/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs b/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs index 75f3eefc34..022c09965f 100644 --- a/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs +++ b/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs @@ -632,7 +632,7 @@ public void Dispose() internal static string GetTemporaryDirectory(IHostEnvironment env) { - string tempDirectory = Path.Combine((env as IHostEnvironmentInternal).TempFilePath, Path.GetRandomFileName()); + string tempDirectory = Path.Combine(((IHostEnvironmentInternal)env).TempFilePath, Path.GetRandomFileName()); Directory.CreateDirectory(tempDirectory); return tempDirectory; } diff --git a/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs b/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs index e28148ba7b..664436a56e 100644 --- a/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs +++ b/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs @@ -185,7 +185,7 @@ public void AutoFitRegressionTest(string culture) int maxModels = culture == "en-US" ? 1 : 75; var context = new MLContext(1); - var experimentSettings = new RegressionExperimentSettings(context) { MaxModels = maxModels }; + var experimentSettings = new RegressionExperimentSettings() { MaxModels = maxModels }; if (!Environment.Is64BitProcess) { @@ -232,7 +232,7 @@ public void AutoFitRankingTest() trainDataView = mlContext.Data.SkipRows(trainDataView, 500); // STEP 2: Run AutoML experiment - var settings = new RankingExperimentSettings(mlContext) + var settings = new RankingExperimentSettings() { MaxExperimentTimeInSeconds = 5, OptimizationMetricTruncationLevel = 3 diff --git a/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs b/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs index c275ed8445..58a46daedb 100644 --- a/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs +++ b/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs @@ -51,7 +51,7 @@ internal IEnumerable AutoFit( { case TaskType.Classification: - var mcs = new MulticlassExperimentSettings(_context) + var mcs = new MulticlassExperimentSettings() { OptimizingMetric = MulticlassClassificationMetric.MicroAccuracy, @@ -73,7 +73,7 @@ internal IEnumerable AutoFit( case TaskType.Regression: - var rs = new RegressionExperimentSettings(_context) + var rs = new RegressionExperimentSettings() { OptimizingMetric = RegressionMetric.RSquared, @@ -95,7 +95,7 @@ internal IEnumerable AutoFit( case TaskType.Recommendation: - var recommendationSettings = new RecommendationExperimentSettings (_context) + var recommendationSettings = new RecommendationExperimentSettings () { OptimizingMetric = RegressionMetric.RSquared, From cd24cc3b089db0ec518dae53a01682947593ff47 Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Wed, 12 May 2021 16:43:22 -0700 Subject: [PATCH 6/9] PR fixes. --- .../API/BinaryClassificationExperiment.cs | 1 - src/Microsoft.ML.AutoML/Experiment/Experiment.cs | 8 ++++---- src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs | 2 +- test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs | 4 ++-- .../Utils/TaskAgnosticAutoFit.cs | 6 +++--- .../ScenariosWithDirectInstantiation/TensorflowTests.cs | 2 +- 6 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs b/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs index fe527e2ba5..84f539c932 100644 --- a/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs +++ b/src/Microsoft.ML.AutoML/API/BinaryClassificationExperiment.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Linq; using Microsoft.ML.Data; -using Microsoft.ML.Runtime; using Microsoft.ML.Trainers; using Microsoft.ML.Trainers.FastTree; using Microsoft.ML.Trainers.LightGbm; diff --git a/src/Microsoft.ML.AutoML/Experiment/Experiment.cs b/src/Microsoft.ML.AutoML/Experiment/Experiment.cs index d1462ac79b..da9793b9f2 100644 --- a/src/Microsoft.ML.AutoML/Experiment/Experiment.cs +++ b/src/Microsoft.ML.AutoML/Experiment/Experiment.cs @@ -56,7 +56,7 @@ public Experiment(MLContext context, _experimentSettings = experimentSettings; _metricsAgent = metricsAgent; _trainerAllowList = trainerAllowList; - _modelDirectory = GetModelDirectory(new DirectoryInfo(Path.Combine(_context.TempFilePath,_experimentSettings.CacheDirectoryName))); + _modelDirectory = GetModelDirectory(_context.TempFilePath, _experimentSettings.CacheDirectoryName); _datasetColumnInfo = datasetColumnInfo; _runner = runner; _logger = logger; @@ -220,14 +220,14 @@ public IList Execute() return iterationResults; } - private static DirectoryInfo GetModelDirectory(DirectoryInfo rootDir) + private static DirectoryInfo GetModelDirectory(string tempDirectory, string cacheDirectoryName) { - if (rootDir == null) + if (cacheDirectoryName == null) { return null; } - var experimentDirFullPath = Path.Combine(rootDir.FullName, $"experiment_{Path.GetRandomFileName()}"); + var experimentDirFullPath = Path.Combine(tempDirectory, cacheDirectoryName, $"experiment_{Path.GetRandomFileName()}"); var experimentDirInfo = new DirectoryInfo(experimentDirFullPath); if (!experimentDirInfo.Exists) { diff --git a/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs b/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs index f602287f73..c44a0cda7e 100644 --- a/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs +++ b/src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs @@ -143,7 +143,7 @@ public OnnxVariableInfo(string name, OnnxShape shape, Type typeInOnnxRuntime, Da /// private readonly InferenceSession _session; /// - /// The location where the used ONNX model loaded from. + /// The FileStream holding onto the loaded ONNX model. /// internal FileStream ModelStream { get; } /// diff --git a/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs b/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs index 664436a56e..d5af1f1be5 100644 --- a/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs +++ b/test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs @@ -183,9 +183,8 @@ public void AutoFitRegressionTest(string culture) // Furthermore, these issues might only occur after ~70 // iterations, so setting the internal maxModels parameter. int maxModels = culture == "en-US" ? 1 : 75; - var context = new MLContext(1); - var experimentSettings = new RegressionExperimentSettings() { MaxModels = maxModels }; + var experimentSettings = new RegressionExperimentSettings { MaxModels = maxModels }; if (!Environment.Is64BitProcess) { @@ -193,6 +192,7 @@ public void AutoFitRegressionTest(string culture) experimentSettings.Trainers.Remove(RegressionTrainer.LightGbm); } + var context = new MLContext(1); var dataPath = DatasetUtil.GetMlNetGeneratedRegressionDataset(); var columnInference = context.Auto().InferColumns(dataPath, DatasetUtil.MlNetGeneratedRegressionLabel); var textLoader = context.Data.CreateTextLoader(columnInference.TextLoaderOptions); diff --git a/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs b/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs index 58a46daedb..49e91e4382 100644 --- a/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs +++ b/test/Microsoft.ML.AutoML.Tests/Utils/TaskAgnosticAutoFit.cs @@ -51,7 +51,7 @@ internal IEnumerable AutoFit( { case TaskType.Classification: - var mcs = new MulticlassExperimentSettings() + var mcs = new MulticlassExperimentSettings { OptimizingMetric = MulticlassClassificationMetric.MicroAccuracy, @@ -73,7 +73,7 @@ internal IEnumerable AutoFit( case TaskType.Regression: - var rs = new RegressionExperimentSettings() + var rs = new RegressionExperimentSettings { OptimizingMetric = RegressionMetric.RSquared, @@ -95,7 +95,7 @@ internal IEnumerable AutoFit( case TaskType.Recommendation: - var recommendationSettings = new RecommendationExperimentSettings () + var recommendationSettings = new RecommendationExperimentSettings { OptimizingMetric = RegressionMetric.RSquared, diff --git a/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs b/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs index cb67fe5244..3508cfe481 100644 --- a/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs +++ b/test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs @@ -1678,7 +1678,7 @@ internal void TensorFlowImageClassificationWithLRScheduling(LearningRateSchedule Assert.True(File.Exists(Path.Combine(options.WorkspacePath, options.TrainSetBottleneckCachedValuesFileName))); Assert.True(File.Exists(Path.Combine(options.WorkspacePath, options.ValidationSetBottleneckCachedValuesFileName))); - Assert.True(File.Exists(Path.Combine(_mlContext.TempFilePath, "MLNET", ImageClassificationTrainer.ModelFileName[options.Arch]))); + Assert.True(File.Exists(Path.Combine(Path.GetTempPath(), "MLNET", ImageClassificationTrainer.ModelFileName[options.Arch]))); (loadedModel as IDisposable)?.Dispose(); } From c03ff076aec3edecec9a813dbfb76f2ee23094d1 Mon Sep 17 00:00:00 2001 From: Michael Sharp <51342856+michaelgsharp@users.noreply.github.com> Date: Wed, 12 May 2021 16:06:45 -0700 Subject: [PATCH 7/9] Update src/Microsoft.ML.AutoML/API/ExperimentSettings.cs Co-authored-by: Eric Erhardt --- src/Microsoft.ML.AutoML/API/ExperimentSettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs b/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs index 7d981a5462..a32b0358f9 100644 --- a/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs +++ b/src/Microsoft.ML.AutoML/API/ExperimentSettings.cs @@ -60,7 +60,7 @@ public abstract class ExperimentSettings internal int MaxModels; /// - /// Initializes a new instance of using the default temp file location. + /// Initializes a new instance of . /// public ExperimentSettings() { From c341f80ac2fe67467f53d5c85e5fbe3ad4f56102 Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Wed, 12 May 2021 16:49:05 -0700 Subject: [PATCH 8/9] Tensorflow fixes from PR comments --- src/Microsoft.ML.Vision/DnnRetrainTransform.cs | 2 +- src/Microsoft.ML.Vision/ImageClassificationTrainer.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.ML.Vision/DnnRetrainTransform.cs b/src/Microsoft.ML.Vision/DnnRetrainTransform.cs index 6ebb5900e2..53a9d0c352 100644 --- a/src/Microsoft.ML.Vision/DnnRetrainTransform.cs +++ b/src/Microsoft.ML.Vision/DnnRetrainTransform.cs @@ -116,7 +116,7 @@ private static DnnRetrainTransformer Create(IHostEnvironment env, ModelLoadConte null, false, addBatchDimensionInput, 1); } - var tempDirPath = Path.GetFullPath(Path.Combine((env as IHostEnvironmentInternal).TempFilePath, nameof(DnnRetrainTransformer) + "_" + Guid.NewGuid())); + var tempDirPath = Path.GetFullPath(Path.Combine(((IHostEnvironmentInternal)env).TempFilePath, nameof(DnnRetrainTransformer) + "_" + Guid.NewGuid())); CreateFolderWithAclIfNotExists(env, tempDirPath); try { diff --git a/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs b/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs index 29b06ba7ed..9c2e173800 100644 --- a/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs +++ b/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs @@ -531,7 +531,7 @@ internal ImageClassificationTrainer(IHostEnvironment env, Options options) Host.CheckNonEmpty(options.ScoreColumnName, nameof(options.ScoreColumnName)); Host.CheckNonEmpty(options.PredictedLabelColumnName, nameof(options.PredictedLabelColumnName)); tf.compat.v1.disable_eager_execution(); - _resourcePath = Path.Combine((env as IHostEnvironmentInternal).TempFilePath, "MLNET"); + _resourcePath = Path.Combine(((IHostEnvironmentInternal)env).TempFilePath, "MLNET"); if (string.IsNullOrEmpty(options.WorkspacePath)) { @@ -1320,12 +1320,12 @@ private void AddTransferLearningLayer(string labelColumn, } - private static TensorFlowSessionWrapper LoadTensorFlowSessionFromMetaGraph(IHostEnvironment env, Architecture arch) + private TensorFlowSessionWrapper LoadTensorFlowSessionFromMetaGraph(IHostEnvironment env, Architecture arch) { var modelFileName = ModelFileName[arch]; - var modelFilePath = Path.Combine((env as IHostEnvironmentInternal).TempFilePath, "MLNET", modelFileName); + var modelFilePath = Path.Combine(_resourcePath, "MLNET", modelFileName); int timeout = 10 * 60 * 1000; - DownloadIfNeeded(env, @"meta\" + modelFileName, Path.Combine((env as IHostEnvironmentInternal).TempFilePath, "MLNET"), modelFileName, timeout); + DownloadIfNeeded(env, @"meta\" + modelFileName, Path.Combine(_resourcePath, "MLNET"), modelFileName, timeout); return new TensorFlowSessionWrapper(GetSession(env, modelFilePath, true), modelFilePath); } From 0313706490bb0b7004cb754ebc7b9e3221e37142 Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Mon, 17 May 2021 15:46:06 -0700 Subject: [PATCH 9/9] fixed filepath issues --- src/Microsoft.ML.Vision/ImageClassificationTrainer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs b/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs index 9c2e173800..7824838dea 100644 --- a/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs +++ b/src/Microsoft.ML.Vision/ImageClassificationTrainer.cs @@ -1323,9 +1323,9 @@ private void AddTransferLearningLayer(string labelColumn, private TensorFlowSessionWrapper LoadTensorFlowSessionFromMetaGraph(IHostEnvironment env, Architecture arch) { var modelFileName = ModelFileName[arch]; - var modelFilePath = Path.Combine(_resourcePath, "MLNET", modelFileName); + var modelFilePath = Path.Combine(_resourcePath, modelFileName); int timeout = 10 * 60 * 1000; - DownloadIfNeeded(env, @"meta\" + modelFileName, Path.Combine(_resourcePath, "MLNET"), modelFileName, timeout); + DownloadIfNeeded(env, @"meta\" + modelFileName, _resourcePath, modelFileName, timeout); return new TensorFlowSessionWrapper(GetSession(env, modelFilePath, true), modelFilePath); }