使用 ML.NET 识别乐高颜色块
每一個樂高迷都擁有很多的顏色塊,需要進行排序和按類型分揀,按照《Organizing your LEGO Bricks》或許有所幫助,但這不是一個簡單的任務,因為有很多顏色塊有非常微妙的差異。如果換作一個典型的程序員可以做什么來解決這個問題呢?你猜對了 - 建立一個程序使用 ML.NET 來識別樂高的顏色塊。
首先,我們將創建一個控制臺應用并添加所需的包
> dotnet new console > dotnet add package Microsoft.ML > dotnet add package Microsoft.ML.Vision > dotnet add package Microsoft.ML.ImageAnalytics > dotnet add package SciSharp.TensorFlow.Redist在項目文件夾的根目錄中,我將創建一個名為 pieces 的子文件夾,并在此文件夾中創建一些顏色分類的子文件夾,放置訓練集中的每種顏色的圖片。
使用時,我們需要定義輸入和輸出模型(分類器提供分類結果)。
public class ModelInput {public string Label { get; set; }public string ImageSource { get; set; } }public class ModelOutput {public String PredictedLabel { get; set; } }為了訓練模型,我們首先創建一個由目錄中的圖像組成的輸入數據集,并將其作為標簽分配它們位于的目錄的名稱。在此之后,我們創建訓練管道,最后,使用數據進行訓練以創建模型。
static void TrainModel() {// Create the input datasetvar inputs = new List<ModelInput>();foreach (var subDir in Directory.GetDirectories(inputDataDirectoryPath)){foreach (var file in Directory.GetFiles(subDir)){inputs.Add(new ModelInput() { Label = subDir.Split("\\").Last(), ImageSource = file });}}var trainingDataView = mlContext.Data.LoadFromEnumerable<ModelInput>(inputs);// Create training pipelinevar dataProcessPipeline = mlContext.Transforms.Conversion.MapValueToKey("Label", "Label").Append(mlContext.Transforms.LoadRawImageBytes("ImageSource_featurized", null, "ImageSource")).Append(mlContext.Transforms.CopyColumns("Features", "ImageSource_featurized"));var trainer = mlContext.MulticlassClassification.Trainers.ImageClassification(new ImageClassificationTrainer.Options() { LabelColumnName = "Label", FeatureColumnName = "Features" }).Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel", "PredictedLabel"));IEstimator<ITransformer> trainingPipeline = dataProcessPipeline.Append(trainer);// Create the modelmlModel = trainingPipeline.Fit(trainingDataView); }現在,使用這個訓練模型,我們可以嘗試對一個新圖像進行分類。通過為其中一個圖像創建模型輸入,然后將它傳遞到使用分類器構建的模型創建的預測引擎。
static ModelOutput Classify(string filePath) {// Create input to classifyModelInput input = new ModelInput() { ImageSource = filePath };// Load model and predictvar predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);return predEngine.Predict(input); }最后讓我們用4種不同的顏色來測試這一點。
static void Main() {TrainModel();var result = Classify(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Black.jpg");Console.WriteLine($"Testing with black piece. Prediction: {result.PredictedLabel}.");result = Classify(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Blue.jpg");Console.WriteLine($"Testing with blue piece. Prediction: {result.PredictedLabel}.");result = Classify(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Green.jpg");Console.WriteLine($"Testing with green piece. Prediction: {result.PredictedLabel}.");result = Classify(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Yellow.jpg");Console.WriteLine($"Testing with yellow piece. Prediction: {result.PredictedLabel}."); }結果如圖所示。
4張圖片對了3個!略微有點令人失望。但這是一個很好的開始,因為它給了我們機會去深入,并試圖了解如何改進分類,使其更準確。也許它需要更多的訓練數據,也許有更好的分類算法我們可以使用!
項目完整示例代碼和訓練數據在GIthub上:https://github.com/BeanHsiang/Vainosamples/tree/master/CSharp/ML/LegoColorIdentifier
總結
以上是生活随笔為你收集整理的使用 ML.NET 识别乐高颜色块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Flash 生命终止,HTML5能否完美
- 下一篇: asp.net ajax控件工具集 Au