NegativeFactory.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * @file NegativeFactory.cpp
  3. * @brief obtain negative examples from some images
  4. * @author Erik Rodner
  5. * @date 11/13/2008
  6. */
  7. #include <iostream>
  8. #include "NegativeFactory.h"
  9. using namespace OBJREC;
  10. using namespace std;
  11. using namespace NICE;
  12. NegativeFactory::NegativeFactory()
  13. {
  14. }
  15. NegativeFactory::~NegativeFactory()
  16. {
  17. }
  18. void NegativeFactory::createNegativesFromText ( Examples & examples,
  19. vector<CachedExample *> & cexamples,
  20. const std::string & filename,
  21. ImageFeatures & imgf,
  22. int negativeClassDST )
  23. {
  24. ifstream ifs ( filename.c_str(), ios::in );
  25. if ( ! ifs.good() )
  26. {
  27. fprintf (stderr, "Error reading %s\n", filename.c_str() );
  28. exit(-1);
  29. }
  30. long int count = 0;
  31. // refactor-nice.pl: check this substitution
  32. // old: string imgfn;
  33. std::string imgfn;
  34. int xi, yi, xa, ya;
  35. map<string, CachedExample *> cexamples_map;
  36. double confidence = 1.0;
  37. while ( ! ifs.eof() )
  38. {
  39. if ( !(ifs >> imgfn) ) break;
  40. if ( !(ifs >> confidence) ) break;
  41. if ( !(ifs >> xi) ) break;
  42. if ( !(ifs >> yi) ) break;
  43. if ( !(ifs >> xa) ) break;
  44. if ( !(ifs >> ya) ) break;
  45. map<string, CachedExample *>::const_iterator i =
  46. cexamples_map.find ( imgfn );
  47. CachedExample *ce = NULL;
  48. if ( i == cexamples_map.end() )
  49. {
  50. fprintf (stderr, "Loading negative image: %s\n", imgfn.c_str() );
  51. ce = new CachedExample ( imgfn );
  52. imgf.fillExample ( ce );
  53. cexamples.push_back ( ce );
  54. cexamples_map.insert ( pair<string, CachedExample *> ( imgfn, ce ) );
  55. } else {
  56. ce = i->second;
  57. }
  58. int xsize;
  59. int ysize;
  60. ce->getImageSize ( xsize, ysize );
  61. int x = (xi+xa) / 2;
  62. int y = (yi+ya) / 2;
  63. int width = xa - xi;
  64. int height = ya - yi;
  65. Example pce ( ce, x, y, width, height );
  66. examples.push_back ( pair<int, Example> ( negativeClassDST, pce ) );
  67. count++;
  68. }
  69. ifs.close ();
  70. fprintf (stderr, "%ld negative examples loaded..\n", count );
  71. }
  72. void NegativeFactory::createNegativesExhaustiveSearch ( Examples & examples,
  73. vector<CachedExample *> & cexamples,
  74. const LabeledSet *ls,
  75. ImageFeatures & imgf,
  76. int examplesPerImage,
  77. int negativeClassSRC,
  78. int negativeClassDST,
  79. FeaturePoolClassifier *fpc,
  80. double & falsePositiveEstimate,
  81. const VVector & bb,
  82. int subsample )
  83. {
  84. falsePositiveEstimate = 0.0;
  85. int noImages = 0;
  86. LOOP_ALL_S ( (*ls) )
  87. {
  88. EACH_S ( classno, filename );
  89. if ( classno != negativeClassSRC ) continue;
  90. long rejected = 0;
  91. fprintf (stderr, "Loading negative image: %s\n", filename.c_str() );
  92. CachedExample *ce = new CachedExample ( filename );
  93. cexamples.push_back ( ce );
  94. imgf.fillExample ( ce );
  95. int xsize;
  96. int ysize;
  97. ce->getImageSize ( xsize, ysize );
  98. long k = 0;
  99. assert ( bb.size() > 0 );
  100. assert ( fpc != NULL );
  101. for ( int i = 0 ; i < (int)bb.size() ; i++ )
  102. {
  103. // refactor-nice.pl: check this substitution
  104. // old: const Vector & box = bb[i];
  105. const NICE::Vector & box = bb[i];
  106. int width = (int)box[0];
  107. int height = (int)box[1];
  108. assert ( width > 0.0 );
  109. assert ( height > 0.0 );
  110. if ( width >= xsize )
  111. width = xsize - 3;
  112. if ( height >= ysize )
  113. height = ysize - 3;
  114. for ( int y = height/2 ; y < ysize - height/2 ; y+=subsample )
  115. {
  116. for ( int x = width/2 ; x < xsize - width/2 ; x+=subsample )
  117. {
  118. Example pce ( ce, x, y, width, height );
  119. ClassificationResult r = fpc->classify(pce);
  120. if ( r.classno == negativeClassDST ) {
  121. rejected++;
  122. continue; // already correctly classified
  123. }
  124. examples.push_back ( pair<int, Example> ( negativeClassDST, pce ) );
  125. k++;
  126. }
  127. }
  128. }
  129. fprintf (stderr, "Image processed: %s (rejected %ld/obtained %ld/%f)\n", filename.c_str(), rejected, k,
  130. k/(double)(rejected+k));
  131. falsePositiveEstimate += k / (double)(rejected+k);
  132. noImages++;
  133. }
  134. falsePositiveEstimate /= noImages;
  135. }
  136. void NegativeFactory::createNegatives ( Examples & examples,
  137. vector<CachedExample *> & cexamples,
  138. const LabeledSet *ls,
  139. ImageFeatures & imgf,
  140. int noImages,
  141. int examplesPerImage,
  142. int negativeClassSRC,
  143. int negativeClassDST,
  144. FeaturePoolClassifier *fpc,
  145. double & falsePositiveEstimate,
  146. const VVector & bb )
  147. {
  148. int maxRejected = 1000*examplesPerImage;
  149. srand(time(NULL));
  150. LabeledSet randSel (true);
  151. LabeledSet dummy (true);
  152. map<int,int> selection;
  153. assert ( ls->count(negativeClassSRC) >= noImages );
  154. selection[negativeClassSRC] = noImages;
  155. LabeledSetSelection<LabeledSet>::selectRandom ( selection,
  156. *ls, randSel, dummy );
  157. falsePositiveEstimate = 0.0;
  158. #define SAVE_RANDOM_POSITIONS
  159. #ifdef SAVE_RANDOM_POSITIONS
  160. ofstream ofs ( "/tmp/randomlocations.txt", ios::out );
  161. if ( ! ofs.good() )
  162. {
  163. fprintf (stderr, "Error writing to /tmp/randomlocations.txt.\n" );
  164. exit(-1);
  165. }
  166. #endif
  167. LOOP_ALL_S ( randSel )
  168. {
  169. EACH_S ( classno, filename );
  170. long rejected = 0;
  171. fprintf (stderr, "Loading negative image: %s\n", filename.c_str() );
  172. CachedExample *ce = new CachedExample ( filename );
  173. cexamples.push_back ( ce );
  174. imgf.fillExample ( ce );
  175. int xsize;
  176. int ysize;
  177. ce->getImageSize ( xsize, ysize );
  178. long k = 0;
  179. bool usebb = (bb.size() > 0);
  180. #ifdef CSFPNegatives_DISTRIBUTION_ANALYSIS
  181. NICE::Image img (xsize, ysize);
  182. img.set(0);
  183. #endif
  184. while( (k < examplesPerImage) && (rejected < maxRejected) )
  185. {
  186. int width, height;
  187. if ( ! usebb ) {
  188. width = rand() % (xsize/3) + 20;
  189. height = rand() % (ysize/3) + 20;
  190. } else {
  191. const NICE::Vector & x = bb[ rand() % bb.size() ];
  192. width = (int)x[0];
  193. height = (int)x[1];
  194. assert ( width > 0.0 );
  195. assert ( height > 0.0 );
  196. if ( width >= xsize )
  197. width = xsize - 3;
  198. if ( height >= ysize )
  199. height = ysize - 3;
  200. }
  201. int x = rand() % (xsize-width) + width/2;
  202. int y = rand() % (ysize-height) + height/2;
  203. Example pce ( ce, x, y, width, height );
  204. if ( fpc != NULL )
  205. {
  206. ClassificationResult r = fpc->classify(pce);
  207. if ( r.classno == negativeClassDST ) {
  208. rejected++;
  209. continue; // already correctly classified
  210. }
  211. }
  212. #ifdef SAVE_RANDOM_POSITIONS
  213. ofs << filename << " 1.0 ";
  214. int xi = x - width/2;
  215. int xa = x + width/2;
  216. int yi = y - height/2;
  217. int ya = y + height/2;
  218. ofs << xi << " " << yi << " " << xa << " " << ya << endl;
  219. #endif
  220. examples.push_back ( pair<int, Example> ( negativeClassDST, pce ) );
  221. k++;
  222. }
  223. fprintf (stderr, "Image processed: %s (rejected %ld/obtained %ld/%f)\n", filename.c_str(), rejected, k,
  224. k/(double)(rejected+k));
  225. falsePositiveEstimate += k / (double)(rejected+k);
  226. }
  227. #ifdef SAVE_RANDOM_POSITIONS
  228. ofs.close();
  229. #endif
  230. falsePositiveEstimate /= noImages;
  231. }