123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- /**
- * @file NegativeFactory.cpp
- * @brief obtain negative examples from some images
- * @author Erik Rodner
- * @date 11/13/2008
- */
- #include <iostream>
- #include "NegativeFactory.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- NegativeFactory::NegativeFactory()
- {
- }
- NegativeFactory::~NegativeFactory()
- {
- }
- void NegativeFactory::createNegativesFromText ( Examples & examples,
- vector<CachedExample *> & cexamples,
- const std::string & filename,
- ImageFeatures & imgf,
- int negativeClassDST )
- {
- ifstream ifs ( filename.c_str(), ios::in );
- if ( ! ifs.good() )
- {
- fprintf (stderr, "Error reading %s\n", filename.c_str() );
- exit(-1);
- }
-
- long int count = 0;
- // refactor-nice.pl: check this substitution
- // old: string imgfn;
- std::string imgfn;
- int xi, yi, xa, ya;
- map<string, CachedExample *> cexamples_map;
- double confidence = 1.0;
- while ( ! ifs.eof() )
- {
- if ( !(ifs >> imgfn) ) break;
- if ( !(ifs >> confidence) ) break;
- if ( !(ifs >> xi) ) break;
- if ( !(ifs >> yi) ) break;
- if ( !(ifs >> xa) ) break;
- if ( !(ifs >> ya) ) break;
- map<string, CachedExample *>::const_iterator i =
- cexamples_map.find ( imgfn );
-
- CachedExample *ce = NULL;
- if ( i == cexamples_map.end() )
- {
- fprintf (stderr, "Loading negative image: %s\n", imgfn.c_str() );
- ce = new CachedExample ( imgfn );
- imgf.fillExample ( ce );
- cexamples.push_back ( ce );
- cexamples_map.insert ( pair<string, CachedExample *> ( imgfn, ce ) );
- } else {
- ce = i->second;
- }
-
- int xsize;
- int ysize;
- ce->getImageSize ( xsize, ysize );
- int x = (xi+xa) / 2;
- int y = (yi+ya) / 2;
- int width = xa - xi;
- int height = ya - yi;
- Example pce ( ce, x, y, width, height );
- examples.push_back ( pair<int, Example> ( negativeClassDST, pce ) );
- count++;
- }
- ifs.close ();
- fprintf (stderr, "%ld negative examples loaded..\n", count );
- }
- void NegativeFactory::createNegativesExhaustiveSearch ( Examples & examples,
- vector<CachedExample *> & cexamples,
- const LabeledSet *ls,
- ImageFeatures & imgf,
- int examplesPerImage,
- int negativeClassSRC,
- int negativeClassDST,
- FeaturePoolClassifier *fpc,
- double & falsePositiveEstimate,
- const VVector & bb,
- int subsample )
- {
- falsePositiveEstimate = 0.0;
- int noImages = 0;
- LOOP_ALL_S ( (*ls) )
- {
- EACH_S ( classno, filename );
- if ( classno != negativeClassSRC ) continue;
-
- long rejected = 0;
- fprintf (stderr, "Loading negative image: %s\n", filename.c_str() );
-
- CachedExample *ce = new CachedExample ( filename );
- cexamples.push_back ( ce );
- imgf.fillExample ( ce );
-
- int xsize;
- int ysize;
- ce->getImageSize ( xsize, ysize );
- long k = 0;
- assert ( bb.size() > 0 );
- assert ( fpc != NULL );
- for ( int i = 0 ; i < (int)bb.size() ; i++ )
- {
- // refactor-nice.pl: check this substitution
- // old: const Vector & box = bb[i];
- const NICE::Vector & box = bb[i];
- int width = (int)box[0];
- int height = (int)box[1];
- assert ( width > 0.0 );
- assert ( height > 0.0 );
- if ( width >= xsize )
- width = xsize - 3;
- if ( height >= ysize )
- height = ysize - 3;
- for ( int y = height/2 ; y < ysize - height/2 ; y+=subsample )
- {
- for ( int x = width/2 ; x < xsize - width/2 ; x+=subsample )
- {
- Example pce ( ce, x, y, width, height );
- ClassificationResult r = fpc->classify(pce);
- if ( r.classno == negativeClassDST ) {
- rejected++;
- continue; // already correctly classified
- }
-
- examples.push_back ( pair<int, Example> ( negativeClassDST, pce ) );
- k++;
- }
- }
- }
- fprintf (stderr, "Image processed: %s (rejected %ld/obtained %ld/%f)\n", filename.c_str(), rejected, k,
- k/(double)(rejected+k));
- falsePositiveEstimate += k / (double)(rejected+k);
- noImages++;
- }
- falsePositiveEstimate /= noImages;
- }
- void NegativeFactory::createNegatives ( Examples & examples,
- vector<CachedExample *> & cexamples,
- const LabeledSet *ls,
- ImageFeatures & imgf,
- int noImages,
- int examplesPerImage,
- int negativeClassSRC,
- int negativeClassDST,
- FeaturePoolClassifier *fpc,
- double & falsePositiveEstimate,
- const VVector & bb )
- {
- int maxRejected = 1000*examplesPerImage;
- srand(time(NULL));
- LabeledSet randSel (true);
- LabeledSet dummy (true);
- map<int,int> selection;
- assert ( ls->count(negativeClassSRC) >= noImages );
- selection[negativeClassSRC] = noImages;
- LabeledSetSelection<LabeledSet>::selectRandom ( selection,
- *ls, randSel, dummy );
- falsePositiveEstimate = 0.0;
- #define SAVE_RANDOM_POSITIONS
- #ifdef SAVE_RANDOM_POSITIONS
- ofstream ofs ( "/tmp/randomlocations.txt", ios::out );
- if ( ! ofs.good() )
- {
- fprintf (stderr, "Error writing to /tmp/randomlocations.txt.\n" );
- exit(-1);
- }
- #endif
- LOOP_ALL_S ( randSel )
- {
- EACH_S ( classno, filename );
-
- long rejected = 0;
- fprintf (stderr, "Loading negative image: %s\n", filename.c_str() );
-
- CachedExample *ce = new CachedExample ( filename );
- cexamples.push_back ( ce );
- imgf.fillExample ( ce );
-
- int xsize;
- int ysize;
- ce->getImageSize ( xsize, ysize );
- long k = 0;
- bool usebb = (bb.size() > 0);
- #ifdef CSFPNegatives_DISTRIBUTION_ANALYSIS
- NICE::Image img (xsize, ysize);
- img.set(0);
- #endif
- while( (k < examplesPerImage) && (rejected < maxRejected) )
- {
- int width, height;
- if ( ! usebb ) {
- width = rand() % (xsize/3) + 20;
- height = rand() % (ysize/3) + 20;
- } else {
- const NICE::Vector & x = bb[ rand() % bb.size() ];
- width = (int)x[0];
- height = (int)x[1];
- assert ( width > 0.0 );
- assert ( height > 0.0 );
- if ( width >= xsize )
- width = xsize - 3;
- if ( height >= ysize )
- height = ysize - 3;
- }
- int x = rand() % (xsize-width) + width/2;
- int y = rand() % (ysize-height) + height/2;
- Example pce ( ce, x, y, width, height );
- if ( fpc != NULL )
- {
- ClassificationResult r = fpc->classify(pce);
- if ( r.classno == negativeClassDST ) {
- rejected++;
- continue; // already correctly classified
- }
- }
- #ifdef SAVE_RANDOM_POSITIONS
- ofs << filename << " 1.0 ";
- int xi = x - width/2;
- int xa = x + width/2;
- int yi = y - height/2;
- int ya = y + height/2;
- ofs << xi << " " << yi << " " << xa << " " << ya << endl;
- #endif
- examples.push_back ( pair<int, Example> ( negativeClassDST, pce ) );
- k++;
- }
- fprintf (stderr, "Image processed: %s (rejected %ld/obtained %ld/%f)\n", filename.c_str(), rejected, k,
- k/(double)(rejected+k));
- falsePositiveEstimate += k / (double)(rejected+k);
- }
- #ifdef SAVE_RANDOM_POSITIONS
- ofs.close();
- #endif
- falsePositiveEstimate /= noImages;
- }
|