8000 add sample for model builder object detection tutorial (#882) · dotnet/machinelearning-samples@d7a7c88 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7a7c88

Browse files
Bri AchtmanYoussef1313
Bri Achtman
andauthored
add sample for model builder object detection tutorial (#882)
* add files * remove unnecessary using statement * remove console app * remove onnx * add download model * update comment * update csproj to download model (aka link) on build * remove download model method * remove console app * remove unused code * add readme for samples browser * change readme to md * remove ml-dotnet, add visual-studio * Update README.md * Update README.md Co-authored-by: Youssef Victor <youssefvictor00@gmail.com>
1 parent 81d8bc0 commit d7a7c88

File tree

11 files changed

+275
-1
lines changed

11 files changed

+275
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,7 @@ ASALocalRun/
327327
/test/data/external/
328328

329329
# macOS
330-
.DS_Store
330+
.DS_Store
331+
samples/modelbuilder/ObjectDetection_StopSigns/StopSignDetectionML.Model/MLModel.zip
332+
samples/modelbuilder/ObjectDetection_StopSigns/StopSignDetectionML.Model/bestModel.onnx
333+
samples/modelbuilder/ObjectDetection_StopSigns/assets.zip
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30611.23
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StopSignDetection", "StopSignDetection\StopSignDetection.csproj", "{88B7BCEF-6961-4C8F-A151-604F001514AD}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StopSignDetectionML.Model", "StopSignDetectionML.Model\StopSignDetectionML.Model.csproj", "{B7208A41-133D-4BC8-B0E6-C8622CB1DE8B}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{88B7BCEF-6961-4C8F-A151-604F001514AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{88B7BCEF-6961-4C8F-A151-604F001514AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{88B7BCEF-6961-4C8F-A151-604F001514AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{88B7BCEF-6961-4C8F-A151-604F001514AD}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{B7208A41-133D-4BC8-B0E6-C8622CB1DE8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{B7208A41-133D-4BC8-B0E6-C8622CB1DE8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{B7208A41-133D-4BC8-B0E6-C8622CB1DE8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{B7208A41-133D-4BC8-B0E6-C8622CB1DE8B}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {693C7232-4883-40F2-BC7F-CA7BB68CD09B}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using StopSignDetectionML.Model;
3+
4+
namespace StopSignDetection
5+
{
6+
class Program
7+
{
8+
static voi B41A d Main(string[] args)
9+
{
10+
var input = new ModelInput()
11+
{
12+
ImageSource = "test-image1.jpeg"
13+
};
14+
15+
ModelOutput output = ConsumeModel.Predict(input);
16+
17+
Console.WriteLine("\n\nPredicted Boxes:\n");
18+
Console.WriteLine(output);
19+
}
20+
}
21+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
page_type: sample
3+
name: "ML.NET Model Builder Stop Sign Object Detection"
4+
description: "Use ML.NET Model Builder to train a deep learning object detection model in Azure."
5+
urlFragment: "mlnet-object-detection-model-builder"
6+
languages:
7+
- csharp
8+
products:
9+
- dotnet
10+
- dotnet-core
11+
- visual-studio
12+
---
13+
14+
# ML.NET Model Builder Stop Sign Object Detection
15+
16+
This sample relates to the [ML.NET Model Builder Object Detection tutorial](https://docs.microsoft.com/dotnet/machine-learning/tutorials/object-detection-model-builder).
17+
18+
## Contents
19+
20+
| Project | Description |
21+
|-----------------------------|-------------------------------------------------------------|
22+
| `StopSignDetection` | Console app that consumes the trained model. |
23+
| `StopSignDetectionML.Model` | Class library that contains the model and consumption code. |
24+
25+
The test image `test-image1.jpeg` is from [pexels](https://www.pexels.com/photo/red-stop-sign-39080/).
26+
27+
## Prerequisites
28+
29+
- Visual Studio 2019 16.6.1 or later
30+
- .NET workload installed with ML.NET Model Builder component checked
31+
- ML.NET Model Builder Preview Feature enabled
32+
33+
You can see Model Builder set up instructions in this [tutorial](https://dotnet.microsoft.com/learn/ml-dotnet/get-started-tutorial/install).
34+
35+
## Running the sample
36+
37+
You can either:
38+
39+
1. Follow along with the tutorial and have Model Builder generate the code for you, or
40+
2. Clone this sample which already contains the necessary code and run the `StopSignDetection` console app to consume a trained ML.NET object detection model.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="1.5.0" />
10+
</ItemGroup>
11+
EF5E
12+
<ItemGroup>
13+
<ProjectReference Include="..\StopSignDetectionML.Model\StopSignDetectionML.Model.csproj" />
14+
</ItemGroup>
15+
16+
<Target Name="DownloadModelFiles" BeforeTargets="Build">
17+
<DownloadFile
18+
SourceUrl="https://aka.ms/mlnet-object-detection-tutorial-model"
19+
SkipUnchangedFiles="true"
20+
DestinationFolder="$(OutDir)">
21+
</DownloadFile>
22+
</Target>
23+
24+
<ItemGroup>
25+
<None Update="test-image1.jpeg">
26+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
27+
</None>
28+
</ItemGroup>
29+
30+
</Project>
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This file was auto-generated by ML.NET Model Builder.
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.IO;
8+
using Microsoft.ML;
9+
using StopSignDetectionML.Model;
10+
11+
namespace StopSignDetectionML.Model
12+
{
13+
public class ConsumeModel
14+
{
15+
private static Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictionEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(CreatePredictionEngine);
16+
17+
public static string MLNetModelPath = "MLModel.zip";
18+
19+
// For more info on consuming ML.NET models, visit https://aka.ms/mlnet-consume
20+
// Method for consuming model in your app
21+
public static ModelOutput Predict(ModelInput input)
22+
{
23+
ModelOutput result = PredictionEngine.Value.Predict(input);
24+
return result;
25+
}
26+
27+
public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
28+
{
29+
// Create new MLContext
30+
MLContext mlContext = new MLContext();
31+
32+
// Load model & create prediction engine
33+
ITransformer mlModel = mlContext.Model.Load(MLNetModelPath, out var modelInputSchema);
34+
35+
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
36+
37+
return predEngine;
38+
}
39+
}
40+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This file was auto-generated by ML.NET Model Builder.
2+
3+
using Microsoft.ML.Data;
4+
5+
namespace StopSignDetectionML.Model
6+
{
7+
public class ModelInput
8+
{
9+
[ColumnName("Label"), LoadColumn(0)]
10+
public string Label { get; set; }
11+
12+
13+
[ColumnName("ImageSource"), LoadColumn(1)]
14+
public string ImageSource { get; set; }
15+
16+
17+
}
18+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// This file was auto-generated by ML.NET Model Builder.
2+
3+
using System;
4+
using Microsoft.ML.Data;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
namespace StopSignDetectionML.Model
9+
{
10+
public class ModelOutput
11+
{
12+
public string[] ObjectTags = new string[] { "--bg--", "Stop-Sign", };
13+
14+
[ColumnName("boxes")]
15+
public float[] Boxes { get; set; }
16+
17+
[ColumnName("labels")]
18+
public long[] Labels { get; set; }
19+
20+
[ColumnName("scores")]
21+
public float[] Scores { get; set; }
22+
23+
public BoundingBox[] BoundingBoxes
24+
{
25+
get
26+
{
27+
var boundingBoxes = new List<BoundingBox>();
28+
29+
boundingBoxes = Enumerable.Range(0, this.Labels.Length)
30+
.Select((index) =>
31+
{
32+
var boxes = this.Boxes;
33+
var scores = this.Scores;
34+
var labels = this.Labels;
35+
36+
return new BoundingBox()
37+
{
38+
Left = boxes[index * 4],
39+
Top = boxes[(index * 4) + 1],
40+
Right = boxes[(index * 4) + 2],
41+
Bottom = boxes[(index * 4) + 3],
42+
Score = scores[index],
43+
Label = this.ObjectTags[labels[index]],
44+
};
45+
}).ToList();
46+
return boundingBoxes.ToArray();
47+
}
48+
}
49+
50+
public override string ToString()
51+
{
52+
return string.Join("\n", BoundingBoxes.Select(x => x.ToString()));
53+
}
54+
}
55+
56+
public class BoundingBox
57+
{
58+
public float Top;
59+
60+
public float Left;
61+
62+
public float Right;
63+
64+
public float Bottom;
65+
66+
public string Label;
67+
68+
public float Score;
69+
70+
public override string ToString()
71+
{
72+
return $"Top: {this.Top}, Left: {this.Left}, Right: {this.Right}, Bottom: {this.Bottom}, Label: {this.Label}, Score: {this.Score}";
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.ML" Version="1.5.0" />
8+
<PackageReference Include="Microsoft.ML.OnnxTransformer" Version="1.5.0" />
9+
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.3.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectCapability Include="ModelBuilderGenerated" />
14+
</ItemGroup>
15+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["--bg--", "Stop-Sign"]

0 commit comments

Comments
 (0)
0