MCPcopy Create free account
hub / github.com/davisking/dlib / chinese_cluster

Function chinese_cluster

tools/imglab/src/cluster.cpp:70–122  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

68 return assignments;
69}
70std::vector<assignment> chinese_cluster (
71 std::vector<matrix<double,0,1> > feats,
72 unsigned long &num_clusters
73 )
74{
75 DLIB_CASSERT(feats.size() != 0, "The dataset can't be empty");
76 for (unsigned long i = 0; i < feats.size(); ++i)
77 {
78 DLIB_CASSERT(feats[i].size() == feats[0].size(), "All feature vectors must have the same length.");
79 }
80
81 // Try to find a good value to select if we should add a vertex in the graph. First we
82 // normalize the features.
83 const matrix<double,0,1> m = mean(mat(feats));
84
85 for (auto& f : feats)
86 {
87 f = normalize(f-m);
88 }
89
90 // Then we find the average distance between them, that average will be a good threshold to
91 // decide if pairs are connected.
92 running_stats<double> rs;
93 for (size_t i = 0; i < feats.size(); ++i) {
94 for (size_t j = i; j < feats.size(); ++j) {
95 rs.add(length(feats[i] - feats[j]));
96 }
97 }
98
99 // add vertices for chinese whispers to find clusters
100 std::vector<sample_pair> edges;
101 for (size_t i = 0; i < feats.size(); ++i) {
102 for (size_t j = i; j < feats.size(); ++j) {
103 if (length(feats[i] - feats[j]) < rs.mean()) {
104 edges.push_back(sample_pair(i, j, length(feats[i] - feats[j])));
105 }
106 }
107 }
108
109 std::vector<unsigned long> labels;
110 num_clusters = chinese_whispers(edges, labels);
111
112 std::vector<assignment> assignments;
113 for (unsigned long i = 0; i < feats.size(); ++i)
114 {
115 assignment temp;
116 temp.c = labels[i];
117 temp.dist = length(feats[i]);
118 temp.idx = i;
119 assignments.push_back(temp);
120 }
121 return assignments;
122}
123
124// ----------------------------------------------------------------------------------------
125

Callers 1

cluster_datasetFunction · 0.85

Calls 10

meanFunction · 0.85
chinese_whispersFunction · 0.85
matFunction · 0.50
normalizeFunction · 0.50
lengthFunction · 0.50
sample_pairClass · 0.50
sizeMethod · 0.45
addMethod · 0.45
meanMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected