index_testing.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /***********************************************************************
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
  5. * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
  6. *
  7. * THE BSD LICENSE
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  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.
  18. *
  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. *************************************************************************/
  30. #ifndef OPENCV_FLANN_INDEX_TESTING_H_
  31. #define OPENCV_FLANN_INDEX_TESTING_H_
  32. #include <cstring>
  33. #include <cassert>
  34. #include <cmath>
  35. #include "matrix.h"
  36. #include "nn_index.h"
  37. #include "result_set.h"
  38. #include "logger.h"
  39. #include "timer.h"
  40. namespace cvflann
  41. {
  42. inline int countCorrectMatches(int* neighbors, int* groundTruth, int n)
  43. {
  44. int count = 0;
  45. for (int i=0; i<n; ++i) {
  46. for (int k=0; k<n; ++k) {
  47. if (neighbors[i]==groundTruth[k]) {
  48. count++;
  49. break;
  50. }
  51. }
  52. }
  53. return count;
  54. }
  55. template <typename Distance>
  56. typename Distance::ResultType computeDistanceRaport(const Matrix<typename Distance::ElementType>& inputData, typename Distance::ElementType* target,
  57. int* neighbors, int* groundTruth, int veclen, int n, const Distance& distance)
  58. {
  59. typedef typename Distance::ResultType DistanceType;
  60. DistanceType ret = 0;
  61. for (int i=0; i<n; ++i) {
  62. DistanceType den = distance(inputData[groundTruth[i]], target, veclen);
  63. DistanceType num = distance(inputData[neighbors[i]], target, veclen);
  64. if ((den==0)&&(num==0)) {
  65. ret += 1;
  66. }
  67. else {
  68. ret += num/den;
  69. }
  70. }
  71. return ret;
  72. }
  73. template <typename Distance>
  74. float search_with_ground_truth(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
  75. const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches, int nn, int checks,
  76. float& time, typename Distance::ResultType& dist, const Distance& distance, int skipMatches)
  77. {
  78. typedef typename Distance::ResultType DistanceType;
  79. if (matches.cols<size_t(nn)) {
  80. Logger::info("matches.cols=%d, nn=%d\n",matches.cols,nn);
  81. throw FLANNException("Ground truth is not computed for as many neighbors as requested");
  82. }
  83. KNNResultSet<DistanceType> resultSet(nn+skipMatches);
  84. SearchParams searchParams(checks);
  85. std::vector<int> indices(nn+skipMatches);
  86. std::vector<DistanceType> dists(nn+skipMatches);
  87. int* neighbors = &indices[skipMatches];
  88. int correct = 0;
  89. DistanceType distR = 0;
  90. StartStopTimer t;
  91. int repeats = 0;
  92. while (t.value<0.2) {
  93. repeats++;
  94. t.start();
  95. correct = 0;
  96. distR = 0;
  97. for (size_t i = 0; i < testData.rows; i++) {
  98. resultSet.init(&indices[0], &dists[0]);
  99. index.findNeighbors(resultSet, testData[i], searchParams);
  100. correct += countCorrectMatches(neighbors,matches[i], nn);
  101. distR += computeDistanceRaport<Distance>(inputData, testData[i], neighbors, matches[i], (int)testData.cols, nn, distance);
  102. }
  103. t.stop();
  104. }
  105. time = float(t.value/repeats);
  106. float precicion = (float)correct/(nn*testData.rows);
  107. dist = distR/(testData.rows*nn);
  108. Logger::info("%8d %10.4g %10.5g %10.5g %10.5g\n",
  109. checks, precicion, time, 1000.0 * time / testData.rows, dist);
  110. return precicion;
  111. }
  112. template <typename Distance>
  113. float test_index_checks(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
  114. const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches,
  115. int checks, float& precision, const Distance& distance, int nn = 1, int skipMatches = 0)
  116. {
  117. typedef typename Distance::ResultType DistanceType;
  118. Logger::info(" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
  119. Logger::info("---------------------------------------------------------\n");
  120. float time = 0;
  121. DistanceType dist = 0;
  122. precision = search_with_ground_truth(index, inputData, testData, matches, nn, checks, time, dist, distance, skipMatches);
  123. return time;
  124. }
  125. template <typename Distance>
  126. float test_index_precision(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
  127. const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches,
  128. float precision, int& checks, const Distance& distance, int nn = 1, int skipMatches = 0)
  129. {
  130. typedef typename Distance::ResultType DistanceType;
  131. const float SEARCH_EPS = 0.001f;
  132. Logger::info(" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
  133. Logger::info("---------------------------------------------------------\n");
  134. int c2 = 1;
  135. float p2;
  136. int c1 = 1;
  137. //float p1;
  138. float time;
  139. DistanceType dist;
  140. p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
  141. if (p2>precision) {
  142. Logger::info("Got as close as I can\n");
  143. checks = c2;
  144. return time;
  145. }
  146. while (p2<precision) {
  147. c1 = c2;
  148. //p1 = p2;
  149. c2 *=2;
  150. p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
  151. }
  152. int cx;
  153. float realPrecision;
  154. if (fabs(p2-precision)>SEARCH_EPS) {
  155. Logger::info("Start linear estimation\n");
  156. // after we got to values in the vecinity of the desired precision
  157. // use linear approximation get a better estimation
  158. cx = (c1+c2)/2;
  159. realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
  160. while (fabs(realPrecision-precision)>SEARCH_EPS) {
  161. if (realPrecision<precision) {
  162. c1 = cx;
  163. }
  164. else {
  165. c2 = cx;
  166. }
  167. cx = (c1+c2)/2;
  168. if (cx==c1) {
  169. Logger::info("Got as close as I can\n");
  170. break;
  171. }
  172. realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
  173. }
  174. c2 = cx;
  175. p2 = realPrecision;
  176. }
  177. else {
  178. Logger::info("No need for linear estimation\n");
  179. cx = c2;
  180. realPrecision = p2;
  181. }
  182. checks = cx;
  183. return time;
  184. }
  185. template <typename Distance>
  186. void test_index_precisions(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
  187. const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches,
  188. float* precisions, int precisions_length, const Distance& distance, int nn = 1, int skipMatches = 0, float maxTime = 0)
  189. {
  190. typedef typename Distance::ResultType DistanceType;
  191. const float SEARCH_EPS = 0.001;
  192. // make sure precisions array is sorted
  193. std::sort(precisions, precisions+precisions_length);
  194. int pindex = 0;
  195. float precision = precisions[pindex];
  196. Logger::info(" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
  197. Logger::info("---------------------------------------------------------\n");
  198. int c2 = 1;
  199. float p2;
  200. int c1 = 1;
  201. float p1;
  202. float time;
  203. DistanceType dist;
  204. p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
  205. // if precision for 1 run down the tree is already
  206. // better then some of the requested precisions, then
  207. // skip those
  208. while (precisions[pindex]<p2 && pindex<precisions_length) {
  209. pindex++;
  210. }
  211. if (pindex==precisions_length) {
  212. Logger::info("Got as close as I can\n");
  213. return;
  214. }
  215. for (int i=pindex; i<precisions_length; ++i) {
  216. precision = precisions[i];
  217. while (p2<precision) {
  218. c1 = c2;
  219. p1 = p2;
  220. c2 *=2;
  221. p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
  222. if ((maxTime> 0)&&(time > maxTime)&&(p2<precision)) return;
  223. }
  224. int cx;
  225. float realPrecision;
  226. if (fabs(p2-precision)>SEARCH_EPS) {
  227. Logger::info("Start linear estimation\n");
  228. // after we got to values in the vecinity of the desired precision
  229. // use linear approximation get a better estimation
  230. cx = (c1+c2)/2;
  231. realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
  232. while (fabs(realPrecision-precision)>SEARCH_EPS) {
  233. if (realPrecision<precision) {
  234. c1 = cx;
  235. }
  236. else {
  237. c2 = cx;
  238. }
  239. cx = (c1+c2)/2;
  240. if (cx==c1) {
  241. Logger::info("Got as close as I can\n");
  242. break;
  243. }
  244. realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
  245. }
  246. c2 = cx;
  247. p2 = realPrecision;
  248. }
  249. else {
  250. Logger::info("No need for linear estimation\n");
  251. cx = c2;
  252. realPrecision = p2;
  253. }
  254. }
  255. }
  256. }
  257. #endif //OPENCV_FLANN_INDEX_TESTING_H_