Permalink
Browse files

Added cmake support for fastann and libkdtree

  • Loading branch information...
pranjal-rai committed Jul 9, 2017
1 parent fb48194 commit 3935d68550d11de5cde22c849924fb9f7f31a624
View
@@ -2,4 +2,6 @@ build*
install*
*~
CMakeLists.txt.user
3rdparty/flann*
3rdparty/libkdtree*
3rdparty/fastann*

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,13 @@
include(ExternalProject)
ExternalProject_Add(
fastann
GIT_REPOSITORY "https://github.com/philbinj/fastann.git"
GIT_TAG "master"
UPDATE_COMMAND ""
PATCH_COMMAND ""
SOURCE_DIR "${CMAKE_SOURCE_DIR}/3rdparty/fastann"
TEST_COMMAND ""
)
@@ -0,0 +1,16 @@
include(ExternalProject)
ExternalProject_Add(
flann
GIT_REPOSITORY "https://github.com/mariusmuja/flann.git"
GIT_TAG "master"
UPDATE_COMMAND ""
PATCH_COMMAND ""
SOURCE_DIR "${CMAKE_SOURCE_DIR}/3rdparty/flann"
CMAKE_ARGS -DBUILD_C_BINDINGS=OFF -DBUILD_PYTHON_BINDINGS=OFF -DBUILD_MATLAB_BINDINGS=OFF -DBUILD_CUDA_LIB=OFF -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_DOC=OFF
TEST_COMMAND ""
)
@@ -0,0 +1,13 @@
include(ExternalProject)
ExternalProject_Add(
libkdtree
GIT_REPOSITORY "https://github.com/nvmd/libkdtree.git"
GIT_TAG "master"
UPDATE_COMMAND ""
PATCH_COMMAND ""
SOURCE_DIR "${CMAKE_SOURCE_DIR}/3rdparty/libkdtree"
TEST_COMMAND ""
)
View
@@ -102,7 +102,9 @@ set(INSTALL_CMAKE_DIR "lib${LIB_SUFFIX}/cmake/nanoflann")
set(INSTALL_COPYRIGHT_DIR "share/doc/libnanoflann-dev")
# 3rdparty Libraries
include(3rdparty/CMakeLists-ExternalProjects.txt)
include(3rdparty/CMakeLists-flann.txt)
include(3rdparty/CMakeLists-fastann.txt)
include(3rdparty/CMakeLists-libkdtree.txt)
# Generate the cmake config and cmake config-version file:
SET(RELATIVE_INCLUDE_DIR "../../../include")
@@ -1,11 +1,18 @@
set(flann_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/3rdparty/flann/src/cpp")
include_directories(${flann_INCLUDE_DIRS})
set(flann_LIBRARIES "${CMAKE_SOURCE_DIR}/build/flann-prefix/src/flann-build/lib/libflann.so")
# benchmark files:
ADD_EXECUTABLE(nanoflann_test nanoflann_test.cpp)
TARGET_LINK_LIBRARIES(nanoflann_test nanoflann)
set(flann_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/3rdparty/flann/src/cpp")
include_directories(${flann_INCLUDE_DIRS})
set(flann_LIBRARIES "${CMAKE_SOURCE_DIR}/build/flann-prefix/src/flann-build/lib/libflann_cpp.so")
ADD_EXECUTABLE(flann_test flann_test.cpp)
TARGET_LINK_LIBRARIES(flann_test nanoflann ${flann_LIBRARIES})
set(fastann_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/3rdparty/fastann")
include_directories(${fastann_INCLUDE_DIRS})
set(fastann_LIBRARIES "${CMAKE_SOURCE_DIR}/build/fastann-prefix/src/fastann-build/libfastann.so")
ADD_EXECUTABLE(fastann_test fastann_test.cpp)
TARGET_LINK_LIBRARIES(fastann_test nanoflann ${fastann_LIBRARIES})
set(libkdtree_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/3rdparty/libkdtree")
include_directories(${libkdtree_INCLUDE_DIRS})
ADD_EXECUTABLE(libkdtree_test libkdtree_test.cpp)
@@ -0,0 +1,133 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <stdint.h>
#include "fastann.hpp"
#include "rand_point_gen.hpp"
static inline uint64_t rdtsc()
{
#ifdef __i386__
uint32_t a, d;
#elif defined __x86_64__
uint64_t a, d;
#endif
asm volatile ("rdtsc" : "=a" (a), "=d" (d));
return ((uint64_t)a | (((uint64_t)d)<<32));
}
template<class Float>
int
test_kdtree(unsigned N, unsigned D, double min_accuracy)
{
Float* pnts = fastann::gen_unit_random<Float>(N, D, 42);
Float* qus = fastann::gen_unit_random<Float>(N, D, 43);
std::vector<Float> mins_exact(N);
std::vector<unsigned> argmins_exact(N);
std::vector<Float> mins_kdt(N);
std::vector<unsigned> argmins_kdt(N);
fastann::nn_obj<Float>* nnobj_exact = fastann::nn_obj_build_exact(pnts, N, D);
fastann::nn_obj<Float>* nnobj_kdt = fastann::nn_obj_build_kdtree(pnts, N, D, 8, 768);
nnobj_exact->search_nn(qus, N, &argmins_exact[0], &mins_exact[0]);
nnobj_kdt->search_nn(qus, N, &argmins_kdt[0], &mins_kdt[0]);
unsigned num_same = 0;
for (unsigned n = 0; n < N; ++n) {
if (argmins_exact[n] == argmins_kdt[n]) num_same++;
}
double accuracy = (double)num_same/N;
printf("Accuracy: %.1f%%\n", accuracy*100.0);
if (accuracy > min_accuracy) return 1;
else return 0;
delete[] pnts;
delete[] qus;
delete nnobj_exact;
delete nnobj_kdt;
}
template<>
int
test_kdtree<unsigned char>(unsigned N, unsigned D, double min_accuracy)
{
double* pntst = fastann::gen_unit_random<double>(N, D, 42);
double* qust = fastann::gen_unit_random<double>(N, D, 43);
unsigned char* pnts = new unsigned char[N*D];
unsigned char* qus = new unsigned char[N*D];
for (unsigned n=0; n < N; ++n) {
for (unsigned d=0; d < D; ++d) {
pnts[n*D + d] = (unsigned char)(256.0 * pntst[n*D + d]);
qus[n*D + d] = (unsigned char)(256.0 * qust[n*D + d]);
}
}
std::vector<unsigned> mins_exact(N);
std::vector<unsigned> argmins_exact(N);
std::vector<unsigned> mins_kdt(N);
std::vector<unsigned> argmins_kdt(N);
fastann::nn_obj<unsigned char>* nnobj_exact = fastann::nn_obj_build_exact(pnts, N, D);
fastann::nn_obj<unsigned char>* nnobj_kdt = fastann::nn_obj_build_kdtree(pnts, N, D, 8, 768);
nnobj_exact->search_nn(qus, N, &argmins_exact[0], &mins_exact[0]);
nnobj_kdt->search_nn(qus, N, &argmins_kdt[0], &mins_kdt[0]);
unsigned num_same = 0;
for (unsigned n = 0; n < N; ++n) {
if (argmins_exact[n] == argmins_kdt[n]) num_same++;
}
double accuracy = (double)num_same/N;
printf("Accuracy: %.1f%%\n", accuracy*100.0);
if (accuracy > min_accuracy) return 1;
else return 0;
delete[] pnts;
delete[] qus;
delete nnobj_exact;
delete nnobj_kdt;
}
int
main()
{
unsigned N = 10000;
unsigned D = 128;
unsigned num_failed = 0;
unsigned num_passed = 0;
double min_accuracy = 0.36; // Seems to be right.
// This should be repeatable everywhere because of randomkit.c
// It sounds low, but is to be expected with unit random vectors
// With SIFT we do _much_ better.
if (test_kdtree<unsigned char>(N, D, min_accuracy)) { num_passed++; }
else { num_failed++; }
if (test_kdtree<float>(N, D, min_accuracy)) { num_passed++; }
else { num_failed++; }
if (test_kdtree<double>(N, D, min_accuracy)) { num_passed++; }
else { num_failed++; }
printf("NUM_PASSED %d NUM_FAILED %d\n", num_passed, num_failed);
if (num_failed) return -1;
else return 0;
}
Oops, something went wrong.

0 comments on commit 3935d68

Please sign in to comment.