|
| 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