From e30555c834f06f145646b1ef306ff5b3ede569f7 Mon Sep 17 00:00:00 2001 From: Xiaoyun Zhang Date: Fri, 28 Apr 2023 10:27:07 -0700 Subject: [PATCH] add obj-detection automl sweeper (#6633) * add wrapper * add more options * disable format check for generated code --- .editorconfig | 5 +- .../CodeGen/estimator-schema.json | 13 +++- .../object_detection_search_space.json | 61 +++++++++++++++++++ .../CodeGen/search-space-schema.json | 15 ++++- .../CodeGen/trainer-estimators.json | 7 +++ .../Estimators/ObjectDetection.cs | 35 +++++++++++ .../SearchSpaceGenerator.cs | 2 +- 7 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 src/Microsoft.ML.AutoML/CodeGen/object_detection_search_space.json create mode 100644 src/Microsoft.ML.AutoML/SweepableEstimator/Estimators/ObjectDetection.cs diff --git a/.editorconfig b/.editorconfig index cf59606e19..ee8f4639c0 100644 --- a/.editorconfig +++ b/.editorconfig @@ -341,4 +341,7 @@ dotnet_style_readonly_field = false [test/Microsoft.ML.TestFrameworkCommon/Utility/*.cs] # IDE0073: Dont want license file header in code we are using from elsewhere dotnet_diagnostic.IDE0073.severity = none -file_header_template = unset \ No newline at end of file +file_header_template = unset + +[**/*.generated.cs] +generated_code = true diff --git a/src/Microsoft.ML.AutoML/CodeGen/estimator-schema.json b/src/Microsoft.ML.AutoML/CodeGen/estimator-schema.json index 68d731a6a1..b62d4c217f 100644 --- a/src/Microsoft.ML.AutoML/CodeGen/estimator-schema.json +++ b/src/Microsoft.ML.AutoML/CodeGen/estimator-schema.json @@ -72,7 +72,8 @@ "Naive", "ForecastBySsa", "TextClassifcation", - "SentenceSimilarity" + "SentenceSimilarity", + "ObjectDetection" ] }, "nugetDependencies": { @@ -187,7 +188,15 @@ "confidenceLevel", "variableHorizon", "modelFactory", - "sentence1ColumnName" + "sentence1ColumnName", + "boundingBoxColumnName", + "imageColumnName", + "maxEpoch", + "iOUThreshold", + "scoreThreshold", + "steps", + "initLearningRate", + "weightDecay" ] }, "argumentType": { diff --git a/src/Microsoft.ML.AutoML/CodeGen/object_detection_search_space.json b/src/Microsoft.ML.AutoML/CodeGen/object_detection_search_space.json new file mode 100644 index 0000000000..28b3a1da5c --- /dev/null +++ b/src/Microsoft.ML.AutoML/CodeGen/object_detection_search_space.json @@ -0,0 +1,61 @@ +{ + "$schema": "./search-space-schema.json#", + "name": "object_detection_option", + "search_space": [ + { + "name": "LabelColumnName", + "type": "string", + "default": "Label" + }, + { + "name": "PredictedLabelColumnName", + "type": "string", + "default": "PredictedLabel" + }, + { + "name": "PredictedBoundingBoxColumnName", + "type": "string", + "default": "PredictedBoundingBoxes" + }, + { + "name": "BoundingBoxColumnName", + "type": "string", + "default": "BoundingBoxes" + }, + { + "name": "ImageColumnName", + "type": "string", + "default": "Image" + }, + { + "name": "ScoreColumnName", + "type": "string", + "default": "Score" + }, + { + "name": "MaxEpoch", + "type": "integer", + "default": 10 + }, + { + "name": "InitLearningRate", + "type": "double", + "default": 1.0 + }, + { + "name": "WeightDecay", + "type": "double", + "default": 0.0 + }, + { + "name": "IOUThreshold", + "type": "double", + "default": 0.5 + }, + { + "name": "ScoreThreshold", + "type": "double", + "default": 0.5 + } + ] +} diff --git a/src/Microsoft.ML.AutoML/CodeGen/search-space-schema.json b/src/Microsoft.ML.AutoML/CodeGen/search-space-schema.json index 0fa6fa9590..b4c1a5465e 100644 --- a/src/Microsoft.ML.AutoML/CodeGen/search-space-schema.json +++ b/src/Microsoft.ML.AutoML/CodeGen/search-space-schema.json @@ -145,7 +145,8 @@ "matrix_factorization_option", "dnn_featurizer_image_option", "text_classification_option", - "sentence_similarity_option" + "sentence_similarity_option", + "object_detection_option" ] }, "option_name": { @@ -198,7 +199,17 @@ "Epoch", "Architecture", "AddKeyValueAnnotationsAsText", - "Arch" + "Arch", + "PredictedLabelColumnName", + "PredictedBoundingBoxColumnName", + "BoundingBoxColumnName", + "ImageColumnName", + "IOUThreshold", + "ScoreThreshold", + "Steps", + "MaxEpoch", + "InitLearningRate", + "WeightDecay" ] }, "option_type": { diff --git a/src/Microsoft.ML.AutoML/CodeGen/trainer-estimators.json b/src/Microsoft.ML.AutoML/CodeGen/trainer-estimators.json index fae486ca09..9eab12f286 100644 --- a/src/Microsoft.ML.AutoML/CodeGen/trainer-estimators.json +++ b/src/Microsoft.ML.AutoML/CodeGen/trainer-estimators.json @@ -525,6 +525,13 @@ "usingStatements": [ "Microsoft.ML", "Microsoft.ML.Trainers", "Microsoft.ML.TorchSharp" ], "searchOption": "sentence_similarity_option" }, + { + "functionName": "ObjectDetection", + "estimatorTypes": [ "MultiClassification" ], + "nugetDependencies": [ "Microsoft.ML", "Microsoft.ML.TorchSharp" ], + "usingStatements": [ "Microsoft.ML", "Microsoft.ML.Trainers", "Microsoft.ML.TorchSharp" ], + "searchOption": "object_detection_option" + }, { "functionName": "ForecastBySsa", "estimatorTypes": [ "Forecasting" ], diff --git a/src/Microsoft.ML.AutoML/SweepableEstimator/Estimators/ObjectDetection.cs b/src/Microsoft.ML.AutoML/SweepableEstimator/Estimators/ObjectDetection.cs new file mode 100644 index 0000000000..c9ac24737e --- /dev/null +++ b/src/Microsoft.ML.AutoML/SweepableEstimator/Estimators/ObjectDetection.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.ML.TorchSharp; +using Microsoft.ML.TorchSharp.AutoFormerV2; + +namespace Microsoft.ML.AutoML.CodeGen +{ + internal partial class ObjectDetectionMulti + { + public override IEstimator BuildFromOption(MLContext context, ObjectDetectionOption param) + { + var option = new ObjectDetectionTrainer.Options + { + LabelColumnName = param.LabelColumnName, + PredictedLabelColumnName = param.PredictedLabelColumnName, + BoundingBoxColumnName = param.BoundingBoxColumnName, + ImageColumnName = param.ImageColumnName, + ScoreColumnName = param.ScoreColumnName, + MaxEpoch = param.MaxEpoch, + InitLearningRate = param.InitLearningRate, + WeightDecay = param.WeightDecay, + PredictedBoundingBoxColumnName = param.PredictedBoundingBoxColumnName, + ScoreThreshold = param.ScoreThreshold, + IOUThreshold = param.IOUThreshold, + }; + + return context.MulticlassClassification.Trainers.ObjectDetection(option); + } + } +} diff --git a/tools-local/Microsoft.ML.AutoML.SourceGenerator/SearchSpaceGenerator.cs b/tools-local/Microsoft.ML.AutoML.SourceGenerator/SearchSpaceGenerator.cs index af19e405c2..5480d683fa 100644 --- a/tools-local/Microsoft.ML.AutoML.SourceGenerator/SearchSpaceGenerator.cs +++ b/tools-local/Microsoft.ML.AutoML.SourceGenerator/SearchSpaceGenerator.cs @@ -128,7 +128,7 @@ public void Execute(GeneratorExecutionContext context) Properties = options, }.TransformText(); - context.AddSource($"{className}.cs", code); + context.AddSource($"{className}.generated.cs", code); } }