SemSegNovelty.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. #include <sstream>
  2. #include <iostream>
  3. #include "core/image/FilterT.h"
  4. #include "core/basics/numerictools.h"
  5. #include "core/basics/StringTools.h"
  6. #include "core/basics/Timer.h"
  7. #include "gp-hik-exp/GPHIKClassifierNICE.h"
  8. #include "vislearning/baselib/ICETools.h"
  9. #include "vislearning/baselib/Globals.h"
  10. #include "vislearning/features/fpfeatures/SparseVectorFeature.h"
  11. #include "segmentation/GenericRegionSegmentationMethodSelection.h"
  12. #include "SemSegNovelty.h"
  13. using namespace std;
  14. using namespace NICE;
  15. using namespace OBJREC;
  16. void SemSegNovelty::init()
  17. {
  18. globalMaxUncert = -numeric_limits<double>::max();
  19. string section = "SemSegNovelty";
  20. featExtract = new LocalFeatureColorWeijer ( conf );
  21. this->reuseSegmentation = conf->gB ( "FPCPixel", "reuseSegmentation", true ); //save and read segmentation results from files
  22. this->save_classifier = conf->gB ( "FPCPixel", "save_classifier", true ); //save the classifier to a file
  23. this->read_classifier = conf->gB ( "FPCPixel", "read_classifier", false ); //read the classifier from a file
  24. //write uncertainty results in the same folder as done for the segmentation results
  25. resultdir = conf->gS("debug", "resultdir", "result");
  26. cache = conf->gS ( "cache", "root", "" );
  27. findMaximumUncert = conf->gB(section, "findMaximumUncert", true);
  28. whs = conf->gI ( section, "window_size", 10 );
  29. //distance to next descriptor during training
  30. trainWsize = conf->gI ( section, "train_window_size", 10 );
  31. //distance to next descriptor during testing
  32. testWSize = conf->gI (section, "test_window_size", 10);
  33. // select your segmentation method here
  34. string rsMethode = conf->gS ( section, "segmentation", "none" );
  35. if(rsMethode == "none")
  36. {
  37. regionSeg = NULL;
  38. }
  39. else
  40. {
  41. RegionSegmentationMethod *tmpRegionSeg = GenericRegionSegmentationMethodSelection::selectRegionSegmentationMethod(conf, rsMethode);
  42. if ( reuseSegmentation )
  43. regionSeg = new RSCache ( conf, tmpRegionSeg );
  44. else
  45. regionSeg = tmpRegionSeg;
  46. }
  47. //define which measure for "novelty" we want to use
  48. noveltyMethodString = conf->gS( section, "noveltyMethod", "gp-variance");
  49. if (noveltyMethodString.compare("gp-variance") == 0) // novel = large variance
  50. {
  51. this->noveltyMethod = GPVARIANCE;
  52. this->mostNoveltyWithMaxScores = true;
  53. }
  54. else if (noveltyMethodString.compare("gp-uncertainty") == 0) //novel = large uncertainty (mean / var)
  55. {
  56. this->noveltyMethod = GPUNCERTAINTY;
  57. this->mostNoveltyWithMaxScores = false;
  58. globalMaxUncert = numeric_limits<double>::max();
  59. }
  60. else if (noveltyMethodString.compare("gp-mean") == 0) //novel = small mean
  61. {
  62. this->noveltyMethod = GPMINMEAN;
  63. this->mostNoveltyWithMaxScores = false;
  64. globalMaxUncert = numeric_limits<double>::max();
  65. }
  66. else if (noveltyMethodString.compare("gp-meanRatio") == 0) //novel = small difference between mean of most plausible class and mean of snd
  67. // most plausible class (not useful in binary settings)
  68. {
  69. this->noveltyMethod = GPMEANRATIO;
  70. this->mostNoveltyWithMaxScores = false;
  71. globalMaxUncert = numeric_limits<double>::max();
  72. }
  73. else if (noveltyMethodString.compare("gp-weightAll") == 0) // novel = large weight in alpha vector after updating the model (can be predicted exactly)
  74. {
  75. this->noveltyMethod = GPWEIGHTALL;
  76. this->mostNoveltyWithMaxScores = true;
  77. }
  78. else if (noveltyMethodString.compare("gp-weightRatio") == 0) // novel = small difference between weights for alpha vectors
  79. // with assumptions of GT label to be the most
  80. // plausible against the second most plausible class
  81. {
  82. this->noveltyMethod = GPWEIGHTRATIO;
  83. this->mostNoveltyWithMaxScores = false;
  84. globalMaxUncert = numeric_limits<double>::max();
  85. }
  86. else if (noveltyMethodString.compare("random") == 0)
  87. {
  88. initRand();
  89. this->noveltyMethod = RANDOM;
  90. }
  91. else
  92. {
  93. this->noveltyMethod = GPVARIANCE;
  94. this->mostNoveltyWithMaxScores = true;
  95. }
  96. //we don't have queried any region so far
  97. queriedRegions.clear();
  98. visualizeALimages = conf->gB(section, "visualizeALimages", false);
  99. }
  100. SemSegNovelty::SemSegNovelty ( const Config * _conf,
  101. const MultiDataset *md )
  102. : SemanticSegmentation ( _conf, & ( md->getClassNames ( "train" ) ) )
  103. {
  104. this->conf = new NICE::Config ( *_conf );
  105. // set internal variables, default values, and all those funny things
  106. this->init ( );
  107. std::string section = "SemSegNovelty";
  108. classifierString = conf->gS ( section, "classifier", "GPHIKClassifier" );
  109. classifier = NULL;
  110. vclassifier = NULL;
  111. if ( classifierString.compare("GPHIKClassifier") == 0)
  112. {
  113. //just to make sure, that we do NOT perform an optimization after every iteration step
  114. //this would just take a lot of time, which is not desired so far
  115. this->conf->sB( "GPHIKClassifier", "performOptimizationAfterIncrement", false );
  116. classifier = new GPHIKClassifierNICE ( this->conf, "GPHIKClassifier" );
  117. }
  118. else
  119. vclassifier = GenericClassifierSelection::selectVecClassifier ( this->conf, classifierString );
  120. if ( read_classifier )
  121. {
  122. try
  123. {
  124. if ( classifier != NULL )
  125. {
  126. string classifierdst = "/classifier.data";
  127. fprintf ( stderr, "SemSegNovelty:: Reading classifier data from %s\n", ( cache + classifierdst ).c_str() );
  128. classifier->read ( cache + classifierdst );
  129. }
  130. else
  131. {
  132. string classifierdst = "/veccl.data";
  133. fprintf ( stderr, "SemSegNovelty:: Reading classifier data from %s\n", ( cache + classifierdst ).c_str() );
  134. vclassifier->read ( cache + classifierdst );
  135. }
  136. fprintf ( stderr, "SemSegNovelty:: successfully read\n" );
  137. }
  138. catch ( char *str )
  139. {
  140. cerr << "error reading data: " << str << endl;
  141. }
  142. }
  143. else
  144. {
  145. train ( md );
  146. }
  147. }
  148. SemSegNovelty::~SemSegNovelty()
  149. {
  150. if(newTrainExamples.size() > 0)
  151. {
  152. // show most uncertain region
  153. if (visualizeALimages)
  154. showImage(maskedImg);
  155. //incorporate new information into the classifier
  156. if (classifier != NULL)
  157. classifier->addMultipleExamples(newTrainExamples);
  158. //store the classifier, such that we can read it again in the next round (if we like that)
  159. classifier->save ( cache + "/classifier.data" );
  160. }
  161. // clean-up
  162. if ( classifier != NULL )
  163. delete classifier;
  164. if ( vclassifier != NULL )
  165. delete vclassifier;
  166. if ( featExtract != NULL )
  167. delete featExtract;
  168. }
  169. void SemSegNovelty::visualizeRegion(const NICE::ColorImage &img, const NICE::Matrix &regions, int region, NICE::ColorImage &outimage)
  170. {
  171. std::vector<uchar> color;
  172. color.push_back(255);
  173. color.push_back(0);
  174. color.push_back(0);
  175. int width = img.width();
  176. int height = img.height();
  177. outimage.resize(width,height);
  178. for(int y = 0; y < height; y++)
  179. {
  180. for(int x = 0; x < width; x++)
  181. {
  182. if(regions(x,y) == region)
  183. {
  184. for(int c = 0; c < 3; c++)
  185. {
  186. outimage(x,y,c) = color[c];
  187. }
  188. }
  189. else
  190. {
  191. for(int c = 0; c < 3; c++)
  192. {
  193. outimage(x,y,c) = img(x,y,c);
  194. }
  195. }
  196. }
  197. }
  198. }
  199. void SemSegNovelty::train ( const MultiDataset *md )
  200. {
  201. const LabeledSet train = * ( *md ) ["train"];
  202. const LabeledSet *trainp = &train;
  203. ////////////////////////
  204. // feature extraction //
  205. ////////////////////////
  206. //check the same thing for the training classes - this is very specific to our setup
  207. std::string forbidden_classesTrain_s = conf->gS ( "analysis", "donttrainTrain", "" );
  208. if ( forbidden_classesTrain_s == "" )
  209. {
  210. forbidden_classesTrain_s = conf->gS ( "analysis", "forbidden_classesTrain", "" );
  211. }
  212. this->classNames->getSelection ( forbidden_classesTrain_s, forbidden_classesTrain );
  213. ProgressBar pb ( "Local Feature Extraction" );
  214. pb.show();
  215. int imgnb = 0;
  216. Examples examples;
  217. examples.filename = "training";
  218. int featdim = -1;
  219. classesInUse.clear();
  220. LOOP_ALL_S ( *trainp )
  221. {
  222. //EACH_S(classno, currentFile);
  223. EACH_INFO ( classno, info );
  224. std::string currentFile = info.img();
  225. CachedExample *ce = new CachedExample ( currentFile );
  226. const LocalizationResult *locResult = info.localization();
  227. if ( locResult->size() <= 0 )
  228. {
  229. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  230. currentFile.c_str() );
  231. continue;
  232. }
  233. int xsize, ysize;
  234. ce->getImageSize ( xsize, ysize );
  235. Image labels ( xsize, ysize );
  236. labels.set ( 0 );
  237. locResult->calcLabeledImage ( labels, ( *classNames ).getBackgroundClass() );
  238. NICE::ColorImage img;
  239. try {
  240. img = ColorImage ( currentFile );
  241. } catch ( Exception ) {
  242. cerr << "SemSegNovelty: error opening image file <" << currentFile << ">" << endl;
  243. continue;
  244. }
  245. Globals::setCurrentImgFN ( currentFile );
  246. MultiChannelImageT<double> feats;
  247. // extract features
  248. featExtract->getFeats ( img, feats );
  249. featdim = feats.channels();
  250. feats.addChannel(featdim);
  251. for (int c = 0; c < featdim; c++)
  252. {
  253. ImageT<double> tmp = feats[c];
  254. ImageT<double> tmp2 = feats[c+featdim];
  255. NICE::FilterT<double, double, double>::gradientStrength (tmp, tmp2);
  256. }
  257. featdim += featdim;
  258. // compute integral images
  259. for ( int c = 0; c < featdim; c++ )
  260. {
  261. feats.calcIntegral ( c );
  262. }
  263. for ( int y = 0; y < ysize; y += trainWsize)
  264. {
  265. for ( int x = 0; x < xsize; x += trainWsize )
  266. {
  267. int classnoTmp = labels.getPixel ( x, y );
  268. if ( forbidden_classesTrain.find ( classnoTmp ) != forbidden_classesTrain.end() )
  269. {
  270. continue;
  271. }
  272. if (classesInUse.find(classnoTmp) == classesInUse.end())
  273. {
  274. classesInUse.insert(classnoTmp);
  275. }
  276. Example example;
  277. example.vec = NULL;
  278. example.svec = new SparseVector ( featdim );
  279. for ( int f = 0; f < featdim; f++ )
  280. {
  281. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  282. if ( val > 1e-10 )
  283. ( *example.svec ) [f] = val;
  284. }
  285. example.svec->normalize();
  286. example.position = imgnb;
  287. examples.push_back ( pair<int, Example> ( classnoTmp, example ) );
  288. }
  289. }
  290. delete ce;
  291. imgnb++;
  292. pb.update ( trainp->count() );
  293. }
  294. numberOfClasses = classesInUse.size();
  295. std::cerr << "numberOfClasses: " << numberOfClasses << std::endl;
  296. std::cerr << "classes in use: " << std::endl;
  297. for (std::set<int>::const_iterator it = classesInUse.begin(); it != classesInUse.end(); it++)
  298. {
  299. std::cerr << *it << " ";
  300. }
  301. std::cerr << std::endl;
  302. pb.hide();
  303. //////////////////////
  304. // train classifier //
  305. //////////////////////
  306. FeaturePool fp;
  307. Feature *f = new SparseVectorFeature ( featdim );
  308. f->explode ( fp );
  309. delete f;
  310. if ( classifier != NULL )
  311. {
  312. std::cerr << "train FP-classifier with " << examples.size() << " examples" << std::endl;
  313. classifier->train ( fp, examples );
  314. std::cerr << "training finished" << std::endl;
  315. }
  316. else
  317. {
  318. LabeledSetVector lvec;
  319. convertExamplesToLSet ( examples, lvec );
  320. vclassifier->teach ( lvec );
  321. // if ( usegmm )
  322. // convertLSetToSparseExamples ( examples, lvec );
  323. // else
  324. std::cerr << "classifierString: " << classifierString << std::endl;
  325. if (this->classifierString.compare("nn") == 0)
  326. {
  327. convertLSetToExamples ( examples, lvec, true /* only remove pointers to the data in the LSet-struct*/);
  328. }
  329. else
  330. {
  331. convertLSetToExamples ( examples, lvec, false /* remove all training examples of the LSet-struct */);
  332. }
  333. vclassifier->finishTeaching();
  334. }
  335. fp.destroy();
  336. if ( save_classifier )
  337. {
  338. if ( classifier != NULL )
  339. classifier->save ( cache + "/classifier.data" );
  340. else
  341. vclassifier->save ( cache + "/veccl.data" );
  342. }
  343. ////////////
  344. //clean up//
  345. ////////////
  346. for ( int i = 0; i < ( int ) examples.size(); i++ )
  347. {
  348. examples[i].second.clean();
  349. }
  350. examples.clear();
  351. cerr << "SemSeg training finished" << endl;
  352. }
  353. void SemSegNovelty::semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities )
  354. {
  355. Timer timer;
  356. timer.start();
  357. //segResult contains the GT labels when this method is called
  358. // we simply store them in labels, to have an easy access to the GT information lateron
  359. NICE::Image labels = segresult;
  360. //just to be sure that we do not have a GT-biased result :)
  361. segresult.set(0);
  362. int featdim = -1;
  363. std::string currentFile = Globals::getCurrentImgFN();
  364. int xsize, ysize;
  365. ce->getImageSize ( xsize, ysize );
  366. probabilities.reInit( xsize, ysize, this->classNames->getMaxClassno() + 1);
  367. probabilities.setAll ( 0.0 );
  368. NICE::ColorImage img;
  369. try {
  370. img = ColorImage ( currentFile );
  371. } catch ( Exception ) {
  372. cerr << "SemSegNovelty: error opening image file <" << currentFile << ">" << endl;
  373. return;
  374. }
  375. MultiChannelImageT<double> feats;
  376. // extract features
  377. featExtract->getFeats ( img, feats );
  378. featdim = feats.channels();
  379. feats.addChannel(featdim);
  380. for (int c = 0; c < featdim; c++)
  381. {
  382. ImageT<double> tmp = feats[c];
  383. ImageT<double> tmp2 = feats[c+featdim];
  384. NICE::FilterT<double, double, double>::gradientStrength (tmp, tmp2);
  385. }
  386. featdim += featdim;
  387. // compute integral images
  388. for ( int c = 0; c < featdim; c++ )
  389. {
  390. feats.calcIntegral ( c );
  391. }
  392. timer.stop();
  393. std::cout << "AL time for preparation: " << timer.getLastAbsolute() << std::endl;
  394. timer.start();
  395. //classification results currently only needed to be computed separately if we use the vclassifier, i.e., the nearest neighbor used
  396. // for the "novel feature learning" approach
  397. //in all other settings, such as active sem seg in general, we do this within the novelty-computation-methods
  398. if ( classifier == NULL )
  399. {
  400. this->computeClassificationResults( feats, segresult, probabilities, xsize, ysize, featdim);
  401. }
  402. // timer.stop();
  403. //
  404. // std::cerr << "classification results computed" << std::endl;
  405. FloatImage noveltyImage ( xsize, ysize );
  406. noveltyImage.set ( 0.0 );
  407. switch (noveltyMethod)
  408. {
  409. case GPVARIANCE:
  410. {
  411. this->computeNoveltyByVariance( noveltyImage, feats, segresult, probabilities, xsize, ysize, featdim );
  412. break;
  413. }
  414. case GPUNCERTAINTY:
  415. {
  416. this->computeNoveltyByGPUncertainty( noveltyImage, feats, segresult, probabilities, xsize, ysize, featdim );
  417. break;
  418. }
  419. case GPMINMEAN:
  420. {
  421. std::cerr << "compute novelty using the minimum mean" << std::endl;
  422. this->computeNoveltyByGPMean( noveltyImage, feats, segresult, probabilities, xsize, ysize, featdim );
  423. break;
  424. }
  425. case GPMEANRATIO:
  426. {
  427. this->computeNoveltyByGPMeanRatio( noveltyImage, feats, segresult, probabilities, xsize, ysize, featdim );
  428. break;
  429. }
  430. case GPWEIGHTALL:
  431. {
  432. this->computeNoveltyByGPWeightAll( noveltyImage, feats, segresult, probabilities, xsize, ysize, featdim );
  433. break;
  434. }
  435. case GPWEIGHTRATIO:
  436. {
  437. this->computeNoveltyByGPWeightRatio( noveltyImage, feats, segresult, probabilities, xsize, ysize, featdim );
  438. break;
  439. }
  440. case RANDOM:
  441. {
  442. this->computeNoveltyByRandom( noveltyImage, feats, segresult, probabilities, xsize, ysize, featdim );
  443. break;
  444. }
  445. default:
  446. {
  447. //do nothing, keep the image constant to 0.0
  448. break;
  449. }
  450. }
  451. timer.stop();
  452. std::cout << "AL time for novelty score computation: " << timer.getLastAbsolute() << std::endl;
  453. if (visualizeALimages)
  454. {
  455. ColorImage imgrgbTmp (xsize, ysize);
  456. ICETools::convertToRGB ( noveltyImage, imgrgbTmp );
  457. showImage(imgrgbTmp, "Novelty Image without Region Segmentation");
  458. }
  459. timer.start();
  460. //Regionen ermitteln
  461. if(regionSeg != NULL)
  462. {
  463. NICE::Matrix mask;
  464. int amountRegions = regionSeg->segRegions ( img, mask );
  465. //compute probs per region
  466. std::vector<std::vector<double> > regionProb(amountRegions, std::vector<double>(probabilities.channels(),0.0));
  467. std::vector<double> regionNoveltyMeasure (amountRegions, 0.0);
  468. std::vector<int> regionCounter(amountRegions, 0);
  469. std::vector<int> regionCounterNovelty(amountRegions, 0);
  470. for ( int y = 0; y < ysize; y += trainWsize) //y++)
  471. {
  472. for (int x = 0; x < xsize; x += trainWsize) //x++)
  473. {
  474. int r = mask(x,y);
  475. regionCounter[r]++;
  476. for(int j = 0; j < probabilities.channels(); j++)
  477. {
  478. regionProb[r][j] += probabilities ( x, y, j );
  479. }
  480. if ( forbidden_classesActiveLearning.find( labels(x,y) ) == forbidden_classesActiveLearning.end() )
  481. {
  482. //count the amount of "novelty" for the corresponding region
  483. regionNoveltyMeasure[r] += noveltyImage(x,y);
  484. regionCounterNovelty[r]++;
  485. }
  486. }
  487. }
  488. //find best class per region
  489. std::vector<int> bestClassPerRegion(amountRegions,0);
  490. double maxNoveltyScore = -numeric_limits<double>::max();
  491. if (!mostNoveltyWithMaxScores)
  492. {
  493. maxNoveltyScore = numeric_limits<double>::max();
  494. }
  495. int maxUncertRegion = -1;
  496. //loop over all regions and compute averaged novelty scores
  497. for(int r = 0; r < amountRegions; r++)
  498. {
  499. //check for the most plausible class per region
  500. double maxval = -numeric_limits<double>::max();
  501. //loop over all classes
  502. for(int c = 0; c < probabilities.channels(); c++)
  503. {
  504. regionProb[r][c] /= regionCounter[r];
  505. if( (maxval < regionProb[r][c]) ) //&& (regionProb[r][c] != 0.0) )
  506. {
  507. maxval = regionProb[r][c];
  508. bestClassPerRegion[r] = c;
  509. }
  510. }
  511. //if the region only contains unvalid information (e.g., background) skip it
  512. if (regionCounterNovelty[r] == 0)
  513. {
  514. continue;
  515. }
  516. //normalize summed novelty scores to region size
  517. regionNoveltyMeasure[r] /= regionCounterNovelty[r];
  518. //did we find a region that has a higher score as the most novel region known so far within this image?
  519. if( ( mostNoveltyWithMaxScores && (maxNoveltyScore < regionNoveltyMeasure[r]) ) // if we look for large novelty scores, e.g., variance
  520. || ( !mostNoveltyWithMaxScores && (maxNoveltyScore > regionNoveltyMeasure[r]) ) ) // if we look for small novelty scores, e.g., min mean
  521. {
  522. //did we already query a region of this image? -- and it was this specific region
  523. if ( (queriedRegions.find( currentFile ) != queriedRegions.end() ) && ( queriedRegions[currentFile].find(r) != queriedRegions[currentFile].end() ) )
  524. {
  525. continue;
  526. }
  527. else //only accept the region as novel if we never queried it before
  528. {
  529. maxNoveltyScore = regionNoveltyMeasure[r];
  530. maxUncertRegion = r;
  531. }
  532. }
  533. }
  534. // after finding the most novel region for the current image, check whether this region is also the most novel with respect
  535. // to all previously seen test images
  536. // if so, store the corresponding features, since we want to "actively" query them to incorporate useful information
  537. if(findMaximumUncert)
  538. {
  539. if( ( mostNoveltyWithMaxScores && (maxNoveltyScore > globalMaxUncert) )
  540. || ( !mostNoveltyWithMaxScores && (maxNoveltyScore < globalMaxUncert) ) )
  541. {
  542. //current most novel region of the image has "higher" novelty score then previous most novel region of all test images worked on so far
  543. // -> save new important features of this region
  544. Examples examples;
  545. for ( int y = 0; y < ysize; y += trainWsize )
  546. {
  547. for ( int x = 0; x < xsize; x += trainWsize)
  548. {
  549. if(mask(x,y) == maxUncertRegion)
  550. {
  551. int classnoTmp = labels(x,y);
  552. if ( forbidden_classesActiveLearning.find(classnoTmp) != forbidden_classesActiveLearning.end() )
  553. continue;
  554. Example example(NULL, x, y);
  555. example.vec = NULL;
  556. example.svec = new SparseVector ( featdim );
  557. for ( int f = 0; f < featdim; f++ )
  558. {
  559. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  560. if ( val > 1e-10 )
  561. ( *example.svec ) [f] = val;
  562. }
  563. example.svec->normalize();
  564. examples.push_back ( pair<int, Example> ( classnoTmp, example ) );
  565. }
  566. }
  567. }
  568. if(examples.size() > 0)
  569. {
  570. std::cerr << "found " << examples.size() << " new examples in the queried region" << std::endl << std::endl;
  571. newTrainExamples.clear();
  572. newTrainExamples = examples;
  573. globalMaxUncert = maxNoveltyScore;
  574. //prepare for later visualization
  575. // if (visualizeALimages)
  576. visualizeRegion(img,mask,maxUncertRegion,maskedImg);
  577. }
  578. else
  579. {
  580. std::cerr << "the queried region has no valid information" << std::endl << std::endl;
  581. }
  582. //save filename and region index
  583. currentRegionToQuery.first = currentFile;
  584. currentRegionToQuery.second = maxUncertRegion;
  585. }
  586. }
  587. //write back best results per region
  588. //i.e., write normalized novelty scores for every region into the novelty image
  589. for ( int y = 0; y < ysize; y++)
  590. {
  591. for (int x = 0; x < xsize; x++)
  592. {
  593. int r = mask(x,y);
  594. for(int j = 0; j < probabilities.channels(); j++)
  595. {
  596. probabilities ( x, y, j ) = regionProb[r][j];
  597. }
  598. segresult(x,y) = bestClassPerRegion[r];
  599. // write novelty scores for every segment into the "final" image
  600. noveltyImage(x,y) = regionNoveltyMeasure[r];
  601. }
  602. }
  603. } // if regionSeg != null
  604. timer.stop();
  605. std::cout << "AL time for determination of novel regions: " << timer.getLastAbsolute() << std::endl;
  606. // timer.stop();
  607. // cout << "second: " << timer.getLastAbsolute() << endl;
  608. timer.start();
  609. ColorImage imgrgb ( xsize, ysize );
  610. std::stringstream out;
  611. std::vector< std::string > list2;
  612. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  613. out << resultdir << "/" << list2.back();
  614. noveltyImage.writeRaw(out.str() + "_run_" + NICE::intToString(this->iterationCountSuffix) + "_" + noveltyMethodString+".rawfloat");
  615. if (visualizeALimages)
  616. {
  617. ICETools::convertToRGB ( noveltyImage, imgrgb );
  618. showImage(imgrgb, "Novelty Image");
  619. }
  620. timer.stop();
  621. cout << "AL time for writing the raw novelty image: " << timer.getLastAbsolute() << endl;
  622. }
  623. inline void SemSegNovelty::computeClassificationResults( const NICE::MultiChannelImageT<double> & feats,
  624. NICE::Image & segresult,
  625. NICE::MultiChannelImageT<double> & probabilities,
  626. const int & xsize,
  627. const int & ysize,
  628. const int & featdim
  629. )
  630. {
  631. std::cerr << "featdim: " << featdim << std::endl;
  632. if ( classifier != NULL )
  633. {
  634. #pragma omp parallel for
  635. for ( int y = 0; y < ysize; y += testWSize )
  636. {
  637. Example example;
  638. example.vec = NULL;
  639. example.svec = new SparseVector ( featdim );
  640. for ( int x = 0; x < xsize; x += testWSize)
  641. {
  642. for ( int f = 0; f < featdim; f++ )
  643. {
  644. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  645. if ( val > 1e-10 )
  646. ( *example.svec ) [f] = val;
  647. }
  648. example.svec->normalize();
  649. ClassificationResult cr = classifier->classify ( example );
  650. int xs = std::max(0, x - testWSize/2);
  651. int xe = std::min(xsize - 1, x + testWSize/2);
  652. int ys = std::max(0, y - testWSize/2);
  653. int ye = std::min(ysize - 1, y + testWSize/2);
  654. for (int yl = ys; yl <= ye; yl++)
  655. {
  656. for (int xl = xs; xl <= xe; xl++)
  657. {
  658. for ( int j = 0 ; j < cr.scores.size(); j++ )
  659. {
  660. probabilities ( xl, yl, j ) = cr.scores[j];
  661. }
  662. segresult ( xl, yl ) = cr.classno;
  663. }
  664. }
  665. example.svec->clear();
  666. }
  667. delete example.svec;
  668. example.svec = NULL;
  669. }
  670. }
  671. else //vclassifier
  672. {
  673. std::cerr << "compute classification results with vclassifier" << std::endl;
  674. #pragma omp parallel for
  675. for ( int y = 0; y < ysize; y += testWSize )
  676. {
  677. for ( int x = 0; x < xsize; x += testWSize)
  678. {
  679. NICE::Vector v(featdim);
  680. for ( int f = 0; f < featdim; f++ )
  681. {
  682. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  683. v[f] = val;
  684. }
  685. v.normalizeL1();
  686. ClassificationResult cr = vclassifier->classify ( v );
  687. int xs = std::max(0, x - testWSize/2);
  688. int xe = std::min(xsize - 1, x + testWSize/2);
  689. int ys = std::max(0, y - testWSize/2);
  690. int ye = std::min(ysize - 1, y + testWSize/2);
  691. for (int yl = ys; yl <= ye; yl++)
  692. {
  693. for (int xl = xs; xl <= xe; xl++)
  694. {
  695. for ( int j = 0 ; j < cr.scores.size(); j++ )
  696. {
  697. probabilities ( xl, yl, j ) = cr.scores[j];
  698. }
  699. segresult ( xl, yl ) = cr.classno;
  700. }
  701. }
  702. }
  703. }
  704. }
  705. }
  706. // compute novelty images depending on the strategy chosen
  707. void SemSegNovelty::computeNoveltyByRandom( NICE::FloatImage & noveltyImage,
  708. const NICE::MultiChannelImageT<double> & feats,
  709. NICE::Image & segresult,
  710. NICE::MultiChannelImageT<double> & probabilities,
  711. const int & xsize, const int & ysize, const int & featdim )
  712. {
  713. #pragma omp parallel for
  714. for ( int y = 0; y < ysize; y += testWSize )
  715. {
  716. Example example;
  717. example.vec = NULL;
  718. example.svec = new SparseVector ( featdim );
  719. for ( int x = 0; x < xsize; x += testWSize)
  720. {
  721. for ( int f = 0; f < featdim; f++ )
  722. {
  723. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  724. if ( val > 1e-10 )
  725. ( *example.svec ) [f] = val;
  726. }
  727. example.svec->normalize();
  728. ClassificationResult cr = classifier->classify ( example );
  729. int xs = std::max(0, x - testWSize/2);
  730. int xe = std::min(xsize - 1, x + testWSize/2);
  731. int ys = std::max(0, y - testWSize/2);
  732. int ye = std::min(ysize - 1, y + testWSize/2);
  733. double randVal = randDouble();
  734. for (int yl = ys; yl <= ye; yl++)
  735. {
  736. for (int xl = xs; xl <= xe; xl++)
  737. {
  738. for ( int j = 0 ; j < cr.scores.size(); j++ )
  739. {
  740. probabilities ( xl, yl, j ) = cr.scores[j];
  741. }
  742. segresult ( xl, yl ) = cr.classno;
  743. noveltyImage ( xl, yl ) = randVal;
  744. }
  745. }
  746. }
  747. }
  748. }
  749. void SemSegNovelty::computeNoveltyByVariance( NICE::FloatImage & noveltyImage,
  750. const NICE::MultiChannelImageT<double> & feats,
  751. NICE::Image & segresult,
  752. NICE::MultiChannelImageT<double> & probabilities,
  753. const int & xsize, const int & ysize, const int & featdim )
  754. {
  755. #pragma omp parallel for
  756. for ( int y = 0; y < ysize; y += testWSize )
  757. {
  758. Example example;
  759. example.vec = NULL;
  760. example.svec = new SparseVector ( featdim );
  761. for ( int x = 0; x < xsize; x += testWSize)
  762. {
  763. for ( int f = 0; f < featdim; f++ )
  764. {
  765. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  766. if ( val > 1e-10 )
  767. ( *example.svec ) [f] = val;
  768. }
  769. example.svec->normalize();
  770. ClassificationResult cr = classifier->classify ( example );
  771. int xs = std::max(0, x - testWSize/2);
  772. int xe = std::min(xsize - 1, x + testWSize/2);
  773. int ys = std::max(0, y - testWSize/2);
  774. int ye = std::min(ysize - 1, y + testWSize/2);
  775. for (int yl = ys; yl <= ye; yl++)
  776. {
  777. for (int xl = xs; xl <= xe; xl++)
  778. {
  779. for ( int j = 0 ; j < cr.scores.size(); j++ )
  780. {
  781. probabilities ( xl, yl, j ) = cr.scores[j];
  782. }
  783. segresult ( xl, yl ) = cr.classno;
  784. noveltyImage ( xl, yl ) = cr.uncertainty;
  785. }
  786. }
  787. example.svec->clear();
  788. }
  789. delete example.svec;
  790. example.svec = NULL;
  791. }
  792. }
  793. void SemSegNovelty::computeNoveltyByGPUncertainty( NICE::FloatImage & noveltyImage,
  794. const NICE::MultiChannelImageT<double> & feats,
  795. NICE::Image & segresult,
  796. NICE::MultiChannelImageT<double> & probabilities,
  797. const int & xsize, const int & ysize, const int & featdim )
  798. {
  799. double gpNoise = conf->gD("GPHIK", "noise", 0.01);
  800. #pragma omp parallel for
  801. for ( int y = 0; y < ysize; y += testWSize )
  802. {
  803. Example example;
  804. example.vec = NULL;
  805. example.svec = new SparseVector ( featdim );
  806. for ( int x = 0; x < xsize; x += testWSize)
  807. {
  808. for ( int f = 0; f < featdim; f++ )
  809. {
  810. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  811. if ( val > 1e-10 )
  812. ( *example.svec ) [f] = val;
  813. }
  814. example.svec->normalize();
  815. ClassificationResult cr = classifier->classify ( example );
  816. double maxMeanAbs ( 0.0 );
  817. for ( int j = 0 ; j < cr.scores.size(); j++ )
  818. {
  819. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  820. {
  821. continue;
  822. }
  823. //check for larger abs mean
  824. if (abs(cr.scores[j]) > maxMeanAbs)
  825. {
  826. maxMeanAbs = abs(cr.scores[j]);
  827. }
  828. }
  829. double firstTerm (1.0 / sqrt(cr.uncertainty+gpNoise));
  830. //compute the heuristic GP-UNCERTAINTY, as proposed by Kapoor et al. in IJCV 2010
  831. // GP-UNCERTAINTY : |mean| / sqrt(var^2 + gpnoise^2)
  832. double gpUncertaintyVal = maxMeanAbs*firstTerm; //firstTerm = 1.0 / sqrt(r.uncertainty+gpNoise))
  833. int xs = std::max(0, x - testWSize/2);
  834. int xe = std::min(xsize - 1, x + testWSize/2);
  835. int ys = std::max(0, y - testWSize/2);
  836. int ye = std::min(ysize - 1, y + testWSize/2);
  837. for (int yl = ys; yl <= ye; yl++)
  838. {
  839. for (int xl = xs; xl <= xe; xl++)
  840. {
  841. for ( int j = 0 ; j < cr.scores.size(); j++ )
  842. {
  843. probabilities ( xl, yl, j ) = cr.scores[j];
  844. }
  845. segresult ( xl, yl ) = cr.classno;
  846. noveltyImage ( xl, yl ) = gpUncertaintyVal;
  847. }
  848. }
  849. example.svec->clear();
  850. }
  851. delete example.svec;
  852. example.svec = NULL;
  853. }
  854. }
  855. void SemSegNovelty::computeNoveltyByGPMean( NICE::FloatImage & noveltyImage,
  856. const NICE::MultiChannelImageT<double> & feats,
  857. NICE::Image & segresult,
  858. NICE::MultiChannelImageT<double> & probabilities,
  859. const int & xsize, const int & ysize, const int & featdim )
  860. {
  861. double gpNoise = conf->gD("GPHIK", "noise", 0.01);
  862. #pragma omp parallel for
  863. for ( int y = 0; y < ysize; y += testWSize )
  864. {
  865. Example example;
  866. example.vec = NULL;
  867. example.svec = new SparseVector ( featdim );
  868. for ( int x = 0; x < xsize; x += testWSize)
  869. {
  870. for ( int f = 0; f < featdim; f++ )
  871. {
  872. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  873. if ( val > 1e-10 )
  874. ( *example.svec ) [f] = val;
  875. }
  876. example.svec->normalize();
  877. ClassificationResult cr = classifier->classify ( example );
  878. double minMeanAbs ( numeric_limits<double>::max() );
  879. for ( int j = 0 ; j < probabilities.channels(); j++ )
  880. {
  881. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  882. {
  883. continue;
  884. }
  885. //check whether we found a class with higher smaller abs mean than the current minimum
  886. if (abs(probabilities(x,y,j)) < minMeanAbs)
  887. {
  888. minMeanAbs = abs(cr.scores[j]);
  889. }
  890. }
  891. // compute results when we take the lowest mean value of all classes
  892. double gpMeanVal = minMeanAbs;
  893. int xs = std::max(0, x - testWSize/2);
  894. int xe = std::min(xsize - 1, x + testWSize/2);
  895. int ys = std::max(0, y - testWSize/2);
  896. int ye = std::min(ysize - 1, y + testWSize/2);
  897. for (int yl = ys; yl <= ye; yl++)
  898. {
  899. for (int xl = xs; xl <= xe; xl++)
  900. {
  901. for ( int j = 0 ; j < cr.scores.size(); j++ )
  902. {
  903. probabilities ( xl, yl, j ) = cr.scores[j];
  904. }
  905. segresult ( xl, yl ) = cr.classno;
  906. noveltyImage ( xl, yl ) = gpMeanVal;
  907. }
  908. }
  909. }
  910. }
  911. }
  912. void SemSegNovelty::computeNoveltyByGPMeanRatio( NICE::FloatImage & noveltyImage,
  913. const NICE::MultiChannelImageT<double> & feats,
  914. NICE::Image & segresult,
  915. NICE::MultiChannelImageT<double> & probabilities,
  916. const int & xsize, const int & ysize, const int & featdim )
  917. {
  918. double gpNoise = conf->gD("GPHIK", "noise", 0.01);
  919. #pragma omp parallel for
  920. for ( int y = 0; y < ysize; y += testWSize )
  921. {
  922. Example example;
  923. example.vec = NULL;
  924. example.svec = new SparseVector ( featdim );
  925. for ( int x = 0; x < xsize; x += testWSize)
  926. {
  927. for ( int f = 0; f < featdim; f++ )
  928. {
  929. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  930. if ( val > 1e-10 )
  931. ( *example.svec ) [f] = val;
  932. }
  933. example.svec->normalize();
  934. ClassificationResult cr = classifier->classify ( example );
  935. double maxMean ( -numeric_limits<double>::max() );
  936. double sndMaxMean ( -numeric_limits<double>::max() );
  937. for ( int j = 0 ; j < cr.scores.size(); j++ )
  938. {
  939. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  940. {
  941. continue;
  942. }
  943. //check for larger mean without abs as well
  944. if (cr.scores[j] > maxMean)
  945. {
  946. sndMaxMean = maxMean;
  947. maxMean = cr.scores[j];
  948. }
  949. // and also for the second highest mean of all classes
  950. else if (cr.scores[j] > sndMaxMean)
  951. {
  952. sndMaxMean = cr.scores[j];
  953. }
  954. }
  955. //look at the difference in the absolut mean values for the most plausible class
  956. // and the second most plausible class
  957. double gpMeanRatioVal= maxMean - sndMaxMean;
  958. int xs = std::max(0, x - testWSize/2);
  959. int xe = std::min(xsize - 1, x + testWSize/2);
  960. int ys = std::max(0, y - testWSize/2);
  961. int ye = std::min(ysize - 1, y + testWSize/2);
  962. for (int yl = ys; yl <= ye; yl++)
  963. {
  964. for (int xl = xs; xl <= xe; xl++)
  965. {
  966. for ( int j = 0 ; j < cr.scores.size(); j++ )
  967. {
  968. probabilities ( xl, yl, j ) = cr.scores[j];
  969. }
  970. segresult ( xl, yl ) = cr.classno;
  971. noveltyImage ( xl, yl ) = gpMeanRatioVal;
  972. }
  973. }
  974. example.svec->clear();
  975. }
  976. delete example.svec;
  977. example.svec = NULL;
  978. }
  979. }
  980. void SemSegNovelty::computeNoveltyByGPWeightAll( NICE::FloatImage & noveltyImage,
  981. const NICE::MultiChannelImageT<double> & feats,
  982. NICE::Image & segresult,
  983. NICE::MultiChannelImageT<double> & probabilities,
  984. const int & xsize, const int & ysize, const int & featdim )
  985. {
  986. double gpNoise = conf->gD("GPHIK", "noise", 0.01);
  987. #pragma omp parallel for
  988. for ( int y = 0; y < ysize; y += testWSize )
  989. {
  990. Example example;
  991. example.vec = NULL;
  992. example.svec = new SparseVector ( featdim );
  993. for ( int x = 0; x < xsize; x += testWSize)
  994. {
  995. for ( int f = 0; f < featdim; f++ )
  996. {
  997. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  998. if ( val > 1e-10 )
  999. ( *example.svec ) [f] = val;
  1000. }
  1001. example.svec->normalize();
  1002. ClassificationResult cr = classifier->classify ( example );
  1003. double firstTerm (1.0 / sqrt(cr.uncertainty+gpNoise));
  1004. double gpWeightAllVal ( 0.0 );
  1005. if ( numberOfClasses > 2)
  1006. {
  1007. //compute the weight in the alpha-vector for every sample after assuming it to be
  1008. // added to the training set.
  1009. // Thereby, we measure its "importance" for the current model
  1010. //
  1011. //double firstTerm is already computed
  1012. //
  1013. //the second term is only needed when computing impacts
  1014. //double secondTerm; //this is the nasty guy :/
  1015. //--- compute the third term
  1016. // this is the difference between predicted label and GT label
  1017. std::vector<double> diffToPositive; diffToPositive.clear();
  1018. std::vector<double> diffToNegative; diffToNegative.clear();
  1019. double diffToNegativeSum(0.0);
  1020. for ( int j = 0 ; j < cr.scores.size(); j++ )
  1021. {
  1022. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  1023. {
  1024. continue;
  1025. }
  1026. // look at the difference to plus 1
  1027. diffToPositive.push_back(abs(cr.scores[j] - 1));
  1028. // look at the difference to -1
  1029. diffToNegative.push_back(abs(cr.scores[j] + 1));
  1030. //sum up the difference to -1
  1031. diffToNegativeSum += abs(cr.scores[j] - 1);
  1032. }
  1033. //let's subtract for every class its diffToNegative from the sum, add its diffToPositive,
  1034. //and use this as the third term for this specific class.
  1035. //the final value is obtained by minimizing over all classes
  1036. //
  1037. // originally, we minimize over all classes after building the final score
  1038. // however, the first and the second term do not depend on the choice of
  1039. // y*, therefore we minimize here already
  1040. double thirdTerm (numeric_limits<double>::max()) ;
  1041. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1042. {
  1043. double tmpVal ( diffToPositive[tmpCnt] + (diffToNegativeSum-diffToNegative[tmpCnt]) );
  1044. if (tmpVal < thirdTerm)
  1045. thirdTerm = tmpVal;
  1046. }
  1047. gpWeightAllVal = thirdTerm*firstTerm;
  1048. }
  1049. else //binary scenario
  1050. {
  1051. gpWeightAllVal = std::min( abs(cr.scores[*classesInUse.begin()]+1), abs(cr.scores[*classesInUse.begin()]-1) );
  1052. gpWeightAllVal *= firstTerm;
  1053. }
  1054. int xs = std::max(0, x - testWSize/2);
  1055. int xe = std::min(xsize - 1, x + testWSize/2);
  1056. int ys = std::max(0, y - testWSize/2);
  1057. int ye = std::min(ysize - 1, y + testWSize/2);
  1058. for (int yl = ys; yl <= ye; yl++)
  1059. {
  1060. for (int xl = xs; xl <= xe; xl++)
  1061. {
  1062. for ( int j = 0 ; j < cr.scores.size(); j++ )
  1063. {
  1064. probabilities ( xl, yl, j ) = cr.scores[j];
  1065. }
  1066. segresult ( xl, yl ) = cr.classno;
  1067. noveltyImage ( xl, yl ) = gpWeightAllVal;
  1068. }
  1069. }
  1070. example.svec->clear();
  1071. }
  1072. delete example.svec;
  1073. example.svec = NULL;
  1074. }
  1075. }
  1076. void SemSegNovelty::computeNoveltyByGPWeightRatio( NICE::FloatImage & noveltyImage,
  1077. const NICE::MultiChannelImageT<double> & feats,
  1078. NICE::Image & segresult,
  1079. NICE::MultiChannelImageT<double> & probabilities,
  1080. const int & xsize, const int & ysize, const int & featdim )
  1081. {
  1082. double gpNoise = conf->gD("GPHIK", "noise", 0.01);
  1083. #pragma omp parallel for
  1084. for ( int y = 0; y < ysize; y += testWSize )
  1085. {
  1086. Example example;
  1087. example.vec = NULL;
  1088. example.svec = new SparseVector ( featdim );
  1089. for ( int x = 0; x < xsize; x += testWSize)
  1090. {
  1091. for ( int f = 0; f < featdim; f++ )
  1092. {
  1093. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  1094. if ( val > 1e-10 )
  1095. ( *example.svec ) [f] = val;
  1096. }
  1097. example.svec->normalize();
  1098. ClassificationResult cr = classifier->classify ( example );
  1099. double firstTerm (1.0 / sqrt(cr.uncertainty+gpNoise));
  1100. double gpWeightRatioVal ( 0.0 );
  1101. if ( numberOfClasses > 2)
  1102. {
  1103. //compute the weight in the alpha-vector for every sample after assuming it to be
  1104. // added to the training set.
  1105. // Thereby, we measure its "importance" for the current model
  1106. //
  1107. //double firstTerm is already computed
  1108. //
  1109. //the second term is only needed when computing impacts
  1110. //double secondTerm; //this is the nasty guy :/
  1111. //--- compute the third term
  1112. // this is the difference between predicted label and GT label
  1113. std::vector<double> diffToPositive; diffToPositive.clear();
  1114. std::vector<double> diffToNegative; diffToNegative.clear();
  1115. double diffToNegativeSum(0.0);
  1116. for ( int j = 0 ; j < cr.scores.size(); j++ )
  1117. {
  1118. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  1119. {
  1120. continue;
  1121. }
  1122. // look at the difference to plus 1
  1123. diffToPositive.push_back(abs(cr.scores[j] - 1));
  1124. }
  1125. //let's subtract for every class its diffToNegative from the sum, add its diffToPositive,
  1126. //and use this as the third term for this specific class.
  1127. //the final value is obtained by minimizing over all classes
  1128. //
  1129. // originally, we minimize over all classes after building the final score
  1130. // however, the first and the second term do not depend on the choice of
  1131. // y*, therefore we minimize here already
  1132. //now look on the ratio of the resulting weights for the most plausible
  1133. // against the second most plausible class
  1134. double thirdTermMostPlausible ( 0.0 ) ;
  1135. double thirdTermSecondMostPlausible ( 0.0 ) ;
  1136. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1137. {
  1138. if (diffToPositive[tmpCnt] > thirdTermMostPlausible)
  1139. {
  1140. thirdTermSecondMostPlausible = thirdTermMostPlausible;
  1141. thirdTermMostPlausible = diffToPositive[tmpCnt];
  1142. }
  1143. else if (diffToPositive[tmpCnt] > thirdTermSecondMostPlausible)
  1144. {
  1145. thirdTermSecondMostPlausible = diffToPositive[tmpCnt];
  1146. }
  1147. }
  1148. //compute the resulting score
  1149. gpWeightRatioVal = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm;
  1150. //finally, look for this feature how it would affect to whole model (summarized by weight-vector alpha), if we would
  1151. //use it as an additional training example
  1152. //TODO this would be REALLY computational demanding. Do we really want to do this?
  1153. // gpImpactAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm*secondTerm;
  1154. // gpImpactRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm*secondTerm;
  1155. }
  1156. else //binary scenario
  1157. {
  1158. gpWeightRatioVal = std::min( abs(cr.scores[*classesInUse.begin()]+1), abs(cr.scores[*classesInUse.begin()]-1) );
  1159. gpWeightRatioVal *= firstTerm;
  1160. }
  1161. int xs = std::max(0, x - testWSize/2);
  1162. int xe = std::min(xsize - 1, x + testWSize/2);
  1163. int ys = std::max(0, y - testWSize/2);
  1164. int ye = std::min(ysize - 1, y + testWSize/2);
  1165. for (int yl = ys; yl <= ye; yl++)
  1166. {
  1167. for (int xl = xs; xl <= xe; xl++)
  1168. {
  1169. for ( int j = 0 ; j < cr.scores.size(); j++ )
  1170. {
  1171. probabilities ( xl, yl, j ) = cr.scores[j];
  1172. }
  1173. segresult ( xl, yl ) = cr.classno;
  1174. noveltyImage ( xl, yl ) = gpWeightRatioVal;
  1175. }
  1176. }
  1177. example.svec->clear();
  1178. }
  1179. delete example.svec;
  1180. example.svec = NULL;
  1181. }
  1182. }
  1183. void SemSegNovelty::addNewExample(const NICE::Vector& newExample, const int & newClassNo)
  1184. {
  1185. //accept the new class as valid information
  1186. if ( forbidden_classesTrain.find ( newClassNo ) != forbidden_classesTrain.end() )
  1187. {
  1188. forbidden_classesTrain.erase(newClassNo);
  1189. numberOfClasses++;
  1190. }
  1191. if ( classesInUse.find ( newClassNo ) == classesInUse.end() )
  1192. {
  1193. classesInUse.insert( newClassNo );
  1194. }
  1195. //then add it to the classifier used
  1196. if ( classifier != NULL )
  1197. {
  1198. //TODO
  1199. }
  1200. else //vclassifier
  1201. {
  1202. if (this->classifierString.compare("nn") == 0)
  1203. {
  1204. vclassifier->teach ( newClassNo, newExample );
  1205. }
  1206. }
  1207. }
  1208. void SemSegNovelty::addNovelExamples()
  1209. {
  1210. Timer timer;
  1211. //show the image that contains the most novel region
  1212. if (visualizeALimages)
  1213. showImage(maskedImg, "Most novel region");
  1214. timer.start();
  1215. std::stringstream out;
  1216. std::vector< std::string > list2;
  1217. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  1218. out << resultdir << "/" << list2.back();
  1219. maskedImg.writePPM ( out.str() + "_run_" + NICE::intToString(this->iterationCountSuffix) + "_" + noveltyMethodString+ "_query.ppm" );
  1220. timer.stop();
  1221. std::cerr << "AL time for writing queried image: " << timer.getLast() << std::endl;
  1222. timer.start();
  1223. //check which classes will be added using the features from the novel region
  1224. std::set<int> newClassNumbers;
  1225. newClassNumbers.clear(); //just to be sure
  1226. for ( uint i = 0 ; i < newTrainExamples.size() ; i++ )
  1227. {
  1228. if (newClassNumbers.find(newTrainExamples[i].first /* classNumber*/) == newClassNumbers.end() )
  1229. {
  1230. newClassNumbers.insert(newTrainExamples[i].first );
  1231. }
  1232. }
  1233. //accept the new classes as valid information
  1234. for (std::set<int>::const_iterator clNoIt = newClassNumbers.begin(); clNoIt != newClassNumbers.end(); clNoIt++)
  1235. {
  1236. if ( forbidden_classesTrain.find ( *clNoIt ) != forbidden_classesTrain.end() )
  1237. {
  1238. forbidden_classesTrain.erase(*clNoIt);
  1239. numberOfClasses++;
  1240. }
  1241. if ( classesInUse.find ( *clNoIt ) == classesInUse.end() )
  1242. {
  1243. classesInUse.insert( *clNoIt );
  1244. }
  1245. }
  1246. timer.stop();
  1247. std::cerr << "AL time for accepting possible new classes: " << timer.getLast() << std::endl;
  1248. timer.start();
  1249. //then add the new features to the classifier used
  1250. if ( classifier != NULL )
  1251. {
  1252. if (this->classifierString.compare("ClassifierGPHIK") == 0)
  1253. {
  1254. classifier->addMultipleExamples ( this->newTrainExamples );
  1255. }
  1256. }
  1257. else //vclassifier
  1258. {
  1259. //TODO
  1260. }
  1261. timer.stop();
  1262. std::cerr << "AL time for actually updating the classifier: " << timer.getLast() << std::endl;
  1263. std::cerr << "the current region to query is: " << currentRegionToQuery.first << " -- " << currentRegionToQuery.second << std::endl;
  1264. //did we already query a region of this image?
  1265. if ( queriedRegions.find( currentRegionToQuery.first ) != queriedRegions.end() )
  1266. {
  1267. queriedRegions[ currentRegionToQuery.first ].insert(currentRegionToQuery.second);
  1268. }
  1269. else
  1270. {
  1271. std::set<int> tmpSet; tmpSet.insert(currentRegionToQuery.second);
  1272. queriedRegions.insert(std::pair<std::string,std::set<int> > (currentRegionToQuery.first, tmpSet ) );
  1273. }
  1274. std::cerr << "Write already queried regions: " << std::endl;
  1275. for (std::map<std::string,std::set<int> >::const_iterator it = queriedRegions.begin(); it != queriedRegions.end(); it++)
  1276. {
  1277. std::cerr << "image: " << it->first << " -- ";
  1278. for (std::set<int>::const_iterator itReg = it->second.begin(); itReg != it->second.end(); itReg++)
  1279. {
  1280. std::cerr << *itReg << " ";
  1281. }
  1282. std::cerr << std::endl;
  1283. }
  1284. //clear the latest results, since one iteration is over
  1285. globalMaxUncert = -numeric_limits<double>::max();
  1286. if (!mostNoveltyWithMaxScores)
  1287. globalMaxUncert = numeric_limits<double>::max();
  1288. }
  1289. const Examples * SemSegNovelty::getNovelExamples() const
  1290. {
  1291. return &(this->newTrainExamples);
  1292. }
  1293. ///////////////////// INTERFACE PERSISTENT /////////////////////
  1294. // interface specific methods for store and restore
  1295. ///////////////////// INTERFACE PERSISTENT /////////////////////
  1296. void SemSegNovelty::restore ( std::istream & is, int format )
  1297. {
  1298. //delete everything we knew so far...
  1299. this->clear();
  1300. bool b_restoreVerbose ( false );
  1301. #ifdef B_RESTOREVERBOSE
  1302. b_restoreVerbose = true;
  1303. #endif
  1304. if ( is.good() )
  1305. {
  1306. if ( b_restoreVerbose )
  1307. std::cerr << " restore SemSegNovelty" << std::endl;
  1308. std::string tmp;
  1309. is >> tmp; //class name
  1310. if ( ! this->isStartTag( tmp, "SemSegNovelty" ) )
  1311. {
  1312. std::cerr << " WARNING - attempt to restore SemSegNovelty, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  1313. throw;
  1314. }
  1315. if (classifier != NULL)
  1316. {
  1317. delete classifier;
  1318. classifier = NULL;
  1319. }
  1320. is.precision (numeric_limits<double>::digits10 + 1);
  1321. bool b_endOfBlock ( false ) ;
  1322. while ( !b_endOfBlock )
  1323. {
  1324. is >> tmp; // start of block
  1325. if ( this->isEndTag( tmp, "SemSegNovelty" ) )
  1326. {
  1327. b_endOfBlock = true;
  1328. continue;
  1329. }
  1330. tmp = this->removeStartTag ( tmp );
  1331. if ( b_restoreVerbose )
  1332. std::cerr << " currently restore section " << tmp << " in SemSegNovelty" << std::endl;
  1333. if ( tmp.compare("Config") == 0 )
  1334. {
  1335. //TODO think about to put the config fix as first part in store restore... currently, its position is flexible
  1336. // possibly obsolete safety checks
  1337. if ( conf == NULL )
  1338. conf = new Config;
  1339. conf->clear();
  1340. //we do not want to read until the end of the file
  1341. conf->setIoUntilEndOfFile( false );
  1342. //load every options we determined explicitely
  1343. conf->restore(is, format);
  1344. // set internal variables, default values, and all those funny things
  1345. this->init();
  1346. is >> tmp; // end of block
  1347. tmp = this->removeEndTag ( tmp );
  1348. }
  1349. else if ( tmp.compare("classifier") == 0 )
  1350. {
  1351. std::string isNull;
  1352. is >> isNull;
  1353. // check whether we originally used a classifier
  1354. if ( isNull.compare( "NULL" ) == 0 )
  1355. {
  1356. if ( classifier != NULL )
  1357. delete classifier;
  1358. classifier = NULL;
  1359. }
  1360. else
  1361. {
  1362. if ( classifier == NULL )
  1363. classifier = new OBJREC::GPHIKClassifierNICE();
  1364. classifier->restore(is, format);
  1365. }
  1366. is >> tmp; // end of block
  1367. tmp = this->removeEndTag ( tmp );
  1368. }
  1369. else if ( tmp.compare("vclassifier") == 0 )
  1370. {
  1371. std::string isNull;
  1372. is >> isNull;
  1373. // check whether we originally used a vclassifier
  1374. if ( isNull.compare( "NULL" ) == 0 )
  1375. {
  1376. if ( vclassifier != NULL )
  1377. delete vclassifier;
  1378. vclassifier = NULL;
  1379. }
  1380. else
  1381. {
  1382. fthrow ( NICE::Exception, "Restoring of VecClassifiers is not implemented yet!" );
  1383. /* if ( vclassifier == NULL )
  1384. vclassifier = new OBJREC::VecClassifier();
  1385. vclassifier->restore(is, format); */
  1386. }
  1387. is >> tmp; // end of block
  1388. tmp = this->removeEndTag ( tmp );
  1389. }
  1390. else if ( tmp.compare("forbidden_classesTrain") == 0 )
  1391. {
  1392. is >> tmp; // size
  1393. int forbClTrainSize ( 0 );
  1394. is >> forbClTrainSize;
  1395. forbidden_classesTrain.clear();
  1396. if ( b_restoreVerbose )
  1397. std::cerr << "restore forbidden_classesTrain with size: " << forbClTrainSize << std::endl;
  1398. if ( forbClTrainSize > 0 )
  1399. {
  1400. if ( b_restoreVerbose )
  1401. std::cerr << " restore forbidden_classesTrain" << std::endl;
  1402. for (int i = 0; i < forbClTrainSize; i++)
  1403. {
  1404. int classNo;
  1405. is >> classNo;
  1406. forbidden_classesTrain.insert ( classNo );
  1407. }
  1408. }
  1409. else
  1410. {
  1411. if ( b_restoreVerbose )
  1412. std::cerr << " skip restoring forbidden_classesTrain" << std::endl;
  1413. }
  1414. is >> tmp; // end of block
  1415. tmp = this->removeEndTag ( tmp );
  1416. }
  1417. else if ( tmp.compare("forbidden_classesActiveLearning") == 0 )
  1418. {
  1419. is >> tmp; // size
  1420. int forbClALSize ( 0 );
  1421. is >> forbClALSize;
  1422. forbidden_classesActiveLearning.clear();
  1423. if ( b_restoreVerbose )
  1424. std::cerr << "restore forbidden_classesActiveLearning with size: " << forbClALSize << std::endl;
  1425. if ( forbClALSize > 0 )
  1426. {
  1427. if ( b_restoreVerbose )
  1428. std::cerr << " restore forbidden_classesActiveLearning" << std::endl;
  1429. for (int i = 0; i < forbClALSize; i++)
  1430. {
  1431. int classNo;
  1432. is >> classNo;
  1433. forbidden_classesActiveLearning.insert ( classNo );
  1434. }
  1435. }
  1436. else
  1437. {
  1438. if ( b_restoreVerbose )
  1439. std::cerr << " skip restoring forbidden_classesActiveLearning" << std::endl;
  1440. }
  1441. is >> tmp; // end of block
  1442. tmp = this->removeEndTag ( tmp );
  1443. }
  1444. else if ( tmp.compare("classesInUse") == 0 )
  1445. {
  1446. is >> tmp; // size
  1447. int clInUseSize ( 0 );
  1448. is >> clInUseSize;
  1449. classesInUse.clear();
  1450. if ( b_restoreVerbose )
  1451. std::cerr << "restore classesInUse with size: " << clInUseSize << std::endl;
  1452. if ( clInUseSize > 0 )
  1453. {
  1454. if ( b_restoreVerbose )
  1455. std::cerr << " restore classesInUse" << std::endl;
  1456. for (int i = 0; i < clInUseSize; i++)
  1457. {
  1458. int classNo;
  1459. is >> classNo;
  1460. classesInUse.insert ( classNo );
  1461. }
  1462. }
  1463. else
  1464. {
  1465. if ( b_restoreVerbose )
  1466. std::cerr << " skip restoring classesInUse" << std::endl;
  1467. }
  1468. is >> tmp; // end of block
  1469. tmp = this->removeEndTag ( tmp );
  1470. }
  1471. else if ( tmp.compare("SemanticSegmentation") == 0 )
  1472. {
  1473. // restore parent object
  1474. SemanticSegmentation::restore(is);
  1475. }
  1476. else
  1477. {
  1478. std::cerr << "WARNING -- unexpected SemSegNovelty object -- " << tmp << " -- for restoration... aborting" << std::endl;
  1479. throw;
  1480. }
  1481. }
  1482. }
  1483. else
  1484. {
  1485. std::cerr << "SemSegNovelty::restore -- InStream not initialized - restoring not possible!" << std::endl;
  1486. throw;
  1487. }
  1488. }
  1489. void SemSegNovelty::store ( std::ostream & os, int format ) const
  1490. {
  1491. if (os.good())
  1492. {
  1493. // show starting point
  1494. os << this->createStartTag( "SemSegNovelty" ) << std::endl;
  1495. os.precision (numeric_limits<double>::digits10 + 1);
  1496. os << this->createStartTag( "Config" ) << std::endl;
  1497. //we do not want to read until end of file for restoring
  1498. conf->setIoUntilEndOfFile(false);
  1499. conf->store(os,format);
  1500. os << this->createEndTag( "Config" ) << std::endl;
  1501. // now, write all variables which might have changed over time compared to initial settings
  1502. os << this->createStartTag( "forbidden_classesTrain" ) << std::endl;
  1503. os << "size: " << forbidden_classesTrain.size() << std::endl;
  1504. for ( std::set< int >::const_iterator itForbClassTrain = forbidden_classesTrain.begin();
  1505. itForbClassTrain != forbidden_classesTrain.end();
  1506. itForbClassTrain++
  1507. )
  1508. {
  1509. os << *itForbClassTrain << " " << std::endl;
  1510. }
  1511. os << this->createEndTag( "forbidden_classesTrain" ) << std::endl;
  1512. //
  1513. os << this->createStartTag( "forbidden_classesActiveLearning" ) << std::endl;
  1514. os << "size: " << forbidden_classesActiveLearning.size() << std::endl;
  1515. for ( std::set< int >::const_iterator itForbClassAL = forbidden_classesActiveLearning.begin();
  1516. itForbClassAL != forbidden_classesActiveLearning.end();
  1517. itForbClassAL++
  1518. )
  1519. {
  1520. os << *itForbClassAL << " " << std::endl;
  1521. }
  1522. os << this->createEndTag( "forbidden_classesActiveLearning" ) << std::endl;
  1523. //
  1524. os << this->createStartTag( "classesInUse" ) << std::endl;
  1525. os << "size: " << classesInUse.size() << std::endl;
  1526. for ( std::set< int >::const_iterator itClassesInUse = classesInUse.begin();
  1527. itClassesInUse != classesInUse.end();
  1528. itClassesInUse++
  1529. )
  1530. {
  1531. os << *itClassesInUse << " " << std::endl;
  1532. }
  1533. os << this->createEndTag( "classesInUse" ) << std::endl;
  1534. //
  1535. os << this->createStartTag( "classifier" ) << std::endl;
  1536. if ( this->classifier != NULL )
  1537. {
  1538. os << "NOTNULL" << std::endl;
  1539. classifier->store ( os, format );
  1540. }
  1541. else
  1542. {
  1543. os << "NULL" << std::endl;
  1544. }
  1545. os << this->createEndTag( "classifier" ) << std::endl;
  1546. //
  1547. os << this->createStartTag( "vclassifier" ) << std::endl;
  1548. if ( this->classifier != NULL )
  1549. {
  1550. os << "NOTNULL" << std::endl;
  1551. vclassifier->store ( os, format );
  1552. }
  1553. else
  1554. {
  1555. os << "NULL" << std::endl;
  1556. }
  1557. os << this->createEndTag( "vclassifier" ) << std::endl;
  1558. //TODO
  1559. /*
  1560. os << this->createStartTag( "queriedRegions" ) << std::endl;
  1561. os << "size: " << queriedRegions.size() << std::endl;
  1562. for ( std::map<std::string,std::set<int> >::const_iterator itQueriedReg = queriedRegions.begin();
  1563. itQueriedReg != queriedRegions.end();
  1564. itQueriedReg++
  1565. )
  1566. {
  1567. os << *itForbClassTrain << " " << std::endl;
  1568. }
  1569. os << this->createEndTag( "queriedRegions" ) << std::endl; */
  1570. // store parent object
  1571. SemanticSegmentation::store(os);
  1572. // done
  1573. os << this->createEndTag( "SemSegNovelty" ) << std::endl;
  1574. }
  1575. else
  1576. {
  1577. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  1578. }
  1579. }
  1580. void SemSegNovelty::clear ()
  1581. {
  1582. //TODO
  1583. }