SemSegNovelty.cpp 71 KB

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