diff --git a/cmake/flann_utils.cmake b/cmake/flann_utils.cmake index 232012c07aab149444757593168f3b9ed3fa2abc..1a1093918c60b06d2ed92d09756b01f00c35bdc3 100644 --- a/cmake/flann_utils.cmake +++ b/cmake/flann_utils.cmake @@ -1,14 +1,6 @@ macro(GET_OS_INFO) string(REGEX MATCH "Linux" OS_IS_LINUX ${CMAKE_SYSTEM_NAME}) - if(OS_IS_LINUX) - if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64") - set(FLANN_LIB_INSTALL_DIR "lib64") - else(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64") - set(FLANN_LIB_INSTALL_DIR "lib") - endif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64") - else(OS_IS_LINUX) - set(FLANN_LIB_INSTALL_DIR "lib") - endif(OS_IS_LINUX) + set(FLANN_LIB_INSTALL_DIR "lib") set(FLANN_INCLUDE_INSTALL_DIR "include/${PROJECT_NAME_LOWER}-${FLANN_MAJOR_VERSION}.${FLANN_MINOR_VERSION}") endmacro(GET_OS_INFO) diff --git a/doc/manual.tex b/doc/manual.tex index f4b4cd1b2b529384799ac9bbad47e9d5732fcaae..3afd731843836c8029e1bef460ecb3aaead8cbab 100644 --- a/doc/manual.tex +++ b/doc/manual.tex @@ -482,7 +482,7 @@ optimized for searching lower dimensionality data (for example 3D point clouds) \begin{Verbatim}[fontsize=\footnotesize] struct KDTreeSingleIndexParams : public IndexParams { - KDTreeSingleIndexParams( int max_leaf_size = 10 ); + KDTreeSingleIndexParams( int leaf_max_size = 10 ); }; \end{Verbatim} \begin{description} @@ -511,7 +511,7 @@ struct HierarchicalClusteringIndexParams : public IndexParams { HierarchicalClusteringIndexParams(int branching = 32, flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, - int trees = 4, int leaf_size = 100) + int trees = 4, int leaf_max_size = 100) }; \end{Verbatim} \begin{description} @@ -874,6 +874,11 @@ struct FLANNParameters { float memory_weight; /* index memory weigthing factor */ float sample_fraction; /* what fraction of the dataset to use for autotuning */ + /* LSH parameters */ + unsigned int table_number_; /** The number of hash tables to use */ + unsigned int key_size_; /** The length of the key in the hash tables */ + unsigned int multi_probe_level_; /** Number of levels to use in multi-probe LSH, 0 for standard LSH */ + /* other parameters */ enum flann_log_level_t log_level; /* determines the verbosity of each flann function */ long random_seed; /* random seed to use */ @@ -1327,14 +1332,15 @@ used: \subsubsection{Example 1:} -In this example the index is constructed using automatic parameter estimation, requesting 90\% as desired precision and using the default values for the build time and memory usage factors. The index is then used to search for the nearest-neighbors of the points in the testset matrix and finally the index is deleted. +In this example the index is constructed using automatic parameter estimation, requesting 70\% as desired precision and using the default values for the build time and memory usage factors. The index is then used to search for the nearest-neighbors of the points in the testset matrix and finally the index is deleted. \begin{Verbatim}[fontsize=\footnotesize,frame=single] dataset = single(rand(128,10000)); testset = single(rand(128,1000)); -build_params.target_precision = 0.9; +build_params.algorithm = 'autotuned'; +build_params.target_precision = 0.7; build_params.build_weight = 0.01; build_params.memory_weight = 0; @@ -1435,7 +1441,7 @@ dataset = rand(10000, 128) testset = rand(1000, 128) flann = FLANN() -params = flann.build_index(dataset, target_precision=0.9, log_level = "info"); +params = flann.build_index(dataset, algorithm="autotuned", target_precision=0.9, log_level = "info"); print params result, dists = flann.nn_index(testset,5, checks=params["checks"]); diff --git a/src/cpp/CMakeLists.txt b/src/cpp/CMakeLists.txt index 69722e38ba267c8c7f06bb2ac4bc8cdca4ad21d6..f37cefda3e2727419166dd7a22be59ba57ebb190 100644 --- a/src/cpp/CMakeLists.txt +++ b/src/cpp/CMakeLists.txt @@ -63,16 +63,7 @@ if (BUILD_CUDA_LIB) endif() -#debug libraries -add_library(flann_cpp-gd SHARED ${CPP_SOURCES}) -set_target_properties(flann_cpp-gd PROPERTIES - COMPILE_FLAGS ${CMAKE_CXX_FLAGS_DEBUG} - DEFINE_SYMBOL FLANN_EXPORTS -) -add_library(flann_cpp_s-gd STATIC ${CPP_SOURCES}) -set_target_properties(flann_cpp_s-gd PROPERTIES COMPILE_FLAGS ${CMAKE_CXX_FLAGS_DEBUG}) -set_property(TARGET flann_cpp_s-gd PROPERTY COMPILE_DEFINITIONS FLANN_STATIC) @@ -95,17 +86,6 @@ if (HDF5_FOUND) endif() endif() -if (BUILD_CUDA_LIB) - add_library(flann_cuda-gd SHARED ${CPP_SOURCES}) - set_target_properties(flann_cuda-gd PROPERTIES - COMPILE_FLAGS ${CMAKE_CXX_FLAGS_DEBUG} - DEFINE_SYMBOL FLANN_EXPORTS - ) - - add_library(flann_cuda_s-gd STATIC ${CPP_SOURCES}) - set_target_properties(flann_cuda_s-gd PROPERTIES COMPILE_FLAGS ${CMAKE_CXX_FLAGS_DEBUG}) - set_property(TARGET flann_cuda_s-gd PROPERTY COMPILE_DEFINITIONS FLANN_STATIC) -endif() if (BUILD_C_BINDINGS) add_library(flann_s STATIC ${C_SOURCES}) @@ -129,7 +109,6 @@ if (BUILD_C_BINDINGS) ) endif() - if(WIN32) if (BUILD_C_BINDINGS) install ( @@ -141,7 +120,7 @@ endif(WIN32) install ( - TARGETS flann_cpp flann_cpp_s flann_cpp-gd flann_cpp_s-gd + TARGETS flann_cpp flann_cpp_s RUNTIME DESTINATION bin LIBRARY DESTINATION ${FLANN_LIB_INSTALL_DIR} ARCHIVE DESTINATION ${FLANN_LIB_INSTALL_DIR} @@ -149,7 +128,7 @@ install ( if (BUILD_CUDA_LIB) install ( - TARGETS flann_cuda flann_cuda_s flann_cuda-gd flann_cuda_s-gd + TARGETS flann_cuda flann_cuda_s RUNTIME DESTINATION bin LIBRARY DESTINATION ${FLANN_LIB_INSTALL_DIR} ARCHIVE DESTINATION ${FLANN_LIB_INSTALL_DIR} diff --git a/src/cpp/flann/algorithms/hierarchical_clustering_index.h b/src/cpp/flann/algorithms/hierarchical_clustering_index.h index ed68ecb34825f28df3e69f1653ac7248e0c3b68d..292ef582bf100ee99f0959118fac820fd29cb137 100644 --- a/src/cpp/flann/algorithms/hierarchical_clustering_index.h +++ b/src/cpp/flann/algorithms/hierarchical_clustering_index.h @@ -56,7 +56,7 @@ struct HierarchicalClusteringIndexParams : public IndexParams { HierarchicalClusteringIndexParams(int branching = 32, flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, - int trees = 4, int leaf_size = 100) + int trees = 4, int leaf_max_size = 100) { (*this)["algorithm"] = FLANN_INDEX_HIERARCHICAL; // The branching factor used in the hierarchical clustering @@ -66,7 +66,7 @@ struct HierarchicalClusteringIndexParams : public IndexParams // number of parallel trees to build (*this)["trees"] = trees; // maximum leaf size - (*this)["leaf_size"] = leaf_size; + (*this)["leaf_max_size"] = leaf_max_size; } }; @@ -101,7 +101,7 @@ public: branching_ = get_param(index_params_,"branching",32); centers_init_ = get_param(index_params_,"centers_init", FLANN_CENTERS_RANDOM); trees_ = get_param(index_params_,"trees",4); - leaf_size_ = get_param(index_params_,"leaf_size",100); + leaf_max_size_ = get_param(index_params_,"leaf_max_size",100); initCenterChooser(); } @@ -123,7 +123,7 @@ public: branching_ = get_param(index_params_,"branching",32); centers_init_ = get_param(index_params_,"centers_init", FLANN_CENTERS_RANDOM); trees_ = get_param(index_params_,"trees",4); - leaf_size_ = get_param(index_params_,"leaf_size",100); + leaf_max_size_ = get_param(index_params_,"leaf_max_size",100); initCenterChooser(); chooseCenters_->setDataset(inputData); @@ -138,7 +138,7 @@ public: branching_(other.branching_), trees_(other.trees_), centers_init_(other.centers_init_), - leaf_size_(other.leaf_size_) + leaf_max_size_(other.leaf_max_size_) { initCenterChooser(); @@ -256,7 +256,7 @@ public: ar & branching_; ar & trees_; ar & centers_init_; - ar & leaf_size_; + ar & leaf_max_size_; if (Archive::is_loading::value) { tree_roots_.resize(trees_); @@ -273,7 +273,7 @@ public: index_params_["branching"] = branching_; index_params_["trees"] = trees_; index_params_["centers_init"] = centers_init_; - index_params_["leaf_size"] = leaf_size_; + index_params_["leaf_size"] = leaf_max_size_; } } @@ -485,7 +485,7 @@ private: */ void computeClustering(NodePtr node, int* indices, int indices_length) { - if (indices_length < leaf_size_) { // leaf node + if (indices_length < leaf_max_size_) { // leaf node node->points.resize(indices_length); for (int i=0;ipoints[i].index = indices[i]; @@ -638,7 +638,7 @@ private: std::swap(branching_, other.branching_); std::swap(trees_, other.trees_); std::swap(centers_init_, other.centers_init_); - std::swap(leaf_size_, other.leaf_size_); + std::swap(leaf_max_size_, other.leaf_max_size_); std::swap(chooseCenters_, other.chooseCenters_); } @@ -687,7 +687,7 @@ private: /** * Max size of leaf nodes */ - int leaf_size_; + int leaf_max_size_; /** * Algorithm used to choose initial centers diff --git a/src/cpp/flann/algorithms/kdtree_single_index.h b/src/cpp/flann/algorithms/kdtree_single_index.h index f3ac3cb47a3b9ad349cc681bb6e9d2ca31e7f156..96013f38fd78c574ea71cb91e4132de58be12648 100644 --- a/src/cpp/flann/algorithms/kdtree_single_index.h +++ b/src/cpp/flann/algorithms/kdtree_single_index.h @@ -530,6 +530,8 @@ private: if (lim1>count/2) index = lim1; else if (lim2 0 && index < count); } @@ -544,7 +546,6 @@ private: */ void planeSplit(int* ind, int count, int cutfeat, DistanceType cutval, int& lim1, int& lim2) { - /* Move vector indices for left subtree to front of list. */ int left = 0; int right = count-1; for (;; ) { @@ -553,9 +554,6 @@ private: if (left>right) break; std::swap(ind[left], ind[right]); ++left; --right; } - /* If either list is empty, it means that all remaining features - * are identical. Split in the middle to maintain a balanced tree. - */ lim1 = left; right = count-1; for (;; ) { diff --git a/src/cpp/flann/flann.cpp b/src/cpp/flann/flann.cpp index 4ad7ccb9ab76e0c076c0ee9a94aca6da6772f829..31de8369f5470867824ff2f597a68c064b03196a 100644 --- a/src/cpp/flann/flann.cpp +++ b/src/cpp/flann/flann.cpp @@ -62,6 +62,7 @@ flann::IndexParams create_parameters(FLANNParameters* p) params["trees"] = p->trees; params["leaf_max_size"] = p->leaf_max_size; } + #ifdef FLANN_USE_CUDA if (p->algorithm == FLANN_INDEX_KDTREE_CUDA) { params["leaf_max_size"] = p->leaf_max_size; @@ -85,7 +86,7 @@ flann::IndexParams create_parameters(FLANNParameters* p) params["branching"] = p->branching; params["centers_init"] = p->centers_init; params["trees"] = p->trees; - params["leaf_size"] = p->leaf_max_size; + params["leaf_max_size"] = p->leaf_max_size; } if (p->algorithm == FLANN_INDEX_LSH) { @@ -102,6 +103,57 @@ flann::IndexParams create_parameters(FLANNParameters* p) + +void update_flann_parameters(const IndexParams& params, FLANNParameters* flann_params) +{ + if (has_param(params,"algorithm")) { + flann_params->algorithm = get_param(params,"algorithm"); + } + if (has_param(params,"trees")) { + flann_params->trees = get_param(params,"trees"); + } + if (has_param(params,"leaf_max_size")) { + flann_params->leaf_max_size = get_param(params,"leaf_max_size"); + } + if (has_param(params,"branching")) { + flann_params->branching = get_param(params,"branching"); + } + if (has_param(params,"iterations")) { + flann_params->iterations = get_param(params,"iterations"); + } + if (has_param(params,"centers_init")) { + flann_params->centers_init = get_param(params,"centers_init"); + } + if (has_param(params,"target_precision")) { + flann_params->target_precision = get_param(params,"target_precision"); + } + if (has_param(params,"build_weight")) { + flann_params->build_weight = get_param(params,"build_weight"); + } + if (has_param(params,"memory_weight")) { + flann_params->memory_weight = get_param(params,"memory_weight"); + } + if (has_param(params,"sample_fraction")) { + flann_params->sample_fraction = get_param(params,"sample_fraction"); + } + if (has_param(params,"table_number")) { + flann_params->table_number_ = get_param(params,"table_number"); + } + if (has_param(params,"key_size")) { + flann_params->key_size_ = get_param(params,"key_size"); + } + if (has_param(params,"multi_probe_level")) { + flann_params->multi_probe_level_ = get_param(params,"multi_probe_level"); + } + if (has_param(params,"log_level")) { + flann_params->log_level = get_param(params,"log_level"); + } + if (has_param(params,"random_seed")) { + flann_params->random_seed = get_param(params,"random_seed"); + } +} + + void init_flann_parameters(FLANNParameters* p) { if (p != NULL) { @@ -142,10 +194,10 @@ flann_index_t __flann_build_index(typename Distance::ElementType* dataset, int r IndexParams params = create_parameters(flann_params); Index* index = new Index(Matrix(dataset,rows,cols), params, d); index->buildIndex(); - params = index->getParameters(); - if (index->getType()==FLANN_INDEX_AUTOTUNED) { + if (flann_params->algorithm==FLANN_INDEX_AUTOTUNED) { IndexParams params = index->getParameters(); + update_flann_parameters(params,flann_params); SearchParams search_params = get_param(params,"search_params"); *speedup = get_param(params,"speedup"); flann_params->checks = search_params.checks; diff --git a/src/matlab/flann_build_index.m b/src/matlab/flann_build_index.m index 9399385af7b48ec365c8cef70b5f22c7fde01ede..2c8553c9335d36361ab3221fe3b8b84ca72eb484 100644 --- a/src/matlab/flann_build_index.m +++ b/src/matlab/flann_build_index.m @@ -6,7 +6,7 @@ function [index, params, speedup] = flann_build_index(dataset, build_params) % Marius Muja, January 2008 - algos = struct( 'linear', 0, 'kdtree', 1, 'kmeans', 2, 'composite', 3, 'kdtree_single', 4, 'saved', 254, 'autotuned', 255 ); + algos = struct( 'linear', 0, 'kdtree', 1, 'kmeans', 2, 'composite', 3, 'kdtree_single', 4, 'hierarchical', 5, 'lsh', 6, 'saved', 254, 'autotuned', 255 ); center_algos = struct('random', 0, 'gonzales', 1, 'kmeanspp', 2 ); log_levels = struct('none', 0, 'fatal', 1, 'error', 2, 'warning', 3, 'info', 4); function value = id2value(map, id) diff --git a/src/matlab/nearest_neighbors.cpp b/src/matlab/nearest_neighbors.cpp index 3b77c167b3aa90e8485a59ff419385db731b4009..743ffbde79ec1eff07249d2debc08bfb1f1bd7ff 100644 --- a/src/matlab/nearest_neighbors.cpp +++ b/src/matlab/nearest_neighbors.cpp @@ -87,11 +87,15 @@ static void flannStructToMatlabStruct( const FLANNParameters& flannParams, mxArr { mxSetField(mexParams, 0, "algorithm", to_mx_array(flannParams.algorithm)); mxSetField(mexParams, 0, "checks", to_mx_array(flannParams.checks)); + mxSetField(mexParams, 0, "cb_index", to_mx_array(flannParams.cb_index)); + mxSetField(mexParams, 0, "eps", to_mx_array(flannParams.eps)); + mxSetField(mexParams, 0, "trees", to_mx_array(flannParams.trees)); + mxSetField(mexParams, 0, "leaf_max_size", to_mx_array(flannParams.trees)); + mxSetField(mexParams, 0, "branching", to_mx_array(flannParams.branching)); mxSetField(mexParams, 0, "iterations", to_mx_array(flannParams.iterations)); mxSetField(mexParams, 0, "centers_init", to_mx_array(flannParams.centers_init)); - mxSetField(mexParams, 0, "cb_index", to_mx_array(flannParams.cb_index)); } @@ -384,7 +388,7 @@ static void _build_index(int nOutArray, mxArray* OutArray[], int nInArray, const pOut[0] = typedIndex; if (nOutArray > 1) { - const char* fieldnames[] = {"algorithm", "checks", "trees", "branching", "iterations", "centers_init", "cb_index"}; + const char* fieldnames[] = {"algorithm", "checks", "cb_index", "eps", "trees", "leaf_max_size", "branching", "iterations", "centers_init"}; OutArray[1] = mxCreateStructMatrix(1, 1, sizeof(fieldnames)/sizeof(const char*), fieldnames); flannStructToMatlabStruct(p, OutArray[1]); } diff --git a/src/python/pyflann/flann_ctypes.py b/src/python/pyflann/flann_ctypes.py index eb2379175ea19189e7bc78ae3d3ca3ef34d24c4b..0e6c1f49f517cb4a881c78c96cb325b7dd61c402 100644 --- a/src/python/pyflann/flann_ctypes.py +++ b/src/python/pyflann/flann_ctypes.py @@ -121,7 +121,7 @@ class FLANNParameters(CustomStructure): 'random_seed' : -1 } _translation_ = { - "algorithm" : {"linear" : 0, "kdtree" : 1, "kmeans" : 2, "composite" : 3, "kdtree_simple" : 4, "saved": 254, "autotuned" : 255, "default" : 1}, + "algorithm" : {"linear" : 0, "kdtree" : 1, "kmeans" : 2, "composite" : 3, "kdtree_single" : 4, "hierarchical": 5, "lsh": 6, "saved": 254, "autotuned" : 255, "default" : 1}, "centers_init" : {"random" : 0, "gonzales" : 1, "kmeanspp" : 2, "default" : 0}, "log_level" : {"none" : 0, "fatal" : 1, "error" : 2, "warning" : 3, "info" : 4, "default" : 2} }