Skip to content

Commit cccb4fe

Browse files
committed
LegoColorIdentifier ML Sample
1 parent 51f3eb2 commit cccb4fe

File tree

246 files changed

+88
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

246 files changed

+88
-0
lines changed
1.37 MB
Loading
1.21 MB
Loading
1.29 MB
Loading
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.ML" Version="1.5.2" />
10+
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="1.5.2" />
11+
<PackageReference Include="Microsoft.ML.Vision" Version="1.5.2" />
12+
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.3.1" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using Microsoft.ML;
6+
using Microsoft.ML.Data;
7+
using Microsoft.ML.Vision;
8+
9+
public class Program
10+
{
11+
static readonly string inputDataDirectoryPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "pieces";
12+
static readonly string outputModelFilePath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "model.zip";
13+
static MLContext mlContext = new MLContext(seed: 1);
14+
static ITransformer mlModel;
15+
16+
public class ModelInput
17+
{
18+
public string Label { get; set; }
19+
public string ImageSource { get; set; }
20+
}
21+
22+
public class ModelOutput
23+
{
24+
public String PredictedLabel { get; set; }
25+
}
26+
27+
static void TrainModel()
28+
{
29+
// Create the input dataset
30+
var inputs = new List<ModelInput>();
31+
foreach (var subDir in Directory.GetDirectories(inputDataDirectoryPath))
32+
{
33+
foreach (var file in Directory.GetFiles(subDir))
34+
{
35+
inputs.Add(new ModelInput() { Label = subDir.Split("\\").Last(), ImageSource = file });
36+
}
37+
}
38+
var trainingDataView = mlContext.Data.LoadFromEnumerable<ModelInput>(inputs);
39+
// Create training pipeline
40+
var dataProcessPipeline = mlContext.Transforms.Conversion.MapValueToKey("Label", "Label")
41+
.Append(mlContext.Transforms.LoadRawImageBytes("ImageSource_featurized", null, "ImageSource"))
42+
.Append(mlContext.Transforms.CopyColumns("Features", "ImageSource_featurized"));
43+
var trainer = mlContext.MulticlassClassification.Trainers.ImageClassification(new ImageClassificationTrainer.Options() { LabelColumnName = "Label", FeatureColumnName = "Features" })
44+
.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel", "PredictedLabel"));
45+
IEstimator<ITransformer> trainingPipeline = dataProcessPipeline.Append(trainer);
46+
// Create the model
47+
mlModel = trainingPipeline.Fit(trainingDataView);
48+
}
49+
50+
static ModelOutput Classify(string filePath)
51+
{
52+
// Create input to classify
53+
ModelInput input = new ModelInput() { ImageSource = filePath };
54+
// Load model and predict
55+
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
56+
return predEngine.Predict(input);
57+
}
58+
59+
static void Main()
60+
{
61+
TrainModel();
62+
63+
var result = Classify(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Black.jpg");
64+
Console.WriteLine($"Testing with black piece. Prediction: {result.PredictedLabel}.");
65+
result = Classify(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Blue.jpg");
66+
Console.WriteLine($"Testing with blue piece. Prediction: {result.PredictedLabel}.");
67+
result = Classify(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Green.jpg");
68+
Console.WriteLine($"Testing with green piece. Prediction: {result.PredictedLabel}.");
69+
result = Classify(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Yellow.jpg");
70+
Console.WriteLine($"Testing with yellow piece. Prediction: {result.PredictedLabel}.");
71+
}
72+
}
73+

0 commit comments

Comments
 (0)