SemSegNovelty.cpp 70 KB

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