|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.IO; |
| 7 | +using Microsoft.Data.DataView; |
| 8 | +using Microsoft.ML.Auto; |
| 9 | +using Microsoft.ML.CLI.CodeGenerator.CSharp; |
| 10 | +using Microsoft.ML.CLI.Data; |
| 11 | +using Microsoft.ML.CLI.Utilities; |
| 12 | +using Microsoft.ML.Data; |
| 13 | +using NLog; |
| 14 | + |
| 15 | +namespace Microsoft.ML.CLI.CodeGenerator |
| 16 | +{ |
| 17 | + internal class CodeGenerationHelper |
| 18 | + { |
| 19 | + |
| 20 | + private IAutoMLEngine automlEngine; |
| 21 | + private NewCommandSettings settings; |
| 22 | + private static Logger logger = LogManager.GetCurrentClassLogger(); |
| 23 | + private TaskKind taskKind; |
| 24 | + public CodeGenerationHelper(IAutoMLEngine automlEngine, NewCommandSettings settings) |
| 25 | + { |
| 26 | + this.automlEngine = automlEngine; |
| 27 | + this.settings = settings; |
| 28 | + this.taskKind = Utils.GetTaskKind(settings.MlTask); |
| 29 | + } |
| 30 | + |
| 31 | + public void GenerateCode() |
| 32 | + { |
| 33 | + var context = new MLContext(); |
| 34 | + |
| 35 | + // Infer columns |
| 36 | + ColumnInferenceResults columnInference = null; |
| 37 | + try |
| 38 | + { |
| 39 | + columnInference = automlEngine.InferColumns(context); |
| 40 | + } |
| 41 | + catch (Exception e) |
| 42 | + { |
| 43 | + logger.Log(LogLevel.Error, $"{Strings.InferColumnError}"); |
| 44 | + logger.Log(LogLevel.Error, e.Message); |
| 45 | + logger.Log(LogLevel.Debug, e.ToString()); |
| 46 | + logger.Log(LogLevel.Error, Strings.Exiting); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // Sanitize columns |
| 51 | + Array.ForEach(columnInference.TextLoaderOptions.Columns, t => t.Name = Utils.Sanitize(t.Name)); |
| 52 | + |
| 53 | + var sanitizedLabelName = Utils.Sanitize(columnInference.ColumnInformation.LabelColumn); |
| 54 | + |
| 55 | + // Load data |
| 56 | + (IDataView trainData, IDataView validationData) = LoadData(context, columnInference.TextLoaderOptions); |
| 57 | + |
| 58 | + // Explore the models |
| 59 | + (Pipeline, ITransformer) result = default; |
| 60 | + Console.WriteLine($"{Strings.ExplorePipeline}: {settings.MlTask}"); |
| 61 | + try |
| 62 | + { |
| 63 | + result = automlEngine.ExploreModels(context, trainData, validationData, sanitizedLabelName); |
| 64 | + } |
| 65 | + catch (Exception e) |
| 66 | + { |
| 67 | + logger.Log(LogLevel.Error, $"{Strings.ExplorePipelineException}:"); |
| 68 | + logger.Log(LogLevel.Error, e.Message); |
| 69 | + logger.Log(LogLevel.Debug, e.ToString()); |
| 70 | + logger.Log(LogLevel.Error, Strings.Exiting); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + //Get the best pipeline |
| 75 | + Pipeline pipeline = null; |
| 76 | + pipeline = result.Item1; |
| 77 | + var model = result.Item2; |
| 78 | + |
| 79 | + // Save the model |
| 80 | + logger.Log(LogLevel.Info, Strings.SavingBestModel); |
| 81 | + var modelPath = new FileInfo(Path.Combine(settings.OutputPath.FullName, "model.zip")); |
| 82 | + Utils.SaveModel(model, modelPath, context); |
| 83 | + |
| 84 | + // Generate the Project |
| 85 | + GenerateProject(columnInference, pipeline, sanitizedLabelName, modelPath); |
| 86 | + } |
| 87 | + |
| 88 | + internal void GenerateProject(ColumnInferenceResults columnInference, Pipeline pipeline, string labelName, FileInfo modelPath) |
| 89 | + { |
| 90 | + //Generate code |
| 91 | + logger.Log(LogLevel.Info, $"{Strings.GenerateProject} : {settings.OutputPath.FullName}"); |
| 92 | + var codeGenerator = new CodeGenerator.CSharp.CodeGenerator( |
| 93 | + pipeline, |
| 94 | + columnInference, |
| 95 | + new CodeGeneratorSettings() |
| 96 | + { |
| 97 | + TrainDataset = settings.Dataset.FullName, |
| 98 | + MlTask = taskKind, |
| 99 | + TestDataset = settings.TestDataset?.FullName, |
| 100 | + OutputName = settings.Name, |
| 101 | + OutputBaseDir = settings.OutputPath.FullName, |
| 102 | + LabelName = labelName, |
| 103 | + ModelPath = modelPath.FullName |
| 104 | + }); |
| 105 | + codeGenerator.GenerateOutput(); |
| 106 | + } |
| 107 | + |
| 108 | + internal (IDataView, IDataView) LoadData(MLContext context, TextLoader.Options textLoaderOptions) |
| 109 | + { |
| 110 | + logger.Log(LogLevel.Info, Strings.CreateDataLoader); |
| 111 | + var textLoader = context.Data.CreateTextLoader(textLoaderOptions); |
| 112 | + |
| 113 | + logger.Log(LogLevel.Info, Strings.LoadData); |
| 114 | + var trainData = textLoader.Load(settings.Dataset.FullName); |
| 115 | + var validationData = settings.ValidationDataset == null ? null : textLoader.Load(settings.ValidationDataset.FullName); |
| 116 | + |
| 117 | + return (trainData, validationData); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments