Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
34d970f
Buffer re-use using ArrayPool and a few more checks (#4293)
harshithapv Oct 4, 2019
9bc3d7b
Image Classification API: Fix processing incomplete batch(<batchSize)…
ashbhandare Oct 4, 2019
c073e6b
upgrade to 3.1
LittleLittleCloud Apr 28, 2020
8f0fc1a
write inline data using invariantCulture
LittleLittleCloud Apr 28, 2020
e96d716
Merge branch 'master' of https://github.com/dotnet/machinelearning
LittleLittleCloud Apr 29, 2020
8c17bbe
Merge branch 'master' of https://github.com/dotnet/machinelearning
LittleLittleCloud May 14, 2020
b2947f5
Merge branch 'master' of https://github.com/dotnet/machinelearning
LittleLittleCloud May 18, 2020
ff1c909
Merge branch 'master' of https://github.com/dotnet/machinelearning
LittleLittleCloud May 20, 2020
c8f154b
Update: ModelBuilder codegen for Object Detection
ttstanley Jun 2, 2020
7da13e8
Merge branch 'u/tevin/ObjectDetectionCodeGen'
ttstanley Jun 2, 2020
8cedee5
updated testing files to give better test results
ttstanley Jun 2, 2020
44f18ef
Refactored test code
ttstanley Jun 3, 2020
eed5d85
trying to test performance
ttstanley Jun 3, 2020
045f93a
fix commit
Jun 8, 2020
c164c97
Adding changes from prior commit
ttstanley Jun 9, 2020
8d47c48
small changes to finilize codegen
ttstanley Jun 9, 2020
9226395
Made changes based on csproj
ttstanley Jun 10, 2020
b399a27
minor changes to make final build
ttstanley Jun 10, 2020
ce35740
updated onnxruntime to 1.3
ttstanley Jun 10, 2020
7ec0c2b
targetting older automl
ttstanley Jun 10, 2020
0fe683e
taking out dependency on automl taskkind for OD
ttstanley Jun 11, 2020
b7556da
final build got predictions working for OD
ttstanley Jun 11, 2020
737d5a7
Merge upstream into remote origin
ttstanley Jun 12, 2020
aa89ddf
Merge branch 'real/origin/master' into u/tevin/CodeGen
ttstanley Jun 12, 2020
f1aa8ac
took out old test paths to generalize tests
ttstanley Jun 12, 2020
93eaf54
cleaning up outdated comments
ttstanley Jun 15, 2020
2c03cf8
for the build packaging
ttstanley Jun 15, 2020
9f784f4
rebuild
ttstanley Jun 16, 2020
dad9055
Merge branch 'master' of https://github.com/dotnet/machinelearning
LittleLittleCloud Jul 20, 2020
741d77a
Merge branch 'master' of https://github.com/dotnet/machinelearning
LittleLittleCloud Jul 21, 2020
7f42788
Merge branch 'master' into u/tevin/CodeGen
LittleLittleCloud Jul 21, 2020
dad6672
fix tests
LittleLittleCloud Jul 21, 2020
45c462a
fix build error
LittleLittleCloud Jul 21, 2020
445f982
fix e2e bug
LittleLittleCloud Jul 22, 2020
594c828
Merge branch 'master' of https://github.com/dotnet/machinelearning
LittleLittleCloud Aug 26, 2020
a7d20ac
Merge branch 'master' into u/tevin/CodeGen
LittleLittleCloud Aug 26, 2020
82c672f
fix e2e bug
LittleLittleCloud Aug 26, 2020
9116501
Update Microsoft.ML.CodeGenerator.nupkgproj
LittleLittleCloud Aug 27, 2020
9473c48
Update Microsoft.ML.CodeGenerator.csproj
LittleLittleCloud Aug 27, 2020
ccc5d6c
remove .approved.txt that not used
LittleLittleCloud Aug 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Microsoft.ML.AutoML/TaskKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal enum TaskKind
MulticlassClassification,
Regression,
Recommendation,
ObjectDetection,
Ranking
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ public AzureAttachConsoleAppCodeGenerator(Pipeline pipeline, ColumnInferenceResu
IncludeMklComponentsPackage = false,
IncludeLightGBMPackage = false,
IncludeFastTreePackage = false,
IncludeImageTransformerPackage = _settings.IsImage,
IncludeImageTransformerPackage = _settings.IsImage || _settings.IsObjectDetection,
IncludeImageClassificationPackage = false,
IncludeOnnxPackage = true,
IncludeOnnxRuntime = _settings.IsObjectDetection,
IncludeResNet18Package = false,
IncludeRecommenderPackage = false,
StablePackageVersion = _settings.StablePackageVersion,
Expand All @@ -94,6 +95,7 @@ public AzureAttachConsoleAppCodeGenerator(Pipeline pipeline, ColumnInferenceResu
Separator = _columnInferenceResult.TextLoaderOptions.Separators.FirstOrDefault(),
Target = _settings.Target,
SampleData = sampleResult,
IsObjectDetection = _settings.IsObjectDetection,
}.TransformText(),
Name = "Program.cs",
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ internal class AzureAttachModelCodeGenerator : ICSharpProjectGenerator
private readonly string _nameSpaceValue;

public ICSharpFile ModelInputClass { get; private set; }
public ICSharpFile ModelOutputClass { get; private set; }
public ICSharpFile AzureImageModelOutputClass { get; private set; }
public ICSharpFile AzureObjectDetectionModelOutputClass { get; private set; }
public ICSharpFile ModelProject { get; private set; }
public ICSharpFile ConsumeModel { get; private set; }
public ICSharpFile LabelMapping { get; private set; }
public ICSharpFile ImageLabelMapping { get; private set; }
public ICSharpFile ObjectDetectionConsumeModel { get; private set; }
public string Name { get; set; }

public AzureAttachModelCodeGenerator(Pipeline pipeline, ColumnInferenceResults columnInferenceResults, CodeGeneratorSettings options, string namespaceValue)
Expand All @@ -53,7 +56,7 @@ public AzureAttachModelCodeGenerator(Pipeline pipeline, ColumnInferenceResults c
var labelType = _columnInferenceResult.TextLoaderOptions.Columns.Where(t => t.Name == _settings.LabelName).First().DataKind;
Type labelTypeCsharp = Utils.GetCSharpType(labelType);

ModelOutputClass = new CSharpCodeFile()
AzureImageModelOutputClass = new CSharpCodeFile()
{
File = new AzureImageModelOutputClass()
{
Expand All @@ -64,6 +67,17 @@ public AzureAttachModelCodeGenerator(Pipeline pipeline, ColumnInferenceResults c
Name = "ModelOutput.cs",
};

AzureObjectDetectionModelOutputClass = new CSharpCodeFile()
{
File = new AzureObjectDetectionModelOutputClass()
{
Namespace = _nameSpaceValue,
Target = _settings.Target,
Labels = _settings.ObjectLabel,
}.TransformText(),
Name = "ModelOutput.cs",
};

ModelProject = new CSharpProjectFile()
{
File = new ModelProject()
Expand All @@ -74,6 +88,7 @@ public AzureAttachModelCodeGenerator(Pipeline pipeline, ColumnInferenceResults c
IncludeLightGBMPackage = false,
IncludeMklComponentsPackage = false,
IncludeOnnxModel = true,
IncludeOnnxRuntime = _settings.IsObjectDetection,
IncludeRecommenderPackage = false,
StablePackageVersion = _settings.StablePackageVersion,
UnstablePackageVersion = _settings.UnstablePackageVersion,
Expand All @@ -83,19 +98,6 @@ public AzureAttachModelCodeGenerator(Pipeline pipeline, ColumnInferenceResults c
Name = $"{ _settings.OutputName }.Model.csproj",
};

LabelMapping = new CSharpCodeFile()
{
File = new LabelMapping()
{
Target = _settings.Target,
Namespace = _nameSpaceValue,
LabelMappingInputLabelType = typeof(Int64).Name,
PredictionLabelType = labelTypeCsharp.Name,
TaskType = _settings.MlTask.ToString(),
}.TransformText(),
Name = "LabelMapping.cs",
};

ConsumeModel = new CSharpCodeFile()
{
File = new ConsumeModel()
Expand All @@ -105,6 +107,7 @@ public AzureAttachModelCodeGenerator(Pipeline pipeline, ColumnInferenceResults c
MLNetModelName = _settings.ModelName,
OnnxModelName = _settings.OnnxModelName,
IsAzureImage = _settings.IsAzureAttach && _settings.IsImage,
IsAzureObjectDetection = _settings.IsObjectDetection && _settings.IsAzureAttach,
}.TransformText(),
Name = "ConsumeModel.cs",
};
Expand All @@ -118,7 +121,16 @@ public ICSharpProject ToProject()
project = new CSharpProject()
{
ModelInputClass,
ModelOutputClass,
AzureImageModelOutputClass,
ConsumeModel,
ModelProject,
};
} else if(_settings.IsObjectDetection)
{
project = new CSharpProject()
{
ModelInputClass,
AzureObjectDetectionModelOutputClass,
ConsumeModel,
ModelProject,
};
Expand All @@ -128,10 +140,9 @@ public ICSharpProject ToProject()
project = new CSharpProject()
{
ModelInputClass,
ModelOutputClass,
AzureImageModelOutputClass,
ConsumeModel,
ModelProject,
LabelMapping,
};
}
project.Name = Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public CodeGeneratorSettings()
Target = GenerateTarget.Cli;
OnnxInputMapping = new Dictionary<string, ColumnMapping>();
ClassificationLabel = new string[] { };
ObjectLabel = new string[] { };
}

public string LabelName { get; set; }
Expand All @@ -37,6 +38,8 @@ public CodeGeneratorSettings()
/// </summary>
public string[] ClassificationLabel { get; set; }

public string[] ObjectLabel { get; set; }

public string OutputName { get; set; }

public string OutputBaseDir { get; set; }
Expand All @@ -57,6 +60,8 @@ public CodeGeneratorSettings()

public bool IsImage { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

IsImageClassification?


public bool IsObjectDetection { get; set; }

public IDictionary<string, ColumnMapping> OnnxInputMapping { get; set; }

internal TaskKind MlTask { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ internal enum SpecialTransformer
ApplyOnnxModel = 0,
ResizeImage = 1,
ExtractPixel = 2,
ObjectDetectionResizeImage = 3,
}

internal static class TransformGeneratorFactory
{
internal static ITransformGenerator GetInstance(PipelineNode node)
Expand Down Expand Up @@ -78,7 +80,7 @@ internal static ITransformGenerator GetInstance(PipelineNode node)
}
}

// For AzureAttach
// For the AzureAttach
if (Enum.TryParse(node.Name, out SpecialTransformer transformer))
{
switch (transformer)
Expand All @@ -92,6 +94,9 @@ internal static ITransformGenerator GetInstance(PipelineNode node)
case SpecialTransformer.ApplyOnnxModel:
result = new ApplyOnnxModel(node);
break;
case SpecialTransformer.ObjectDetectionResizeImage:
result = new ObjectDetectionImageResizing(node);
break;
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,17 @@ public override string GenerateTransformer()
}
}

internal class ObjectDetectionImageResizing : TransformGeneratorBase
{
public ObjectDetectionImageResizing(PipelineNode node) : base(node) { }
internal override string MethodName => "ResizeImages";

public override string GenerateTransformer()
{
return @"ResizeImages(outputColumnName: ""ImageSource_featurized"", imageWidth: 800, imageHeight: 600, inputColumnName: ""ImageSource_featurized"")";
}
}

internal class PixelExtract : TransformGeneratorBase
{
public PixelExtract(PipelineNode node) : base(node) { }
Expand Down
23 changes: 12 additions & 11 deletions src/Microsoft.ML.CodeGenerator/Microsoft.ML.CodeGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@
<Generator>TextTemplatingFilePreprocessor</Generator>
<LastGenOutput>AzureModelBuilder.cs</LastGenOutput>
</None>
<None Update="Templates\Azure\Model\AzureImageModelOutputClass.tt">
<LastGenOutput>AzureImageModelOutputClass.cs</LastGenOutput>
<None Update="Templates\Azure\Model\AzureObjectDetectionModelOutputClass.tt">
<LastGenOutput>AzureObjectDetectionModelOutputClass.cs</LastGenOutput>
<Generator>TextTemplatingFilePreprocessor</Generator>
</None>
<None Update="Templates\Azure\Model\OnnxModelOutputClass.tt">
<Generator>TextTemplatingFilePreprocessor</Generator>
<None Update="Templates\Azure\Model\AzureImageModelOutputClass.tt">
<LastGenOutput>AzureImageModelOutputClass.cs</LastGenOutput>
</None>
<None Update="Templates\Azure\Model\LabelMapping.tt">
<Generator>TextTemplatingFilePreprocessor</Generator>
<LastGenOutput>LabelMapping.cs</LastGenOutput>
</None>
<None Update="Templates\Console\Annotation.ttinclude">
</None>
Expand Down Expand Up @@ -80,8 +76,8 @@
<AutoGen>True</AutoGen>
<DependentUpon>AzureModelBuilder.tt</DependentUpon>
</Compile>
<Compile Update="Templates\Azure\Model\OnnxModelOutputClass - Copy.cs">
<DependentUpon>OnnxModelOutputClass.tt</DependentUpon>
<Compile Update="Templates\Azure\Model\AzureImageModelOutputClass - Copy.cs">
<DependentUpon>AzureImageModelOutputClass.tt</DependentUpon>
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
Expand All @@ -90,10 +86,15 @@
<AutoGen>True</AutoGen>
<DependentUpon>AzureImageModelOutputClass.tt</DependentUpon>
</Compile>
<Compile Update="Templates\Azure\Model\LabelMapping.cs">
<Compile Update="Templates\Azure\Model\OnnxModelOutputClass - Copy.cs">
<DependentUpon>OnnxModelOutputClass.tt</DependentUpon>
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
<Compile Update="Templates\Azure\Model\AzureObjectDetectionModelOutputClass.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>LabelMapping.tt</DependentUpon>
<DependentUpon>AzureObjectDetectionModelOutputClass.tt</DependentUpon>
</Compile>
<Compile Update="Templates\Console\ConsumeModel.cs">
<DesignTime>True</DesignTime>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Microsoft.ML.CodeGenerator.Templates.Azure.Model
/// Class to produce the template output
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "16.0.0.0")]
internal partial class LabelMapping : LabelMappingBase
internal partial class AzureObjectDetectionModelOutputClass : AzureObjectDetectionModelOutputClassBase
{
/// <summary>
/// Create the template output
Expand All @@ -30,72 +30,47 @@ public virtual string TransformText()
} else if(Target == CSharp.GenerateTarget.ModelBuilder){
MB_Annotation();
}
this.Write("\r\nusing Microsoft.ML.Data;\r\nusing Microsoft.ML.Transforms;\r\nusing System;\r\nusing " +
"System.Linq;\r\n\r\nnamespace ");
this.Write("\r\nusing System;\r\nusing Microsoft.ML.Data;\r\nusing System.Collections.Generic;\r\nusi" +
"ng System.Linq;\r\n\r\nnamespace ");
this.Write(this.ToStringHelper.ToStringWithCulture(Namespace));
this.Write(@".Model
{
[CustomMappingFactoryAttribute(nameof(LabelMapping))]
public class LabelMapping : CustomMappingFactory<LabelMappingInput, LabelMappingOutput>
{
// Custom mapping to determine the label with the highest probability
public static void Mapping(LabelMappingInput input, LabelMappingOutput output)
{
");
if("Boolean".Equals(PredictionLabelType)){
this.Write("\t\t\toutput.Prediction = input.label.GetValues().ToArray().First() == 1;\r\n");
} else{
this.Write(" output.Prediction = input.label.GetValues().ToArray().First();\r\n");
}
this.Write("\r\n");
if("MulticlassClassification".Equals(TaskType)){
this.Write(" output.Score = input.probabilities.GetValues().ToArray();\r\n");
} else {
this.Write(" output.Score = input.probabilities.GetValues().ToArray().First();\r\n");
}
this.Write(@" }
// Factory method called when loading the model to get the mapping operation
public override Action<LabelMappingInput, LabelMappingOutput> GetMapping()
{
return Mapping;
}
}
public class LabelMappingInput
{
[ColumnName(""label"")]
public VBuffer<Int64> label;

[ColumnName(""probabilities"")]
public VBuffer<float> probabilities;
}
public class LabelMappingOutput
{
");
if("BinaryClassification".Equals(TaskType)){
this.Write(" // ColumnName attribute is used to change the column name from\r\n\t\t// its " +
"default value, which is the name of the field\r\n [ColumnName(\"PredictedLab" +
"el\")]\r\n public bool Prediction { get; set; }\r\n");
} if("MulticlassClassification".Equals(TaskType)){
this.Write(" // ColumnName attribute is used to change the column name from\r\n\t\t// its " +
"default value, which is the name of the field\r\n [ColumnName(\"PredictedLab" +
"el\")]\r\n public ");
this.Write(this.ToStringHelper.ToStringWithCulture(PredictionLabelType));
this.Write(" Prediction { get; set; }\r\n");
}
if("MulticlassClassification".Equals(TaskType)){
this.Write(" public float[] Score { get; set; }\r\n");
}else{
this.Write(" public float Score { get; set; }\r\n");
this.Write(".Model\r\n{\r\n public class ModelOutput\r\n {\r\n public string[] ObjectTag" +
"s = new string[]{");
foreach(var label in Labels){
this.Write("\"");
this.Write(this.ToStringHelper.ToStringWithCulture(label));
this.Write("\",");
}
this.Write(" }\r\n}\r\n");
this.Write("};\r\n\r\n [ColumnName(\"boxes\")]\r\n public float[] Boxes { get; set; }\r\n" +
"\r\n [ColumnName(\"labels\")]\r\n public long[] Labels { get; set; }\r\n\r\n" +
" [ColumnName(\"scores\")]\r\n public float[] Scores { get; set; }\r\n\r\n " +
" private BoundingBox[] BoundingBoxes\r\n {\r\n get\r\n " +
" {\r\n var boundingBoxes = new List<BoundingBox>();\r\n\r\n " +
" boundingBoxes = Enumerable.Range(0, this.Labels.Length)\r\n " +
" .Select((index) =>\r\n {\r\n " +
" var boxes = this.Boxes;\r\n var scores = thi" +
"s.Scores;\r\n var labels = this.Labels;\r\n\r\n " +
" return new BoundingBox()\r\n {\r\n " +
" Left = boxes[index * 4],\r\n " +
" Top = boxes[(index * 4) + 1],\r\n Ri" +
"ght = boxes[(index * 4) + 2],\r\n Bottom = boxes[" +
"(index * 4) + 3],\r\n Score = scores[index],\r\n " +
" Label = this.ObjectTags[labels[index]],\r\n " +
" };\r\n }).ToList();\r\n " +
" return boundingBoxes.ToArray();\r\n }\r\n }\r\n\r\n public ov" +
"erride string ToString()\r\n {\r\n return string.Join(\"\\n\", Boundi" +
"ngBoxes.Select(x => x.ToString()));\r\n }\r\n }\r\n\r\n public class Boundi" +
"ngBox\r\n {\r\n public float Top;\r\n\r\n public float Left;\r\n\r\n " +
" public float Right;\r\n\r\n public float Bottom;\r\n\r\n public string La" +
"bel;\r\n\r\n public float Score;\r\n\r\n public override string ToString()" +
"\r\n {\r\n return $\"Top: {this.Top}, Left: {this.Left}, Right: {th" +
"is.Right}, Bottom: {this.Bottom}, Label: {this.Label}, Score: {this.Score}\";\r\n " +
" }\r\n }\r\n}\r\n");
return this.GenerationEnvironment.ToString();
}

public string Namespace {get;set;}
internal CSharp.GenerateTarget Target {get;set;}
public string TaskType {get;set;}
public string PredictionLabelType {get;set;}
public string LabelMappingInputLabelType {get;set;}
public string[] Labels {get; set;} = new string[0];


void CLI_Annotation()
Expand Down Expand Up @@ -124,7 +99,7 @@ void MB_Annotation()
/// Base class for this transformation
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "16.0.0.0")]
internal class LabelMappingBase
internal class AzureObjectDetectionModelOutputClassBase
{
#region Fields
private global::System.Text.StringBuilder generationEnvironmentField;
Expand Down
Loading