博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用 ML.NET 识别乐高颜色块
阅读量:4034 次
发布时间:2019-05-24

本文共 3386 字,大约阅读时间需要 11 分钟。

每一个乐高迷都拥有很多的颜色块,需要进行排序和按类型分拣,按照《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 dataset    var inputs = new List
(); 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
(inputs); // Create training pipeline var 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
trainingPipeline = dataProcessPipeline.Append(trainer); // Create the model mlModel = trainingPipeline.Fit(trainingDataView);}

现在,使用这个训练模型,我们可以尝试对一个新图像进行分类。通过为其中一个图像创建模型输入,然后将它传递到使用分类器构建的模型创建的预测引擎。

static ModelOutput Classify(string filePath){    // Create input to classify    ModelInput input = new ModelInput() { ImageSource = filePath };    // Load model and predict    var predEngine = mlContext.Model.CreatePredictionEngine
(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

转载地址:http://vuudi.baihongyu.com/

你可能感兴趣的文章
非关系型数据库(nosql)介绍
查看>>
移动端自动化测试-Windows-Android-Appium环境搭建
查看>>
Xpath使用方法
查看>>
移动端自动化测试-Mac-IOS-Appium环境搭建
查看>>
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>
Linux常用统计命令之wc
查看>>
测试必会之 Linux 三剑客之 sed
查看>>
Socket请求XML客户端程序
查看>>
Java中数字转大写货币(支持到千亿)
查看>>
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>