SemSegContextTree.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. #include "SemSegContextTree.h"
  2. #include "vislearning/baselib/Globals.h"
  3. #include "vislearning/baselib/ProgressBar.h"
  4. #include "core/basics/StringTools.h"
  5. #include "vislearning/cbaselib/CachedExample.h"
  6. #include "vislearning/cbaselib/PascalResults.h"
  7. #include "vislearning/baselib/ColorSpace.h"
  8. #include "objrec/segmentation/RSMeanShift.h"
  9. #include "objrec/segmentation/RSGraphBased.h"
  10. #include "core/basics/numerictools.h"
  11. #include "core/basics/StringTools.h"
  12. #include "core/basics/FileName.h"
  13. #include "vislearning/baselib/ICETools.h"
  14. #include "core/basics/Timer.h"
  15. #include "core/basics/vectorio.h"
  16. #include "core/image/FilterT.h"
  17. #include <omp.h>
  18. #include <iostream>
  19. #undef LOCALFEATS
  20. #undef WRITEGLOB
  21. #undef TEXTONMAP
  22. #undef DEBUG
  23. //#define LOCALFEATS
  24. using namespace OBJREC;
  25. using namespace std;
  26. using namespace NICE;
  27. SemSegContextTree::SemSegContextTree ( const Config *conf, const MultiDataset *md )
  28. : SemanticSegmentation ( conf, & ( md->getClassNames ( "train" ) ) )
  29. {
  30. this->conf = conf;
  31. string section = "SSContextTree";
  32. lfcw = new LFColorWeijer ( conf );
  33. firstiteration = true;
  34. grid = conf->gI ( section, "grid", 10 );
  35. maxSamples = conf->gI ( section, "max_samples", 2000 );
  36. minFeats = conf->gI ( section, "min_feats", 50 );
  37. maxDepth = conf->gI ( section, "max_depth", 10 );
  38. windowSize = conf->gI ( section, "window_size", 16 );
  39. featsPerSplit = conf->gI ( section, "feats_per_split", 200 );
  40. useShannonEntropy = conf->gB ( section, "use_shannon_entropy", true );
  41. nbTrees = conf->gI ( section, "amount_trees", 1 );
  42. string segmentationtype = conf->gS ( section, "segmentation_type", "meanshift" );
  43. randomTests = conf->gI ( section, "random_tests", 10 );
  44. bool saveLoadData = conf->gB ( "debug", "save_load_data", false );
  45. string fileLocation = conf->gS ( "debug", "datafile", "tmp.txt" );
  46. pixelWiseLabeling = false;
  47. useRegionFeature = true;
  48. if ( segmentationtype == "meanshift" )
  49. segmentation = new RSMeanShift ( conf );
  50. else if ( segmentationtype == "none" )
  51. {
  52. segmentation = NULL;
  53. pixelWiseLabeling = true;
  54. useRegionFeature = false;
  55. }
  56. else if ( segmentationtype == "felzenszwalb" )
  57. segmentation = new RSGraphBased ( conf );
  58. else
  59. throw ( "no valid segmenation_type\n please choose between none, meanshift and felzenszwalb\n" );
  60. ftypes = conf->gI ( section, "features", 100 );;
  61. string featsec = "Features";
  62. vector<Operation*> tops;
  63. if ( conf->gB ( featsec, "minus", true ) )
  64. tops.push_back ( new Minus() );
  65. if ( conf->gB ( featsec, "minus_abs", true ) )
  66. tops.push_back ( new MinusAbs() );
  67. if ( conf->gB ( featsec, "addition", true ) )
  68. tops.push_back ( new Addition() );
  69. if ( conf->gB ( featsec, "only1", true ) )
  70. tops.push_back ( new Only1() );
  71. if ( conf->gB ( featsec, "rel_x", true ) )
  72. tops.push_back ( new RelativeXPosition() );
  73. if ( conf->gB ( featsec, "rel_y", true ) )
  74. tops.push_back ( new RelativeYPosition() );
  75. ops.push_back ( tops );
  76. tops.clear();
  77. tops.push_back ( new Equality() );
  78. ops.push_back ( tops );
  79. tops.clear();
  80. if ( conf->gB ( featsec, "bi_int_cent", true ) )
  81. tops.push_back ( new BiIntegralCenteredOps() );
  82. if ( conf->gB ( featsec, "int_cent", true ) )
  83. tops.push_back ( new IntegralCenteredOps() );
  84. if ( conf->gB ( featsec, "int", true ) )
  85. tops.push_back ( new IntegralOps() );
  86. if ( conf->gB ( featsec, "haar_horz", true ) )
  87. tops.push_back ( new HaarHorizontal() );
  88. if ( conf->gB ( featsec, "haar_vert", true ) )
  89. tops.push_back ( new HaarVertical() );
  90. if ( conf->gB ( featsec, "haar_diag", true ) )
  91. tops.push_back ( new HaarDiag() );
  92. if ( conf->gB ( featsec, "haar3_horz", true ) )
  93. tops.push_back ( new Haar3Horiz() );
  94. if ( conf->gB ( featsec, "haar3_vert", true ) )
  95. tops.push_back ( new Haar3Vert() );
  96. if ( conf->gB ( featsec, "glob", true ) )
  97. tops.push_back ( new GlobalFeats() );
  98. ops.push_back ( tops );
  99. ops.push_back ( tops );
  100. tops.clear();
  101. if ( conf->gB ( featsec, "minus", true ) )
  102. tops.push_back ( new Minus() );
  103. if ( conf->gB ( featsec, "minus_abs", true ) )
  104. tops.push_back ( new MinusAbs() );
  105. if ( conf->gB ( featsec, "addition", true ) )
  106. tops.push_back ( new Addition() );
  107. if ( conf->gB ( featsec, "only1", true ) )
  108. tops.push_back ( new Only1() );
  109. if ( conf->gB ( featsec, "rel_x", true ) )
  110. tops.push_back ( new RelativeXPosition() );
  111. if ( conf->gB ( featsec, "rel_y", true ) )
  112. tops.push_back ( new RelativeYPosition() );
  113. ops.push_back ( tops );
  114. useGradient = conf->gB ( featsec, "use_gradient", true );
  115. // geometric features of hoiem
  116. useHoiemFeatures = conf->gB ( featsec, "use_hoiem_features", false );
  117. if ( useHoiemFeatures )
  118. {
  119. hoiemDirectory = conf->gS ( featsec, "hoiem_directory" );
  120. }
  121. opOverview = vector<int> ( NBOPERATIONS, 0 );
  122. contextOverview = vector<vector<double> > ( maxDepth, vector<double> ( 2, 0.0 ) );
  123. calcVal.push_back ( new MCImageAccess() );
  124. calcVal.push_back ( new MCImageAccess() );
  125. calcVal.push_back ( new MCImageAccess() );
  126. calcVal.push_back ( new MCImageAccess() );
  127. calcVal.push_back ( new ClassificationResultAccess() );
  128. classnames = md->getClassNames ( "train" );
  129. ///////////////////////////////////
  130. // Train Segmentation Context Trees
  131. ///////////////////////////////////
  132. if ( saveLoadData )
  133. {
  134. if ( FileMgt::fileExists ( fileLocation ) )
  135. read ( fileLocation );
  136. else
  137. {
  138. train ( md );
  139. write ( fileLocation );
  140. }
  141. }
  142. else
  143. {
  144. train ( md );
  145. }
  146. }
  147. SemSegContextTree::~SemSegContextTree()
  148. {
  149. }
  150. double SemSegContextTree::getBestSplit ( std::vector<NICE::MultiChannelImageT<double> > &feats, std::vector<NICE::MultiChannelImageT<unsigned short int> > &currentfeats, const std::vector<NICE::MatrixT<int> > &labels, int node, Operation *&splitop, double &splitval, const int &tree )
  151. {
  152. Timer t;
  153. t.start();
  154. int imgCount = 0;
  155. try
  156. {
  157. imgCount = (int)feats.size();
  158. }
  159. catch ( Exception )
  160. {
  161. cerr << "no features computed?" << endl;
  162. }
  163. double bestig = -numeric_limits< double >::max();
  164. splitop = NULL;
  165. splitval = -1.0;
  166. set<vector<int> >selFeats;
  167. map<int, int> e;
  168. int featcounter = forest[tree][node].featcounter;
  169. if ( featcounter < minFeats )
  170. {
  171. //cout << "only " << featcounter << " feats in current node -> it's a leaf" << endl;
  172. return 0.0;
  173. }
  174. vector<double> fraction ( a.size(), 0.0 );
  175. for ( uint i = 0; i < fraction.size(); i++ )
  176. {
  177. if ( forbidden_classes.find ( labelmapback[i] ) != forbidden_classes.end() )
  178. fraction[i] = 0;
  179. else
  180. fraction[i] = ( (double)maxSamples ) / ( (double)featcounter * a[i] * a.size() );
  181. }
  182. featcounter = 0;
  183. for ( int iCounter = 0; iCounter < imgCount; iCounter++ )
  184. {
  185. int xsize = (int)currentfeats[iCounter].width();
  186. int ysize = (int)currentfeats[iCounter].height();
  187. for ( int x = 0; x < xsize; x++ )
  188. {
  189. for ( int y = 0; y < ysize; y++ )
  190. {
  191. if ( currentfeats[iCounter].get ( x, y, tree ) == node )
  192. {
  193. int cn = labels[iCounter] ( x, y );
  194. double randD = (double)rand() / (double)RAND_MAX;
  195. if ( labelmap.find ( cn ) == labelmap.end() )
  196. continue;
  197. if ( randD < fraction[labelmap[cn]] )
  198. {
  199. vector<int> tmp ( 3, 0 );
  200. tmp[0] = iCounter;
  201. tmp[1] = x;
  202. tmp[2] = y;
  203. featcounter++;
  204. selFeats.insert ( tmp );
  205. e[cn]++;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. map<int, int>::iterator mapit;
  212. double globent = 0.0;
  213. for ( mapit = e.begin() ; mapit != e.end(); mapit++ )
  214. {
  215. double p = (double)( *mapit ).second / (double)featcounter;
  216. globent += p * log2 ( p );
  217. }
  218. globent = -globent;
  219. if ( globent < 0.5 )
  220. {
  221. return 0.0;
  222. }
  223. /** vector of all possible features */
  224. std::vector<Operation*> featsel;
  225. for ( int i = 0; i < featsPerSplit; i++ )
  226. {
  227. int x1, x2, y1, y2;
  228. int ft = (int)( (double)rand() / (double)RAND_MAX * (double)ftypes );
  229. int tmpws = windowSize;
  230. if ( ft > 1 && firstiteration )
  231. ft = 0;
  232. if ( channelsPerType[ft].size() == 0 )
  233. {
  234. ft = 0;
  235. }
  236. if ( ft > 1 )
  237. {
  238. //use larger window size for context features
  239. tmpws *= 4;
  240. }
  241. x1 = (int)( (double)rand() / (double)RAND_MAX * (double)tmpws ) - tmpws / 2;
  242. x2 = (int)( (double)rand() / (double)RAND_MAX * (double)tmpws ) - tmpws / 2;
  243. y1 = (int)( (double)rand() / (double)RAND_MAX * (double)tmpws ) - tmpws / 2;
  244. y2 = (int)( (double)rand() / (double)RAND_MAX * (double)tmpws ) - tmpws / 2;
  245. int f1 = ( int ) ( (double) rand() / (double) RAND_MAX * (double) channelsPerType[ft].size() );
  246. int f2 = ( int ) ( (double) rand() / (double) RAND_MAX * (double) channelsPerType[ft].size() );
  247. int o = ( int ) ( (double) rand() / (double) RAND_MAX * (double) ops[ft].size() );
  248. Operation *op = ops[ft][o]->clone();
  249. op->set ( x1, y1, x2, y2, channelsPerType[ft][f1], channelsPerType[ft][f2], calcVal[ft] );
  250. //if( i < 10)
  251. //cerr << "type: " << ft << " features: " << op->writeInfos() << endl;
  252. if ( ft == 3 || ft == 4 )
  253. op->setContext ( true );
  254. else
  255. op->setContext ( false );
  256. //if(ft == 4)
  257. //cerr << "type: " << ft << " features: " << op->writeInfos() << endl;
  258. featsel.push_back ( op );
  259. }
  260. #pragma omp parallel for private(mapit)
  261. for ( int f = 0; f < featsPerSplit; f++ )
  262. {
  263. double l_bestig = -numeric_limits< double >::max();
  264. double l_splitval = -1.0;
  265. set<vector<int> >::iterator it;
  266. vector<double> vals;
  267. double maxval = -numeric_limits<double>::max();
  268. double minval = numeric_limits<double>::max();
  269. for ( it = selFeats.begin() ; it != selFeats.end(); it++ )
  270. {
  271. Features feat;
  272. feat.feats = &feats[ ( *it ) [0]];
  273. feat.cfeats = &currentfeats[ ( *it ) [0]];
  274. feat.cTree = tree;
  275. feat.tree = &forest[tree];
  276. double val = featsel[f]->getVal ( feat, ( *it ) [1], ( *it ) [2] );
  277. vals.push_back ( val );
  278. maxval = std::max ( val, maxval );
  279. minval = std::min ( val, minval );
  280. }
  281. if ( minval == maxval )
  282. continue;
  283. double scale = maxval - minval;
  284. vector<double> splits;
  285. for ( int r = 0; r < randomTests; r++ )
  286. {
  287. splits.push_back ( ( (double)rand() / (double)RAND_MAX*scale ) + minval );
  288. }
  289. for ( int run = 0 ; run < randomTests; run++ )
  290. {
  291. set<vector<int> >::iterator it2;
  292. double val = splits[run];
  293. map<int, int> eL, eR;
  294. int counterL = 0, counterR = 0;
  295. int counter2 = 0;
  296. for ( it2 = selFeats.begin() ; it2 != selFeats.end(); it2++, counter2++ )
  297. {
  298. int cn = labels[ ( *it2 ) [0]] ( ( *it2 ) [1], ( *it2 ) [2] );
  299. //cout << "vals[counter2] " << vals[counter2] << " val: " << val << endl;
  300. if ( vals[counter2] < val )
  301. {
  302. //left entropie:
  303. eL[cn] = eL[cn] + 1;
  304. counterL++;
  305. }
  306. else
  307. {
  308. //right entropie:
  309. eR[cn] = eR[cn] + 1;
  310. counterR++;
  311. }
  312. }
  313. double leftent = 0.0;
  314. for ( mapit = eL.begin() ; mapit != eL.end(); mapit++ )
  315. {
  316. double p = (double)( *mapit ).second / (double)counterL;
  317. leftent -= p * log2 ( p );
  318. }
  319. double rightent = 0.0;
  320. for ( mapit = eR.begin() ; mapit != eR.end(); mapit++ )
  321. {
  322. double p = (double)( *mapit ).second / (double)counterR;
  323. rightent -= p * log2 ( p );
  324. }
  325. //cout << "rightent: " << rightent << " leftent: " << leftent << endl;
  326. double pl = (double)counterL / (double)( counterL + counterR );
  327. double ig = globent - ( 1.0 - pl ) * rightent - pl * leftent;
  328. //double ig = globent - rightent - leftent;
  329. if ( useShannonEntropy )
  330. {
  331. double esplit = - ( pl * log ( pl ) + ( 1 - pl ) * log ( 1 - pl ) );
  332. ig = 2 * ig / ( globent + esplit );
  333. }
  334. if ( ig > l_bestig )
  335. {
  336. l_bestig = ig;
  337. l_splitval = val;
  338. }
  339. }
  340. #pragma omp critical
  341. {
  342. if ( l_bestig > bestig )
  343. {
  344. bestig = l_bestig;
  345. splitop = featsel[f];
  346. splitval = l_splitval;
  347. }
  348. }
  349. }
  350. //FIXME: delete all features!
  351. /*for(int i = 0; i < featsPerSplit; i++)
  352. {
  353. if(featsel[i] != splitop)
  354. delete featsel[i];
  355. }*/
  356. #ifdef DEBUG
  357. cout << "globent: " << globent << " bestig " << bestig << " splitval: " << splitval << endl;
  358. #endif
  359. return bestig;
  360. }
  361. inline double SemSegContextTree::getMeanProb ( const int &x, const int &y, const int &channel, const MultiChannelImageT<unsigned short int> &currentfeats )
  362. {
  363. double val = 0.0;
  364. for ( int tree = 0; tree < nbTrees; tree++ )
  365. {
  366. val += forest[tree][currentfeats.get ( x,y,tree ) ].dist[channel];
  367. }
  368. return val / (double)nbTrees;
  369. }
  370. void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImageT<SparseVectorInt> &infeats, NICE::MultiChannelImageT<SparseVectorInt> &integralImage )
  371. {
  372. int xsize = infeats.width();
  373. int ysize = infeats.height();
  374. integralImage ( 0, 0 ).add ( infeats.get ( 0, 0 ) );
  375. //first column
  376. for ( int y = 1; y < ysize; y++ )
  377. {
  378. integralImage ( 0, y ).add ( infeats.get ( 0, y ) );
  379. integralImage ( 0, y ).add ( integralImage ( 0, y - 1 ) );
  380. }
  381. //first row
  382. for ( int x = 1; x < xsize; x++ )
  383. {
  384. integralImage ( x, 0 ).add ( infeats.get ( x, 0 ) );
  385. integralImage ( x, 0 ).add ( integralImage ( x - 1, 0 ) );
  386. }
  387. //rest
  388. for ( int y = 1; y < ysize; y++ )
  389. {
  390. for ( int x = 1; x < xsize; x++ )
  391. {
  392. integralImage ( x, y ).add ( infeats.get ( x, y ) );
  393. integralImage ( x, y ).add ( integralImage ( x, y - 1 ) );
  394. integralImage ( x, y ).add ( integralImage ( x - 1, y ) );
  395. integralImage ( x, y ).sub ( integralImage ( x - 1, y - 1 ) );
  396. }
  397. }
  398. }
  399. void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, NICE::MultiChannelImageT<double> &feats, int firstChannel )
  400. {
  401. int xsize = currentfeats.width();
  402. int ysize = currentfeats.height();
  403. xsize = feats.width();
  404. ysize = feats.height();
  405. if ( firstiteration )
  406. {
  407. firstiteration = false;
  408. #pragma omp parallel for
  409. for ( int it = 0; it < integralMap.size(); it++ )
  410. {
  411. int c1 = integralMap[it].first;
  412. int c = integralMap[it].second;
  413. feats( 0, 0, c ) = feats( 0, 0, c1 );
  414. //first column
  415. for ( int y = 1; y < ysize; y++ )
  416. {
  417. feats( 0, y, c ) = feats.get ( 0, y, c1 )
  418. + feats.get ( 0, y-1, c );
  419. }
  420. //first row
  421. for ( int x = 1; x < xsize; x++ )
  422. {
  423. feats( x, 0, c ) = feats.get ( x, 0, c1 )
  424. + feats.get ( x-1, 0, c );
  425. }
  426. //rest
  427. for ( int y = 1; y < ysize; y++ )
  428. {
  429. for ( int x = 1; x < xsize; x++ )
  430. {
  431. feats ( x, y, c ) = feats ( x, y, c1 )
  432. + feats ( x, y - 1, c )
  433. + feats ( x - 1, y, c )
  434. - feats ( x - 1, y - 1, c );
  435. }
  436. }
  437. }
  438. }
  439. int channels = ( int ) forest[0][0].dist.size();
  440. #pragma omp parallel for
  441. for ( int c = 0; c < channels; c++ )
  442. {
  443. feats ( 0, 0, firstChannel + c ) = getMeanProb ( 0, 0, c, currentfeats );
  444. //first column
  445. for ( int y = 1; y < ysize; y++ )
  446. {
  447. feats ( 0, y, firstChannel + c ) = getMeanProb ( 0, y, c, currentfeats )
  448. + feats ( 0, y - 1, firstChannel + c );
  449. }
  450. //first row
  451. for ( int x = 1; x < xsize; x++ )
  452. {
  453. feats ( x, 0, firstChannel + c ) = getMeanProb ( x, 0, c, currentfeats )
  454. + feats ( x - 1, 0, firstChannel + c );
  455. }
  456. //rest
  457. for ( int y = 1; y < ysize; y++ )
  458. {
  459. for ( int x = 1; x < xsize; x++ )
  460. {
  461. feats ( x, y, firstChannel + c ) = getMeanProb ( x, y, c, currentfeats )
  462. + feats ( x, y - 1, firstChannel + c )
  463. + feats ( x - 1, y, firstChannel + c )
  464. - feats ( x - 1, y - 1, firstChannel + c );
  465. }
  466. }
  467. }
  468. }
  469. inline double computeWeight ( const double &d, const double &dim )
  470. {
  471. return 1.0 / ( pow ( 2, (double)( dim - d + 1 ) ) );
  472. }
  473. void SemSegContextTree::train ( const MultiDataset *md )
  474. {
  475. Timer timer;
  476. timer.start();
  477. const LabeledSet train = * ( *md ) ["train"];
  478. const LabeledSet *trainp = &train;
  479. ProgressBar pb ( "compute feats" );
  480. pb.show();
  481. //TODO: Speichefresser!, lohnt sich sparse?
  482. vector<MultiChannelImageT<double> > allfeats;
  483. vector<MultiChannelImageT<unsigned short int> > currentfeats;
  484. vector<MatrixT<int> > labels;
  485. #ifdef TEXTONMAP
  486. vector<MultiChannelImageT<SparseVectorInt> > textonMap;
  487. #endif
  488. vector<MultiChannelImageT<SparseVectorInt> > integralTexton;
  489. std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
  490. if ( forbidden_classes_s == "" )
  491. {
  492. forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  493. }
  494. classnames.getSelection ( forbidden_classes_s, forbidden_classes );
  495. int imgcounter = 0;
  496. int amountPixels = 0;
  497. LOOP_ALL_S ( *trainp )
  498. {
  499. EACH_INFO ( classno, info );
  500. NICE::ColorImage img;
  501. std::string currentFile = info.img();
  502. CachedExample *ce = new CachedExample ( currentFile );
  503. const LocalizationResult *locResult = info.localization();
  504. if ( locResult->size() <= 0 )
  505. {
  506. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  507. currentFile.c_str() );
  508. continue;
  509. }
  510. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n", currentFile.c_str() );
  511. int xsize, ysize;
  512. ce->getImageSize ( xsize, ysize );
  513. amountPixels += xsize * ysize;
  514. MatrixT<int> tmpMat ( xsize, ysize );
  515. currentfeats.push_back ( MultiChannelImageT<unsigned short int> ( xsize, ysize, nbTrees ) );
  516. currentfeats[imgcounter].setAll ( 0 );
  517. #ifdef TEXTONMAP
  518. textonMap.push_back ( MultiChannelImageT<SparseVectorInt> ( xsize / grid + 1, ysize / grid + 1, 1 ) );
  519. integralTexton.push_back ( MultiChannelImageT<SparseVectorInt> ( xsize / grid + 1, ysize / grid + 1, 1 ) );
  520. #endif
  521. labels.push_back ( tmpMat );
  522. try {
  523. img = ColorImage ( currentFile );
  524. } catch ( Exception ) {
  525. cerr << "SemSeg: error opening image file <" << currentFile << ">" << endl;
  526. continue;
  527. }
  528. Globals::setCurrentImgFN ( currentFile );
  529. //TODO: resize image?!
  530. MultiChannelImageT<double> feats;
  531. allfeats.push_back ( feats );
  532. // read image and do some simple transformations
  533. extractBasicFeatures ( allfeats[imgcounter], img, currentFile );
  534. // getting groundtruth
  535. NICE::Image pixelLabels ( xsize, ysize );
  536. pixelLabels.set ( 0 );
  537. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  538. for ( int x = 0; x < xsize; x++ )
  539. {
  540. for ( int y = 0; y < ysize; y++ )
  541. {
  542. classno = pixelLabels.getPixel ( x, y );
  543. labels[imgcounter] ( x, y ) = classno;
  544. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  545. continue;
  546. labelcounter[classno]++;
  547. }
  548. }
  549. imgcounter++;
  550. pb.update ( trainp->count() );
  551. delete ce;
  552. }
  553. pb.hide();
  554. map<int, int>::iterator mapit;
  555. int classes = 0;
  556. for ( mapit = labelcounter.begin(); mapit != labelcounter.end(); mapit++ )
  557. {
  558. labelmap[mapit->first] = classes;
  559. labelmapback[classes] = mapit->first;
  560. classes++;
  561. }
  562. ////////////////////////////////////////////////////
  563. //define which featurextraction methods should be used for each channel
  564. #ifdef LOCALFEATS
  565. rawChannels = 11;
  566. #else
  567. rawChannels = 3;
  568. #endif
  569. // how many channels without integral image
  570. int shift = 0;
  571. if ( useGradient )
  572. rawChannels *= 2;
  573. if ( useHoiemFeatures )
  574. rawChannels += 8;
  575. // gray value images
  576. for ( int i = 0; i < rawChannels; i++ )
  577. {
  578. channelType.push_back ( 0 );
  579. }
  580. // regions
  581. if ( useRegionFeature )
  582. {
  583. channelType.push_back ( 1 );
  584. shift++;
  585. }
  586. for ( int i = 0; i < rawChannels; i++ )
  587. {
  588. channelType.push_back ( 2 );
  589. }
  590. // integral images
  591. for ( int i = 0; i < classes; i++ )
  592. {
  593. channelType.push_back ( 3 );
  594. }
  595. integralMap.clear();
  596. int integralImageAmount = rawChannels;
  597. for ( int ii = 0; ii < integralImageAmount; ii++ )
  598. {
  599. integralMap.push_back ( pair<int, int> ( ii, ii + integralImageAmount + shift ) );
  600. }
  601. int amountTypes = 5;
  602. channelsPerType = vector<vector<int> > ( amountTypes, vector<int>() );
  603. for ( int i = 0; i < channelType.size(); i++ )
  604. {
  605. channelsPerType[channelType[i]].push_back ( i );
  606. }
  607. for ( int i = 0; i < classes; i++ )
  608. {
  609. channelsPerType[channelsPerType.size()-1].push_back ( i );
  610. }
  611. ftypes = std::min ( amountTypes, ftypes );
  612. ////////////////////////////////////////////////////
  613. //balancing
  614. int featcounter = 0;
  615. a = vector<double> ( classes, 0.0 );
  616. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  617. {
  618. int xsize = (int)currentfeats[iCounter].width();
  619. int ysize = (int)currentfeats[iCounter].height();
  620. for ( int x = 0; x < xsize; x++ )
  621. {
  622. for ( int y = 0; y < ysize; y++ )
  623. {
  624. featcounter++;
  625. int cn = labels[iCounter] ( x, y );
  626. if ( labelmap.find ( cn ) == labelmap.end() )
  627. continue;
  628. a[labelmap[cn]] ++;
  629. }
  630. }
  631. }
  632. for ( int i = 0; i < (int)a.size(); i++ )
  633. {
  634. a[i] /= (double)featcounter;
  635. }
  636. #ifdef DEBUG
  637. for ( int i = 0; i < (int)a.size(); i++ )
  638. {
  639. cout << "a[" << i << "]: " << a[i] << endl;
  640. }
  641. cout << "a.size: " << a.size() << endl;
  642. #endif
  643. depth = 0;
  644. int uniquenumber = 0;
  645. for ( int t = 0; t < nbTrees; t++ )
  646. {
  647. vector<TreeNode> tree;
  648. tree.push_back ( TreeNode() );
  649. tree[0].dist = vector<double> ( classes, 0.0 );
  650. tree[0].depth = depth;
  651. tree[0].featcounter = amountPixels;
  652. tree[0].nodeNumber = uniquenumber;
  653. uniquenumber++;
  654. forest.push_back ( tree );
  655. }
  656. vector<int> startnode ( nbTrees, 0 );
  657. bool allleaf = false;
  658. //int baseFeatSize = allfeats[0].size();
  659. timer.stop();
  660. cerr << "preprocessing finished in: " << timer.getLastAbsolute() << " seconds" << endl;
  661. timer.start();
  662. while ( !allleaf && depth < maxDepth )
  663. {
  664. depth++;
  665. #ifdef DEBUG
  666. cout << "depth: " << depth << endl;
  667. #endif
  668. allleaf = true;
  669. vector<MultiChannelImageT<unsigned short int> > lastfeats = currentfeats;
  670. #if 1
  671. Timer timerDepth;
  672. timerDepth.start();
  673. #endif
  674. double weight = computeWeight ( depth, maxDepth ) - computeWeight ( depth - 1, maxDepth );
  675. if ( depth == 1 )
  676. {
  677. weight = computeWeight ( 1, maxDepth );
  678. }
  679. #pragma omp parallel for
  680. for ( int tree = 0; tree < nbTrees; tree++ )
  681. {
  682. int t = (int)forest[tree].size();
  683. int s = startnode[tree];
  684. startnode[tree] = t;
  685. #pragma omp parallel for
  686. for ( int i = s; i < t; i++ )
  687. {
  688. if ( !forest[tree][i].isleaf && forest[tree][i].left < 0 )
  689. {
  690. Operation *splitfeat = NULL;
  691. double splitval;
  692. double bestig = getBestSplit(allfeats, lastfeats, labels, i, splitfeat, splitval, tree);
  693. for ( int ii = 0; ii < lastfeats.size(); ii++ )
  694. {
  695. for ( int c = 0; c < lastfeats[ii].channels(); c++ )
  696. {
  697. short unsigned int minv, maxv;
  698. lastfeats[ii].statistics ( minv, maxv, c );
  699. }
  700. }
  701. forest[tree][i].feat = splitfeat;
  702. forest[tree][i].decision = splitval;
  703. if ( splitfeat != NULL )
  704. {
  705. allleaf = false;
  706. int left = forest[tree].size();
  707. forest[tree].push_back ( TreeNode() );
  708. forest[tree].push_back ( TreeNode() );
  709. int right = left + 1;
  710. forest[tree][i].left = left;
  711. forest[tree][i].right = right;
  712. forest[tree][left].dist = vector<double> ( classes, 0.0 );
  713. forest[tree][right].dist = vector<double> ( classes, 0.0 );
  714. forest[tree][left].depth = depth;
  715. forest[tree][right].depth = depth;
  716. forest[tree][left].featcounter = 0;
  717. forest[tree][right].featcounter = 0;
  718. forest[tree][left].nodeNumber = uniquenumber;
  719. int leftu = uniquenumber;
  720. uniquenumber++;
  721. forest[tree][right].nodeNumber = uniquenumber;
  722. int rightu = uniquenumber;
  723. uniquenumber++;
  724. forest[tree][right].featcounter = 0;
  725. #pragma omp parallel for
  726. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  727. {
  728. int xsize = currentfeats[iCounter].width();
  729. int ysize = currentfeats[iCounter].height();
  730. for ( int x = 0; x < xsize; x++ )
  731. {
  732. for ( int y = 0; y < ysize; y++ )
  733. {
  734. if ( currentfeats[iCounter].get ( x, y, tree ) == i )
  735. {
  736. Features feat;
  737. feat.feats = &allfeats[iCounter];
  738. feat.cfeats = &lastfeats[iCounter];
  739. feat.cTree = tree;
  740. feat.tree = &forest[tree];
  741. double val = splitfeat->getVal ( feat, x, y );
  742. int subx = x / grid;
  743. int suby = y / grid;
  744. #pragma omp critical
  745. if ( val < splitval )
  746. {
  747. currentfeats[iCounter].set ( x, y, left, tree );
  748. if ( labelmap.find ( labels[iCounter] ( x, y ) ) != labelmap.end() )
  749. forest[tree][left].dist[labelmap[labels[iCounter] ( x, y ) ]]++;
  750. forest[tree][left].featcounter++;
  751. SparseVectorInt v;
  752. v.insert ( pair<int, double> ( leftu, weight ) );
  753. #ifdef TEXTONMAP
  754. textonMap[iCounter] ( subx, suby ).add ( v );
  755. #endif
  756. }
  757. else
  758. {
  759. currentfeats[iCounter].set ( x, y, right, tree );
  760. if ( labelmap.find ( labels[iCounter] ( x, y ) ) != labelmap.end() )
  761. forest[tree][right].dist[labelmap[labels[iCounter] ( x, y ) ]]++;
  762. forest[tree][right].featcounter++;
  763. //feld im subsampled finden und in diesem rechts hochzählen
  764. SparseVectorInt v;
  765. v.insert ( pair<int, double> ( rightu, weight ) );
  766. #ifdef TEXTONMAP
  767. textonMap[iCounter] ( subx, suby ).add ( v );
  768. #endif
  769. }
  770. }
  771. }
  772. }
  773. }
  774. double lcounter = 0.0, rcounter = 0.0;
  775. for ( uint d = 0; d < forest[tree][left].dist.size(); d++ )
  776. {
  777. if ( forbidden_classes.find ( labelmapback[d] ) != forbidden_classes.end() )
  778. {
  779. forest[tree][left].dist[d] = 0;
  780. forest[tree][right].dist[d] = 0;
  781. }
  782. else
  783. {
  784. forest[tree][left].dist[d] /= a[d];
  785. lcounter += forest[tree][left].dist[d];
  786. forest[tree][right].dist[d] /= a[d];
  787. rcounter += forest[tree][right].dist[d];
  788. }
  789. }
  790. if ( lcounter <= 0 || rcounter <= 0 )
  791. {
  792. cout << "lcounter : " << lcounter << " rcounter: " << rcounter << endl;
  793. cout << "splitval: " << splitval << " splittype: " << splitfeat->writeInfos() << endl;
  794. cout << "bestig: " << bestig << endl;
  795. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  796. {
  797. int xsize = currentfeats[iCounter].width();
  798. int ysize = currentfeats[iCounter].height();
  799. int counter = 0;
  800. for ( int x = 0; x < xsize; x++ )
  801. {
  802. for ( int y = 0; y < ysize; y++ )
  803. {
  804. if ( lastfeats[iCounter].get ( x, y, tree ) == i )
  805. {
  806. if ( ++counter > 30 )
  807. break;
  808. Features feat;
  809. feat.feats = &allfeats[iCounter];
  810. feat.cfeats = &lastfeats[iCounter];
  811. feat.cTree = tree;
  812. feat.tree = &forest[tree];
  813. double val = splitfeat->getVal ( feat, x, y );
  814. cout << "splitval: " << splitval << " val: " << val << endl;
  815. }
  816. }
  817. }
  818. }
  819. assert ( lcounter > 0 && rcounter > 0 );
  820. }
  821. for ( uint d = 0; d < forest[tree][left].dist.size(); d++ )
  822. {
  823. forest[tree][left].dist[d] /= lcounter;
  824. forest[tree][right].dist[d] /= rcounter;
  825. }
  826. }
  827. else
  828. {
  829. forest[tree][i].isleaf = true;
  830. }
  831. }
  832. }
  833. }
  834. //compute integral images
  835. if ( firstiteration )
  836. {
  837. for ( int i = 0; i < imgcounter; i++ )
  838. {
  839. int pos = allfeats.size();
  840. allfeats[i].addChannel ( ( int ) ( classes + rawChannels ) );
  841. }
  842. }
  843. #pragma omp parallel for
  844. for ( int i = 0; i < imgcounter; i++ )
  845. {
  846. computeIntegralImage ( currentfeats[i], allfeats[i], channelType.size() - classes );
  847. #ifdef TEXTONMAP
  848. computeIntegralImage ( textonMap[i], integralTexton[i] );
  849. #endif
  850. }
  851. #if 1
  852. timerDepth.stop();
  853. cout << "time for depth " << depth << ": " << timerDepth.getLastAbsolute() << endl;
  854. #endif
  855. }
  856. timer.stop();
  857. cerr << "learning finished in: " << timer.getLastAbsolute() << " seconds" << endl;
  858. timer.start();
  859. #ifdef WRITEGLOB
  860. ofstream outstream ( "globtrain.feat" );
  861. for ( int i = 0; i < textonMap.size(); i++ )
  862. {
  863. set<int> usedclasses;
  864. for ( uint x = 0; x < labels[i].rows(); x++ )
  865. {
  866. for ( uint y = 0; y < labels[i].cols(); y++ )
  867. {
  868. int classno = labels[i] ( x, y );
  869. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  870. continue;
  871. usedclasses.insert ( classno );
  872. }
  873. }
  874. cout << "labels.cols: " << labels[i].cols() << " labels.rows " << labels[i].rows() << endl;
  875. cout << "currentfeats : " << allfeats[i].width() << " allfeats[i].height(); " << allfeats[i].height() << endl;
  876. set<int>::iterator it;
  877. for ( it = usedclasses.begin() ; it != usedclasses.end(); it++ )
  878. outstream << *it << " ";
  879. outstream << endl;
  880. integralTexton[i] ( integralTexton[i].width() - 1, integralTexton[i].height() - 1 ).store ( outstream );
  881. }
  882. outstream.close();
  883. #endif
  884. cout << "uniquenumber " << uniquenumber << endl;
  885. //getchar();
  886. #ifdef DEBUG
  887. for ( int tree = 0; tree < nbTrees; tree++ )
  888. {
  889. int t = (int)forest[tree].size();
  890. for ( int i = 0; i < t; i++ )
  891. {
  892. printf ( "tree[%i]: left: %i, right: %i", i, forest[tree][i].left, forest[tree][i].right );
  893. if ( !forest[tree][i].isleaf && forest[tree][i].left != -1 )
  894. {
  895. cout << ", feat: " << forest[tree][i].feat->writeInfos() << " ";
  896. opOverview[forest[tree][i].feat->getOps() ]++;
  897. contextOverview[forest[tree][i].depth][ (int)forest[tree][i].feat->getContext() ]++;
  898. }
  899. for ( int d = 0; d < (int)forest[tree][i].dist.size(); d++ )
  900. {
  901. cout << " " << forest[tree][i].dist[d];
  902. }
  903. cout << endl;
  904. }
  905. }
  906. for ( uint c = 0; c < ops.size(); c++ )
  907. {
  908. for ( int t = 0; t < ops[c].size(); t++ )
  909. {
  910. cout << ops[c][t]->writeInfos() << ": " << opOverview[ops[c][t]->getOps() ] << endl;
  911. }
  912. }
  913. for ( int d = 0; d < maxDepth; d++ )
  914. {
  915. double sum = contextOverview[d][0] + contextOverview[d][1];
  916. contextOverview[d][0] /= sum;
  917. contextOverview[d][1] /= sum;
  918. cout << "depth: " << d << " woContext: " << contextOverview[d][0] << " wContext: " << contextOverview[d][1] << endl;
  919. }
  920. #endif
  921. timer.stop();
  922. cerr << "rest finished in: " << timer.getLastAbsolute() << " seconds" << endl;
  923. timer.start();
  924. }
  925. void SemSegContextTree::extractBasicFeatures ( NICE::MultiChannelImageT<double> &feats, const ColorImage &img, const string &currentFile )
  926. {
  927. int xsize = img.width();
  928. int ysize = img.height();
  929. //TODO: resize image?!
  930. #ifdef LOCALFEATS
  931. lfcw->getFeats ( img, feats );
  932. #else
  933. feats.reInit ( xsize, ysize, 3 );
  934. for ( int x = 0; x < xsize; x++ )
  935. {
  936. for ( int y = 0; y < ysize; y++ )
  937. {
  938. for ( int r = 0; r < 3; r++ )
  939. {
  940. feats.set ( x, y, img.getPixel ( x, y, r ), r );
  941. }
  942. }
  943. }
  944. feats = ColorSpace::rgbtolab ( feats );
  945. #endif
  946. if ( useGradient )
  947. {
  948. int currentsize = feats.channels();
  949. feats.addChannel ( currentsize );
  950. for ( int c = 0; c < currentsize; c++ )
  951. {
  952. ImageT<double> tmp = feats[c];
  953. ImageT<double> tmp2 = feats[c+currentsize];
  954. NICE::FilterT<double,double,double>::gradientStrength( tmp, tmp2 );
  955. }
  956. }
  957. // read the geometric cues produced by Hoiem et al.
  958. if ( useHoiemFeatures )
  959. {
  960. // we could also give the following set as a config option
  961. string hoiemClasses_s = "sky 000 090-045 090-090 090-135 090 090-por 090-sol";
  962. vector<string> hoiemClasses;
  963. StringTools::split ( hoiemClasses_s, ' ', hoiemClasses );
  964. // Now we have to do some fancy regular expressions :)
  965. // Original image filename: basel_000083.jpg
  966. // hoiem result: basel_000083_c_sky.png
  967. // Fancy class of Ferid which supports string handling especially for filenames
  968. FileName fn ( currentFile );
  969. fn.removeExtension();
  970. FileName fnBase = fn.extractFileName();
  971. // counter for the channel index, starts with the current size of the destination multi-channel image
  972. int currentChannel = feats.channels();
  973. // add a channel for each feature in advance
  974. feats.addChannel ( hoiemClasses.size() );
  975. // loop through all geometric categories and add the images
  976. for ( vector<string>::const_iterator i = hoiemClasses.begin(); i != hoiemClasses.end(); i++, currentChannel++ )
  977. {
  978. string hoiemClass = *i;
  979. FileName fnConfidenceImage ( hoiemDirectory + fnBase.str() + "_c_" + hoiemClass + ".png" );
  980. if ( ! fnConfidenceImage.fileExists() )
  981. {
  982. fthrow ( Exception, "Unable to read the Hoiem geometric confidence image: " << fnConfidenceImage.str() << " (original image is " << currentFile << ")" );
  983. } else {
  984. Image confidenceImage ( fnConfidenceImage.str() );
  985. // check whether the image size is consistent
  986. if ( confidenceImage.width() != feats.width() || confidenceImage.height() != feats.height() )
  987. {
  988. fthrow ( Exception, "The size of the geometric confidence image does not match with the original image size: " << fnConfidenceImage.str() );
  989. }
  990. ImageT<double> dst = feats[currentChannel];
  991. // copy standard image to double image
  992. for ( uint y = 0 ; y < confidenceImage.height(); y++ )
  993. for ( uint x = 0 ; x < confidenceImage.width(); x++ )
  994. feats ( x, y, currentChannel ) = (double) confidenceImage ( x, y );
  995. }
  996. }
  997. }
  998. }
  999. void SemSegContextTree::semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities )
  1000. {
  1001. int xsize;
  1002. int ysize;
  1003. ce->getImageSize ( xsize, ysize );
  1004. firstiteration = true;
  1005. int classes = labelmapback.size();
  1006. int numClasses = classNames->numClasses();
  1007. fprintf ( stderr, "ContextTree classification !\n" );
  1008. probabilities.reInit ( xsize, ysize, numClasses );
  1009. probabilities.setAll ( 0 );
  1010. #ifdef TEXTONMAP
  1011. MultiChannelImageT<SparseVectorInt> textonMap ( xsize / grid + 1, ysize / grid + 1, 1 );
  1012. MultiChannelImageT<SparseVectorInt> integralTexton ( xsize / grid + 1, ysize / grid + 1, 1 );
  1013. #endif
  1014. std::string currentFile = Globals::getCurrentImgFN();
  1015. MultiChannelImageT<double> feats;
  1016. NICE::ColorImage img;
  1017. try {
  1018. img = ColorImage ( currentFile );
  1019. } catch ( Exception ) {
  1020. cerr << "SemSeg: error opening image file <" << currentFile << ">" << endl;
  1021. return;
  1022. }
  1023. extractBasicFeatures ( feats, img, currentFile ); //read image and do some simple transformations
  1024. bool allleaf = false;
  1025. MultiChannelImageT<unsigned short int> currentfeats ( xsize, ysize, nbTrees );
  1026. currentfeats.setAll ( 0 );
  1027. depth = 0;
  1028. for ( int d = 0; d < maxDepth && !allleaf; d++ )
  1029. {
  1030. depth++;
  1031. #ifdef TEXTONMAP
  1032. double weight = computeWeight ( depth, maxDepth ) - computeWeight ( depth - 1, maxDepth );
  1033. if ( depth == 1 )
  1034. {
  1035. weight = computeWeight ( 1, maxDepth );
  1036. }
  1037. #endif
  1038. allleaf = true;
  1039. MultiChannelImageT<unsigned short int> lastfeats = currentfeats;
  1040. int tree;
  1041. #pragma omp parallel for private(tree)
  1042. for ( tree = 0; tree < nbTrees; tree++ )
  1043. {
  1044. for ( int x = 0; x < xsize; x++ )
  1045. {
  1046. for ( int y = 0; y < ysize; y++ )
  1047. {
  1048. int t = currentfeats.get ( x, y, tree );
  1049. if ( forest[tree][t].left > 0 )
  1050. {
  1051. allleaf = false;
  1052. Features feat;
  1053. feat.feats = &feats;
  1054. feat.cfeats = &lastfeats;
  1055. feat.cTree = tree;
  1056. feat.tree = &forest[tree];
  1057. double val = forest[tree][t].feat->getVal ( feat, x, y );
  1058. int subx = x / grid;
  1059. int suby = y / grid;
  1060. if ( val < forest[tree][t].decision )
  1061. {
  1062. currentfeats.set ( x, y, forest[tree][t].left, tree );
  1063. #ifdef TEXTONMAP
  1064. #pragma omp critical
  1065. {
  1066. SparseVectorInt v;
  1067. v.insert ( pair<int, double> ( forest[tree][forest[tree][t].left].nodeNumber, weight ) );
  1068. textonMap ( subx, suby ).add ( v );
  1069. }
  1070. #endif
  1071. }
  1072. else
  1073. {
  1074. currentfeats.set ( x, y, forest[tree][t].right, tree );
  1075. #ifdef TEXTONMAP
  1076. #pragma omp critical
  1077. {
  1078. SparseVectorInt v;
  1079. v.insert ( pair<int, double> ( forest[tree][forest[tree][t].right].nodeNumber, weight ) );
  1080. textonMap ( subx, suby ).add ( v );
  1081. }
  1082. #endif
  1083. }
  1084. }
  1085. }
  1086. }
  1087. }
  1088. if ( depth < maxDepth )
  1089. {
  1090. //compute integral images
  1091. if ( firstiteration )
  1092. {
  1093. feats.addChannel ( classes + rawChannels );
  1094. }
  1095. computeIntegralImage ( currentfeats, feats, channelType.size() - classes );
  1096. #ifdef TEXTONMAP
  1097. computeIntegralImage ( textonMap, integralTexton );
  1098. #endif
  1099. }
  1100. }
  1101. #ifdef WRITEGLOB
  1102. ofstream outstream ( "globtest.feat", ofstream::app );
  1103. outstream << 0 << endl;
  1104. integralTexton ( integralTexton.width() - 1, integralTexton.height() - 1 ).store ( outstream );
  1105. outstream.close();
  1106. #endif
  1107. string cndir = conf->gS ( "SSContextTree", "cndir", "" );
  1108. int allClasses = ( int ) probabilities.channels();
  1109. vector<int> useclass ( allClasses, 1 );
  1110. #ifdef WRITEGLOB
  1111. std::vector< std::string > list;
  1112. StringTools::split ( currentFile, '/', list );
  1113. string orgname = list.back();
  1114. ofstream ostream ( "filelist.txt", ofstream::app );
  1115. ostream << orgname << ".dat" << endl;
  1116. ostream.close();
  1117. if ( cndir != "" )
  1118. {
  1119. useclass = vector<int> ( allClasses, 0 );
  1120. ifstream infile ( ( cndir + "/" + orgname + ".dat" ).c_str() );
  1121. #undef OLD
  1122. #ifdef OLD
  1123. while ( !infile.eof() && infile.good() )
  1124. {
  1125. int tmp;
  1126. infile >> tmp;
  1127. assert ( tmp >= 0 && tmp < allClasses );
  1128. useclass[tmp] = 1;
  1129. }
  1130. #else
  1131. int c = 0;
  1132. vector<double> probs ( allClasses, 0.0 );
  1133. while ( !infile.eof() && infile.good() )
  1134. {
  1135. infile >> probs[c];
  1136. c++;
  1137. }
  1138. vector<double> sorted = probs;
  1139. sort ( sorted.begin(), sorted.end() );
  1140. double thr = sorted[10];
  1141. if ( thr < 0.0 )
  1142. thr = 0.0;
  1143. for ( int c = 0; c < allClasses; c++ )
  1144. {
  1145. if ( probs[c] < thr )
  1146. {
  1147. useclass[c] = 1;
  1148. }
  1149. }
  1150. #endif
  1151. for ( int c = 0; c < allClasses; c++ )
  1152. {
  1153. if ( useclass[c] == 0 )
  1154. {
  1155. probabilities.set ( -numeric_limits< double >::max(), c );
  1156. }
  1157. }
  1158. }
  1159. #endif
  1160. if ( pixelWiseLabeling )
  1161. {
  1162. //finales labeln:
  1163. //long int offset = 0;
  1164. for ( int x = 0; x < xsize; x++ )
  1165. {
  1166. for ( int y = 0; y < ysize; y++ )
  1167. {
  1168. double maxvalue = - numeric_limits<double>::max(); //TODO: das kann auch nur pro knoten gemacht werden, nicht pro pixel
  1169. int maxindex = 0;
  1170. uint s = forest[0][0].dist.size();
  1171. for ( uint i = 0; i < s; i++ )
  1172. {
  1173. int currentclass = labelmapback[i];
  1174. if ( useclass[currentclass] )
  1175. {
  1176. probabilities ( x, y, currentclass ) = getMeanProb ( x, y, i, currentfeats );
  1177. if ( probabilities ( x, y, currentclass ) > maxvalue )
  1178. {
  1179. maxvalue = probabilities ( x, y, currentclass );
  1180. maxindex = currentclass;
  1181. }
  1182. }
  1183. }
  1184. segresult.setPixel ( x, y, maxindex );
  1185. if ( maxvalue > 1 )
  1186. cout << "maxvalue: " << maxvalue << endl;
  1187. }
  1188. }
  1189. #undef VISUALIZE
  1190. #ifdef VISUALIZE
  1191. for ( int j = 0 ; j < (int)probabilities.numChannels; j++ )
  1192. {
  1193. //cout << "class: " << j << endl;//" " << cn.text ( j ) << endl;
  1194. NICE::Matrix tmp ( probabilities.height(), probabilities.width() );
  1195. double maxval = -numeric_limits<double>::max();
  1196. double minval = numeric_limits<double>::max();
  1197. for ( int y = 0; y < probabilities.height(); y++ )
  1198. for ( int x = 0; x < probabilities.width(); x++ )
  1199. {
  1200. double val = probabilities ( x, y, j );
  1201. tmp ( y, x ) = val;
  1202. maxval = std::max ( val, maxval );
  1203. minval = std::min ( val, minval );
  1204. }
  1205. tmp ( 0, 0 ) = 1.0;
  1206. tmp ( 0, 1 ) = 0.0;
  1207. NICE::ColorImage imgrgb ( probabilities.width(), probabilities.height() );
  1208. ICETools::convertToRGB ( tmp, imgrgb );
  1209. cout << "maxval = " << maxval << " minval: " << minval << " for class " << j << endl; //cn.text ( j ) << endl;
  1210. std::string s;
  1211. std::stringstream out;
  1212. out << "tmpprebmap" << j << ".ppm";
  1213. s = out.str();
  1214. imgrgb.write ( s );
  1215. //showImage(imgrgb, "Ergebnis");
  1216. //getchar();
  1217. }
  1218. cout << "fertsch" << endl;
  1219. getchar();
  1220. cout << "weiter gehtsch" << endl;
  1221. #endif
  1222. }
  1223. else
  1224. {
  1225. //final labeling using segmentation
  1226. Matrix regions;
  1227. //showImage(img);
  1228. int regionNumber = segmentation->segRegions ( img, regions );
  1229. cout << "regions: " << regionNumber << endl;
  1230. int dSize = forest[0][0].dist.size();
  1231. vector<vector<double> > regionProbs ( regionNumber, vector<double> ( dSize, 0.0 ) );
  1232. vector<int> bestlabels ( regionNumber, 0 );
  1233. for ( int y = 0; y < img.height(); y++ )
  1234. {
  1235. for ( int x = 0; x < img.width(); x++ )
  1236. {
  1237. int cregion = regions ( x, y );
  1238. for ( int d = 0; d < dSize; d++ )
  1239. {
  1240. regionProbs[cregion][d] += getMeanProb ( x, y, d, currentfeats );
  1241. }
  1242. }
  1243. }
  1244. for ( int r = 0; r < regionNumber; r++ )
  1245. {
  1246. double maxval = regionProbs[r][0];
  1247. bestlabels[r] = 0;
  1248. for ( int d = 1; d < dSize; d++ )
  1249. {
  1250. if ( maxval < regionProbs[r][d] )
  1251. {
  1252. maxval = regionProbs[r][d];
  1253. bestlabels[r] = d;
  1254. }
  1255. }
  1256. bestlabels[r] = labelmapback[bestlabels[r]];
  1257. }
  1258. for ( int y = 0; y < img.height(); y++ )
  1259. {
  1260. for ( int x = 0; x < img.width(); x++ )
  1261. {
  1262. segresult.setPixel ( x, y, bestlabels[regions ( x,y ) ] );
  1263. }
  1264. }
  1265. #define WRITEREGIONS
  1266. #ifdef WRITEREGIONS
  1267. RegionGraph rg;
  1268. segmentation->getGraphRepresentation ( img, regions, rg );
  1269. for ( uint pos = 0; pos < regionProbs.size(); pos++ )
  1270. {
  1271. rg[pos]->setProbs ( regionProbs[pos] );
  1272. }
  1273. std::string s;
  1274. std::stringstream out;
  1275. std::vector< std::string > list;
  1276. StringTools::split ( Globals::getCurrentImgFN (), '/', list );
  1277. out << "rgout/" << list.back() << ".graph";
  1278. string writefile = out.str();
  1279. rg.write ( writefile );
  1280. #endif
  1281. }
  1282. cout << "segmentation finished" << endl;
  1283. }
  1284. void SemSegContextTree::store ( std::ostream & os, int format ) const
  1285. {
  1286. os.precision (numeric_limits<double>::digits10 + 1);
  1287. os << nbTrees << endl;
  1288. classnames.store ( os );
  1289. map<int, int>::const_iterator it;
  1290. os << labelmap.size() << endl;
  1291. for ( it = labelmap.begin() ; it != labelmap.end(); it++ )
  1292. os << ( *it ).first << " " << ( *it ).second << endl;
  1293. os << labelmapback.size() << endl;
  1294. for ( it = labelmapback.begin() ; it != labelmapback.end(); it++ )
  1295. os << ( *it ).first << " " << ( *it ).second << endl;
  1296. int trees = forest.size();
  1297. os << trees << endl;
  1298. for ( int t = 0; t < trees; t++ )
  1299. {
  1300. int nodes = forest[t].size();
  1301. os << nodes << endl;
  1302. for ( int n = 0; n < nodes; n++ )
  1303. {
  1304. os << forest[t][n].left << " " << forest[t][n].right << " " << forest[t][n].decision << " " << forest[t][n].isleaf << " " << forest[t][n].depth << " " << forest[t][n].featcounter << " " << forest[t][n].nodeNumber << endl;
  1305. os << forest[t][n].dist << endl;
  1306. if ( forest[t][n].feat == NULL )
  1307. os << -1 << endl;
  1308. else
  1309. {
  1310. os << forest[t][n].feat->getOps() << endl;
  1311. forest[t][n].feat->store ( os );
  1312. }
  1313. }
  1314. }
  1315. os << channelType.size() << endl;
  1316. for ( int i = 0; i < channelType.size(); i++ )
  1317. {
  1318. os << channelType[i] << " ";
  1319. }
  1320. os << endl;
  1321. os << integralMap.size() << endl;
  1322. for ( int i = 0; i < integralMap.size(); i++ )
  1323. {
  1324. os << integralMap[i].first << " " << integralMap[i].second << endl;
  1325. }
  1326. os << rawChannels << endl;
  1327. }
  1328. void SemSegContextTree::restore ( std::istream & is, int format )
  1329. {
  1330. is >> nbTrees;
  1331. classnames.restore ( is );
  1332. int lsize;
  1333. is >> lsize;
  1334. labelmap.clear();
  1335. for ( int l = 0; l < lsize; l++ )
  1336. {
  1337. int first, second;
  1338. is >> first;
  1339. is >> second;
  1340. labelmap[first] = second;
  1341. }
  1342. is >> lsize;
  1343. labelmapback.clear();
  1344. for ( int l = 0; l < lsize; l++ )
  1345. {
  1346. int first, second;
  1347. is >> first;
  1348. is >> second;
  1349. labelmapback[first] = second;
  1350. }
  1351. int trees;
  1352. is >> trees;
  1353. forest.clear();
  1354. for ( int t = 0; t < trees; t++ )
  1355. {
  1356. vector<TreeNode> tmptree;
  1357. forest.push_back ( tmptree );
  1358. int nodes;
  1359. is >> nodes;
  1360. //cout << "nodes: " << nodes << endl;
  1361. for ( int n = 0; n < nodes; n++ )
  1362. {
  1363. TreeNode tmpnode;
  1364. forest[t].push_back ( tmpnode );
  1365. is >> forest[t][n].left;
  1366. is >> forest[t][n].right;
  1367. is >> forest[t][n].decision;
  1368. is >> forest[t][n].isleaf;
  1369. is >> forest[t][n].depth;
  1370. is >> forest[t][n].featcounter;
  1371. is >> forest[t][n].nodeNumber;
  1372. is >> forest[t][n].dist;
  1373. int feattype;
  1374. is >> feattype;
  1375. assert ( feattype < NBOPERATIONS );
  1376. forest[t][n].feat = NULL;
  1377. if ( feattype >= 0 )
  1378. {
  1379. for ( uint o = 0; o < ops.size(); o++ )
  1380. {
  1381. for ( uint o2 = 0; o2 < ops[o].size(); o2++ )
  1382. {
  1383. if ( forest[t][n].feat == NULL )
  1384. {
  1385. for ( uint c = 0; c < ops[o].size(); c++ )
  1386. {
  1387. if ( ops[o][o2]->getOps() == feattype )
  1388. {
  1389. forest[t][n].feat = ops[o][o2]->clone();
  1390. break;
  1391. }
  1392. }
  1393. }
  1394. }
  1395. }
  1396. assert ( forest[t][n].feat != NULL );
  1397. forest[t][n].feat->restore ( is );
  1398. }
  1399. }
  1400. }
  1401. channelType.clear();
  1402. int ctsize;
  1403. is >> ctsize;
  1404. for ( int i = 0; i < ctsize; i++ )
  1405. {
  1406. int tmp;
  1407. is >> tmp;
  1408. channelType.push_back ( tmp );
  1409. }
  1410. integralMap.clear();
  1411. int iMapSize;
  1412. is >> iMapSize;
  1413. for ( int i = 0; i < iMapSize; i++ )
  1414. {
  1415. int first;
  1416. int second;
  1417. is >> first;
  1418. is >> second;
  1419. integralMap.push_back ( pair<int, int> ( first, second ) );
  1420. }
  1421. is >> rawChannels;
  1422. }