Microsoft.ML KMeans获取群集

时间:2019-09-07 19:12:11

标签: c# .net-core

我想使用Microsoft.ML群集KMeans进行颜色量化。我有一个带有颜色的数据集,关键是要获取有关训练好的聚类的信息。

有什么方法可以从模型/上下文中获取有关训练集群的信息吗?

代码:

var mlContext = new MLContext();

// color is System.Drawing.Color array    
var trainingColors = colors
                    .Select(s => new ColorTrainingModel() { R = s.R, G = s.G, B = s.B })
                    .ToArray();

var trainingData = mlContext.Data.LoadFromEnumerable(trainingColors);

var kmeans = mlContext.Clustering.Trainers.KMeans(
        featureColumnName: "Features",
        numberOfClusters: 4);

var pipeline = mlContext.Transforms.Concatenate( 
        "Features",
        nameof(ColorTrainingModel.R), 
        nameof(ColorTrainingModel.G), 
        nameof(ColorTrainingModel.B)
    )

    .Append(kmeans);

var model = pipeline.Fit(trainingData);

// kinda result I am looking for
var colorPalette = model.Clusters.Select(s => new { R = s[0], G = s[1], ...

1 个答案:

答案 0 :(得分:0)

有一个基本上与Microsoft.ML不相关的解决方案,但是使用KMeans来完成:

使用Accord.NET框架:http://accord-framework.net/docs/html/T_Accord_MachineLearning_KMeans.htm

var observations = colors
                    .Select(s => new double[] { s.R, s.G, s.B })
                    .ToArray();

var kmeans = new Accord.MachineLearning.KMeans(k: colorCount);

var clusters = kmeans.Learn(observations);

var palette = new List<System.Drawing.Color>();

foreach (var c in clusters)
{
    var col = System.Drawing.Color.FromArgb(

        (int)Math.Round(c.Centroid[0], 0),  //R
        (int)Math.Round(c.Centroid[1], 0),  //G
        (int)Math.Round(c.Centroid[2], 0)   //B
    );

    palette.Add(col);
}

return palette.ToArray();