SemSegNovelty.cpp 70 KB

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