1 #include <gtest/gtest.h>
2 #include <time.h>
3 
4 #include <flann/flann.h>
5 #include <flann/io/hdf5.h>
6 #include <flann/nn/ground_truth.h>
7 
8 #include "flann_tests.h"
9 
10 using namespace flann;
11 
12 
13 
14 
15 
16 class Autotuned_SIFT100K : public FLANNTestFixture {
17 protected:
18     flann::Matrix<float> data;
19     flann::Matrix<float> query;
20     flann::Matrix<size_t> match;
21     flann::Matrix<float> dists;
22     flann::Matrix<size_t> indices;
23 
SetUp()24     void SetUp()
25     {
26         dists = flann::Matrix<float>(new float[1000*5], 1000, 5);
27         indices = flann::Matrix<size_t>(new size_t[1000*5], 1000, 5);
28         printf("Reading test data...");
29         fflush(stdout);
30         flann::load_from_file(data, "sift100K.h5","dataset");
31         flann::load_from_file(query,"sift100K.h5","query");
32         flann::load_from_file(match,"sift100K.h5","match");
33         printf("done\n");
34     }
35 
TearDown()36     void TearDown()
37     {
38         delete[] data.ptr();
39         delete[] query.ptr();
40         delete[] match.ptr();
41         delete[] dists.ptr();
42         delete[] indices.ptr();
43     }
44 };
45 
46 
TEST_F(Autotuned_SIFT100K,TestSearch)47 TEST_F(Autotuned_SIFT100K, TestSearch)
48 {
49     flann::log_verbosity(FLANN_LOG_INFO);
50 
51     Index<L2<float> > index(data, flann::AutotunedIndexParams(0.8,0.01,0,0.1)); // 80% precision
52 
53     start_timer("Building autotuned index...");
54     index.buildIndex();
55     printf("done (%g seconds)\n", stop_timer());
56 
57     index.save("autotuned.idx");
58 
59     start_timer("Searching KNN...");
60     index.knnSearch(query, indices, dists, 5, flann::SearchParams(FLANN_CHECKS_AUTOTUNED) );
61     printf("done (%g seconds)\n", stop_timer());
62 
63     float precision = compute_precision(match, indices);
64     EXPECT_GE(precision, 0.75);
65     printf("Precision: %g\n", precision);
66 }
67 
68 
TEST_F(Autotuned_SIFT100K,SavedTest)69 TEST_F(Autotuned_SIFT100K, SavedTest)
70 {
71     float precision;
72 
73     // -------------------------------------
74     // autotuned index
75     printf("Loading autotuned index\n");
76     flann::Index<L2<float> > autotuned_index(data, flann::SavedIndexParams("autotuned.idx"));
77 
78     const flann::IndexParams index_params = autotuned_index.getParameters();
79     printf("The index has the following parameters:\n");
80     flann::print_params(index_params);
81 
82     printf("Index type is: %d\n", autotuned_index.getType());
83 
84     start_timer("Searching KNN...");
85     autotuned_index.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
86     printf("done (%g seconds)\n", stop_timer());
87 
88     precision = compute_precision(match, indices);
89     EXPECT_GE(precision, 0.75);
90     printf("Precision: %g\n", precision);
91 }
92 
TEST_F(Autotuned_SIFT100K,TestCopy)93 TEST_F(Autotuned_SIFT100K, TestCopy)
94 {
95     float precision;
96 
97     // -------------------------------------
98     // autotuned index
99     printf("Loading autotuned index\n");
100     flann::Index<L2<float> > index(data, flann::SavedIndexParams("autotuned.idx"));
101 
102     start_timer("Searching KNN...");
103     index.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
104     printf("done (%g seconds)\n", stop_timer());
105 
106     precision = compute_precision(match, indices);
107     EXPECT_GE(precision, 0.75);
108     printf("Precision: %g\n", precision);
109 
110 
111     // test copy constructor
112     Index<L2<float> > index2(index);
113 
114     start_timer("Searching KNN...");
115     index2.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
116     printf("done (%g seconds)\n", stop_timer());
117 
118     float precision2 = compute_precision(match, indices);
119     printf("Precision: %g\n", precision2);
120     EXPECT_EQ(precision, precision2);
121 
122     // test assignment operator
123     Index<L2<float> > index3(data, flann::LinearIndexParams());
124     index3 = index;
125 
126     start_timer("Searching KNN...");
127     index3.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
128     printf("done (%g seconds)\n", stop_timer());
129 
130     float precision3 = compute_precision(match, indices);
131     printf("Precision: %g\n", precision3);
132     EXPECT_EQ(precision, precision3);
133 }
134 
135 
TEST_F(Autotuned_SIFT100K,TestCopy2)136 TEST_F(Autotuned_SIFT100K, TestCopy2)
137 {
138     float precision;
139 
140     // -------------------------------------
141     // autotuned index
142     printf("Loading autotuned index\n");
143     flann::AutotunedIndex<L2<float> > index(data);
144     FILE* f = fopen("autotuned.idx", "r");
145     index.loadIndex(f);
146     fclose(f);
147 
148     start_timer("Searching KNN...");
149     index.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
150     printf("done (%g seconds)\n", stop_timer());
151 
152     precision = compute_precision(match, indices);
153     EXPECT_GE(precision, 0.75);
154     printf("Precision: %g\n", precision);
155 
156 
157     // test copy constructor
158     AutotunedIndex<L2<float> > index2(index);
159 
160     start_timer("Searching KNN...");
161     index2.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
162     printf("done (%g seconds)\n", stop_timer());
163 
164     float precision2 = compute_precision(match, indices);
165     printf("Precision: %g\n", precision2);
166     EXPECT_EQ(precision, precision2);
167 
168     // test assignment operator
169     AutotunedIndex<L2<float> > index3(data);
170     index3 = index;
171 
172     start_timer("Searching KNN...");
173     index3.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
174     printf("done (%g seconds)\n", stop_timer());
175 
176     float precision3 = compute_precision(match, indices);
177     printf("Precision: %g\n", precision3);
178     EXPECT_EQ(precision, precision3);
179 }
180 
181 
182 
main(int argc,char ** argv)183 int main(int argc, char** argv)
184 {
185     testing::InitGoogleTest(&argc, argv);
186     return RUN_ALL_TESTS();
187 }
188