不使用域名做网站,南同网站建设软件下载,wordpress更改登录地址,亚马逊跨境电商平台介绍每一个乐高迷都拥有很多的颜色块#xff0c;需要进行排序和按类型分拣#xff0c;按照《Organizing your LEGO Bricks》或许有所帮助#xff0c;但这不是一个简单的任务#xff0c;因为有很多颜色块有非常微妙的差异。如果换作一个典型的程序员可以做什么来解决这个问题呢需要进行排序和按类型分拣按照《Organizing your LEGO Bricks》或许有所帮助但这不是一个简单的任务因为有很多颜色块有非常微妙的差异。如果换作一个典型的程序员可以做什么来解决这个问题呢你猜对了 - 建立一个程序使用 ML.NET 来识别乐高的颜色块。首先我们将创建一个控制台应用并添加所需的包 dotnet new consoledotnet add package Microsoft.MLdotnet add package Microsoft.ML.Visiondotnet add package Microsoft.ML.ImageAnalyticsdotnet 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 ListModelInput();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.LoadFromEnumerableModelInput(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));IEstimatorITransformer 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.CreatePredictionEngineModelInput, 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