SemSegNovelty.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. #include <sstream>
  2. #include <iostream>
  3. #include "SemSegNovelty.h"
  4. #include "core/image/FilterT.h"
  5. #include "gp-hik-exp/GPHIKClassifierNICE.h"
  6. #include "vislearning/baselib/ICETools.h"
  7. #include "vislearning/baselib/Globals.h"
  8. #include "vislearning/features/fpfeatures/SparseVectorFeature.h"
  9. #include "core/basics/StringTools.h"
  10. #include "core/basics/Timer.h"
  11. #include "segmentation/GenericRegionSegmentationMethodSelection.h"
  12. using namespace std;
  13. using namespace NICE;
  14. using namespace OBJREC;
  15. SemSegNovelty::SemSegNovelty ( const Config *conf,
  16. const MultiDataset *md )
  17. : SemanticSegmentation ( conf, & ( md->getClassNames ( "train" ) ) )
  18. {
  19. this->conf = conf;
  20. globalMaxUncert = -numeric_limits<double>::max();
  21. string section = "SemSegNovelty";
  22. featExtract = new LFColorWeijer ( conf );
  23. save_cache = conf->gB ( "FPCPixel", "save_cache", true );
  24. read_cache = conf->gB ( "FPCPixel", "read_cache", false );
  25. // uncertdir = conf->gS("debug", "uncertainty", "uncertainty");
  26. //write uncertainty results in the same folder as done for the segmentation results
  27. uncertdir = conf->gS("debug", "resultdir", "result");
  28. cache = conf->gS ( "cache", "root", "" );
  29. classifier = new GPHIKClassifierNICE ( conf, "ClassiferGPHIK" );;
  30. findMaximumUncert = conf->gB(section, "findMaximumUncert", true);
  31. whs = conf->gI ( section, "window_size", 10 );
  32. //distance to next descriptor during training
  33. trainWsize = conf->gI ( section, "train_window_size", 10 );
  34. //distance to next descriptor during testing
  35. testWSize = conf->gI (section, "test_window_size", 10);
  36. // select your segmentation method here
  37. string rsMethode = conf->gS ( section, "segmentation", "none" );
  38. if(rsMethode == "none")
  39. {
  40. regionSeg = NULL;
  41. }
  42. else
  43. {
  44. RegionSegmentationMethod *tmpRegionSeg = GenericRegionSegmentationMethodSelection::selectRegionSegmentationMethod(conf, rsMethode);
  45. if ( save_cache )
  46. regionSeg = new RSCache ( conf, tmpRegionSeg );
  47. else
  48. regionSeg = tmpRegionSeg;
  49. }
  50. cn = md->getClassNames ( "train" );
  51. if ( read_cache )
  52. {
  53. string classifierdst = "/classifier.data";
  54. fprintf ( stderr, "SemSegNovelty:: Reading classifier data from %s\n", ( cache + classifierdst ).c_str() );
  55. try
  56. {
  57. if ( classifier != NULL )
  58. {
  59. classifier->read ( cache + classifierdst );
  60. }
  61. fprintf ( stderr, "SemSegNovelty:: successfully read\n" );
  62. }
  63. catch ( char *str )
  64. {
  65. cerr << "error reading data: " << str << endl;
  66. }
  67. }
  68. else
  69. {
  70. train ( md );
  71. }
  72. //define which measure for "novelty" we want to use
  73. string noveltyMethodString = conf->gS( section, "noveltyMethod", "gp-variance");
  74. if (noveltyMethodString.compare("gp-variance") == 0) // novel = large variance
  75. {
  76. this->noveltyMethod = GPVARIANCE;
  77. }
  78. else if (noveltyMethodString.compare("gp-uncertainty") == 0) //novel = large uncertainty (mean / var)
  79. {
  80. this->noveltyMethod = GPUNCERTAINTY;
  81. }
  82. else if (noveltyMethodString.compare("gp-mean") == 0) //novel = small mean
  83. {
  84. this->noveltyMethod = GPMINMEAN;
  85. }
  86. else if (noveltyMethodString.compare("gp-meanRatio") == 0) //novel = small difference between mean of most plausible class and mean of snd
  87. // most plausible class (not useful in binary settings)
  88. {
  89. this->noveltyMethod = GPMEANRATIO;
  90. }
  91. else if (noveltyMethodString.compare("gp-weightAll") == 0) // novel = large weight in alpha vector after updating the model (can be predicted exactly)
  92. {
  93. this->noveltyMethod = GPWEIGHTALL;
  94. }
  95. else if (noveltyMethodString.compare("gp-weightRatio") == 0) // novel = small difference between weights for alpha vectors
  96. // with assumptions of GT label to be the most
  97. // plausible against the second most plausible class
  98. {
  99. this->noveltyMethod = GPWEIGHTRATIO;
  100. }
  101. else
  102. {
  103. this->noveltyMethod = GPVARIANCE;
  104. }
  105. }
  106. SemSegNovelty::~SemSegNovelty()
  107. {
  108. if(newTrainExamples.size() > 0)
  109. {
  110. // most uncertain region
  111. showImage(maskedImg);
  112. //classifier->add(newTrainExamples)
  113. classifier->save ( cache + "/classifier.data" );
  114. }
  115. // clean-up
  116. if ( classifier != NULL )
  117. delete classifier;
  118. if ( featExtract != NULL )
  119. delete featExtract;
  120. }
  121. void SemSegNovelty::visualizeRegion(const NICE::ColorImage &img, const NICE::Matrix &regions, int region, NICE::ColorImage &outimage)
  122. {
  123. vector<uchar> color;
  124. color.push_back(255);
  125. color.push_back(0);
  126. color.push_back(0);
  127. int width = img.width();
  128. int height = img.height();
  129. outimage.resize(width,height);
  130. for(int y = 0; y < height; y++)
  131. {
  132. for(int x = 0; x < width; x++)
  133. {
  134. if(regions(x,y) == region)
  135. {
  136. for(int c = 0; c < 3; c++)
  137. {
  138. outimage(x,y,c) = color[c];
  139. }
  140. }
  141. else
  142. {
  143. for(int c = 0; c < 3; c++)
  144. {
  145. outimage(x,y,c) = img(x,y,c);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. void SemSegNovelty::train ( const MultiDataset *md )
  152. {
  153. const LabeledSet train = * ( *md ) ["train"];
  154. const LabeledSet *trainp = &train;
  155. ////////////////////////
  156. // feature extraction //
  157. ////////////////////////
  158. std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
  159. if ( forbidden_classes_s == "" )
  160. {
  161. forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  162. }
  163. cn.getSelection ( forbidden_classes_s, forbidden_classes );
  164. //check the same thing for the training classes - this is very specific to our setup
  165. std::string forbidden_classesTrain_s = conf->gS ( "analysis", "donttrainTrain", "" );
  166. if ( forbidden_classesTrain_s == "" )
  167. {
  168. forbidden_classesTrain_s = conf->gS ( "analysis", "forbidden_classesTrain", "" );
  169. }
  170. cn.getSelection ( forbidden_classesTrain_s, forbidden_classesTrain );
  171. ProgressBar pb ( "Local Feature Extraction" );
  172. pb.show();
  173. int imgnb = 0;
  174. Examples examples;
  175. examples.filename = "training";
  176. int featdim = -1;
  177. classesInUse.clear();
  178. LOOP_ALL_S ( *trainp )
  179. {
  180. //EACH_S(classno, currentFile);
  181. EACH_INFO ( classno, info );
  182. std::string currentFile = info.img();
  183. CachedExample *ce = new CachedExample ( currentFile );
  184. const LocalizationResult *locResult = info.localization();
  185. if ( locResult->size() <= 0 )
  186. {
  187. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  188. currentFile.c_str() );
  189. continue;
  190. }
  191. int xsize, ysize;
  192. ce->getImageSize ( xsize, ysize );
  193. Image labels ( xsize, ysize );
  194. labels.set ( 0 );
  195. locResult->calcLabeledImage ( labels, ( *classNames ).getBackgroundClass() );
  196. NICE::ColorImage img;
  197. try {
  198. img = ColorImage ( currentFile );
  199. } catch ( Exception ) {
  200. cerr << "SemSegNovelty: error opening image file <" << currentFile << ">" << endl;
  201. continue;
  202. }
  203. Globals::setCurrentImgFN ( currentFile );
  204. MultiChannelImageT<double> feats;
  205. // extract features
  206. featExtract->getFeats ( img, feats );
  207. featdim = feats.channels();
  208. feats.addChannel(featdim);
  209. for (int c = 0; c < featdim; c++)
  210. {
  211. ImageT<double> tmp = feats[c];
  212. ImageT<double> tmp2 = feats[c+featdim];
  213. NICE::FilterT<double, double, double>::gradientStrength (tmp, tmp2);
  214. }
  215. featdim += featdim;
  216. // compute integral images
  217. for ( int c = 0; c < featdim; c++ )
  218. {
  219. feats.calcIntegral ( c );
  220. }
  221. for ( int y = 0; y < ysize; y += trainWsize )
  222. {
  223. for ( int x = 0; x < xsize; x += trainWsize )
  224. {
  225. int classnoTmp = labels.getPixel ( x, y );
  226. if ( forbidden_classesTrain.find ( classnoTmp ) != forbidden_classesTrain.end() )
  227. {
  228. continue;
  229. }
  230. if (classesInUse.find(classnoTmp) == classesInUse.end())
  231. {
  232. classesInUse.insert(classnoTmp);
  233. }
  234. Example example;
  235. example.vec = NULL;
  236. example.svec = new SparseVector ( featdim );
  237. for ( int f = 0; f < featdim; f++ )
  238. {
  239. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  240. if ( val > 1e-10 )
  241. ( *example.svec ) [f] = val;
  242. }
  243. example.svec->normalize();
  244. example.position = imgnb;
  245. examples.push_back ( pair<int, Example> ( classnoTmp, example ) );
  246. }
  247. }
  248. delete ce;
  249. imgnb++;
  250. pb.update ( trainp->count() );
  251. }
  252. numberOfClasses = classesInUse.size();
  253. std::cerr << "numberOfClasses: " << numberOfClasses << std::endl;
  254. std::cerr << "classes in use: " << std::endl;
  255. for (std::set<int>::const_iterator it = classesInUse.begin(); it != classesInUse.end(); it++)
  256. {
  257. std::cerr << *it << " ";
  258. }
  259. std::cerr << std::endl;
  260. pb.hide();
  261. //////////////////////
  262. // train classifier //
  263. //////////////////////
  264. FeaturePool fp;
  265. Feature *f = new SparseVectorFeature ( featdim );
  266. f->explode ( fp );
  267. delete f;
  268. if ( classifier != NULL )
  269. classifier->train ( fp, examples );
  270. else
  271. {
  272. cerr << "no classifier selected?!" << endl;
  273. exit ( -1 );
  274. }
  275. fp.destroy();
  276. if ( save_cache )
  277. {
  278. if ( classifier != NULL )
  279. classifier->save ( cache + "/classifier.data" );
  280. }
  281. ////////////
  282. //clean up//
  283. ////////////
  284. for ( int i = 0; i < ( int ) examples.size(); i++ )
  285. {
  286. examples[i].second.clean();
  287. }
  288. examples.clear();
  289. cerr << "SemSeg training finished" << endl;
  290. }
  291. void SemSegNovelty::semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities )
  292. {
  293. Timer timer;
  294. timer.start();
  295. Image labels = segresult;
  296. segresult.set(0);
  297. int featdim = -1;
  298. std::string currentFile = Globals::getCurrentImgFN();
  299. int xsize, ysize;
  300. ce->getImageSize ( xsize, ysize );
  301. probabilities.reInit( xsize, ysize, cn.getMaxClassno() + 1);
  302. probabilities.setAll ( 0.0 );
  303. NICE::ColorImage img;
  304. try {
  305. img = ColorImage ( currentFile );
  306. } catch ( Exception ) {
  307. cerr << "SemSegNovelty: error opening image file <" << currentFile << ">" << endl;
  308. return;
  309. }
  310. MultiChannelImageT<double> feats;
  311. // extract features
  312. featExtract->getFeats ( img, feats );
  313. featdim = feats.channels();
  314. feats.addChannel(featdim);
  315. for (int c = 0; c < featdim; c++)
  316. {
  317. ImageT<double> tmp = feats[c];
  318. ImageT<double> tmp2 = feats[c+featdim];
  319. NICE::FilterT<double, double, double>::gradientStrength (tmp, tmp2);
  320. }
  321. featdim += featdim;
  322. // compute integral images
  323. for ( int c = 0; c < featdim; c++ )
  324. {
  325. feats.calcIntegral ( c );
  326. }
  327. FloatImage noveltyImage ( xsize, ysize );
  328. noveltyImage.set ( 0.0 );
  329. FloatImage uncert ( xsize, ysize );
  330. uncert.set ( 0.0 );
  331. FloatImage gpUncertainty ( xsize, ysize );
  332. FloatImage gpMean ( xsize, ysize );
  333. FloatImage gpMeanRatio ( xsize, ysize );
  334. FloatImage gpWeightAll ( xsize, ysize );
  335. FloatImage gpWeightRatio ( xsize, ysize );
  336. gpUncertainty.set ( 0.0 );
  337. gpMean.set ( 0.0 );
  338. gpMeanRatio.set ( 0.0 );
  339. gpWeightAll.set ( 0.0 );
  340. gpWeightRatio.set ( 0.0 );
  341. double maxNovelty = -numeric_limits<double>::max();
  342. double maxunc = -numeric_limits<double>::max();
  343. double maxGPUncertainty = -numeric_limits<double>::max();
  344. double maxGPMean = -numeric_limits<double>::max();
  345. double maxGPMeanRatio = -numeric_limits<double>::max();
  346. double maxGPWeightAll = -numeric_limits<double>::max();
  347. double maxGPWeightRatio = -numeric_limits<double>::max();
  348. timer.stop();
  349. cout << "first: " << timer.getLastAbsolute() << endl;
  350. //we need this lateron for active learning stuff
  351. double gpNoise = conf->gD("GPHIK", "noise", 0.01);
  352. timer.start();
  353. this->computeClassificationResults( feats, segresult, probabilities, xsize, ysize, featdim);
  354. timer.stop();
  355. switch (noveltyMethod)
  356. {
  357. case GPVARIANCE:
  358. {
  359. this->computeNoveltyByVariance( noveltyImage, feats, segresult, probabilities, xsize, ysize, featdim );
  360. break;
  361. }
  362. case GPUNCERTAINTY:
  363. {
  364. this->computeNoveltyByGPUncertainty( noveltyImage, feats, segresult, probabilities, xsize, ysize, featdim );
  365. break;
  366. }
  367. default:
  368. {
  369. //do nothing, keep the image constant to 0.0
  370. break;
  371. }
  372. }
  373. #pragma omp parallel for
  374. for ( int y = 0; y < ysize; y += testWSize )
  375. {
  376. Example example;
  377. example.vec = NULL;
  378. example.svec = new SparseVector ( featdim );
  379. for ( int x = 0; x < xsize; x += testWSize)
  380. {
  381. for ( int f = 0; f < featdim; f++ )
  382. {
  383. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  384. if ( val > 1e-10 )
  385. ( *example.svec ) [f] = val;
  386. }
  387. example.svec->normalize();
  388. ClassificationResult cr = classifier->classify ( example );
  389. //we need this if we want to compute GP-AL-measure lateron
  390. double minMeanAbs ( numeric_limits<double>::max() );
  391. double maxMeanAbs ( 0.0 );
  392. double sndMaxMeanAbs ( 0.0 );
  393. double maxMean ( -numeric_limits<double>::max() );
  394. double sndMaxMean ( -numeric_limits<double>::max() );
  395. for ( int j = 0 ; j < cr.scores.size(); j++ )
  396. {
  397. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  398. {
  399. continue;
  400. }
  401. //check whether we found a class with higher smaller abs mean than the current minimum
  402. if (abs(cr.scores[j]) < minMeanAbs)
  403. minMeanAbs = abs(cr.scores[j]);
  404. //check for larger abs mean as well
  405. if (abs(cr.scores[j]) > maxMeanAbs)
  406. {
  407. sndMaxMeanAbs = maxMeanAbs;
  408. maxMeanAbs = abs(cr.scores[j]);
  409. }
  410. // and also for the second highest mean of all classes
  411. else if (abs(cr.scores[j]) > sndMaxMeanAbs)
  412. {
  413. sndMaxMeanAbs = abs(cr.scores[j]);
  414. }
  415. //check for larger mean without abs as well
  416. if (cr.scores[j] > maxMean)
  417. {
  418. sndMaxMean = maxMean;
  419. maxMean = cr.scores[j];
  420. }
  421. // and also for the second highest mean of all classes
  422. else if (cr.scores[j] > sndMaxMean)
  423. {
  424. sndMaxMean = cr.scores[j];
  425. }
  426. }
  427. double firstTerm (1.0 / sqrt(cr.uncertainty+gpNoise));
  428. //compute the heuristic GP-UNCERTAINTY, as proposed by Kapoor et al. in IJCV 2010
  429. // GP-UNCERTAINTY : |mean| / sqrt(var^2 + gpnoise^2)
  430. double gpUncertaintyVal = maxMeanAbs*firstTerm; //firstTerm = 1.0 / sqrt(r.uncertainty+gpNoise))
  431. // compute results when we take the lowest mean value of all classes
  432. double gpMeanVal = minMeanAbs;
  433. //look at the difference in the absolut mean values for the most plausible class
  434. // and the second most plausible class
  435. double gpMeanRatioVal= maxMean - sndMaxMean;
  436. double gpWeightAllVal ( 0.0 );
  437. double gpWeightRatioVal ( 0.0 );
  438. if ( numberOfClasses > 2)
  439. {
  440. //compute the weight in the alpha-vector for every sample after assuming it to be
  441. // added to the training set.
  442. // Thereby, we measure its "importance" for the current model
  443. //
  444. //double firstTerm is already computed
  445. //
  446. //the second term is only needed when computing impacts
  447. //double secondTerm; //this is the nasty guy :/
  448. //--- compute the third term
  449. // this is the difference between predicted label and GT label
  450. std::vector<double> diffToPositive; diffToPositive.clear();
  451. std::vector<double> diffToNegative; diffToNegative.clear();
  452. double diffToNegativeSum(0.0);
  453. for ( int j = 0 ; j < cr.scores.size(); j++ )
  454. {
  455. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  456. {
  457. continue;
  458. }
  459. // look at the difference to plus 1
  460. diffToPositive.push_back(abs(cr.scores[j] - 1));
  461. // look at the difference to -1
  462. diffToNegative.push_back(abs(cr.scores[j] + 1));
  463. //sum up the difference to -1
  464. diffToNegativeSum += abs(cr.scores[j] - 1);
  465. }
  466. //let's subtract for every class its diffToNegative from the sum, add its diffToPositive,
  467. //and use this as the third term for this specific class.
  468. //the final value is obtained by minimizing over all classes
  469. //
  470. // originally, we minimize over all classes after building the final score
  471. // however, the first and the second term do not depend on the choice of
  472. // y*, therefore we minimize here already
  473. double thirdTerm (numeric_limits<double>::max()) ;
  474. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  475. {
  476. double tmpVal ( diffToPositive[tmpCnt] + (diffToNegativeSum-diffToNegative[tmpCnt]) );
  477. if (tmpVal < thirdTerm)
  478. thirdTerm = tmpVal;
  479. }
  480. gpWeightAllVal = thirdTerm*firstTerm;
  481. //now look on the ratio of the resulting weights for the most plausible
  482. // against the second most plausible class
  483. double thirdTermMostPlausible ( 0.0 ) ;
  484. double thirdTermSecondMostPlausible ( 0.0 ) ;
  485. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  486. {
  487. if (diffToPositive[tmpCnt] > thirdTermMostPlausible)
  488. {
  489. thirdTermSecondMostPlausible = thirdTermMostPlausible;
  490. thirdTermMostPlausible = diffToPositive[tmpCnt];
  491. }
  492. else if (diffToPositive[tmpCnt] > thirdTermSecondMostPlausible)
  493. {
  494. thirdTermSecondMostPlausible = diffToPositive[tmpCnt];
  495. }
  496. }
  497. //compute the resulting score
  498. gpWeightRatioVal = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm;
  499. //finally, look for this feature how it would affect to whole model (summarized by weight-vector alpha), if we would
  500. //use it as an additional training example
  501. //TODO this would be REALLY computational demanding. Do we really want to do this?
  502. // gpImpactAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm*secondTerm;
  503. // gpImpactRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm*secondTerm;
  504. }
  505. else //binary scenario
  506. {
  507. gpWeightAllVal = std::min( abs(cr.scores[*classesInUse.begin()]+1), abs(cr.scores[*classesInUse.begin()]-1) );
  508. gpWeightAllVal *= firstTerm;
  509. gpWeightRatioVal = gpWeightAllVal;
  510. }
  511. int xs = std::max(0, x - testWSize/2);
  512. int xe = std::min(xsize - 1, x + testWSize/2);
  513. int ys = std::max(0, y - testWSize/2);
  514. int ye = std::min(ysize - 1, y + testWSize/2);
  515. for (int yl = ys; yl <= ye; yl++)
  516. {
  517. for (int xl = xs; xl <= xe; xl++)
  518. {
  519. for ( int j = 0 ; j < cr.scores.size(); j++ )
  520. {
  521. probabilities ( xl, yl, j ) = cr.scores[j];
  522. }
  523. segresult ( xl, yl ) = cr.classno;
  524. uncert ( xl, yl ) = cr.uncertainty;
  525. gpUncertainty ( xl, yl ) = gpUncertaintyVal;
  526. gpMean ( xl, yl ) = gpMeanVal;
  527. gpMeanRatio ( xl, yl ) = gpMeanRatioVal;
  528. gpWeightAll ( xl, yl ) = gpWeightAllVal;
  529. gpWeightRatio ( xl, yl ) = gpWeightRatioVal;
  530. }
  531. }
  532. if (maxunc < cr.uncertainty)
  533. maxunc = cr.uncertainty;
  534. if (maxGPUncertainty < gpUncertaintyVal)
  535. maxGPUncertainty = gpUncertaintyVal;
  536. if (maxGPMean < gpMeanVal)
  537. maxGPMean = gpMeanVal;
  538. if (maxGPMeanRatio < gpMeanRatioVal)
  539. maxGPMeanRatio = gpMeanRatioVal;
  540. if (maxGPWeightAll < gpMeanRatioVal)
  541. maxGPWeightAll = gpWeightAllVal;
  542. if (maxGPWeightRatio < gpWeightRatioVal)
  543. maxGPWeightRatio = gpWeightRatioVal;
  544. example.svec->clear();
  545. }
  546. delete example.svec;
  547. example.svec = NULL;
  548. }
  549. // std::cerr << "uncertainty: " << gpUncertaintyVal << " minMean: " << gpMeanVal << " gpMeanRatio: " << gpMeanRatioVal << " weightAll: " << gpWeightAllVal << " weightRatio: "<< gpWeightRatioVal << std::endl;
  550. //Regionen ermitteln
  551. if(regionSeg != NULL)
  552. {
  553. NICE::Matrix mask;
  554. int amountRegions = regionSeg->segRegions ( img, mask );
  555. //compute probs per region
  556. vector<vector<double> > regionProb(amountRegions,vector<double>(probabilities.channels(),0.0));
  557. vector<double> regionNoveltyMeasure (amountRegions, 0.0);
  558. std::vector<double> regionGPUncertainty (amountRegions, 0.0);
  559. std::vector<double> regionGPMean (amountRegions, 0.0);
  560. std::vector<double> regionGPMeanRatio (amountRegions, 0.0);
  561. std::vector<double> regionGPWeightAll (amountRegions, 0.0);
  562. std::vector<double> regionGPWeightRatio (amountRegions, 0.0);
  563. vector<int> regionCounter(amountRegions, 0);
  564. for ( int y = 0; y < ysize; y++)
  565. {
  566. for (int x = 0; x < xsize; x++)
  567. {
  568. int r = mask(x,y);
  569. regionCounter[r]++;
  570. for(int j = 0; j < probabilities.channels(); j++)
  571. {
  572. regionProb[r][j] += probabilities ( x, y, j );
  573. }
  574. //count the amount of "novelty" for the corresponding region
  575. regionNoveltyMeasure[r] += uncert(x,y);
  576. //
  577. regionGPUncertainty[r] += gpUncertainty(x,y);
  578. regionGPMean[r] += gpMean(x,y);
  579. regionGPMeanRatio[r] += gpMeanRatio(x,y);
  580. regionGPWeightAll[r] += gpWeightAll(x,y);
  581. regionGPWeightRatio[r] += gpWeightRatio(x,y);
  582. }
  583. }
  584. //find best class per region
  585. vector<int> bestClassPerRegion(amountRegions,0);
  586. double maxuncert = -numeric_limits<double>::max();
  587. int maxUncertRegion = -1;
  588. for(int r = 0; r < amountRegions; r++)
  589. {
  590. double maxval = -numeric_limits<double>::max();
  591. for(int c = 0; c < probabilities.channels(); c++)
  592. {
  593. regionProb[r][c] /= regionCounter[r];
  594. if(maxval < regionProb[r][c] && regionProb[r][c] != 0.0)
  595. {
  596. maxval = regionProb[r][c];
  597. bestClassPerRegion[r] = c;
  598. }
  599. }
  600. //normalize summed novelty scores to region size
  601. regionNoveltyMeasure[r] /= regionCounter[r];
  602. //
  603. regionGPUncertainty[r] /= regionCounter[r];
  604. regionGPMean[r] /= regionCounter[r];
  605. regionGPMeanRatio[r] /= regionCounter[r];
  606. regionGPWeightAll[r] /= regionCounter[r];
  607. regionGPWeightRatio[r] /= regionCounter[r];
  608. if(maxuncert < regionNoveltyMeasure[r])
  609. {
  610. maxuncert = regionNoveltyMeasure[r];
  611. maxUncertRegion = r;
  612. }
  613. }
  614. if(findMaximumUncert)
  615. {
  616. if(maxuncert > globalMaxUncert)
  617. {
  618. //save new important features
  619. Examples examples;
  620. for ( int y = 0; y < ysize; y += testWSize )
  621. {
  622. for ( int x = 0; x < xsize; x += testWSize)
  623. {
  624. if(mask(x,y) == maxUncertRegion)
  625. {
  626. Example example;
  627. example.vec = NULL;
  628. example.svec = new SparseVector ( featdim );
  629. int classnoTmp = labels(x,y);
  630. for ( int f = 0; f < featdim; f++ )
  631. {
  632. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  633. if ( val > 1e-10 )
  634. ( *example.svec ) [f] = val;
  635. }
  636. example.svec->normalize();
  637. examples.push_back ( pair<int, Example> ( classnoTmp, example ) );
  638. }
  639. }
  640. }
  641. if(examples.size() > 0)
  642. {
  643. newTrainExamples.clear();
  644. newTrainExamples = examples;
  645. globalMaxUncert = maxuncert;
  646. visualizeRegion(img,mask,maxUncertRegion,maskedImg);
  647. }
  648. }
  649. }
  650. //write back best results per region
  651. for ( int y = 0; y < ysize; y++)
  652. {
  653. for (int x = 0; x < xsize; x++)
  654. {
  655. int r = mask(x,y);
  656. for(int j = 0; j < probabilities.channels(); j++)
  657. {
  658. probabilities ( x, y, j ) = regionProb[r][j];
  659. }
  660. segresult(x,y) = bestClassPerRegion[r];
  661. // write novelty scores for every segment into the "final" image
  662. uncert(x,y) = regionNoveltyMeasure[r];
  663. //
  664. gpUncertainty(x,y) = regionGPUncertainty[r];
  665. gpMean(x,y) = regionGPMean[r];
  666. gpMeanRatio(x,y) = regionGPMeanRatio[r];
  667. gpWeightAll(x,y) = regionGPWeightAll[r];
  668. gpWeightRatio(x,y) = regionGPWeightRatio[r];
  669. }
  670. }
  671. }
  672. timer.stop();
  673. cout << "second: " << timer.getLastAbsolute() << endl;
  674. timer.start();
  675. ColorImage imgrgb ( xsize, ysize );
  676. std::stringstream out;
  677. std::vector< std::string > list2;
  678. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  679. out << uncertdir << "/" << list2.back();
  680. uncert.writeRaw(out.str() + ".rawfloat");
  681. gpUncertainty.writeRaw(out.str() + "_gpUncertainty.rawfloat");
  682. gpMean.writeRaw(out.str() + "_gpMean.rawfloat");
  683. gpMeanRatio.writeRaw(out.str() + "_gpMeanRatio.rawfloat");
  684. gpWeightAll.writeRaw(out.str() + "_gpWeightAll.rawfloat");
  685. gpWeightRatio.writeRaw(out.str() + "_gpWeightRatio.rawfloat");
  686. timer.stop();
  687. cout << "last: " << timer.getLastAbsolute() << endl;
  688. }
  689. inline void SemSegNovelty::computeClassificationResults( const NICE::MultiChannelImageT<double> & feats,
  690. NICE::Image & segresult,
  691. NICE::MultiChannelImageT<double> & probabilities,
  692. const int & xsize,
  693. const int & ysize,
  694. const int & featdim
  695. )
  696. {
  697. #pragma omp parallel for
  698. for ( int y = 0; y < ysize; y += testWSize )
  699. {
  700. Example example;
  701. example.vec = NULL;
  702. example.svec = new SparseVector ( featdim );
  703. for ( int x = 0; x < xsize; x += testWSize)
  704. {
  705. for ( int f = 0; f < featdim; f++ )
  706. {
  707. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  708. if ( val > 1e-10 )
  709. ( *example.svec ) [f] = val;
  710. }
  711. example.svec->normalize();
  712. ClassificationResult cr = classifier->classify ( example );
  713. int xs = std::max(0, x - testWSize/2);
  714. int xe = std::min(xsize - 1, x + testWSize/2);
  715. int ys = std::max(0, y - testWSize/2);
  716. int ye = std::min(ysize - 1, y + testWSize/2);
  717. for (int yl = ys; yl <= ye; yl++)
  718. {
  719. for (int xl = xs; xl <= xe; xl++)
  720. {
  721. for ( int j = 0 ; j < cr.scores.size(); j++ )
  722. {
  723. probabilities ( xl, yl, j ) = cr.scores[j];
  724. }
  725. segresult ( xl, yl ) = cr.classno;
  726. }
  727. }
  728. example.svec->clear();
  729. }
  730. delete example.svec;
  731. example.svec = NULL;
  732. }
  733. }
  734. void SemSegNovelty::computeNoveltyByVariance( NICE::FloatImage & noveltyImage,
  735. const NICE::MultiChannelImageT<double> & feats,
  736. NICE::Image & segresult,
  737. NICE::MultiChannelImageT<double> & probabilities,
  738. const int & xsize, const int & ysize, const int & featdim )
  739. {
  740. #pragma omp parallel for
  741. for ( int y = 0; y < ysize; y += testWSize )
  742. {
  743. Example example;
  744. example.vec = NULL;
  745. example.svec = new SparseVector ( featdim );
  746. for ( int x = 0; x < xsize; x += testWSize)
  747. {
  748. for ( int f = 0; f < featdim; f++ )
  749. {
  750. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  751. if ( val > 1e-10 )
  752. ( *example.svec ) [f] = val;
  753. }
  754. example.svec->normalize();
  755. ClassificationResult cr = classifier->classify ( example );
  756. int xs = std::max(0, x - testWSize/2);
  757. int xe = std::min(xsize - 1, x + testWSize/2);
  758. int ys = std::max(0, y - testWSize/2);
  759. int ye = std::min(ysize - 1, y + testWSize/2);
  760. for (int yl = ys; yl <= ye; yl++)
  761. {
  762. for (int xl = xs; xl <= xe; xl++)
  763. {
  764. for ( int j = 0 ; j < cr.scores.size(); j++ )
  765. {
  766. probabilities ( xl, yl, j ) = cr.scores[j];
  767. }
  768. segresult ( xl, yl ) = cr.classno;
  769. noveltyImage ( xl, yl ) = cr.uncertainty;
  770. }
  771. }
  772. example.svec->clear();
  773. }
  774. delete example.svec;
  775. example.svec = NULL;
  776. }
  777. }
  778. void SemSegNovelty::computeNoveltyByGPUncertainty( NICE::FloatImage & noveltyImage,
  779. const NICE::MultiChannelImageT<double> & feats,
  780. NICE::Image & segresult,
  781. NICE::MultiChannelImageT<double> & probabilities,
  782. const int & xsize, const int & ysize, const int & featdim )
  783. {
  784. double gpNoise = conf->gD("GPHIK", "noise", 0.01);
  785. #pragma omp parallel for
  786. for ( int y = 0; y < ysize; y += testWSize )
  787. {
  788. Example example;
  789. example.vec = NULL;
  790. example.svec = new SparseVector ( featdim );
  791. for ( int x = 0; x < xsize; x += testWSize)
  792. {
  793. for ( int f = 0; f < featdim; f++ )
  794. {
  795. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  796. if ( val > 1e-10 )
  797. ( *example.svec ) [f] = val;
  798. }
  799. example.svec->normalize();
  800. ClassificationResult cr = classifier->classify ( example );
  801. double maxMeanAbs ( 0.0 );
  802. for ( int j = 0 ; j < cr.scores.size(); j++ )
  803. {
  804. if ( forbidden_classesTrain.find ( j ) != forbidden_classesTrain.end() )
  805. {
  806. continue;
  807. }
  808. //check for larger abs mean
  809. if (abs(cr.scores[j]) > maxMeanAbs)
  810. {
  811. maxMeanAbs = abs(cr.scores[j]);
  812. }
  813. }
  814. double firstTerm (1.0 / sqrt(cr.uncertainty+gpNoise));
  815. //compute the heuristic GP-UNCERTAINTY, as proposed by Kapoor et al. in IJCV 2010
  816. // GP-UNCERTAINTY : |mean| / sqrt(var^2 + gpnoise^2)
  817. double gpUncertaintyVal = maxMeanAbs*firstTerm; //firstTerm = 1.0 / sqrt(r.uncertainty+gpNoise))
  818. int xs = std::max(0, x - testWSize/2);
  819. int xe = std::min(xsize - 1, x + testWSize/2);
  820. int ys = std::max(0, y - testWSize/2);
  821. int ye = std::min(ysize - 1, y + testWSize/2);
  822. for (int yl = ys; yl <= ye; yl++)
  823. {
  824. for (int xl = xs; xl <= xe; xl++)
  825. {
  826. for ( int j = 0 ; j < cr.scores.size(); j++ )
  827. {
  828. probabilities ( xl, yl, j ) = cr.scores[j];
  829. }
  830. segresult ( xl, yl ) = cr.classno;
  831. noveltyImage ( xl, yl ) = gpUncertaintyVal;
  832. }
  833. }
  834. example.svec->clear();
  835. }
  836. delete example.svec;
  837. example.svec = NULL;
  838. }
  839. }