1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
*
* THE BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#include <stdexcept>
#include <vector>
#include "flann.h"
#include "timer.h"
#include "common.h"
#include "logger.h"
#include "index_testing.h"
#include "saving.h"
#include "object_factory.h"
// index types
#include "kdtree_index.h"
#include "kmeans_index.h"
#include "composite_index.h"
#include "linear_index.h"
#include "autotuned_index.h"
#include <typeinfo>
using namespace std;
#include "flann.h"
#ifdef WIN32
#define EXPORTED extern "C" __declspec(dllexport)
#else
#define EXPORTED extern "C"
#endif
namespace cvflann
{
typedef ObjectFactory<IndexParams, flann_algorithm_t> ParamsFactory;
IndexParams* IndexParams::createFromParameters(const FLANNParameters& p)
{
IndexParams* params = ParamsFactory::instance().create(p.algorithm);
params->fromParameters(p);
return params;
}
NNIndex* LinearIndexParams::createIndex(const Matrix<float>& dataset) const
{
return new LinearIndex(dataset, *this);
}
NNIndex* KDTreeIndexParams::createIndex(const Matrix<float>& dataset) const
{
return new KDTreeIndex(dataset, *this);
}
NNIndex* KMeansIndexParams::createIndex(const Matrix<float>& dataset) const
{
return new KMeansIndex(dataset, *this);
}
NNIndex* CompositeIndexParams::createIndex(const Matrix<float>& dataset) const
{
return new CompositeIndex(dataset, *this);
}
NNIndex* AutotunedIndexParams::createIndex(const Matrix<float>& dataset) const
{
return new AutotunedIndex(dataset, *this);
}
NNIndex* SavedIndexParams::createIndex(const Matrix<float>& dataset) const
{
FILE* fin = fopen(filename.c_str(), "rb");
if (fin==NULL) {
return NULL;
}
IndexHeader header = load_header(fin);
rewind(fin);
IndexParams* params = ParamsFactory::instance().create(header.index_type);
NNIndex* nnIndex = params->createIndex(dataset);
nnIndex->loadIndex(fin);
fclose(fin);
delete params; //?
return nnIndex;
}
class StaticInit
{
public:
StaticInit()
{
ParamsFactory::instance().register_<LinearIndexParams>(LINEAR);
ParamsFactory::instance().register_<KDTreeIndexParams>(KDTREE);
ParamsFactory::instance().register_<KMeansIndexParams>(KMEANS);
ParamsFactory::instance().register_<CompositeIndexParams>(COMPOSITE);
ParamsFactory::instance().register_<AutotunedIndexParams>(AUTOTUNED);
ParamsFactory::instance().register_<SavedIndexParams>(SAVED);
}
};
StaticInit __init;
Index::Index(const Matrix<float>& dataset, const IndexParams& params)
{
nnIndex = params.createIndex(dataset);
nnIndex->buildIndex();
}
Index::~Index()
{
delete nnIndex;
}
void Index::knnSearch(const Matrix<float>& queries, Matrix<int>& indices, Matrix<float>& dists, int knn, const SearchParams& searchParams)
{
assert(queries.cols==nnIndex->veclen());
assert(indices.rows>=queries.rows);
assert(dists.rows>=queries.rows);
assert(indices.cols>=knn);
assert(dists.cols>=knn);
search_for_neighbors(*nnIndex, queries, indices, dists, searchParams);
}
int Index::radiusSearch(const Matrix<float>& query, Matrix<int> indices, Matrix<float> dists, float radius, const SearchParams& searchParams)
{
if (query.rows!=1) {
printf("I can only search one feature at a time for range search\n");
return -1;
}
assert(query.cols==nnIndex->veclen());
RadiusResultSet resultSet(radius);
resultSet.init(query.data, query.cols);
nnIndex->findNeighbors(resultSet,query.data,searchParams);
// TODO: optimize here
int* neighbors = resultSet.getNeighbors();
float* distances = resultSet.getDistances();
int count_nn = min((long)resultSet.size(), indices.cols);
assert (dists.cols>=count_nn);
for (int i=0;i<count_nn;++i) {
indices[0][i] = neighbors[i];
dists[0][i] = distances[i];
}
return count_nn;
}
void Index::save(string filename)
{
FILE* fout = fopen(filename.c_str(), "wb");
if (fout==NULL) {
logger.error("Cannot open file: %s", filename.c_str());
throw FLANNException("Cannot open file");
}
nnIndex->saveIndex(fout);
fclose(fout);
}
int Index::size() const
{
return nnIndex->size();
}
int Index::veclen() const
{
return nnIndex->veclen();
}
int hierarchicalClustering(const Matrix<float>& features, Matrix<float>& centers, const KMeansIndexParams& params)
{
KMeansIndex kmeans(features, params);
kmeans.buildIndex();
int clusterNum = kmeans.getClusterCenters(centers);
return clusterNum;
}
} // namespace FLANN
using namespace cvflann;
typedef NNIndex* NNIndexPtr;
typedef Matrix<float>* MatrixPtr;
void init_flann_parameters(FLANNParameters* p)
{
if (p != NULL) {
flann_log_verbosity(p->log_level);
if (p->random_seed>0) {
seed_random(p->random_seed);
}
}
}
EXPORTED void flann_log_verbosity(int level)
{
if (level>=0) {
logger.setLevel(level);
}
}
EXPORTED void flann_set_distance_type(flann_distance_t distance_type, int order)
{
flann_distance_type = distance_type;
flann_minkowski_order = order;
}
EXPORTED flann_index_t flann_build_index(float* dataset, int rows, int cols, float* /*speedup*/, FLANNParameters* flann_params)
{
try {
init_flann_parameters(flann_params);
if (flann_params == NULL) {
throw FLANNException("The flann_params argument must be non-null");
}
IndexParams* params = IndexParams::createFromParameters(*flann_params);
Index* index = new Index(Matrix<float>(rows,cols,dataset), *params);
return index;
}
catch (runtime_error& e) {
logger.error("Caught exception: %s\n",e.what());
return NULL;
}
}
EXPORTED int flann_save_index(flann_index_t index_ptr, char* filename)
{
try {
if (index_ptr==NULL) {
throw FLANNException("Invalid index");
}
Index* index = (Index*)index_ptr;
index->save(filename);
return 0;
}
catch(runtime_error& e) {
logger.error("Caught exception: %s\n",e.what());
return -1;
}
}
EXPORTED FLANN_INDEX flann_load_index(char* filename, float* dataset, int rows, int cols)
{
try {
Index* index = new Index(Matrix<float>(rows,cols,dataset), SavedIndexParams(filename));
return index;
}
catch(runtime_error& e) {
logger.error("Caught exception: %s\n",e.what());
return NULL;
}
}
EXPORTED int flann_find_nearest_neighbors(float* dataset, int rows, int cols, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
{
int _result = 0;
try {
init_flann_parameters(flann_params);
IndexParams* params = IndexParams::createFromParameters(*flann_params);
Index* index = new Index(Matrix<float>(rows,cols,dataset), *params);
Matrix<int> m_indices(tcount, nn, result);
Matrix<float> m_dists(tcount, nn, dists);
index->knnSearch(Matrix<float>(tcount, index->veclen(), testset),
m_indices,
m_dists, nn, SearchParams(flann_params->checks) );
}
catch(runtime_error& e) {
logger.error("Caught exception: %s\n",e.what());
_result = -1;
}
return _result;
}
EXPORTED int flann_find_nearest_neighbors_index(flann_index_t index_ptr, float* testset, int tcount, int* result, float* dists, int nn, int checks, FLANNParameters* flann_params)
{
try {
init_flann_parameters(flann_params);
if (index_ptr==NULL) {
throw FLANNException("Invalid index");
}
Index* index = (Index*) index_ptr;
Matrix<int> m_indices(tcount, nn, result);
Matrix<float> m_dists(tcount, nn, dists);
index->knnSearch(Matrix<float>(tcount, index->veclen(), testset),
m_indices,
m_dists, nn, SearchParams(checks) );
}
catch(runtime_error& e) {
logger.error("Caught exception: %s\n",e.what());
return -1;
}
return -1;
}
EXPORTED int flann_radius_search(FLANN_INDEX index_ptr,
float* query,
int* indices,
float* dists,
int max_nn,
float radius,
int checks,
FLANNParameters* flann_params)
{
try {
init_flann_parameters(flann_params);
if (index_ptr==NULL) {
throw FLANNException("Invalid index");
}
Index* index = (Index*) index_ptr;
Matrix<int> m_indices(1, max_nn, indices);
Matrix<float> m_dists(1, max_nn, dists);
int count = index->radiusSearch(Matrix<float>(1, index->veclen(), query),
m_indices,
m_dists, radius, SearchParams(checks) );
return count;
}
catch(runtime_error& e) {
logger.error("Caught exception: %s\n",e.what());
return -1;
}
}
EXPORTED int flann_free_index(FLANN_INDEX index_ptr, FLANNParameters* flann_params)
{
try {
init_flann_parameters(flann_params);
if (index_ptr==NULL) {
throw FLANNException("Invalid index");
}
Index* index = (Index*) index_ptr;
delete index;
return 0;
}
catch(runtime_error& e) {
logger.error("Caught exception: %s\n",e.what());
return -1;
}
}
EXPORTED int flann_compute_cluster_centers(float* dataset, int rows, int cols, int clusters, float* result, FLANNParameters* flann_params)
{
try {
init_flann_parameters(flann_params);
MatrixPtr inputData = new Matrix<float>(rows,cols,dataset);
KMeansIndexParams params(flann_params->branching, flann_params->iterations, flann_params->centers_init, flann_params->cb_index);
Matrix<float> centers(clusters, cols, result);
int clusterNum = hierarchicalClustering(*inputData,centers, params);
return clusterNum;
} catch (runtime_error& e) {
logger.error("Caught exception: %s\n",e.what());
return -1;
}
}
EXPORTED void compute_ground_truth_float(float* dataset, int dshape[], float* testset, int tshape[], int* match, int mshape[], int skip)
{
assert(dshape[1]==tshape[1]);
assert(tshape[0]==mshape[0]);
Matrix<int> _match(mshape[0], mshape[1], match);
compute_ground_truth(Matrix<float>(dshape[0], dshape[1], dataset), Matrix<float>(tshape[0], tshape[1], testset), _match, skip);
}
EXPORTED float test_with_precision(FLANN_INDEX index_ptr, float* dataset, int dshape[], float* testset, int tshape[], int* matches, int mshape[],
int nn, float precision, int* checks, int skip = 0)
{
assert(dshape[1]==tshape[1]);
assert(tshape[0]==mshape[0]);
try {
if (index_ptr==NULL) {
throw FLANNException("Invalid index");
}
NNIndexPtr index = (NNIndexPtr)index_ptr;
return test_index_precision(*index, Matrix<float>(dshape[0], dshape[1],dataset), Matrix<float>(tshape[0], tshape[1], testset),
Matrix<int>(mshape[0],mshape[1],matches), precision, *checks, nn, skip);
} catch (runtime_error& e) {
logger.error("Caught exception: %s\n",e.what());
return -1;
}
}
EXPORTED float test_with_checks(FLANN_INDEX index_ptr, float* dataset, int dshape[], float* testset, int tshape[], int* matches, int mshape[],
int nn, int checks, float* precision, int skip = 0)
{
assert(dshape[1]==tshape[1]);
assert(tshape[0]==mshape[0]);
try {
if (index_ptr==NULL) {
throw FLANNException("Invalid index");
}
NNIndexPtr index = (NNIndexPtr)index_ptr;
return test_index_checks(*index, Matrix<float>(dshape[0], dshape[1],dataset), Matrix<float>(tshape[0], tshape[1], testset),
Matrix<int>(mshape[0],mshape[1],matches), checks, *precision, nn, skip);
} catch (runtime_error& e) {
logger.error("Caught exception: %s\n",e.what());
return -1;
}
}