2014-06-08 81 views
4

我使用k-means對我的數據進行聚類,但我沒有使用標準算法,我使用近似最近鄰(ANN)算法來加速採樣 - 中心比較。這很容易用下面來完成:使用VLFeat創建集羣后,將描述符分配到集羣中心

[clusterCenters, trainAssignments] = vl_kmeans(trainDescriptors, clusterCount, 'Algorithm', 'ANN', 'MaxNumComparisons', ceil(clusterCount/50)); 

現在,當我運行這段代碼的變量「trainDescriptors」的集羣,並且每個描述符分配給使用ANN的「clusterCenters」。

我也有另一個變量'testDescriptors'。我想將這些分配給集羣中心。而這種分配必須使用帶有「trainDescriptors」同樣的方法來完成,但據我所知vl_kmeans功能不會返回它建立快速分配樹。

所以,我的問題是,是否有可能分配 'testDescriptors' 到 'clustersCenters' 爲 'trainDescriptors' 分配給 'clusterCenters' 在vl_kmeans功能,如果是我怎樣才能做到這一點?

回答

4

嗯,我已經想通了。這是可以做到這樣的:

clusterCount = 1024; 
datasetTrain = single(rand(128, 100000)); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% 1 - cluster train data and get train assignments 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

[clusterCenters, trainAssignments_actual] = vl_kmeans(datasetTrain, clusterCount, ... 
    'Algorithm', 'ANN', ... 
    'Distance', 'l2', ... 
    'NumRepetitions', 1, ... 
    'NumTrees', 3, ... 
    'MaxNumComparisons', ceil(clusterCount/50) ... 
); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% 2 - assign train data to clusters centers 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

forest = vl_kdtreebuild(clusterCenters, ... 
    'Distance', 'l2', ... 
    'NumTrees', 3 ... 
); 

trainAssignments_expected = vl_kdtreequery(forest, clusterCenters, datasetTrain); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% 3 - validate second assignment 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

validation = isequal(trainAssignments_actual, trainAssignments_expected); 

在步驟2中我使用聚類中心創建一個新的樹,然後重新分配數據中心。它給出了一個有效的結果。

+2

+1用於回答你自己的問題。 :) – gsamaras