惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
量子位
小众软件
小众软件
C
Cybersecurity and Infrastructure Security Agency CISA
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
T
Threat Research - Cisco Blogs
Latest news
Latest news
Spread Privacy
Spread Privacy
C
Cyber Attacks, Cyber Crime and Cyber Security
NISL@THU
NISL@THU
T
Tor Project blog
Hacker News: Ask HN
Hacker News: Ask HN
V2EX - 技术
V2EX - 技术
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
K
Kaspersky official blog
Cyberwarzone
Cyberwarzone
博客园 - 叶小钗
博客园 - 聂微东
Last Week in AI
Last Week in AI
爱范儿
爱范儿
腾讯CDC
博客园 - Franky
美团技术团队
J
Java Code Geeks
O
OpenAI News
L
Lohrmann on Cybersecurity
Simon Willison's Weblog
Simon Willison's Weblog
有赞技术团队
有赞技术团队
T
Threatpost
G
GRAHAM CLULEY
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
I
Intezer
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
S
Security @ Cisco Blogs
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
Stack Overflow Blog
Stack Overflow Blog
Scott Helme
Scott Helme
H
Hacker News: Front Page
Cloudbric
Cloudbric

博客园 - Bean.Hsiang

评估和改进 ML.NET 分类器 ML.NET 和 Model Builder 2021年三月更新 ML.NET 推荐引擎中一类矩阵因子分解的缺陷 使用 ML.NET 实现峰值检测来排查异常 4倍速!ML.NET Model Builder GPU 与 CPU 对比测试 ML.NET 十一月更新 借助AI激发组织创新活力 Virtual ML.NET Hackathon 2020 来啦 ML.NET 九月更新 ML.NET API 和工具八月更新 通过 ML.NET 使用预训练残差网络 ResNet 模型实现手势识别 通过 Continual Learning 提高 ML.NET 模型准确性并增强性能 在WSL2启用cuda进行深度学习尝鲜 基于 ONNX 在 ML.NET 中使用 Pytorch 训练的垃圾分类模型 在 Blazor WebAssembly 静态网站中部署ML.NET机器学习模型 使用 Scikit-learn 和 ML.NET 实现朴素贝叶斯(Naive Bayes)分类器 恢复已取消ML.NET训练中的模型 说说 ML.NET and AutoML 使用ML.NET进行自定义机器学习
使用 ML.NET 识别乐高颜色块
Bean.Hsiang · 2020-12-27 · via 博客园 - Bean.Hsiang

每一个乐高迷都拥有很多的颜色块,需要进行排序和按类型分拣,按照《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<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 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<ITransformer> 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<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