NegativeFactory.cpp 6.8 KB

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