SemSegNovelty.cpp 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  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> m_CurrentImageFeatures;
  422. // extract features
  423. m_CurrentImageFeatures.freeData();
  424. featExtract->getFeats ( img, m_CurrentImageFeatures );
  425. featdim = m_CurrentImageFeatures.channels();
  426. m_CurrentImageFeatures.addChannel(featdim);
  427. for (int c = 0; c < featdim; c++)
  428. {
  429. ImageT<double> tmp = m_CurrentImageFeatures[c];
  430. ImageT<double> tmp2 = m_CurrentImageFeatures[c+featdim];
  431. NICE::FilterT<double, double, double>::gradientStrength (tmp, tmp2);
  432. }
  433. featdim += featdim;
  434. // compute integral images
  435. for ( int c = 0; c < featdim; c++ )
  436. {
  437. m_CurrentImageFeatures.calcIntegral ( c );
  438. }
  439. timer.stop();
  440. std::cout << "AL time for preparation: " << timer.getLastAbsolute() << std::endl;
  441. timer.start();
  442. //classification results currently only needed to be computed separately if we use the vclassifier, i.e., the nearest neighbor used
  443. // for the "novel feature learning" approach
  444. //in all other settings, such as active sem seg in general, we do this within the novelty-computation-methods
  445. if ( classifier == NULL )
  446. {
  447. this->computeClassificationResults( m_CurrentImageFeatures, segresult, probabilities, xsize, ysize, featdim);
  448. }
  449. // timer.stop();
  450. //
  451. // std::cerr << "classification results computed" << std::endl;
  452. FloatImage noveltyImage ( xsize, ysize );
  453. noveltyImage.set ( 0.0 );
  454. switch (noveltyMethod)
  455. {
  456. case GPVARIANCE:
  457. {
  458. this->computeNoveltyByVariance( noveltyImage, m_CurrentImageFeatures, segresult, probabilities, xsize, ysize, featdim );
  459. break;
  460. }
  461. case GPUNCERTAINTY:
  462. {
  463. this->computeNoveltyByGPUncertainty( noveltyImage, m_CurrentImageFeatures, segresult, probabilities, xsize, ysize, featdim );
  464. break;
  465. }
  466. case GPMINMEAN:
  467. {
  468. std::cerr << "compute novelty using the minimum mean" << std::endl;
  469. this->computeNoveltyByGPMean( noveltyImage, m_CurrentImageFeatures, segresult, probabilities, xsize, ysize, featdim );
  470. break;
  471. }
  472. case GPMEANRATIO:
  473. {
  474. this->computeNoveltyByGPMeanRatio( noveltyImage, m_CurrentImageFeatures, segresult, probabilities, xsize, ysize, featdim );
  475. break;
  476. }
  477. case GPWEIGHTALL:
  478. {
  479. this->computeNoveltyByGPWeightAll( noveltyImage, m_CurrentImageFeatures, segresult, probabilities, xsize, ysize, featdim );
  480. break;
  481. }
  482. case GPWEIGHTRATIO:
  483. {
  484. this->computeNoveltyByGPWeightRatio( noveltyImage, m_CurrentImageFeatures, segresult, probabilities, xsize, ysize, featdim );
  485. break;
  486. }
  487. case RANDOM:
  488. {
  489. this->computeNoveltyByRandom( noveltyImage, m_CurrentImageFeatures, segresult, probabilities, xsize, ysize, featdim );
  490. break;
  491. }
  492. default:
  493. {
  494. //do nothing, keep the image constant to 0.0
  495. break;
  496. }
  497. }
  498. timer.stop();
  499. std::cout << "AL time for novelty score computation: " << timer.getLastAbsolute() << std::endl;
  500. if (b_visualizeALimages)
  501. {
  502. ColorImage imgrgbTmp (xsize, ysize);
  503. ICETools::convertToRGB ( noveltyImage, imgrgbTmp );
  504. showImage(imgrgbTmp, "Novelty Image without Region Segmentation");
  505. }
  506. timer.start();
  507. //Regionen ermitteln
  508. if(regionSeg != NULL)
  509. {
  510. NICE::Matrix mask;
  511. int amountRegions = regionSeg->segRegions ( img, mask );
  512. //compute probs per region
  513. std::vector<std::vector<double> > regionProb(amountRegions, std::vector<double>(probabilities.channels(), -std::numeric_limits<double>::max() ));
  514. std::vector<double> regionNoveltyMeasure (amountRegions, 0.0);
  515. std::vector<int> regionCounter(amountRegions, 0);
  516. std::vector<int> regionCounterNovelty(amountRegions, 0);
  517. for ( int y = 0; y < ysize; y += testWSize) //y++)
  518. {
  519. for (int x = 0; x < xsize; x += testWSize) //x++)
  520. {
  521. int r = mask(x,y);
  522. regionCounter[r]++;
  523. for(int j = 0; j < probabilities.channels(); j++)
  524. {
  525. if( regionProb[r][j] == -std::numeric_limits<double>::max() )
  526. regionProb[r][j] = 0.0f;
  527. regionProb[r][j] += probabilities ( x, y, j );
  528. }
  529. if ( forbidden_classesActiveLearning.find( labels(x,y) ) == forbidden_classesActiveLearning.end() )
  530. {
  531. //count the amount of "novelty" for the corresponding region
  532. regionNoveltyMeasure[r] += noveltyImage(x,y);
  533. regionCounterNovelty[r]++;
  534. }
  535. }
  536. }
  537. //find best class per region
  538. std::vector<int> bestClassPerRegion(amountRegions,0);
  539. double maxNoveltyScore = -numeric_limits<double>::max();
  540. if (!mostNoveltyWithMaxScores)
  541. {
  542. maxNoveltyScore = numeric_limits<double>::max();
  543. }
  544. int maxUncertRegion = -1;
  545. //loop over all regions and compute averaged novelty scores
  546. for(int r = 0; r < amountRegions; r++)
  547. {
  548. //check for the most plausible class per region
  549. double maxval = -numeric_limits<double>::max();
  550. //loop over all classes
  551. for(int c = 0; c < probabilities.channels(); c++)
  552. {
  553. regionProb[r][c] /= regionCounter[r];
  554. if( (maxval < regionProb[r][c]) ) //&& (regionProb[r][c] != 0.0) )
  555. {
  556. maxval = regionProb[r][c];
  557. bestClassPerRegion[r] = c;
  558. }
  559. }
  560. //if the region only contains unvalid information (e.g., background) skip it
  561. if (regionCounterNovelty[r] == 0)
  562. {
  563. continue;
  564. }
  565. //normalize summed novelty scores to region size
  566. regionNoveltyMeasure[r] /= regionCounterNovelty[r];
  567. //did we find a region that has a higher score as the most novel region known so far within this image?
  568. if( ( mostNoveltyWithMaxScores && (maxNoveltyScore < regionNoveltyMeasure[r]) ) // if we look for large novelty scores, e.g., variance
  569. || ( !mostNoveltyWithMaxScores && (maxNoveltyScore > regionNoveltyMeasure[r]) ) ) // if we look for small novelty scores, e.g., min mean
  570. {
  571. //did we already query a region of this image? -- and it was this specific region
  572. if ( (queriedRegions.find( currentFile ) != queriedRegions.end() ) && ( queriedRegions[currentFile].find(r) != queriedRegions[currentFile].end() ) )
  573. {
  574. continue;
  575. }
  576. else //only accept the region as novel if we never queried it before
  577. {
  578. maxNoveltyScore = regionNoveltyMeasure[r];
  579. maxUncertRegion = r;
  580. }
  581. }
  582. }
  583. // after finding the most novel region for the current image, check whether this region is also the most novel with respect
  584. // to all previously seen test images
  585. // if so, store the corresponding features, since we want to "actively" query them to incorporate useful information
  586. if(findMaximumUncert)
  587. {
  588. if( ( mostNoveltyWithMaxScores && (maxNoveltyScore > globalMaxUncert) )
  589. || ( !mostNoveltyWithMaxScores && (maxNoveltyScore < globalMaxUncert) ) )
  590. {
  591. //current most novel region of the image has "higher" novelty score then previous most novel region of all test images worked on so far
  592. // -> save new important features of this region
  593. Examples examples;
  594. for ( int y = 0; y < ysize; y += testWSize )
  595. {
  596. for ( int x = 0; x < xsize; x += testWSize)
  597. {
  598. if(mask(x,y) == maxUncertRegion)
  599. {
  600. if ( forbidden_classesActiveLearning.find(classnoTmp) != forbidden_classesActiveLearning.end() )
  601. continue;
  602. int classnoTmp = labels(x,y);
  603. Example t_Example(NULL, x, y);
  604. t_Example.vec = NULL;
  605. t_Example.svec = new SparseVector ( featdim );
  606. for ( int f = 0; f < featdim; f++ )
  607. {
  608. // double val = ( *example.svec ) [f];
  609. double val = m_CurrentImageFeatures.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  610. if ( val > 1e-10 )
  611. ( *t_Example.svec ) [f] = val;
  612. }
  613. examples.push_back ( pair<int, Example> ( classnoTmp, t_Example ) );
  614. }
  615. }
  616. }
  617. if(examples.size() > 0)
  618. {
  619. std::cerr << "found " << examples.size() << " new examples in the queried region" << std::endl << std::endl;
  620. // sauber aufräumen
  621. for( int i=0; i< newTrainExamples.size(); i++)
  622. {
  623. delete newTrainExamples.at(i).second.svec;
  624. newTrainExamples.at(i).second.svec = NULL;
  625. }
  626. newTrainExamples.clear();
  627. newTrainExamples = examples;
  628. globalMaxUncert = maxNoveltyScore;
  629. //prepare for later visualization
  630. // if (b_visualizeALimages)
  631. visualizeRegion(img,mask,maxUncertRegion,maskedImg);
  632. }
  633. else
  634. {
  635. std::cerr << "the queried region has no valid information" << std::endl << std::endl;
  636. }
  637. //save filename and region index
  638. currentRegionToQuery.first = currentFile;
  639. currentRegionToQuery.second = maxUncertRegion;
  640. }
  641. }
  642. //write back best results per region
  643. //i.e., write normalized novelty scores for every region into the novelty image
  644. for ( int y = 0; y < ysize; y++)
  645. {
  646. for (int x = 0; x < xsize; x++)
  647. {
  648. int r = mask(x,y);
  649. for(int j = 0; j < probabilities.channels(); j++)
  650. {
  651. probabilities ( x, y, j ) = regionProb[r][j];
  652. }
  653. segresult(x,y) = bestClassPerRegion[r];
  654. // write novelty scores for every segment into the "final" image
  655. noveltyImage(x,y) = regionNoveltyMeasure[r];
  656. }
  657. }
  658. } // if regionSeg != null
  659. timer.stop();
  660. std::cout << "AL time for determination of novel regions: " << timer.getLastAbsolute() << std::endl;
  661. if (b_visualizeALimages)
  662. {
  663. timer.start();
  664. std::stringstream out;
  665. std::vector< std::string > list2;
  666. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  667. out << resultdir << "/" << list2.back();
  668. noveltyImage.writeRaw(out.str() + "_run_" + NICE::intToString(this->iterationCountSuffix) + "_" + noveltyMethodString+".rawfloat");
  669. ColorImage imgrgb ( xsize, ysize );
  670. ICETools::convertToRGB ( noveltyImage, imgrgb );
  671. showImage(imgrgb, "Novelty Image");
  672. timer.stop();
  673. cout << "AL time for writing the raw novelty image: " << timer.getLastAbsolute() << endl;
  674. }
  675. }
  676. inline void SemSegNovelty::computeClassificationResults( const NICE::MultiChannelImageT<double> & feats,
  677. NICE::Image & segresult,
  678. NICE::MultiChannelImageT<double> & probabilities,
  679. const int & xsize,
  680. const int & ysize,
  681. const int & featdim
  682. )
  683. {
  684. std::cerr << "featdim: " << featdim << std::endl;
  685. if ( classifier != NULL )
  686. {
  687. #pragma omp parallel for
  688. for ( int y = 0; y < ysize; y += testWSize )
  689. {
  690. Example example;
  691. example.vec = NULL;
  692. example.svec = new SparseVector ( featdim );
  693. for ( int x = 0; x < xsize; x += testWSize)
  694. {
  695. for ( int f = 0; f < featdim; f++ )
  696. {
  697. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  698. if ( val > 1e-10 )
  699. ( *example.svec ) [f] = val;
  700. }
  701. example.svec->normalize();
  702. ClassificationResult cr = classifier->classify ( example );
  703. int xs = std::max(0, x - testWSize/2);
  704. int xe = std::min(xsize - 1, x + testWSize/2);
  705. int ys = std::max(0, y - testWSize/2);
  706. int ye = std::min(ysize - 1, y + testWSize/2);
  707. for (int yl = ys; yl <= ye; yl++)
  708. {
  709. for (int xl = xs; xl <= xe; xl++)
  710. {
  711. for ( int j = 0 ; j < cr.scores.size(); j++ )
  712. {
  713. probabilities ( xl, yl, j ) = cr.scores[j];
  714. }
  715. segresult ( xl, yl ) = cr.classno;
  716. }
  717. }
  718. example.svec->clear();
  719. }
  720. delete example.svec;
  721. example.svec = NULL;
  722. }
  723. }
  724. else //vclassifier
  725. {
  726. std::cerr << "compute classification results with vclassifier" << std::endl;
  727. #pragma omp parallel for
  728. for ( int y = 0; y < ysize; y += testWSize )
  729. {
  730. for ( int x = 0; x < xsize; x += testWSize)
  731. {
  732. NICE::Vector v(featdim);
  733. for ( int f = 0; f < featdim; f++ )
  734. {
  735. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  736. v[f] = val;
  737. }
  738. v.normalizeL1();
  739. ClassificationResult cr = vclassifier->classify ( v );
  740. int xs = std::max(0, x - testWSize/2);
  741. int xe = std::min(xsize - 1, x + testWSize/2);
  742. int ys = std::max(0, y - testWSize/2);
  743. int ye = std::min(ysize - 1, y + testWSize/2);
  744. for (int yl = ys; yl <= ye; yl++)
  745. {
  746. for (int xl = xs; xl <= xe; xl++)
  747. {
  748. for ( int j = 0 ; j < cr.scores.size(); j++ )
  749. {
  750. probabilities ( xl, yl, j ) = cr.scores[j];
  751. }
  752. segresult ( xl, yl ) = cr.classno;
  753. }
  754. }
  755. }
  756. }
  757. }
  758. }
  759. // compute novelty images depending on the strategy chosen
  760. void SemSegNovelty::computeNoveltyByRandom( NICE::FloatImage & noveltyImage,
  761. const NICE::MultiChannelImageT<double> & feats,
  762. NICE::Image & segresult,
  763. NICE::MultiChannelImageT<double> & probabilities,
  764. const int & xsize, const int & ysize, const int & featdim )
  765. {
  766. #pragma omp parallel for
  767. for ( int y = 0; y < ysize; y += testWSize )
  768. {
  769. Example example;
  770. example.vec = NULL;
  771. example.svec = new SparseVector ( featdim );
  772. for ( int x = 0; x < xsize; x += testWSize)
  773. {
  774. for ( int f = 0; f < featdim; f++ )
  775. {
  776. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  777. if ( val > 1e-10 )
  778. ( *example.svec ) [f] = val;
  779. }
  780. example.svec->normalize();
  781. ClassificationResult cr = classifier->classify ( example );
  782. int xs = std::max(0, x - testWSize/2);
  783. int xe = std::min(xsize - 1, x + testWSize/2);
  784. int ys = std::max(0, y - testWSize/2);
  785. int ye = std::min(ysize - 1, y + testWSize/2);
  786. double randVal = randDouble();
  787. for (int yl = ys; yl <= ye; yl++)
  788. {
  789. for (int xl = xs; xl <= xe; xl++)
  790. {
  791. for ( int j = 0 ; j < cr.scores.size(); j++ )
  792. {
  793. probabilities ( xl, yl, j ) = cr.scores[j];
  794. }
  795. segresult ( xl, yl ) = cr.classno;
  796. noveltyImage ( xl, yl ) = randVal;
  797. }
  798. }
  799. }
  800. }
  801. }
  802. void SemSegNovelty::computeNoveltyByVariance( NICE::FloatImage & noveltyImage,
  803. const NICE::MultiChannelImageT<double> & feats,
  804. NICE::Image & segresult,
  805. NICE::MultiChannelImageT<double> & probabilities,
  806. const int & xsize, const int & ysize, const int & featdim )
  807. {
  808. #pragma omp parallel for
  809. for ( int y = 0; y < ysize; y += testWSize )
  810. {
  811. Example example;
  812. example.vec = NULL;
  813. example.svec = new SparseVector ( featdim );
  814. for ( int x = 0; x < xsize; x += testWSize)
  815. {
  816. for ( int f = 0; f < featdim; f++ )
  817. {
  818. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  819. if ( val > 1e-10 )
  820. ( *example.svec ) [f] = val;
  821. }
  822. example.svec->normalize();
  823. ClassificationResult cr = classifier->classify ( example );
  824. int xs = std::max(0, x - testWSize/2);
  825. int xe = std::min(xsize - 1, x + testWSize/2);
  826. int ys = std::max(0, y - testWSize/2);
  827. int ye = std::min(ysize - 1, y + testWSize/2);
  828. for (int yl = ys; yl <= ye; yl++)
  829. {
  830. for (int xl = xs; xl <= xe; xl++)
  831. {
  832. for ( int j = 0 ; j < cr.scores.size(); j++ )
  833. {
  834. probabilities ( xl, yl, j ) = cr.scores[j];
  835. }
  836. segresult ( xl, yl ) = cr.classno;
  837. noveltyImage ( xl, yl ) = cr.uncertainty;
  838. }
  839. }
  840. example.svec->clear();
  841. }
  842. delete example.svec;
  843. example.svec = NULL;
  844. }
  845. }
  846. void SemSegNovelty::computeNoveltyByGPUncertainty( NICE::FloatImage & noveltyImage,
  847. const NICE::MultiChannelImageT<double> & feats,
  848. NICE::Image & segresult,
  849. NICE::MultiChannelImageT<double> & probabilities,
  850. const int & xsize, const int & ysize, const int & featdim )
  851. {
  852. double gpNoise = 0.01;
  853. //TODO getMethod for GPHIK
  854. //conf->gD("GPHIK", "noise", 0.01);
  855. #pragma omp parallel for
  856. for ( int y = 0; y < ysize; y += testWSize )
  857. {
  858. Example example;
  859. example.vec = NULL;
  860. example.svec = new SparseVector ( featdim );
  861. for ( int x = 0; x < xsize; x += testWSize)
  862. {
  863. for ( int f = 0; f < featdim; f++ )
  864. {
  865. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  866. if ( val > 1e-10 )
  867. ( *example.svec ) [f] = val;
  868. }
  869. example.svec->normalize();
  870. ClassificationResult cr = classifier->classify ( example );
  871. double maxMeanAbs ( 0.0 );
  872. for ( int j = 0 ; j < cr.scores.size(); j++ )
  873. {
  874. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  875. {
  876. continue;
  877. }
  878. //check for larger abs mean
  879. if (abs(cr.scores[j]) > maxMeanAbs)
  880. {
  881. maxMeanAbs = abs(cr.scores[j]);
  882. }
  883. }
  884. double firstTerm (1.0 / sqrt(cr.uncertainty+gpNoise));
  885. //compute the heuristic GP-UNCERTAINTY, as proposed by Kapoor et al. in IJCV 2010
  886. // GP-UNCERTAINTY : |mean| / sqrt(var^2 + gpnoise^2)
  887. double gpUncertaintyVal = maxMeanAbs*firstTerm; //firstTerm = 1.0 / sqrt(r.uncertainty+gpNoise))
  888. int xs = std::max(0, x - testWSize/2);
  889. int xe = std::min(xsize - 1, x + testWSize/2);
  890. int ys = std::max(0, y - testWSize/2);
  891. int ye = std::min(ysize - 1, y + testWSize/2);
  892. for (int yl = ys; yl <= ye; yl++)
  893. {
  894. for (int xl = xs; xl <= xe; xl++)
  895. {
  896. for ( int j = 0 ; j < cr.scores.size(); j++ )
  897. {
  898. probabilities ( xl, yl, j ) = cr.scores[j];
  899. }
  900. segresult ( xl, yl ) = cr.classno;
  901. noveltyImage ( xl, yl ) = gpUncertaintyVal;
  902. }
  903. }
  904. example.svec->clear();
  905. }
  906. delete example.svec;
  907. example.svec = NULL;
  908. }
  909. }
  910. void SemSegNovelty::computeNoveltyByGPMean( NICE::FloatImage & noveltyImage,
  911. const NICE::MultiChannelImageT<double> & feats,
  912. NICE::Image & segresult,
  913. NICE::MultiChannelImageT<double> & probabilities,
  914. const int & xsize, const int & ysize, const int & featdim )
  915. {
  916. double gpNoise = 0.01;
  917. //TODO getMethod for GPHIK
  918. //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 minMeanAbs ( numeric_limits<double>::max() );
  936. for ( int j = 0 ; j < probabilities.channels(); j++ )
  937. {
  938. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  939. {
  940. continue;
  941. }
  942. //check whether we found a class with higher smaller abs mean than the current minimum
  943. if (abs( cr.scores[j] ) < minMeanAbs)
  944. {
  945. minMeanAbs = abs(cr.scores[j]);
  946. }
  947. }
  948. // compute results when we take the lowest mean value of all classes
  949. double gpMeanVal = minMeanAbs;
  950. int xs = std::max(0, x - testWSize/2);
  951. int xe = std::min(xsize - 1, x + testWSize/2);
  952. int ys = std::max(0, y - testWSize/2);
  953. int ye = std::min(ysize - 1, y + testWSize/2);
  954. for (int yl = ys; yl <= ye; yl++)
  955. {
  956. for (int xl = xs; xl <= xe; xl++)
  957. {
  958. for ( int j = 0 ; j < cr.scores.size(); j++ )
  959. {
  960. probabilities ( xl, yl, j ) = cr.scores[j];
  961. }
  962. segresult ( xl, yl ) = cr.classno;
  963. noveltyImage ( xl, yl ) = gpMeanVal;
  964. }
  965. }
  966. }
  967. }
  968. }
  969. void SemSegNovelty::computeNoveltyByGPMeanRatio( NICE::FloatImage & noveltyImage,
  970. const NICE::MultiChannelImageT<double> & feats,
  971. NICE::Image & segresult,
  972. NICE::MultiChannelImageT<double> & probabilities,
  973. const int & xsize, const int & ysize, const int & featdim )
  974. {
  975. double gpNoise = 0.01;
  976. //TODO getMethod for GPHIK
  977. //conf->gD("GPHIK", "noise", 0.01);
  978. #pragma omp parallel for
  979. for ( int y = 0; y < ysize; y += testWSize )
  980. {
  981. Example example;
  982. example.vec = NULL;
  983. example.svec = new SparseVector ( featdim );
  984. for ( int x = 0; x < xsize; x += testWSize)
  985. {
  986. for ( int f = 0; f < featdim; f++ )
  987. {
  988. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  989. if ( val > 1e-10 )
  990. ( *example.svec ) [f] = val;
  991. }
  992. example.svec->normalize();
  993. ClassificationResult cr = classifier->classify ( example );
  994. double maxMean ( -numeric_limits<double>::max() );
  995. double sndMaxMean ( -numeric_limits<double>::max() );
  996. for ( int j = 0 ; j < cr.scores.size(); j++ )
  997. {
  998. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  999. {
  1000. continue;
  1001. }
  1002. //check for larger mean without abs as well
  1003. if (cr.scores[j] > maxMean)
  1004. {
  1005. sndMaxMean = maxMean;
  1006. maxMean = cr.scores[j];
  1007. }
  1008. // and also for the second highest mean of all classes
  1009. else if (cr.scores[j] > sndMaxMean)
  1010. {
  1011. sndMaxMean = cr.scores[j];
  1012. }
  1013. }
  1014. //look at the difference in the absolut mean values for the most plausible class
  1015. // and the second most plausible class
  1016. double gpMeanRatioVal= maxMean - sndMaxMean;
  1017. int xs = std::max(0, x - testWSize/2);
  1018. int xe = std::min(xsize - 1, x + testWSize/2);
  1019. int ys = std::max(0, y - testWSize/2);
  1020. int ye = std::min(ysize - 1, y + testWSize/2);
  1021. for (int yl = ys; yl <= ye; yl++)
  1022. {
  1023. for (int xl = xs; xl <= xe; xl++)
  1024. {
  1025. for ( int j = 0 ; j < cr.scores.size(); j++ )
  1026. {
  1027. probabilities ( xl, yl, j ) = cr.scores[j];
  1028. }
  1029. segresult ( xl, yl ) = cr.classno;
  1030. noveltyImage ( xl, yl ) = gpMeanRatioVal;
  1031. }
  1032. }
  1033. example.svec->clear();
  1034. }
  1035. delete example.svec;
  1036. example.svec = NULL;
  1037. }
  1038. }
  1039. void SemSegNovelty::computeNoveltyByGPWeightAll( NICE::FloatImage & noveltyImage,
  1040. const NICE::MultiChannelImageT<double> & feats,
  1041. NICE::Image & segresult,
  1042. NICE::MultiChannelImageT<double> & probabilities,
  1043. const int & xsize, const int & ysize, const int & featdim )
  1044. {
  1045. double gpNoise = 0.01;
  1046. //TODO getMethod for GPHIK
  1047. //conf->gD("GPHIK", "noise", 0.01);
  1048. #pragma omp parallel for
  1049. for ( int y = 0; y < ysize; y += testWSize )
  1050. {
  1051. Example example;
  1052. example.vec = NULL;
  1053. example.svec = new SparseVector ( featdim );
  1054. for ( int x = 0; x < xsize; x += testWSize)
  1055. {
  1056. for ( int f = 0; f < featdim; f++ )
  1057. {
  1058. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  1059. if ( val > 1e-10 )
  1060. ( *example.svec ) [f] = val;
  1061. }
  1062. example.svec->normalize();
  1063. ClassificationResult cr = classifier->classify ( example );
  1064. double firstTerm (1.0 / sqrt(cr.uncertainty+gpNoise));
  1065. double gpWeightAllVal ( 0.0 );
  1066. if ( numberOfClasses > 2)
  1067. {
  1068. //compute the weight in the alpha-vector for every sample after assuming it to be
  1069. // added to the training set.
  1070. // Thereby, we measure its "importance" for the current model
  1071. //
  1072. //double firstTerm is already computed
  1073. //
  1074. //the second term is only needed when computing impacts
  1075. //double secondTerm; //this is the nasty guy :/
  1076. //--- compute the third term
  1077. // this is the difference between predicted label and GT label
  1078. std::vector<double> diffToPositive; diffToPositive.clear();
  1079. std::vector<double> diffToNegative; diffToNegative.clear();
  1080. double diffToNegativeSum(0.0);
  1081. for ( int j = 0 ; j < cr.scores.size(); j++ )
  1082. {
  1083. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  1084. {
  1085. continue;
  1086. }
  1087. // look at the difference to plus 1
  1088. diffToPositive.push_back(abs(cr.scores[j] - 1));
  1089. // look at the difference to -1
  1090. diffToNegative.push_back(abs(cr.scores[j] + 1));
  1091. //sum up the difference to -1
  1092. diffToNegativeSum += abs(cr.scores[j] - 1);
  1093. }
  1094. //let's subtract for every class its diffToNegative from the sum, add its diffToPositive,
  1095. //and use this as the third term for this specific class.
  1096. //the final value is obtained by minimizing over all classes
  1097. //
  1098. // originally, we minimize over all classes after building the final score
  1099. // however, the first and the second term do not depend on the choice of
  1100. // y*, therefore we minimize here already
  1101. double thirdTerm (numeric_limits<double>::max()) ;
  1102. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1103. {
  1104. double tmpVal ( diffToPositive[tmpCnt] + (diffToNegativeSum-diffToNegative[tmpCnt]) );
  1105. if (tmpVal < thirdTerm)
  1106. thirdTerm = tmpVal;
  1107. }
  1108. gpWeightAllVal = thirdTerm*firstTerm;
  1109. }
  1110. else //binary scenario
  1111. {
  1112. gpWeightAllVal = std::min( abs(cr.scores[*classesInUse.begin()]+1), abs(cr.scores[*classesInUse.begin()]-1) );
  1113. gpWeightAllVal *= firstTerm;
  1114. }
  1115. int xs = std::max(0, x - testWSize/2);
  1116. int xe = std::min(xsize - 1, x + testWSize/2);
  1117. int ys = std::max(0, y - testWSize/2);
  1118. int ye = std::min(ysize - 1, y + testWSize/2);
  1119. for (int yl = ys; yl <= ye; yl++)
  1120. {
  1121. for (int xl = xs; xl <= xe; xl++)
  1122. {
  1123. for ( int j = 0 ; j < cr.scores.size(); j++ )
  1124. {
  1125. probabilities ( xl, yl, j ) = cr.scores[j];
  1126. }
  1127. segresult ( xl, yl ) = cr.classno;
  1128. noveltyImage ( xl, yl ) = gpWeightAllVal;
  1129. }
  1130. }
  1131. example.svec->clear();
  1132. }
  1133. delete example.svec;
  1134. example.svec = NULL;
  1135. }
  1136. }
  1137. void SemSegNovelty::computeNoveltyByGPWeightRatio( NICE::FloatImage & noveltyImage,
  1138. const NICE::MultiChannelImageT<double> & feats,
  1139. NICE::Image & segresult,
  1140. NICE::MultiChannelImageT<double> & probabilities,
  1141. const int & xsize, const int & ysize, const int & featdim )
  1142. {
  1143. double gpNoise = 0.01;
  1144. //TODO getMethod for GPHIK
  1145. //conf->gD("GPHIK", "noise", 0.01);
  1146. #pragma omp parallel for
  1147. for ( int y = 0; y < ysize; y += testWSize )
  1148. {
  1149. Example example;
  1150. example.vec = NULL;
  1151. example.svec = new SparseVector ( featdim );
  1152. for ( int x = 0; x < xsize; x += testWSize)
  1153. {
  1154. for ( int f = 0; f < featdim; f++ )
  1155. {
  1156. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  1157. if ( val > 1e-10 )
  1158. ( *example.svec ) [f] = val;
  1159. }
  1160. example.svec->normalize();
  1161. ClassificationResult cr = classifier->classify ( example );
  1162. double firstTerm (1.0 / sqrt(cr.uncertainty+gpNoise));
  1163. double gpWeightRatioVal ( 0.0 );
  1164. if ( numberOfClasses > 2)
  1165. {
  1166. //compute the weight in the alpha-vector for every sample after assuming it to be
  1167. // added to the training set.
  1168. // Thereby, we measure its "importance" for the current model
  1169. //
  1170. //double firstTerm is already computed
  1171. //
  1172. //the second term is only needed when computing impacts
  1173. //double secondTerm; //this is the nasty guy :/
  1174. //--- compute the third term
  1175. // this is the difference between predicted label and GT label
  1176. std::vector<double> diffToPositive; diffToPositive.clear();
  1177. std::vector<double> diffToNegative; diffToNegative.clear();
  1178. double diffToNegativeSum(0.0);
  1179. for ( int j = 0 ; j < cr.scores.size(); j++ )
  1180. {
  1181. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  1182. {
  1183. continue;
  1184. }
  1185. // look at the difference to plus 1
  1186. diffToPositive.push_back(abs(cr.scores[j] - 1));
  1187. }
  1188. //let's subtract for every class its diffToNegative from the sum, add its diffToPositive,
  1189. //and use this as the third term for this specific class.
  1190. //the final value is obtained by minimizing over all classes
  1191. //
  1192. // originally, we minimize over all classes after building the final score
  1193. // however, the first and the second term do not depend on the choice of
  1194. // y*, therefore we minimize here already
  1195. //now look on the ratio of the resulting weights for the most plausible
  1196. // against the second most plausible class
  1197. double thirdTermMostPlausible ( 0.0 ) ;
  1198. double thirdTermSecondMostPlausible ( 0.0 ) ;
  1199. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1200. {
  1201. if (diffToPositive[tmpCnt] > thirdTermMostPlausible)
  1202. {
  1203. thirdTermSecondMostPlausible = thirdTermMostPlausible;
  1204. thirdTermMostPlausible = diffToPositive[tmpCnt];
  1205. }
  1206. else if (diffToPositive[tmpCnt] > thirdTermSecondMostPlausible)
  1207. {
  1208. thirdTermSecondMostPlausible = diffToPositive[tmpCnt];
  1209. }
  1210. }
  1211. //compute the resulting score
  1212. gpWeightRatioVal = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm;
  1213. //finally, look for this feature how it would affect to whole model (summarized by weight-vector alpha), if we would
  1214. //use it as an additional training example
  1215. //TODO this would be REALLY computational demanding. Do we really want to do this?
  1216. // gpImpactAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm*secondTerm;
  1217. // gpImpactRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm*secondTerm;
  1218. }
  1219. else //binary scenario
  1220. {
  1221. gpWeightRatioVal = std::min( abs(cr.scores[*classesInUse.begin()]+1), abs(cr.scores[*classesInUse.begin()]-1) );
  1222. gpWeightRatioVal *= firstTerm;
  1223. }
  1224. int xs = std::max(0, x - testWSize/2);
  1225. int xe = std::min(xsize - 1, x + testWSize/2);
  1226. int ys = std::max(0, y - testWSize/2);
  1227. int ye = std::min(ysize - 1, y + testWSize/2);
  1228. for (int yl = ys; yl <= ye; yl++)
  1229. {
  1230. for (int xl = xs; xl <= xe; xl++)
  1231. {
  1232. for ( int j = 0 ; j < cr.scores.size(); j++ )
  1233. {
  1234. probabilities ( xl, yl, j ) = cr.scores[j];
  1235. }
  1236. segresult ( xl, yl ) = cr.classno;
  1237. noveltyImage ( xl, yl ) = gpWeightRatioVal;
  1238. }
  1239. }
  1240. example.svec->clear();
  1241. }
  1242. delete example.svec;
  1243. example.svec = NULL;
  1244. }
  1245. }
  1246. void SemSegNovelty::addNewExample(const NICE::Vector& v_newExample, const int & newClassNo)
  1247. {
  1248. //accept the new class as valid information
  1249. if ( forbidden_classesTrain.find ( newClassNo ) != forbidden_classesTrain.end() )
  1250. {
  1251. forbidden_classesTrain.erase(newClassNo);
  1252. numberOfClasses++;
  1253. }
  1254. if ( classesInUse.find ( newClassNo ) == classesInUse.end() )
  1255. {
  1256. classesInUse.insert( newClassNo );
  1257. }
  1258. //then add it to the classifier used
  1259. if ( classifier != NULL )
  1260. {
  1261. if (this->classifierString.compare("GPHIKClassifier") == 0)
  1262. {
  1263. Example newExample;
  1264. SparseVector svec ( v_newExample );
  1265. newExample.svec = &svec;
  1266. static_cast<GPHIKClassifierNICE*>(classifier)->addExample ( newExample, newClassNo );
  1267. }
  1268. }
  1269. else //vclassifier
  1270. {
  1271. if (this->classifierString.compare("nn") == 0)
  1272. {
  1273. vclassifier->teach ( newClassNo, v_newExample );
  1274. }
  1275. }
  1276. }
  1277. void SemSegNovelty::addNovelExamples()
  1278. {
  1279. Timer timer;
  1280. //show the image that contains the most novel region
  1281. if (b_visualizeALimages)
  1282. showImage(maskedImg, "Most novel region");
  1283. timer.start();
  1284. std::stringstream out;
  1285. std::vector< std::string > list2;
  1286. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  1287. out << resultdir << "/" << list2.back();
  1288. maskedImg.writePPM ( out.str() + "_run_" + NICE::intToString(this->iterationCountSuffix) + "_" + noveltyMethodString+ "_query.ppm" );
  1289. timer.stop();
  1290. std::cerr << "AL time for writing queried image: " << timer.getLast() << std::endl;
  1291. timer.start();
  1292. //check which classes will be added using the features from the novel region
  1293. std::set<int> newClassNumbers;
  1294. newClassNumbers.clear(); //just to be sure
  1295. for ( uint i = 0 ; i < newTrainExamples.size() ; i++ )
  1296. {
  1297. if (newClassNumbers.find(newTrainExamples[i].first /* classNumber*/) == newClassNumbers.end() )
  1298. {
  1299. newClassNumbers.insert(newTrainExamples[i].first );
  1300. }
  1301. }
  1302. //accept the new classes as valid information
  1303. for (std::set<int>::const_iterator clNoIt = newClassNumbers.begin(); clNoIt != newClassNumbers.end(); clNoIt++)
  1304. {
  1305. if ( forbidden_classesTrain.find ( *clNoIt ) != forbidden_classesTrain.end() )
  1306. {
  1307. forbidden_classesTrain.erase(*clNoIt);
  1308. numberOfClasses++;
  1309. }
  1310. if ( classesInUse.find ( *clNoIt ) == classesInUse.end() )
  1311. {
  1312. classesInUse.insert( *clNoIt );
  1313. }
  1314. }
  1315. timer.stop();
  1316. std::cerr << "AL time for accepting possible new classes: " << timer.getLast() << std::endl;
  1317. timer.start();
  1318. //then add the new features to the classifier used
  1319. if ( classifier != NULL )
  1320. {
  1321. if (this->classifierString.compare("GPHIKClassifier") == 0)
  1322. {
  1323. classifier->addMultipleExamples ( this->newTrainExamples );
  1324. }
  1325. }
  1326. else //vclassifier
  1327. {
  1328. //TODO
  1329. }
  1330. timer.stop();
  1331. std::cerr << "AL time for actually updating the classifier: " << timer.getLast() << std::endl;
  1332. std::cerr << "the current region to query is: " << currentRegionToQuery.first << " -- " << currentRegionToQuery.second << std::endl;
  1333. //did we already query a region of this image?
  1334. if ( queriedRegions.find( currentRegionToQuery.first ) != queriedRegions.end() )
  1335. {
  1336. queriedRegions[ currentRegionToQuery.first ].insert(currentRegionToQuery.second);
  1337. }
  1338. else
  1339. {
  1340. std::set<int> tmpSet; tmpSet.insert(currentRegionToQuery.second);
  1341. queriedRegions.insert(std::pair<std::string,std::set<int> > (currentRegionToQuery.first, tmpSet ) );
  1342. }
  1343. std::cerr << "Write already queried regions: " << std::endl;
  1344. for (std::map<std::string,std::set<int> >::const_iterator it = queriedRegions.begin(); it != queriedRegions.end(); it++)
  1345. {
  1346. std::cerr << "image: " << it->first << " -- ";
  1347. for (std::set<int>::const_iterator itReg = it->second.begin(); itReg != it->second.end(); itReg++)
  1348. {
  1349. std::cerr << *itReg << " ";
  1350. }
  1351. std::cerr << std::endl;
  1352. }
  1353. //clear the latest results, since one iteration is over
  1354. globalMaxUncert = -numeric_limits<double>::max();
  1355. if (!mostNoveltyWithMaxScores)
  1356. globalMaxUncert = numeric_limits<double>::max();
  1357. }
  1358. const Examples * SemSegNovelty::getNovelExamples() const
  1359. {
  1360. return &(this->newTrainExamples);
  1361. }
  1362. ///////////////////// INTERFACE PERSISTENT /////////////////////
  1363. // interface specific methods for store and restore
  1364. ///////////////////// INTERFACE PERSISTENT /////////////////////
  1365. void SemSegNovelty::restore ( std::istream & is, int format )
  1366. {
  1367. //delete everything we knew so far...
  1368. this->clear();
  1369. bool b_restoreVerbose ( false );
  1370. #ifdef B_RESTOREVERBOSE
  1371. b_restoreVerbose = true;
  1372. #endif
  1373. if ( is.good() )
  1374. {
  1375. if ( b_restoreVerbose )
  1376. std::cerr << " restore SemSegNovelty" << std::endl;
  1377. std::string tmp;
  1378. is >> tmp; //class name
  1379. if ( ! this->isStartTag( tmp, "SemSegNovelty" ) )
  1380. {
  1381. std::cerr << " WARNING - attempt to restore SemSegNovelty, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  1382. throw;
  1383. }
  1384. if (classifier != NULL)
  1385. {
  1386. delete classifier;
  1387. classifier = NULL;
  1388. }
  1389. is.precision (numeric_limits<double>::digits10 + 1);
  1390. bool b_endOfBlock ( false ) ;
  1391. while ( !b_endOfBlock )
  1392. {
  1393. is >> tmp; // start of block
  1394. if ( this->isEndTag( tmp, "SemSegNovelty" ) )
  1395. {
  1396. b_endOfBlock = true;
  1397. continue;
  1398. }
  1399. tmp = this->removeStartTag ( tmp );
  1400. if ( b_restoreVerbose )
  1401. std::cerr << " currently restore section " << tmp << " in SemSegNovelty" << std::endl;
  1402. ///////////////////////////////
  1403. // FEATURE EXTRACTION //
  1404. ///////////////////////////////
  1405. if ( tmp.compare("featExtract") == 0 )
  1406. {
  1407. featExtract->restore(is, format);
  1408. is >> tmp; // end of block
  1409. tmp = this->removeEndTag ( tmp );
  1410. }
  1411. else if ( tmp.compare("trainWSize") == 0 )
  1412. {
  1413. is >> trainWSize;
  1414. is >> tmp; // end of block
  1415. tmp = this->removeEndTag ( tmp );
  1416. }
  1417. else if ( tmp.compare("whs") == 0 )
  1418. {
  1419. is >> whs;
  1420. is >> tmp; // end of block
  1421. tmp = this->removeEndTag ( tmp );
  1422. }
  1423. else if ( tmp.compare("testWSize") == 0 )
  1424. {
  1425. is >> testWSize;
  1426. is >> tmp; // end of block
  1427. tmp = this->removeEndTag ( tmp );
  1428. }
  1429. ///////////////////////////////
  1430. // NOVELTY COMPUTATION //
  1431. ///////////////////////////////
  1432. else if ( tmp.compare("noveltyMethod") == 0 )
  1433. {
  1434. unsigned int ui_noveltyMethod;
  1435. is >> ui_noveltyMethod;
  1436. this->noveltyMethod = static_cast<NoveltyMethod> ( ui_noveltyMethod );
  1437. is >> tmp; // end of block
  1438. tmp = this->removeEndTag ( tmp );
  1439. }
  1440. else if ( tmp.compare("noveltyMethodString") == 0 )
  1441. {
  1442. is >> noveltyMethodString;
  1443. is >> tmp; // end of block
  1444. tmp = this->removeEndTag ( tmp );
  1445. }
  1446. else if ( tmp.compare("globalMaxUncert") == 0 )
  1447. {
  1448. is >> globalMaxUncert;
  1449. is >> tmp; // end of block
  1450. tmp = this->removeEndTag ( tmp );
  1451. }
  1452. else if ( tmp.compare("mostNoveltyWithMaxScores") == 0 )
  1453. {
  1454. is >> mostNoveltyWithMaxScores;
  1455. is >> tmp; // end of block
  1456. tmp = this->removeEndTag ( tmp );
  1457. }
  1458. else if ( tmp.compare("findMaximumUncert") == 0 )
  1459. {
  1460. is >> findMaximumUncert;
  1461. is >> tmp; // end of block
  1462. tmp = this->removeEndTag ( tmp );
  1463. }
  1464. //TODO maskedImg
  1465. else if ( tmp.compare("b_visualizeALimages") == 0 )
  1466. {
  1467. is >> b_visualizeALimages;
  1468. is >> tmp; // end of block
  1469. tmp = this->removeEndTag ( tmp );
  1470. }
  1471. ///////////////////////////////
  1472. // CLASSIFICATION STUFF //
  1473. ///////////////////////////////
  1474. else if ( tmp.compare("classifier") == 0 )
  1475. {
  1476. std::string isNull;
  1477. is >> isNull;
  1478. // check whether we originally used a classifier
  1479. if ( isNull.compare( "NULL" ) == 0 )
  1480. {
  1481. if ( classifier != NULL )
  1482. delete classifier;
  1483. classifier = NULL;
  1484. }
  1485. else
  1486. {
  1487. if ( classifier == NULL )
  1488. classifier = new OBJREC::GPHIKClassifierNICE();
  1489. classifier->restore(is, format);
  1490. }
  1491. is >> tmp; // end of block
  1492. tmp = this->removeEndTag ( tmp );
  1493. }
  1494. else if ( tmp.compare("vclassifier") == 0 )
  1495. {
  1496. std::string isNull;
  1497. is >> isNull;
  1498. // check whether we originally used a vclassifier
  1499. if ( isNull.compare( "NULL" ) == 0 )
  1500. {
  1501. if ( vclassifier != NULL )
  1502. delete vclassifier;
  1503. vclassifier = NULL;
  1504. }
  1505. else
  1506. {
  1507. fthrow ( NICE::Exception, "Restoring of VecClassifiers is not implemented yet!" );
  1508. /* if ( vclassifier == NULL )
  1509. vclassifier = new OBJREC::VecClassifier();
  1510. vclassifier->restore(is, format); */
  1511. }
  1512. is >> tmp; // end of block
  1513. tmp = this->removeEndTag ( tmp );
  1514. }
  1515. else if ( tmp.compare("forbidden_classesTrain") == 0 )
  1516. {
  1517. is >> tmp; // size
  1518. int forbClTrainSize ( 0 );
  1519. is >> forbClTrainSize;
  1520. forbidden_classesTrain.clear();
  1521. if ( b_restoreVerbose )
  1522. std::cerr << "restore forbidden_classesTrain with size: " << forbClTrainSize << std::endl;
  1523. if ( forbClTrainSize > 0 )
  1524. {
  1525. if ( b_restoreVerbose )
  1526. std::cerr << " restore forbidden_classesTrain" << std::endl;
  1527. for (int i = 0; i < forbClTrainSize; i++)
  1528. {
  1529. int classNo;
  1530. is >> classNo;
  1531. forbidden_classesTrain.insert ( classNo );
  1532. }
  1533. }
  1534. else
  1535. {
  1536. if ( b_restoreVerbose )
  1537. std::cerr << " skip restoring forbidden_classesTrain" << std::endl;
  1538. }
  1539. is >> tmp; // end of block
  1540. tmp = this->removeEndTag ( tmp );
  1541. }
  1542. else if ( tmp.compare("forbidden_classesActiveLearning") == 0 )
  1543. {
  1544. is >> tmp; // size
  1545. int forbClALSize ( 0 );
  1546. is >> forbClALSize;
  1547. forbidden_classesActiveLearning.clear();
  1548. if ( b_restoreVerbose )
  1549. std::cerr << "restore forbidden_classesActiveLearning with size: " << forbClALSize << std::endl;
  1550. if ( forbClALSize > 0 )
  1551. {
  1552. if ( b_restoreVerbose )
  1553. std::cerr << " restore forbidden_classesActiveLearning" << std::endl;
  1554. for (int i = 0; i < forbClALSize; i++)
  1555. {
  1556. int classNo;
  1557. is >> classNo;
  1558. forbidden_classesActiveLearning.insert ( classNo );
  1559. }
  1560. }
  1561. else
  1562. {
  1563. if ( b_restoreVerbose )
  1564. std::cerr << " skip restoring forbidden_classesActiveLearning" << std::endl;
  1565. }
  1566. is >> tmp; // end of block
  1567. tmp = this->removeEndTag ( tmp );
  1568. }
  1569. else if ( tmp.compare("classesInUse") == 0 )
  1570. {
  1571. is >> tmp; // size
  1572. int clInUseSize ( 0 );
  1573. is >> clInUseSize;
  1574. classesInUse.clear();
  1575. if ( b_restoreVerbose )
  1576. std::cerr << "restore classesInUse with size: " << clInUseSize << std::endl;
  1577. if ( clInUseSize > 0 )
  1578. {
  1579. if ( b_restoreVerbose )
  1580. std::cerr << " restore classesInUse" << std::endl;
  1581. for (int i = 0; i < clInUseSize; i++)
  1582. {
  1583. int classNo;
  1584. is >> classNo;
  1585. classesInUse.insert ( classNo );
  1586. }
  1587. }
  1588. else
  1589. {
  1590. if ( b_restoreVerbose )
  1591. std::cerr << " skip restoring classesInUse" << std::endl;
  1592. }
  1593. is >> tmp; // end of block
  1594. tmp = this->removeEndTag ( tmp );
  1595. }
  1596. else if ( tmp.compare("numberOfClasses") == 0 )
  1597. {
  1598. is >> numberOfClasses;
  1599. is >> tmp; // end of block
  1600. tmp = this->removeEndTag ( tmp );
  1601. }
  1602. else if ( tmp.compare("read_classifier") == 0 )
  1603. {
  1604. is >> read_classifier;
  1605. is >> tmp; // end of block
  1606. tmp = this->removeEndTag ( tmp );
  1607. }
  1608. else if ( tmp.compare("save_classifier") == 0 )
  1609. {
  1610. is >> save_classifier;
  1611. is >> tmp; // end of block
  1612. tmp = this->removeEndTag ( tmp );
  1613. }
  1614. else if ( tmp.compare("cache") == 0 )
  1615. {
  1616. is >> cache;
  1617. is >> tmp; // end of block
  1618. tmp = this->removeEndTag ( tmp );
  1619. }
  1620. else if ( tmp.compare("resultdir") == 0 )
  1621. {
  1622. is >> resultdir;
  1623. is >> tmp; // end of block
  1624. tmp = this->removeEndTag ( tmp );
  1625. }
  1626. //TODO newTrainExamples
  1627. ///////////////////////////////
  1628. // SEGMENTATION STUFF //
  1629. ///////////////////////////////
  1630. //TODO regionSeg
  1631. else if ( tmp.compare("s_rsMethode") == 0 )
  1632. {
  1633. is >> this->s_rsMethode;
  1634. // theoretically, we should properly store and restore the regionSeg object. However, its parent class does not provide
  1635. // a Persistent interface yet. Hence, we perform this tiny workaround which works, since regionSeg is not changed over time...
  1636. // only be aware of parameters originally set via config...
  1637. is >> tmp; // end of block
  1638. tmp = this->removeEndTag ( tmp );
  1639. }
  1640. //NOTE regionSeg seems really important to keep track off
  1641. else if ( tmp.compare("reuseSegmentation") == 0 )
  1642. {
  1643. is >> reuseSegmentation;
  1644. is >> tmp; // end of block
  1645. tmp = this->removeEndTag ( tmp );
  1646. }
  1647. else if ( tmp.compare("queriedRegions") == 0 )
  1648. {
  1649. is >> tmp; // size
  1650. int queriedRegionsSize ( 0 );
  1651. is >> queriedRegionsSize;
  1652. queriedRegions.clear();
  1653. if ( b_restoreVerbose )
  1654. std::cerr << "restore queriedRegions with size: " << queriedRegionsSize << std::endl;
  1655. for ( int i = 0; i < queriedRegionsSize; i++ )
  1656. {
  1657. // restore key
  1658. std::string key;
  1659. is >> key;
  1660. // restore values -- inner loop over sets
  1661. is >> tmp; // size
  1662. int regionsOfImgSize ( 0 );
  1663. is >> regionsOfImgSize;
  1664. std::set< int > regionsOfImg;
  1665. regionsOfImg.clear();
  1666. for (int i = 0; i < regionsOfImgSize; i++)
  1667. {
  1668. int idxRegion;
  1669. is >> idxRegion;
  1670. regionsOfImg.insert ( idxRegion );
  1671. }
  1672. queriedRegions.insert ( std::pair<std::string, std::set< int > > ( key, regionsOfImg ) );
  1673. }
  1674. is >> tmp; // end of block
  1675. tmp = this->removeEndTag ( tmp );
  1676. }
  1677. //
  1678. //TODO currentRegionToQuery
  1679. //
  1680. ///////////////////////////////
  1681. // PARENT OBJECT //
  1682. ///////////////////////////////
  1683. else if ( tmp.compare("SemSegNovelty--Parent") == 0 )
  1684. {
  1685. // restore parent object
  1686. SemanticSegmentation::restore(is);
  1687. }
  1688. else
  1689. {
  1690. std::cerr << "WARNING -- unexpected SemSegNovelty object -- " << tmp << " -- for restoration... aborting" << std::endl;
  1691. throw;
  1692. }
  1693. // INSTANTIATE (YET) NON-RESTORABLE OBJECTS
  1694. //TODO destructor of regionSeg is non-virtual so far - change this accordingly!
  1695. if ( this->regionSeg != NULL )
  1696. delete this->regionSeg;
  1697. if( this->s_rsMethode == "none" )
  1698. {
  1699. this->regionSeg = NULL;
  1700. }
  1701. else
  1702. {
  1703. //NOTE using an empty config file might not be save...
  1704. NICE::Config tmpConfEmpty;
  1705. RegionSegmentationMethod *tmpRegionSeg = GenericRegionSegmentationMethodSelection::selectRegionSegmentationMethod( &tmpConfEmpty, this->s_rsMethode );
  1706. if ( reuseSegmentation )
  1707. this->regionSeg = new RSCache ( &tmpConfEmpty, tmpRegionSeg );
  1708. else
  1709. this->regionSeg = tmpRegionSeg;
  1710. }
  1711. // done restoration
  1712. }
  1713. }
  1714. else
  1715. {
  1716. std::cerr << "SemSegNovelty::restore -- InStream not initialized - restoring not possible!" << std::endl;
  1717. throw;
  1718. }
  1719. }
  1720. void SemSegNovelty::store ( std::ostream & os, int format ) const
  1721. {
  1722. if (os.good())
  1723. {
  1724. // show starting point
  1725. os << this->createStartTag( "SemSegNovelty" ) << std::endl;
  1726. ///////////////////////////////
  1727. // FEATURE EXTRACTION //
  1728. ///////////////////////////////
  1729. os << this->createStartTag( "featExtract" ) << std::endl;
  1730. featExtract->store ( os );
  1731. os << this->createStartTag( "featExtract" ) << std::endl;
  1732. os << this->createStartTag( "trainWsize" ) << std::endl;
  1733. os << this->trainWSize << std::endl;
  1734. os << this->createStartTag( "trainWsize" ) << std::endl;
  1735. os << this->createStartTag( "whs" ) << std::endl;
  1736. os << this->whs << std::endl;
  1737. os << this->createStartTag( "whs" ) << std::endl;
  1738. os << this->createStartTag( "testWSize" ) << std::endl;
  1739. os << this->testWSize << std::endl;
  1740. os << this->createStartTag( "testWSize" ) << std::endl;
  1741. ///////////////////////////////
  1742. // NOVELTY COMPUTATION //
  1743. ///////////////////////////////
  1744. os << this->createStartTag( "noveltyMethod" ) << std::endl;
  1745. os << this->noveltyMethod << std::endl;
  1746. os << this->createStartTag( "noveltyMethod" ) << std::endl;
  1747. os << this->createStartTag( "noveltyMethodString" ) << std::endl;
  1748. os << this->noveltyMethodString << std::endl;
  1749. os << this->createStartTag( "noveltyMethodString" ) << std::endl;
  1750. os << this->createStartTag( "globalMaxUncert" ) << std::endl;
  1751. os << this->globalMaxUncert << std::endl;
  1752. os << this->createStartTag( "globalMaxUncert" ) << std::endl;
  1753. os << this->createStartTag( "mostNoveltyWithMaxScores" ) << std::endl;
  1754. os << this->mostNoveltyWithMaxScores << std::endl;
  1755. os << this->createStartTag( "mostNoveltyWithMaxScores" ) << std::endl;
  1756. os << this->createStartTag( "findMaximumUncert" ) << std::endl;
  1757. os << this->findMaximumUncert << std::endl;
  1758. os << this->createStartTag( "findMaximumUncert" ) << std::endl;
  1759. //TODO maskedImg
  1760. os << this->createStartTag( "b_visualizeALimages" ) << std::endl;
  1761. os << this->b_visualizeALimages << std::endl;
  1762. os << this->createStartTag( "b_visualizeALimages" ) << std::endl;
  1763. ///////////////////////////////
  1764. // CLASSIFICATION STUFF //
  1765. ///////////////////////////////
  1766. os << this->createStartTag( "classifierString" ) << std::endl;
  1767. os << this->classifierString << std::endl;
  1768. os << this->createStartTag( "classifierString" ) << std::endl;
  1769. os << this->createStartTag( "classifier" ) << std::endl;
  1770. if ( this->classifier != NULL )
  1771. {
  1772. os << "NOTNULL" << std::endl;
  1773. classifier->store ( os, format );
  1774. }
  1775. else
  1776. {
  1777. os << "NULL" << std::endl;
  1778. }
  1779. os << this->createEndTag( "classifier" ) << std::endl;
  1780. //
  1781. os << this->createStartTag( "vclassifier" ) << std::endl;
  1782. if ( this->classifier != NULL )
  1783. {
  1784. os << "NOTNULL" << std::endl;
  1785. vclassifier->store ( os, format );
  1786. }
  1787. else
  1788. {
  1789. os << "NULL" << std::endl;
  1790. }
  1791. os << this->createEndTag( "vclassifier" ) << std::endl;
  1792. os << this->createStartTag( "forbidden_classesTrain" ) << std::endl;
  1793. os << "size: " << forbidden_classesTrain.size() << std::endl;
  1794. for ( std::set< int >::const_iterator itForbClassTrain = forbidden_classesTrain.begin();
  1795. itForbClassTrain != forbidden_classesTrain.end();
  1796. itForbClassTrain++
  1797. )
  1798. {
  1799. os << *itForbClassTrain << " " << std::endl;
  1800. }
  1801. os << this->createEndTag( "forbidden_classesTrain" ) << std::endl;
  1802. //
  1803. os << this->createStartTag( "forbidden_classesActiveLearning" ) << std::endl;
  1804. os << "size: " << forbidden_classesActiveLearning.size() << std::endl;
  1805. for ( std::set< int >::const_iterator itForbClassAL = forbidden_classesActiveLearning.begin();
  1806. itForbClassAL != forbidden_classesActiveLearning.end();
  1807. itForbClassAL++
  1808. )
  1809. {
  1810. os << *itForbClassAL << " " << std::endl;
  1811. }
  1812. os << this->createEndTag( "forbidden_classesActiveLearning" ) << std::endl;
  1813. //
  1814. os << this->createStartTag( "classesInUse" ) << std::endl;
  1815. os << "size: " << classesInUse.size() << std::endl;
  1816. for ( std::set< int >::const_iterator itClassesInUse = classesInUse.begin();
  1817. itClassesInUse != classesInUse.end();
  1818. itClassesInUse++
  1819. )
  1820. {
  1821. os << *itClassesInUse << " " << std::endl;
  1822. }
  1823. os << this->createEndTag( "classesInUse" ) << std::endl;
  1824. os << this->createStartTag( "numberOfClasses" ) << std::endl;
  1825. os << this->numberOfClasses << std::endl;
  1826. os << this->createStartTag( "numberOfClasses" ) << std::endl;
  1827. os << this->createStartTag( "read_classifier" ) << std::endl;
  1828. os << this->read_classifier << std::endl;
  1829. os << this->createStartTag( "read_classifier" ) << std::endl;
  1830. os << this->createStartTag( "save_classifier" ) << std::endl;
  1831. os << this->save_classifier << std::endl;
  1832. os << this->createStartTag( "save_classifier" ) << std::endl;
  1833. os << this->createStartTag( "cache" ) << std::endl;
  1834. os << this->cache << std::endl;
  1835. os << this->createStartTag( "cache" ) << std::endl;
  1836. os << this->createStartTag( "resultdir" ) << std::endl;
  1837. os << this->resultdir << std::endl;
  1838. os << this->createStartTag( "resultdir" ) << std::endl;
  1839. //TODO newTrainExamples
  1840. ///////////////////////////////
  1841. // SEGMENTATION STUFF //
  1842. ///////////////////////////////
  1843. // theoretically, we should properly store and restore the regionSeg object. However, its parent class does not provide
  1844. // a Persistent interface yet. Hence, we perform this tiny workaround which works, since regionSeg is not changed over time...
  1845. // only be aware of parameters originally set via config...
  1846. os << this->createStartTag( "s_rsMethode" ) << std::endl;
  1847. os << this->s_rsMethode << std::endl;
  1848. os << this->createStartTag( "s_rsMethode" ) << std::endl;
  1849. os << this->createStartTag( "reuseSegmentation" ) << std::endl;
  1850. os << this->reuseSegmentation << std::endl;
  1851. os << this->createStartTag( "reuseSegmentation" ) << std::endl;
  1852. os << this->createStartTag( "queriedRegions" ) << std::endl;
  1853. os << "size: " << queriedRegions.size() << std::endl;
  1854. std::map< std::string, std::set< int > >::const_iterator itQueriedRegions = queriedRegions.begin();
  1855. for ( uint i = 0; i < queriedRegions.size(); i++ )
  1856. {
  1857. // store key
  1858. os << itQueriedRegions->first << std::endl;
  1859. // store values -- inner loop over sets
  1860. os << "size: " << ( itQueriedRegions->second ).size() << std::endl;
  1861. for ( std::set< int >::const_iterator itRegionsOfImg = ( itQueriedRegions->second ).begin();
  1862. itRegionsOfImg != ( itQueriedRegions->second ).end();
  1863. itRegionsOfImg++
  1864. )
  1865. {
  1866. os << *itRegionsOfImg << " " << std::endl;
  1867. }
  1868. itQueriedRegions++;
  1869. }
  1870. os << this->createStartTag( "queriedRegions" ) << std::endl;
  1871. //
  1872. //TODO currentRegionToQuery
  1873. ///////////////////////////////
  1874. // PARENT OBJECT //
  1875. ///////////////////////////////
  1876. os << this->createStartTag( "SemSegNovelty--Parent" ) << std::endl;
  1877. SemanticSegmentation::store(os);
  1878. os << this->createStartTag( "SemSegNovelty--Parent" ) << std::endl;
  1879. // done
  1880. os << this->createEndTag( "SemSegNovelty" ) << std::endl;
  1881. }
  1882. else
  1883. {
  1884. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  1885. }
  1886. }
  1887. void SemSegNovelty::clear ()
  1888. {
  1889. //TODO
  1890. }