NegativeFactory.cpp 6.8 KB

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