fr_lfw_benchmark.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2014, Itseez Inc, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Itseez Inc or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "opencv2/core.hpp"
  42. #include "opencv2/imgcodecs.hpp"
  43. #include "opencv2/datasets/fr_lfw.hpp"
  44. #include <iostream>
  45. #include <cstdio>
  46. #include <string>
  47. #include <vector>
  48. using namespace std;
  49. using namespace cv;
  50. using namespace cv::datasets;
  51. int main(int argc, const char *argv[])
  52. {
  53. const char *keys =
  54. "{ help h usage ? | | show this message }"
  55. "{ path p |true| path to dataset (lfw2 folder) }"
  56. "{ train t |dev | train method: 'dev'(pairsDevTrain.txt) or 'split'(pairs.txt) }";
  57. CommandLineParser parser(argc, argv, keys);
  58. string path(parser.get<string>("path"));
  59. if (parser.has("help") || path=="true")
  60. {
  61. parser.printMessage();
  62. return -1;
  63. }
  64. string trainMethod(parser.get<string>("train"));
  65. // our trained threshold for "same":
  66. double threshold = 0;
  67. // load dataset
  68. Ptr<FR_lfw> dataset = FR_lfw::create();
  69. dataset->load(path);
  70. unsigned int numSplits = dataset->getNumSplits();
  71. printf("splits number: %u\n", numSplits);
  72. if (trainMethod == "dev")
  73. printf("train size: %u\n", (unsigned int)dataset->getTrain().size());
  74. else
  75. printf("train size: %u\n", (numSplits-1) * (unsigned int)dataset->getTest().size());
  76. printf("test size: %u\n", (unsigned int)dataset->getTest().size());
  77. if (trainMethod == "dev") // train on personsDevTrain.txt
  78. {
  79. // collect average same-distances:
  80. double avg = 0;
  81. int count = 0;
  82. for (unsigned int i=0; i<dataset->getTrain().size(); ++i)
  83. {
  84. FR_lfwObj *example = static_cast<FR_lfwObj *>(dataset->getTrain()[i].get());
  85. Mat a = imread(path+example->image1, IMREAD_GRAYSCALE);
  86. Mat b = imread(path+example->image2, IMREAD_GRAYSCALE);
  87. double dist = norm(a,b);
  88. if (example->same)
  89. {
  90. avg += dist;
  91. count ++;
  92. }
  93. }
  94. threshold = avg / count;
  95. }
  96. vector<double> p;
  97. for (unsigned int j=0; j<numSplits; ++j)
  98. {
  99. if (trainMethod == "split") // train on the remaining 9 splits from pairs.txt
  100. {
  101. double avg = 0;
  102. int count = 0;
  103. for (unsigned int j2=0; j2<numSplits; ++j2)
  104. {
  105. if (j==j2) continue; // skip test split for training
  106. vector < Ptr<Object> > &curr = dataset->getTest(j2);
  107. for (unsigned int i=0; i<curr.size(); ++i)
  108. {
  109. FR_lfwObj *example = static_cast<FR_lfwObj *>(curr[i].get());
  110. Mat a = imread(path+example->image1, IMREAD_GRAYSCALE);
  111. Mat b = imread(path+example->image2, IMREAD_GRAYSCALE);
  112. double dist = norm(a,b);
  113. if (example->same)
  114. {
  115. avg += dist;
  116. count ++;
  117. }
  118. }
  119. }
  120. threshold = avg / count;
  121. }
  122. unsigned int incorrect = 0, correct = 0;
  123. vector < Ptr<Object> > &curr = dataset->getTest(j);
  124. for (unsigned int i=0; i<curr.size(); ++i)
  125. {
  126. FR_lfwObj *example = static_cast<FR_lfwObj *>(curr[i].get());
  127. Mat a = imread(path+example->image1, IMREAD_GRAYSCALE);
  128. Mat b = imread(path+example->image2, IMREAD_GRAYSCALE);
  129. bool same = (norm(a,b) <= threshold);
  130. if (same == example->same)
  131. correct++;
  132. else
  133. incorrect++;
  134. }
  135. p.push_back(1.0*correct/(correct+incorrect));
  136. printf("correct: %u, from: %u -> %f\n", correct, correct+incorrect, p.back());
  137. }
  138. double mu = 0.0;
  139. for (vector<double>::iterator it=p.begin(); it!=p.end(); ++it)
  140. {
  141. mu += *it;
  142. }
  143. mu /= p.size();
  144. double sigma = 0.0;
  145. for (vector<double>::iterator it=p.begin(); it!=p.end(); ++it)
  146. {
  147. sigma += (*it - mu)*(*it - mu);
  148. }
  149. sigma = sqrt(sigma/p.size());
  150. double se = sigma/sqrt(double(p.size()));
  151. printf("estimated mean accuracy: %f and the standard error of the mean: %f\n", mu, se);
  152. return 0;
  153. }