1
/***********************************************************************
2
* Software License Agreement (BSD License)
4
* Copyright 2008-2011 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5
* Copyright 2008-2011 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
19
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
*************************************************************************/
31
#ifndef OPENCV_FLANN_HIERARCHICAL_CLUSTERING_INDEX_H_
32
#define OPENCV_FLANN_HIERARCHICAL_CLUSTERING_INDEX_H_
44
#include "result_set.h"
46
#include "allocator.h"
54
struct HierarchicalClusteringIndexParams : public IndexParams
56
HierarchicalClusteringIndexParams(int branching = 32,
57
flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM,
58
int trees = 4, int leaf_size = 100)
60
(*this)["algorithm"] = FLANN_INDEX_HIERARCHICAL;
61
// The branching factor used in the hierarchical clustering
62
(*this)["branching"] = branching;
63
// Algorithm used for picking the initial cluster centers
64
(*this)["centers_init"] = centers_init;
65
// number of parallel trees to build
66
(*this)["trees"] = trees;
68
(*this)["leaf_size"] = leaf_size;
76
* Contains a tree constructed through a hierarchical clustering
77
* and other information for indexing a set of points for nearest-neighbour matching.
79
template <typename Distance>
80
class HierarchicalClusteringIndex : public NNIndex<Distance>
83
typedef typename Distance::ElementType ElementType;
84
typedef typename Distance::ResultType DistanceType;
89
typedef void (HierarchicalClusteringIndex::* centersAlgFunction)(int, int*, int, int*, int&);
92
* The function used for choosing the cluster centers.
94
centersAlgFunction chooseCenters;
99
* Chooses the initial centers in the k-means clustering in a random manner.
102
* k = number of centers
103
* vecs = the dataset of points
104
* indices = indices in the dataset
105
* indices_length = length of indices vector
108
void chooseCentersRandom(int k, int* dsindices, int indices_length, int* centers, int& centers_length)
110
UniqueRandom r(indices_length);
113
for (index=0; index<k; ++index) {
114
bool duplicate = true;
120
centers_length = index;
124
centers[index] = dsindices[rnd];
126
for (int j=0; j<index; ++j) {
127
DistanceType sq = distance(dataset[centers[index]], dataset[centers[j]], dataset.cols);
135
centers_length = index;
140
* Chooses the initial centers in the k-means using Gonzales' algorithm
141
* so that the centers are spaced apart from each other.
144
* k = number of centers
145
* vecs = the dataset of points
146
* indices = indices in the dataset
149
void chooseCentersGonzales(int k, int* dsindices, int indices_length, int* centers, int& centers_length)
151
int n = indices_length;
153
int rnd = rand_int(n);
154
assert(rnd >=0 && rnd < n);
156
centers[0] = dsindices[rnd];
159
for (index=1; index<k; ++index) {
162
DistanceType best_val = 0;
163
for (int j=0; j<n; ++j) {
164
DistanceType dist = distance(dataset[centers[0]],dataset[dsindices[j]],dataset.cols);
165
for (int i=1; i<index; ++i) {
166
DistanceType tmp_dist = distance(dataset[centers[i]],dataset[dsindices[j]],dataset.cols);
176
if (best_index!=-1) {
177
centers[index] = dsindices[best_index];
183
centers_length = index;
188
* Chooses the initial centers in the k-means using the algorithm
189
* proposed in the KMeans++ paper:
190
* Arthur, David; Vassilvitskii, Sergei - k-means++: The Advantages of Careful Seeding
192
* Implementation of this function was converted from the one provided in Arthur's code.
195
* k = number of centers
196
* vecs = the dataset of points
197
* indices = indices in the dataset
200
void chooseCentersKMeanspp(int k, int* dsindices, int indices_length, int* centers, int& centers_length)
202
int n = indices_length;
204
double currentPot = 0;
205
DistanceType* closestDistSq = new DistanceType[n];
207
// Choose one random center and set the closestDistSq values
208
int index = rand_int(n);
209
assert(index >=0 && index < n);
210
centers[0] = dsindices[index];
212
// Computing distance^2 will have the advantage of even higher probability further to pick new centers
213
// far from previous centers (and this complies to "k-means++: the advantages of careful seeding" article)
214
for (int i = 0; i < n; i++) {
215
closestDistSq[i] = distance(dataset[dsindices[i]], dataset[dsindices[index]], dataset.cols);
216
closestDistSq[i] = ensureSquareDistance<Distance>( closestDistSq[i] );
217
currentPot += closestDistSq[i];
221
const int numLocalTries = 1;
223
// Choose each center
225
for (centerCount = 1; centerCount < k; centerCount++) {
227
// Repeat several trials
228
double bestNewPot = -1;
229
int bestNewIndex = 0;
230
for (int localTrial = 0; localTrial < numLocalTries; localTrial++) {
232
// Choose our center - have to be slightly careful to return a valid answer even accounting
233
// for possible rounding errors
234
double randVal = rand_double(currentPot);
235
for (index = 0; index < n-1; index++) {
236
if (randVal <= closestDistSq[index]) break;
237
else randVal -= closestDistSq[index];
240
// Compute the new potential
242
for (int i = 0; i < n; i++) {
243
DistanceType dist = distance(dataset[dsindices[i]], dataset[dsindices[index]], dataset.cols);
244
newPot += std::min( ensureSquareDistance<Distance>(dist), closestDistSq[i] );
247
// Store the best result
248
if ((bestNewPot < 0)||(newPot < bestNewPot)) {
250
bestNewIndex = index;
254
// Add the appropriate center
255
centers[centerCount] = dsindices[bestNewIndex];
256
currentPot = bestNewPot;
257
for (int i = 0; i < n; i++) {
258
DistanceType dist = distance(dataset[dsindices[i]], dataset[dsindices[bestNewIndex]], dataset.cols);
259
closestDistSq[i] = std::min( ensureSquareDistance<Distance>(dist), closestDistSq[i] );
263
centers_length = centerCount;
265
delete[] closestDistSq;
270
* Chooses the initial centers in a way inspired by Gonzales (by Pierre-Emmanuel Viel):
271
* select the first point of the list as a candidate, then parse the points list. If another
272
* point is further than current candidate from the other centers, test if it is a good center
273
* of a local aggregation. If it is, replace current candidate by this point. And so on...
275
* Used with KMeansIndex that computes centers coordinates by averaging positions of clusters points,
276
* this doesn't make a real difference with previous methods. But used with HierarchicalClusteringIndex
277
* class that pick centers among existing points instead of computing the barycenters, there is a real
281
* k = number of centers
282
* vecs = the dataset of points
283
* indices = indices in the dataset
286
void GroupWiseCenterChooser(int k, int* dsindices, int indices_length, int* centers, int& centers_length)
288
const float kSpeedUpFactor = 1.3f;
290
int n = indices_length;
292
DistanceType* closestDistSq = new DistanceType[n];
294
// Choose one random center and set the closestDistSq values
295
int index = rand_int(n);
296
assert(index >=0 && index < n);
297
centers[0] = dsindices[index];
299
for (int i = 0; i < n; i++) {
300
closestDistSq[i] = distance(dataset[dsindices[i]], dataset[dsindices[index]], dataset.cols);
304
// Choose each center
306
for (centerCount = 1; centerCount < k; centerCount++) {
308
// Repeat several trials
309
double bestNewPot = -1;
310
int bestNewIndex = 0;
311
DistanceType furthest = 0;
312
for (index = 0; index < n; index++) {
314
// We will test only the potential of the points further than current candidate
315
if( closestDistSq[index] > kSpeedUpFactor * (float)furthest ) {
317
// Compute the new potential
319
for (int i = 0; i < n; i++) {
320
newPot += std::min( distance(dataset[dsindices[i]], dataset[dsindices[index]], dataset.cols)
321
, closestDistSq[i] );
324
// Store the best result
325
if ((bestNewPot < 0)||(newPot <= bestNewPot)) {
327
bestNewIndex = index;
328
furthest = closestDistSq[index];
333
// Add the appropriate center
334
centers[centerCount] = dsindices[bestNewIndex];
335
for (int i = 0; i < n; i++) {
336
closestDistSq[i] = std::min( distance(dataset[dsindices[i]], dataset[dsindices[bestNewIndex]], dataset.cols)
337
, closestDistSq[i] );
341
centers_length = centerCount;
343
delete[] closestDistSq;
354
* inputData = dataset with the input features
355
* params = parameters passed to the hierarchical k-means algorithm
357
HierarchicalClusteringIndex(const Matrix<ElementType>& inputData, const IndexParams& index_params = HierarchicalClusteringIndexParams(),
358
Distance d = Distance())
359
: dataset(inputData), params(index_params), root(NULL), indices(NULL), distance(d)
363
size_ = dataset.rows;
364
veclen_ = dataset.cols;
366
branching_ = get_param(params,"branching",32);
367
centers_init_ = get_param(params,"centers_init", FLANN_CENTERS_RANDOM);
368
trees_ = get_param(params,"trees",4);
369
leaf_size_ = get_param(params,"leaf_size",100);
371
if (centers_init_==FLANN_CENTERS_RANDOM) {
372
chooseCenters = &HierarchicalClusteringIndex::chooseCentersRandom;
374
else if (centers_init_==FLANN_CENTERS_GONZALES) {
375
chooseCenters = &HierarchicalClusteringIndex::chooseCentersGonzales;
377
else if (centers_init_==FLANN_CENTERS_KMEANSPP) {
378
chooseCenters = &HierarchicalClusteringIndex::chooseCentersKMeanspp;
380
else if (centers_init_==FLANN_CENTERS_GROUPWISE) {
381
chooseCenters = &HierarchicalClusteringIndex::GroupWiseCenterChooser;
384
throw FLANNException("Unknown algorithm for choosing initial centers.");
387
trees_ = get_param(params,"trees",4);
388
root = new NodePtr[trees_];
389
indices = new int*[trees_];
391
for (int i=0; i<trees_; ++i) {
397
HierarchicalClusteringIndex(const HierarchicalClusteringIndex&);
398
HierarchicalClusteringIndex& operator=(const HierarchicalClusteringIndex&);
403
* Release the memory used by the index.
405
virtual ~HierarchicalClusteringIndex()
420
* Release the inner elements of indices[]
425
for(int i=0; i<trees_; ++i) {
426
if (indices[i]!=NULL) {
436
* Returns size of index.
444
* Returns the length of an index feature.
446
size_t veclen() const
453
* Computes the inde memory usage
454
* Returns: memory used by the index
456
int usedMemory() const
458
return pool.usedMemory+pool.wastedMemory+memoryCounter;
467
throw FLANNException("Branching factor must be at least 2");
472
for (int i=0; i<trees_; ++i) {
473
indices[i] = new int[size_];
474
for (size_t j=0; j<size_; ++j) {
475
indices[i][j] = (int)j;
477
root[i] = pool.allocate<Node>();
478
computeClustering(root[i], indices[i], (int)size_, branching_,0);
483
flann_algorithm_t getType() const
485
return FLANN_INDEX_HIERARCHICAL;
489
void saveIndex(FILE* stream)
491
save_value(stream, branching_);
492
save_value(stream, trees_);
493
save_value(stream, centers_init_);
494
save_value(stream, leaf_size_);
495
save_value(stream, memoryCounter);
496
for (int i=0; i<trees_; ++i) {
497
save_value(stream, *indices[i], size_);
498
save_tree(stream, root[i], i);
504
void loadIndex(FILE* stream)
516
load_value(stream, branching_);
517
load_value(stream, trees_);
518
load_value(stream, centers_init_);
519
load_value(stream, leaf_size_);
520
load_value(stream, memoryCounter);
522
indices = new int*[trees_];
523
root = new NodePtr[trees_];
524
for (int i=0; i<trees_; ++i) {
525
indices[i] = new int[size_];
526
load_value(stream, *indices[i], size_);
527
load_tree(stream, root[i], i);
530
params["algorithm"] = getType();
531
params["branching"] = branching_;
532
params["trees"] = trees_;
533
params["centers_init"] = centers_init_;
534
params["leaf_size"] = leaf_size_;
539
* Find set of nearest neighbors to vec. Their indices are stored inside
543
* result = the result object in which the indices of the nearest-neighbors are stored
544
* vec = the vector for which to search the nearest neighbors
545
* searchParams = parameters that influence the search algorithm (checks)
547
void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams)
550
int maxChecks = get_param(searchParams,"checks",32);
552
// Priority queue storing intermediate branches in the best-bin-first search
553
Heap<BranchSt>* heap = new Heap<BranchSt>((int)size_);
555
std::vector<bool> checked(size_,false);
557
for (int i=0; i<trees_; ++i) {
558
findNN(root[i], result, vec, checks, maxChecks, heap, checked);
562
while (heap->popMin(branch) && (checks<maxChecks || !result.full())) {
563
NodePtr node = branch.node;
564
findNN(node, result, vec, checks, maxChecks, heap, checked);
566
assert(result.full());
572
IndexParams getParameters() const
581
* Struture representing a node in the hierarchical k-means tree.
586
* The cluster center index
590
* The cluster size (number of points in the cluster)
594
* Child nodes (only for non-terminal nodes)
598
* Node points (only for terminal nodes)
606
typedef Node* NodePtr;
611
* Alias definition for a nicer syntax.
613
typedef BranchStruct<NodePtr, DistanceType> BranchSt;
617
void save_tree(FILE* stream, NodePtr node, int num)
619
save_value(stream, *node);
620
if (node->childs==NULL) {
621
int indices_offset = (int)(node->indices - indices[num]);
622
save_value(stream, indices_offset);
625
for(int i=0; i<branching_; ++i) {
626
save_tree(stream, node->childs[i], num);
632
void load_tree(FILE* stream, NodePtr& node, int num)
634
node = pool.allocate<Node>();
635
load_value(stream, *node);
636
if (node->childs==NULL) {
638
load_value(stream, indices_offset);
639
node->indices = indices[num] + indices_offset;
642
node->childs = pool.allocate<NodePtr>(branching_);
643
for(int i=0; i<branching_; ++i) {
644
load_tree(stream, node->childs[i], num);
652
void computeLabels(int* dsindices, int indices_length, int* centers, int centers_length, int* labels, DistanceType& cost)
655
for (int i=0; i<indices_length; ++i) {
656
ElementType* point = dataset[dsindices[i]];
657
DistanceType dist = distance(point, dataset[centers[0]], veclen_);
659
for (int j=1; j<centers_length; ++j) {
660
DistanceType new_dist = distance(point, dataset[centers[j]], veclen_);
671
* The method responsible with actually doing the recursive hierarchical
675
* node = the node to cluster
676
* indices = indices of the points belonging to the current node
677
* branching = the branching factor to use in the clustering
679
* TODO: for 1-sized clusters don't store a cluster center (it's the same as the single cluster point)
681
void computeClustering(NodePtr node, int* dsindices, int indices_length, int branching, int level)
683
node->size = indices_length;
686
if (indices_length < leaf_size_) { // leaf node
687
node->indices = dsindices;
688
std::sort(node->indices,node->indices+indices_length);
693
std::vector<int> centers(branching);
694
std::vector<int> labels(indices_length);
697
(this->*chooseCenters)(branching, dsindices, indices_length, ¢ers[0], centers_length);
699
if (centers_length<branching) {
700
node->indices = dsindices;
701
std::sort(node->indices,node->indices+indices_length);
707
// assign points to clusters
709
computeLabels(dsindices, indices_length, ¢ers[0], centers_length, &labels[0], cost);
711
node->childs = pool.allocate<NodePtr>(branching);
714
for (int i=0; i<branching; ++i) {
715
for (int j=0; j<indices_length; ++j) {
717
std::swap(dsindices[j],dsindices[end]);
718
std::swap(labels[j],labels[end]);
723
node->childs[i] = pool.allocate<Node>();
724
node->childs[i]->pivot = centers[i];
725
node->childs[i]->indices = NULL;
726
computeClustering(node->childs[i],dsindices+start, end-start, branching, level+1);
734
* Performs one descent in the hierarchical k-means tree. The branches not
735
* visited are stored in a priority queue.
738
* node = node to explore
739
* result = container for the k-nearest neighbors found
741
* checks = how many points in the dataset have been checked so far
742
* maxChecks = maximum dataset points to checks
746
void findNN(NodePtr node, ResultSet<DistanceType>& result, const ElementType* vec, int& checks, int maxChecks,
747
Heap<BranchSt>* heap, std::vector<bool>& checked)
749
if (node->childs==NULL) {
750
if (checks>=maxChecks) {
751
if (result.full()) return;
753
for (int i=0; i<node->size; ++i) {
754
int index = node->indices[i];
755
if (!checked[index]) {
756
DistanceType dist = distance(dataset[index], vec, veclen_);
757
result.addPoint(dist, index);
758
checked[index] = true;
764
DistanceType* domain_distances = new DistanceType[branching_];
766
domain_distances[best_index] = distance(vec, dataset[node->childs[best_index]->pivot], veclen_);
767
for (int i=1; i<branching_; ++i) {
768
domain_distances[i] = distance(vec, dataset[node->childs[i]->pivot], veclen_);
769
if (domain_distances[i]<domain_distances[best_index]) {
773
for (int i=0; i<branching_; ++i) {
775
heap->insert(BranchSt(node->childs[i],domain_distances[i]));
778
delete[] domain_distances;
779
findNN(node->childs[best_index],result,vec, checks, maxChecks, heap, checked);
787
* The dataset used by this index
789
const Matrix<ElementType> dataset;
792
* Parameters used by this index
798
* Number of features in the dataset.
803
* Length of each feature.
808
* The root node in the tree.
813
* Array of indices to vectors in the dataset.
824
* Pooled memory allocator.
826
* Using a pooled memory allocator is more efficient
827
* than allocating memory directly when there is a large
828
* number small of memory allocations.
830
PooledAllocator pool;
833
* Memory occupied by the index.
837
/** index parameters */
840
flann_centers_init_t centers_init_;
848
#endif /* OPENCV_FLANN_HIERARCHICAL_CLUSTERING_INDEX_H_ */