SemSegContextTree.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  1. #include "SemSegContextTree.h"
  2. #include "vislearning/baselib/Globals.h"
  3. #include "vislearning/baselib/ProgressBar.h"
  4. #include "core/basics/StringTools.h"
  5. #include "core/imagedisplay/ImageDisplay.h"
  6. #include "vislearning/cbaselib/CachedExample.h"
  7. #include "vislearning/cbaselib/PascalResults.h"
  8. #include "vislearning/baselib/ColorSpace.h"
  9. #include "segmentation/RSMeanShift.h"
  10. #include "segmentation/RSGraphBased.h"
  11. #include "segmentation/RSSlic.h"
  12. #include "core/basics/numerictools.h"
  13. #include "core/basics/StringTools.h"
  14. #include "core/basics/FileName.h"
  15. #include "vislearning/baselib/ICETools.h"
  16. #include "core/basics/Timer.h"
  17. #include "core/basics/vectorio.h"
  18. #include "core/image/FilterT.h"
  19. #include <omp.h>
  20. #include <iostream>
  21. //#define DEBUG
  22. using namespace OBJREC;
  23. using namespace std;
  24. using namespace NICE;
  25. SemSegContextTree::SemSegContextTree ( const Config *conf, const MultiDataset *md )
  26. : SemanticSegmentation ( conf, & ( md->getClassNames ( "train" ) ) )
  27. {
  28. this->conf = conf;
  29. string section = "SSContextTree";
  30. lfcw = new LFColorWeijer ( conf );
  31. firstiteration = true;
  32. maxSamples = conf->gI ( section, "max_samples", 2000 );
  33. minFeats = conf->gI ( section, "min_feats", 50 );
  34. maxDepth = conf->gI ( section, "max_depth", 10 );
  35. windowSize = conf->gI ( section, "window_size", 16 );
  36. featsPerSplit = conf->gI ( section, "feats_per_split", 200 );
  37. useShannonEntropy = conf->gB ( section, "use_shannon_entropy", true );
  38. nbTrees = conf->gI ( section, "amount_trees", 1 );
  39. string segmentationtype = conf->gS ( section, "segmentation_type", "slic" );
  40. useCategorization = conf->gB ( section, "use_categorization", false );
  41. cndir = conf->gS ( "SSContextTree", "cndir", "" );
  42. if ( useCategorization && cndir == "" )
  43. {
  44. fasthik = new GPHIKClassifierNICE ( conf );
  45. }
  46. else
  47. {
  48. fasthik = NULL;
  49. }
  50. randomTests = conf->gI ( section, "random_tests", 10 );
  51. bool saveLoadData = conf->gB ( "debug", "save_load_data", false );
  52. string fileLocation = conf->gS ( "debug", "datafile", "tmp.txt" );
  53. pixelWiseLabeling = false;
  54. useRegionFeature = conf->gB ( section, "use_region_feat", false );
  55. if ( segmentationtype == "meanshift" )
  56. segmentation = new RSMeanShift ( conf );
  57. else if ( segmentationtype == "none" )
  58. {
  59. segmentation = NULL;
  60. pixelWiseLabeling = true;
  61. useRegionFeature = false;
  62. }
  63. else if ( segmentationtype == "felzenszwalb" )
  64. segmentation = new RSGraphBased ( conf );
  65. else if ( segmentationtype == "slic" )
  66. segmentation = new RSSlic ( conf );
  67. else
  68. throw ( "no valid segmenation_type\n please choose between none, meanshift, slic and felzenszwalb\n" );
  69. ftypes = conf->gI ( section, "features", 100 );;
  70. string featsec = "Features";
  71. vector<Operation*> tops;
  72. if ( conf->gB ( featsec, "minus", true ) )
  73. tops.push_back ( new Minus() );
  74. if ( conf->gB ( featsec, "minus_abs", true ) )
  75. tops.push_back ( new MinusAbs() );
  76. if ( conf->gB ( featsec, "addition", true ) )
  77. tops.push_back ( new Addition() );
  78. if ( conf->gB ( featsec, "only1", true ) )
  79. tops.push_back ( new Only1() );
  80. if ( conf->gB ( featsec, "rel_x", true ) )
  81. tops.push_back ( new RelativeXPosition() );
  82. if ( conf->gB ( featsec, "rel_y", true ) )
  83. tops.push_back ( new RelativeYPosition() );
  84. if ( conf->gB ( featsec, "rel_z", true ) )
  85. tops.push_back ( new RelativeZPosition() );
  86. ops.push_back ( tops );
  87. tops.clear();
  88. tops.push_back ( new RegionFeat() );
  89. ops.push_back ( tops );
  90. tops.clear();
  91. if ( conf->gB ( featsec, "int", true ) )
  92. tops.push_back ( new IntegralOps() );
  93. if ( conf->gB ( featsec, "bi_int_cent", true ) )
  94. tops.push_back ( new BiIntegralCenteredOps() );
  95. if ( conf->gB ( featsec, "int_cent", true ) )
  96. tops.push_back ( new IntegralCenteredOps() );
  97. if ( conf->gB ( featsec, "haar_horz", true ) )
  98. tops.push_back ( new HaarHorizontal() );
  99. if ( conf->gB ( featsec, "haar_vert", true ) )
  100. tops.push_back ( new HaarVertical() );
  101. if ( conf->gB ( featsec, "haar_stack", true ) )
  102. tops.push_back ( new HaarStacked() );
  103. if ( conf->gB ( featsec, "haar_diagxy", true ) )
  104. tops.push_back ( new HaarDiagXY() );
  105. if ( conf->gB ( featsec, "haar_diagxz", true ) )
  106. tops.push_back ( new HaarDiagXZ() );
  107. if ( conf->gB ( featsec, "haar_diagyz", true ) )
  108. tops.push_back ( new HaarDiagYZ() );
  109. if ( conf->gB ( featsec, "haar3_horz", true ) )
  110. tops.push_back ( new Haar3Horiz() );
  111. if ( conf->gB ( featsec, "haar3_vert", true ) )
  112. tops.push_back ( new Haar3Vert() );
  113. if ( conf->gB ( featsec, "haar3_stack", true ) )
  114. tops.push_back ( new Haar3Stack() );
  115. ops.push_back ( tops );
  116. ops.push_back ( tops );
  117. tops.clear();
  118. if ( conf->gB ( featsec, "minus", true ) )
  119. tops.push_back ( new Minus() );
  120. if ( conf->gB ( featsec, "minus_abs", true ) )
  121. tops.push_back ( new MinusAbs() );
  122. if ( conf->gB ( featsec, "addition", true ) )
  123. tops.push_back ( new Addition() );
  124. if ( conf->gB ( featsec, "only1", true ) )
  125. tops.push_back ( new Only1() );
  126. if ( conf->gB ( featsec, "rel_x", true ) )
  127. tops.push_back ( new RelativeXPosition() );
  128. if ( conf->gB ( featsec, "rel_y", true ) )
  129. tops.push_back ( new RelativeYPosition() );
  130. if ( conf->gB ( featsec, "rel_z", true ) )
  131. tops.push_back ( new RelativeZPosition() );
  132. ops.push_back ( tops );
  133. useGradient = conf->gB ( featsec, "use_gradient", true );
  134. useWeijer = conf->gB ( featsec, "use_weijer", true );
  135. useGaussian = conf->gB ( featsec, "use_diff_gaussian", false );
  136. useVariance = conf->gB ( featsec, "use_variance", false );
  137. // geometric features of hoiem
  138. useHoiemFeatures = conf->gB ( featsec, "use_hoiem_features", false );
  139. if ( useHoiemFeatures )
  140. {
  141. hoiemDirectory = conf->gS ( featsec, "hoiem_directory" );
  142. }
  143. opOverview = vector<int> ( NBOPERATIONS, 0 );
  144. contextOverview = vector<vector<double> > ( maxDepth, vector<double> ( 2, 0.0 ) );
  145. calcVal.push_back ( new MCImageAccess() );
  146. calcVal.push_back ( new MCImageAccess() );
  147. calcVal.push_back ( new MCImageAccess() );
  148. calcVal.push_back ( new MCImageAccess() );
  149. calcVal.push_back ( new ClassificationResultAccess() );
  150. classnames = md->getClassNames ( "train" );
  151. ///////////////////////////////////
  152. // Train Segmentation Context Trees
  153. ///////////////////////////////////
  154. if ( saveLoadData )
  155. {
  156. if ( FileMgt::fileExists ( fileLocation ) )
  157. read ( fileLocation );
  158. else
  159. {
  160. train ( md );
  161. write ( fileLocation );
  162. }
  163. }
  164. else
  165. {
  166. train ( md );
  167. }
  168. }
  169. SemSegContextTree::~SemSegContextTree()
  170. {
  171. }
  172. double SemSegContextTree::getBestSplit ( std::vector<NICE::MultiChannelImage3DT<double> > &feats, std::vector<NICE::MultiChannelImage3DT<unsigned short int> > &currentfeats, const std::vector<NICE::MultiChannelImageT<int> > &labels, int node, Operation *&splitop, double &splitval, const int &tree, vector<vector<vector<double> > > &regionProbs )
  173. {
  174. Timer t;
  175. t.start();
  176. int imgCount = 0;
  177. try
  178. {
  179. imgCount = ( int ) feats.size();
  180. }
  181. catch ( Exception )
  182. {
  183. cerr << "no features computed?" << endl;
  184. }
  185. double bestig = -numeric_limits< double >::max();
  186. splitop = NULL;
  187. splitval = -1.0;
  188. set<vector<int> >selFeats;
  189. map<int, int> e;
  190. int featcounter = forest[tree][node].featcounter;
  191. if ( featcounter < minFeats )
  192. {
  193. return 0.0;
  194. }
  195. vector<double> fraction ( a.size(), 0.0 );
  196. for ( uint i = 0; i < fraction.size(); i++ )
  197. {
  198. if ( forbidden_classes.find ( labelmapback[i] ) != forbidden_classes.end() )
  199. fraction[i] = 0;
  200. else
  201. fraction[i] = ( ( double ) maxSamples ) / ( ( double ) featcounter * a[i] * a.size() );
  202. }
  203. featcounter = 0;
  204. for ( int iCounter = 0; iCounter < imgCount; iCounter++ )
  205. {
  206. int xsize = ( int ) currentfeats[iCounter].width();
  207. int ysize = ( int ) currentfeats[iCounter].height();
  208. int zsize = ( int ) currentfeats[iCounter].depth();
  209. for ( int x = 0; x < xsize; x++ )
  210. {
  211. for ( int y = 0; y < ysize; y++ )
  212. {
  213. for ( int z = 0; z < zsize; z++ )
  214. {
  215. if ( currentfeats[iCounter].get ( x, y, z, tree ) == node )
  216. {
  217. int cn = labels[iCounter].get ( x, y, ( uint ) z );
  218. double randD = ( double ) rand() / ( double ) RAND_MAX;
  219. if ( labelmap.find ( cn ) == labelmap.end() )
  220. continue;
  221. if ( randD < fraction[labelmap[cn]] )
  222. {
  223. vector<int> tmp ( 4, 0 );
  224. tmp[0] = iCounter;
  225. tmp[1] = x;
  226. tmp[2] = y;
  227. tmp[3] = z;
  228. featcounter++;
  229. selFeats.insert ( tmp );
  230. e[cn]++;
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. map<int, int>::iterator mapit;
  238. // global entropy
  239. double globent = 0.0;
  240. for ( mapit = e.begin() ; mapit != e.end(); mapit++ )
  241. {
  242. double p = ( double ) ( *mapit ).second / ( double ) featcounter;
  243. globent += p * log2 ( p );
  244. }
  245. globent = -globent;
  246. if ( globent < 0.5 )
  247. {
  248. return 0.0;
  249. }
  250. // vector of all possible features
  251. std::vector<Operation*> featsel;
  252. for ( int i = 0; i < featsPerSplit; i++ )
  253. {
  254. int x1, x2, y1, y2, z1, z2;
  255. int ft = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) ftypes );
  256. int tmpws = windowSize;
  257. if ( firstiteration )
  258. ft = 0;
  259. if ( channelsPerType[ft].size() == 0 )
  260. {
  261. ft = 0;
  262. }
  263. if ( ft > 1 )
  264. {
  265. //use larger window size for context features
  266. tmpws *= 2;
  267. }
  268. if ( ft == 1 )
  269. {
  270. if ( depth < 8 )
  271. {
  272. ft = 0;
  273. }
  274. }
  275. // random value range between (-window-size/2) and (window-size/2)
  276. double z_ratio = conf->gB ( "SSContextTree", "z_ratio", 1.0 );
  277. int tmp_z = ( int ) floor( (tmpws * z_ratio) + 0.5 );
  278. x1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  279. x2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  280. y1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  281. y2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  282. z1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmp_z ) - tmp_z / 2;
  283. z2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmp_z ) - tmp_z / 2;
  284. int f1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) channelsPerType[ft].size() );
  285. int f2 = f1;
  286. if ( ( double ) rand() / ( double ) RAND_MAX > 0.5 )
  287. f2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) channelsPerType[ft].size() );
  288. int o = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) ops[ft].size() );
  289. f1 = channelsPerType[ft][f1];
  290. f2 = channelsPerType[ft][f2];
  291. if ( ft == 1 )
  292. {
  293. int classes = ( int ) regionProbs[0][0].size();
  294. f2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) classes );
  295. }
  296. // only depth-values in front of the current pixel are allowed
  297. bool z_negative_only = conf->gB ( "SSContextTree", "z_negative_only", false );
  298. if (z_negative_only)
  299. {
  300. z1 = -abs(z1);
  301. z2 = -abs(z2);
  302. }
  303. Operation *op = ops[ft][o]->clone();
  304. op->set ( x1, y1, z1, x2, y2, z2, f1, f2, calcVal[ft] );
  305. op->setFeatType ( ft );
  306. if ( ft == 3 || ft == 4 )
  307. op->setContext ( true );
  308. else
  309. op->setContext ( false );
  310. featsel.push_back ( op );
  311. }
  312. for ( int f = 0; f < featsPerSplit; f++ )
  313. {
  314. double l_bestig = -numeric_limits< double >::max();
  315. double l_splitval = -1.0;
  316. set<vector<int> >::iterator it;
  317. vector<double> vals;
  318. double maxval = -numeric_limits<double>::max();
  319. double minval = numeric_limits<double>::max();
  320. for ( it = selFeats.begin() ; it != selFeats.end(); it++ )
  321. {
  322. Features feat;
  323. feat.feats = &feats[ ( *it ) [0]];
  324. feat.cfeats = &currentfeats[ ( *it ) [0]];
  325. feat.cTree = tree;
  326. feat.tree = &forest[tree];
  327. assert ( forest.size() > ( uint ) tree );
  328. assert ( forest[tree][0].dist.size() > 0 );
  329. feat.rProbs = &regionProbs[ ( *it ) [0]];
  330. double val = featsel[f]->getVal ( feat, ( *it ) [1], ( *it ) [2], ( *it ) [3] );
  331. if ( !isfinite ( val ) )
  332. {
  333. //cerr << "feat " << feat.feats->width() << " " << feat.feats->height() << " " << feat.feats->depth() << endl;
  334. //cerr << "non finite value " << val << " for " << featsel[f]->writeInfos() << endl << (*it) [1] << " " << (*it) [2] << " " << (*it) [3] << endl;
  335. val = 0.0;
  336. }
  337. vals.push_back ( val );
  338. maxval = std::max ( val, maxval );
  339. minval = std::min ( val, minval );
  340. }
  341. if ( minval == maxval )
  342. continue;
  343. double scale = maxval - minval;
  344. vector<double> splits;
  345. for ( int r = 0; r < randomTests; r++ )
  346. {
  347. splits.push_back ( ( ( double ) rand() / ( double ) RAND_MAX*scale ) + minval );
  348. }
  349. for ( int run = 0 ; run < randomTests; run++ )
  350. {
  351. set<vector<int> >::iterator it2;
  352. double val = splits[run];
  353. map<int, int> eL, eR;
  354. int counterL = 0, counterR = 0;
  355. int counter2 = 0;
  356. for ( it2 = selFeats.begin() ; it2 != selFeats.end(); it2++, counter2++ )
  357. {
  358. int cn = labels[ ( *it2 ) [0]].get ( ( *it2 ) [1], ( *it2 ) [2], ( *it2 ) [3] );
  359. //cout << "vals[counter2] " << vals[counter2] << " val: " << val << endl;
  360. if ( vals[counter2] < val )
  361. {
  362. //left entropie:
  363. eL[cn] = eL[cn] + 1;
  364. counterL++;
  365. }
  366. else
  367. {
  368. //right entropie:
  369. eR[cn] = eR[cn] + 1;
  370. counterR++;
  371. }
  372. }
  373. double leftent = 0.0;
  374. for ( mapit = eL.begin() ; mapit != eL.end(); mapit++ )
  375. {
  376. double p = ( double ) ( *mapit ).second / ( double ) counterL;
  377. leftent -= p * log2 ( p );
  378. }
  379. double rightent = 0.0;
  380. for ( mapit = eR.begin() ; mapit != eR.end(); mapit++ )
  381. {
  382. double p = ( double ) ( *mapit ).second / ( double ) counterR;
  383. rightent -= p * log2 ( p );
  384. }
  385. //cout << "rightent: " << rightent << " leftent: " << leftent << endl;
  386. double pl = ( double ) counterL / ( double ) ( counterL + counterR );
  387. double ig = globent - ( 1.0 - pl ) * rightent - pl * leftent;
  388. //double ig = globent - rightent - leftent;
  389. if ( useShannonEntropy )
  390. {
  391. double esplit = - ( pl * log ( pl ) + ( 1 - pl ) * log ( 1 - pl ) );
  392. ig = 2 * ig / ( globent + esplit );
  393. }
  394. if ( ig > l_bestig )
  395. {
  396. l_bestig = ig;
  397. l_splitval = val;
  398. }
  399. }
  400. if ( l_bestig > bestig )
  401. {
  402. bestig = l_bestig;
  403. splitop = featsel[f];
  404. splitval = l_splitval;
  405. }
  406. }
  407. //FIXME: delete all features!
  408. /*for(int i = 0; i < featsPerSplit; i++)
  409. {
  410. if(featsel[i] != splitop)
  411. delete featsel[i];
  412. }*/
  413. #ifdef DEBUG
  414. //cout << "globent: " << globent << " bestig " << bestig << " splitval: " << splitval << endl;
  415. #endif
  416. return bestig;
  417. }
  418. inline double SemSegContextTree::getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const MultiChannelImage3DT<unsigned short int> &currentfeats )
  419. {
  420. double val = 0.0;
  421. for ( int tree = 0; tree < nbTrees; tree++ )
  422. {
  423. val += forest[tree][currentfeats.get ( x,y,z,tree ) ].dist[channel];
  424. }
  425. return val / ( double ) nbTrees;
  426. }
  427. void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImage3DT<unsigned short int> &currentfeats, NICE::MultiChannelImage3DT<double> &feats, int firstChannel )
  428. {
  429. int xsize = feats.width();
  430. int ysize = feats.height();
  431. int zsize = feats.depth();
  432. if ( firstiteration )
  433. {
  434. #pragma omp parallel for
  435. for ( int it = 0; it < ( int ) integralMap.size(); it++ )
  436. {
  437. int corg = integralMap[it].first;
  438. int cint = integralMap[it].second;
  439. for ( int z = 0; z < zsize; z++ )
  440. {
  441. for ( int y = 0; y < ysize; y++ )
  442. {
  443. for ( int x = 0; x < xsize; x++ )
  444. {
  445. feats ( x, y, z, cint ) = feats ( x, y, z, corg );
  446. }
  447. }
  448. }
  449. feats.calcIntegral ( cint );
  450. }
  451. }
  452. int channels = ( int ) forest[0][0].dist.size();
  453. #pragma omp parallel for
  454. for ( int c = 0; c < channels; c++ )
  455. {
  456. feats ( 0, 0, 0, firstChannel + c ) = getMeanProb ( 0, 0, 0, c, currentfeats );
  457. //first column
  458. for ( int y = 1; y < ysize; y++ )
  459. {
  460. feats ( 0, y, 0, firstChannel + c ) = getMeanProb ( 0, y, 0, c, currentfeats )
  461. + feats ( 0, y - 1, 0, firstChannel + c );
  462. }
  463. //first row
  464. for ( int x = 1; x < xsize; x++ )
  465. {
  466. feats ( x, 0, 0, firstChannel + c ) = getMeanProb ( x, 0, 0, c, currentfeats )
  467. + feats ( x - 1, 0, 0, firstChannel + c );
  468. }
  469. //first stack
  470. for ( int z = 1; z < zsize; z++ )
  471. {
  472. feats ( 0, 0, z, firstChannel + c ) = getMeanProb ( 0, 0, z, c, currentfeats )
  473. + feats ( 0, 0, z - 1, firstChannel + c );
  474. }
  475. //x-y plane
  476. for ( int y = 1; y < ysize; y++ )
  477. {
  478. for ( int x = 1; x < xsize; x++ )
  479. {
  480. feats ( x, y, 0, firstChannel + c ) = getMeanProb ( x, y, 0, c, currentfeats )
  481. + feats ( x, y - 1, 0, firstChannel + c )
  482. + feats ( x - 1, y, 0, firstChannel + c )
  483. - feats ( x - 1, y - 1, 0, firstChannel + c );
  484. }
  485. }
  486. //y-z plane
  487. for ( int z = 1; z < zsize; z++ )
  488. {
  489. for ( int y = 1; y < ysize; y++ )
  490. {
  491. feats ( 0, y, z, firstChannel + c ) = getMeanProb ( 0, y, z, c, currentfeats )
  492. + feats ( 0, y - 1, z, firstChannel + c )
  493. + feats ( 0, y, z - 1, firstChannel + c )
  494. - feats ( 0, y - 1, z - 1, firstChannel + c );
  495. }
  496. }
  497. //x-z plane
  498. for ( int z = 1; z < zsize; z++ )
  499. {
  500. for ( int x = 1; x < xsize; x++ )
  501. {
  502. feats ( x, 0, z, firstChannel + c ) = getMeanProb ( x, 0, z, c, currentfeats )
  503. + feats ( x - 1, 0, z, firstChannel + c )
  504. + feats ( x, 0, z - 1, firstChannel + c )
  505. - feats ( x - 1, 0, z - 1, firstChannel + c );
  506. }
  507. }
  508. //rest
  509. for ( int z = 1; z < zsize; z++ )
  510. {
  511. for ( int y = 1; y < ysize; y++ )
  512. {
  513. for ( int x = 1; x < xsize; x++ )
  514. {
  515. feats ( x, y, z, firstChannel + c ) = getMeanProb ( x, y, z, c, currentfeats )
  516. + feats ( x - 1, y, z, firstChannel + c )
  517. + feats ( x, y - 1, z, firstChannel + c )
  518. + feats ( x, y, z - 1, firstChannel + c )
  519. + feats ( x - 1, y - 1, z - 1, firstChannel + c )
  520. - feats ( x - 1, y - 1, z, firstChannel + c )
  521. - feats ( x - 1, y, z - 1, firstChannel + c )
  522. - feats ( x, y - 1, z - 1, firstChannel + c );
  523. }
  524. }
  525. }
  526. }
  527. }
  528. inline double computeWeight ( const double &d, const double &dim )
  529. {
  530. return 1.0 / ( pow ( 2, ( double ) ( dim - d + 1 ) ) );
  531. }
  532. void SemSegContextTree::train ( const MultiDataset *md )
  533. {
  534. int shortsize = numeric_limits<short>::max();
  535. Timer timer;
  536. timer.start();
  537. const LabeledSet train = * ( *md ) ["train"];
  538. const LabeledSet *trainp = &train;
  539. vector<int> zsizeVec;
  540. getDepthVector ( &train, zsizeVec );
  541. bool run_3dseg = conf->gB ( "debug", "run_3dseg", true );
  542. ProgressBar pb ( "compute feats" );
  543. pb.show();
  544. //TODO: Speichefresser!, lohnt sich sparse?
  545. vector<MultiChannelImage3DT<double> > allfeats;
  546. vector<MultiChannelImage3DT<unsigned short int> > currentfeats;
  547. vector<MultiChannelImageT<int> > labels;
  548. vector<SparseVector*> globalCategorFeats;
  549. vector<map<int,int> > classesPerImage;
  550. std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
  551. vector<vector<vector<double> > > regionProbs;
  552. vector<vector<int> > rSize;
  553. vector<int> amountRegionpI;
  554. if ( forbidden_classes_s == "" )
  555. {
  556. forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  557. }
  558. classnames.getSelection ( forbidden_classes_s, forbidden_classes );
  559. int imgcounter = 0;
  560. int amountPixels = 0;
  561. ////////////////////////////////////////////////////
  562. //define which featurextraction methods should be used for each channel
  563. if ( imagetype == IMAGETYPE_RGB )
  564. {
  565. rawChannels = 3;
  566. }
  567. else
  568. {
  569. rawChannels = 1;
  570. }
  571. // how many channels without integral image
  572. int shift = 0;
  573. if ( useGradient )
  574. rawChannels *= 2;
  575. if ( useWeijer )
  576. rawChannels += 11;
  577. if ( useHoiemFeatures )
  578. rawChannels += 8;
  579. if ( useGaussian )
  580. rawChannels += 1;
  581. if ( useVariance )
  582. rawChannels += 1;
  583. // gray value images
  584. for ( int i = 0; i < rawChannels; i++ )
  585. {
  586. channelType.push_back ( 0 );
  587. }
  588. // regions
  589. if ( useRegionFeature )
  590. {
  591. channelType.push_back ( 1 );
  592. shift++;
  593. }
  594. ///////////////////////////// read input data /////////////////////////////////
  595. ///////////////////////////////////////////////////////////////////////////////
  596. int depthCount = 0;
  597. vector< string > filelist;
  598. NICE::MultiChannelImageT<uchar> pixelLabels;
  599. LOOP_ALL_S ( *trainp )
  600. {
  601. EACH_INFO ( classno, info );
  602. std::string file = info.img();
  603. filelist.push_back ( file );
  604. depthCount++;
  605. const LocalizationResult *locResult = info.localization();
  606. // getting groundtruth
  607. NICE::Image pL;
  608. pL.resize ( locResult->xsize, locResult->ysize );
  609. pL.set ( 0 );
  610. locResult->calcLabeledImage ( pL, ( *classNames ).getBackgroundClass() );
  611. pixelLabels.addChannel ( pL );
  612. if ( locResult->size() <= 0 )
  613. {
  614. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  615. file.c_str() );
  616. continue;
  617. }
  618. fprintf ( stderr, "SSContext: Collecting pixel examples from localization info: %s\n", file.c_str() );
  619. int depthBoundary = 0;
  620. if ( run_3dseg )
  621. {
  622. depthBoundary = zsizeVec[imgcounter];
  623. }
  624. if ( depthCount < depthBoundary ) continue;
  625. // all image slices collected -> make a 3d image
  626. NICE::MultiChannelImage3DT<double> imgData;
  627. make3DImage ( filelist, imgData );
  628. int xsize = imgData.width();
  629. int ysize = imgData.height();
  630. int zsize = imgData.depth();
  631. amountPixels += xsize * ysize * zsize;
  632. MultiChannelImageT<int> tmpMat ( xsize, ysize, ( uint ) zsize );
  633. labels.push_back ( tmpMat );
  634. currentfeats.push_back ( MultiChannelImage3DT<unsigned short int> ( xsize, ysize, zsize, nbTrees ) );
  635. currentfeats[imgcounter].setAll ( 0 );
  636. //TODO: resize image?!
  637. MultiChannelImage3DT<double> feats;
  638. allfeats.push_back ( feats );
  639. int amountRegions;
  640. // read image and do some simple transformations
  641. extractBasicFeatures ( allfeats[imgcounter], imgData, filelist, amountRegions );
  642. if ( useRegionFeature )
  643. {
  644. amountRegionpI.push_back ( amountRegions );
  645. rSize.push_back ( vector<int> ( amountRegions, 0 ) );
  646. for ( int z = 0; z < zsize; z++ )
  647. {
  648. for ( int y = 0; y < ysize; y++ )
  649. {
  650. for ( int x = 0; x < xsize; x++ )
  651. {
  652. rSize[imgcounter][allfeats[imgcounter] ( x, y, z, rawChannels ) ]++;
  653. }
  654. }
  655. }
  656. }
  657. for ( int x = 0; x < xsize; x++ )
  658. {
  659. for ( int y = 0; y < ysize; y++ )
  660. {
  661. for ( int z = 0; z < zsize; z++ )
  662. {
  663. if ( run_3dseg )
  664. classno = pixelLabels ( x, y, ( uint ) z );
  665. else
  666. classno = pL.getPixelQuick ( x,y );
  667. labels[imgcounter].set ( x, y, classno, ( uint ) z );
  668. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  669. continue;
  670. labelcounter[classno]++;
  671. }
  672. }
  673. }
  674. if ( useCategorization )
  675. {
  676. globalCategorFeats.push_back ( new SparseVector() );
  677. classesPerImage.push_back ( map<int,int>() );
  678. for ( int x = 0; x < xsize; x++ )
  679. {
  680. for ( int y = 0; y < ysize; y++ )
  681. {
  682. for ( int z = 0; z < zsize; z++ )
  683. {
  684. if ( run_3dseg )
  685. classno = pixelLabels ( x, y, ( uint ) z );
  686. else
  687. classno = pL.getPixelQuick ( x,y );
  688. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  689. continue;
  690. classesPerImage[imgcounter][classno] = 1;
  691. }
  692. }
  693. }
  694. }
  695. pb.update ( trainp->count() );
  696. filelist.clear();
  697. pixelLabels.reInit ( 0,0,0 );
  698. depthCount = 0;
  699. imgcounter++;
  700. }
  701. pb.hide();
  702. map<int, int>::iterator mapit;
  703. int classes = 0;
  704. for ( mapit = labelcounter.begin(); mapit != labelcounter.end(); mapit++ )
  705. {
  706. labelmap[mapit->first] = classes;
  707. labelmapback[classes] = mapit->first;
  708. classes++;
  709. }
  710. //////////////////////////// channel configuration ////////////////////////////
  711. ///////////////////////////////////////////////////////////////////////////////
  712. for ( int i = 0; i < rawChannels; i++ )
  713. {
  714. channelType.push_back ( 2 );
  715. }
  716. // integral images
  717. for ( int i = 0; i < classes; i++ )
  718. {
  719. channelType.push_back ( 3 );
  720. }
  721. integralMap.clear();
  722. for ( int i = 0; i < rawChannels; i++ )
  723. {
  724. integralMap.push_back ( pair<int, int> ( i, i + rawChannels + shift ) );
  725. }
  726. int amountTypes = 5;
  727. channelsPerType = vector<vector<int> > ( amountTypes, vector<int>() );
  728. for ( int i = 0; i < ( int ) channelType.size(); i++ )
  729. {
  730. channelsPerType[channelType[i]].push_back ( i );
  731. }
  732. for ( int i = 0; i < classes; i++ )
  733. {
  734. channelsPerType[channelsPerType.size()-1].push_back ( i );
  735. }
  736. ftypes = std::min ( amountTypes, ftypes );
  737. ///////////////////////////////////////////////////////////////////////////////
  738. ///////////////////////////////////////////////////////////////////////////////
  739. if ( useRegionFeature )
  740. {
  741. for ( int a = 0; a < ( int ) amountRegionpI.size(); a++ )
  742. {
  743. regionProbs.push_back ( vector<vector<double> > ( amountRegionpI[a], vector<double> ( classes, 0.0 ) ) );
  744. }
  745. }
  746. //balancing
  747. int featcounter = 0;
  748. a = vector<double> ( classes, 0.0 );
  749. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  750. {
  751. int xsize = ( int ) currentfeats[iCounter].width();
  752. int ysize = ( int ) currentfeats[iCounter].height();
  753. int zsize = ( int ) currentfeats[iCounter].depth();
  754. for ( int x = 0; x < xsize; x++ )
  755. {
  756. for ( int y = 0; y < ysize; y++ )
  757. {
  758. for ( int z = 0; z < zsize; z++ )
  759. {
  760. featcounter++;
  761. int cn = labels[iCounter] ( x, y, ( uint ) z );
  762. if ( labelmap.find ( cn ) == labelmap.end() )
  763. continue;
  764. a[labelmap[cn]] ++;
  765. }
  766. }
  767. }
  768. }
  769. for ( int i = 0; i < ( int ) a.size(); i++ )
  770. {
  771. a[i] /= ( double ) featcounter;
  772. }
  773. #ifdef DEBUG
  774. for ( int i = 0; i < ( int ) a.size(); i++ )
  775. {
  776. cout << "a[" << i << "]: " << a[i] << endl;
  777. }
  778. cout << "a.size: " << a.size() << endl;
  779. #endif
  780. depth = 0;
  781. uniquenumber = 0;
  782. for ( int t = 0; t < nbTrees; t++ )
  783. {
  784. vector<TreeNode> singletree;
  785. singletree.push_back ( TreeNode() );
  786. singletree[0].dist = vector<double> ( classes, 0.0 );
  787. singletree[0].depth = depth;
  788. singletree[0].featcounter = amountPixels;
  789. singletree[0].nodeNumber = uniquenumber;
  790. uniquenumber++;
  791. forest.push_back ( singletree );
  792. }
  793. vector<int> startnode ( nbTrees, 0 );
  794. bool allleaf = false;
  795. //int baseFeatSize = allfeats[0].size();
  796. timer.stop();
  797. cerr << "preprocessing finished in: " << timer.getLastAbsolute() << " seconds" << endl;
  798. timer.start();
  799. while ( !allleaf && depth < maxDepth )
  800. {
  801. depth++;
  802. #ifdef DEBUG
  803. cout << "depth: " << depth << endl;
  804. #endif
  805. allleaf = true;
  806. vector<MultiChannelImage3DT<unsigned short int> > lastfeats = currentfeats;
  807. vector<vector<vector<double> > > lastRegionProbs = regionProbs;
  808. if ( useRegionFeature )
  809. {
  810. int a_max = ( int ) regionProbs.size();
  811. for ( int a = 0; a < a_max; a++ )
  812. {
  813. int b_max = ( int ) regionProbs[a].size();
  814. for ( int b = 0; b < b_max; b++ )
  815. {
  816. int c_max = ( int ) regionProbs[a][b].size();
  817. for ( int c = 0; c < c_max; c++ )
  818. {
  819. regionProbs[a][b][c] = 0.0;
  820. }
  821. }
  822. }
  823. }
  824. #if 1
  825. Timer timerDepth;
  826. timerDepth.start();
  827. #endif
  828. double weight = computeWeight ( depth, maxDepth ) - computeWeight ( depth - 1, maxDepth );
  829. if ( depth == 1 )
  830. {
  831. weight = computeWeight ( 1, maxDepth );
  832. }
  833. // omp_set_dynamic(0);
  834. #pragma omp parallel for
  835. for ( int tree = 0; tree < nbTrees; tree++ )
  836. {
  837. const int t = ( int ) forest[tree].size();
  838. const int s = startnode[tree];
  839. startnode[tree] = t;
  840. //#pragma omp parallel for
  841. for ( int i = s; i < t; i++ )
  842. {
  843. if ( !forest[tree][i].isleaf && forest[tree][i].left < 0 )
  844. {
  845. Operation *splitfeat = NULL;
  846. double splitval;
  847. double bestig = getBestSplit ( allfeats, lastfeats, labels, i, splitfeat, splitval, tree, lastRegionProbs );
  848. for ( int ii = 0; ii < ( int ) lastfeats.size(); ii++ )
  849. {
  850. for ( int c = 0; c < lastfeats[ii].channels(); c++ )
  851. {
  852. short unsigned int minv, maxv;
  853. lastfeats[ii].statistics ( minv, maxv, c );
  854. }
  855. }
  856. forest[tree][i].feat = splitfeat;
  857. forest[tree][i].decision = splitval;
  858. if ( splitfeat != NULL )
  859. {
  860. allleaf = false;
  861. int left;
  862. #pragma omp critical
  863. {
  864. left = forest[tree].size();
  865. forest[tree].push_back ( TreeNode() );
  866. forest[tree].push_back ( TreeNode() );
  867. }
  868. int right = left + 1;
  869. forest[tree][i].left = left;
  870. forest[tree][i].right = right;
  871. forest[tree][left].dist = vector<double> ( classes, 0.0 );
  872. forest[tree][right].dist = vector<double> ( classes, 0.0 );
  873. forest[tree][left].depth = depth;
  874. forest[tree][right].depth = depth;
  875. forest[tree][left].featcounter = 0;
  876. forest[tree][right].featcounter = 0;
  877. forest[tree][left].nodeNumber = uniquenumber;
  878. int leftu = uniquenumber;
  879. uniquenumber++;
  880. forest[tree][right].nodeNumber = uniquenumber;
  881. int rightu = uniquenumber;
  882. uniquenumber++;
  883. forest[tree][right].featcounter = 0;
  884. #pragma omp parallel for
  885. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  886. {
  887. int xsize = currentfeats[iCounter].width();
  888. int ysize = currentfeats[iCounter].height();
  889. int zsize = currentfeats[iCounter].depth();
  890. for ( int x = 0; x < xsize; x++ )
  891. {
  892. for ( int y = 0; y < ysize; y++ )
  893. {
  894. for ( int z = 0; z < zsize; z++ )
  895. {
  896. if ( currentfeats[iCounter].get ( x, y, z, tree ) == i )
  897. {
  898. Features feat;
  899. feat.feats = &allfeats[iCounter];
  900. feat.cfeats = &lastfeats[iCounter];
  901. feat.cTree = tree;
  902. feat.tree = &forest[tree];
  903. feat.rProbs = &lastRegionProbs[iCounter];
  904. double val = splitfeat->getVal ( feat, x, y, z );
  905. if ( !isfinite ( val ) )
  906. {
  907. val = 0.0;
  908. }
  909. #pragma omp critical
  910. if ( val < splitval )
  911. {
  912. currentfeats[iCounter].set ( x, y, z, left, tree );
  913. if ( labelmap.find ( labels[iCounter] ( x, y, ( uint ) z ) ) != labelmap.end() )
  914. forest[tree][left].dist[labelmap[labels[iCounter] ( x, y, ( uint ) z ) ]]++;
  915. forest[tree][left].featcounter++;
  916. if ( useCategorization && leftu < shortsize )
  917. ( *globalCategorFeats[iCounter] ) [leftu]+=weight;
  918. }
  919. else
  920. {
  921. currentfeats[iCounter].set ( x, y, z, right, tree );
  922. if ( labelmap.find ( labels[iCounter] ( x, y, ( uint ) z ) ) != labelmap.end() )
  923. forest[tree][right].dist[labelmap[labels[iCounter] ( x, y, ( uint ) z ) ]]++;
  924. forest[tree][right].featcounter++;
  925. if ( useCategorization && rightu < shortsize )
  926. ( *globalCategorFeats[iCounter] ) [rightu]+=weight;
  927. }
  928. }
  929. }
  930. }
  931. }
  932. }
  933. double lcounter = 0.0, rcounter = 0.0;
  934. for ( uint d = 0; d < forest[tree][left].dist.size(); d++ )
  935. {
  936. if ( forbidden_classes.find ( labelmapback[d] ) != forbidden_classes.end() )
  937. {
  938. forest[tree][left].dist[d] = 0;
  939. forest[tree][right].dist[d] = 0;
  940. }
  941. else
  942. {
  943. forest[tree][left].dist[d] /= a[d];
  944. lcounter += forest[tree][left].dist[d];
  945. forest[tree][right].dist[d] /= a[d];
  946. rcounter += forest[tree][right].dist[d];
  947. }
  948. }
  949. if ( lcounter <= 0 || rcounter <= 0 )
  950. {
  951. cout << "lcounter : " << lcounter << " rcounter: " << rcounter << endl;
  952. cout << "splitval: " << splitval << " splittype: " << splitfeat->writeInfos() << endl;
  953. cout << "bestig: " << bestig << endl;
  954. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  955. {
  956. int xsize = currentfeats[iCounter].width();
  957. int ysize = currentfeats[iCounter].height();
  958. int zsize = currentfeats[iCounter].depth();
  959. int counter = 0;
  960. for ( int x = 0; x < xsize; x++ )
  961. {
  962. for ( int y = 0; y < ysize; y++ )
  963. {
  964. for ( int z = 0; z < zsize; z++ )
  965. {
  966. if ( lastfeats[iCounter].get ( x, y, tree ) == i )
  967. {
  968. if ( ++counter > 30 )
  969. break;
  970. Features feat;
  971. feat.feats = &allfeats[iCounter];
  972. feat.cfeats = &lastfeats[iCounter];
  973. feat.cTree = tree;
  974. feat.tree = &forest[tree];
  975. feat.rProbs = &lastRegionProbs[iCounter];
  976. double val = splitfeat->getVal ( feat, x, y, z );
  977. if ( !isfinite ( val ) )
  978. {
  979. val = 0.0;
  980. }
  981. cout << "splitval: " << splitval << " val: " << val << endl;
  982. }
  983. }
  984. }
  985. }
  986. }
  987. assert ( lcounter > 0 && rcounter > 0 );
  988. }
  989. for ( uint d = 0; d < forest[tree][left].dist.size(); d++ )
  990. {
  991. forest[tree][left].dist[d] /= lcounter;
  992. forest[tree][right].dist[d] /= rcounter;
  993. }
  994. }
  995. else
  996. {
  997. forest[tree][i].isleaf = true;
  998. }
  999. }
  1000. }
  1001. }
  1002. if ( useRegionFeature )
  1003. {
  1004. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  1005. {
  1006. int xsize = currentfeats[iCounter].width();
  1007. int ysize = currentfeats[iCounter].height();
  1008. int zsize = currentfeats[iCounter].depth();
  1009. #pragma omp parallel for
  1010. for ( int x = 0; x < xsize; x++ )
  1011. {
  1012. for ( int y = 0; y < ysize; y++ )
  1013. {
  1014. for ( int z = 0; z < zsize; z++ )
  1015. {
  1016. for ( int tree = 0; tree < nbTrees; tree++ )
  1017. {
  1018. int node = currentfeats[iCounter].get ( x, y, z, tree );
  1019. for ( uint d = 0; d < forest[tree][node].dist.size(); d++ )
  1020. {
  1021. regionProbs[iCounter][ ( int ) ( allfeats[iCounter] ( x, y, z, rawChannels ) ) ][d] += forest[tree][node].dist[d];
  1022. }
  1023. }
  1024. }
  1025. }
  1026. }
  1027. }
  1028. int a_max = ( int ) regionProbs.size();
  1029. for ( int a = 0; a < a_max; a++ )
  1030. {
  1031. int b_max = ( int ) regionProbs[a].size();
  1032. for ( int b = 0; b < b_max; b++ )
  1033. {
  1034. int c_max = ( int ) regionProbs[a][b].size();
  1035. for ( int c = 0; c < c_max; c++ )
  1036. {
  1037. regionProbs[a][b][c] /= ( double ) ( rSize[a][b] );
  1038. }
  1039. }
  1040. }
  1041. }
  1042. //compute integral images
  1043. if ( firstiteration )
  1044. {
  1045. for ( int i = 0; i < imgcounter; i++ )
  1046. {
  1047. allfeats[i].addChannel ( ( int ) ( classes + rawChannels ) );
  1048. }
  1049. }
  1050. for ( int i = 0; i < imgcounter; i++ )
  1051. {
  1052. computeIntegralImage ( currentfeats[i], allfeats[i], channelType.size() - classes );
  1053. }
  1054. if ( firstiteration )
  1055. {
  1056. firstiteration = false;
  1057. }
  1058. #if 1
  1059. timerDepth.stop();
  1060. cout << "time for depth " << depth << ": " << timerDepth.getLastAbsolute() << endl;
  1061. #endif
  1062. lastfeats.clear();
  1063. lastRegionProbs.clear();
  1064. }
  1065. timer.stop();
  1066. cerr << "learning finished in: " << timer.getLastAbsolute() << " seconds" << endl;
  1067. timer.start();
  1068. cout << "uniquenumber " << uniquenumber << endl;
  1069. if ( useCategorization && fasthik != NULL )
  1070. {
  1071. uniquenumber = std::min ( shortsize, uniquenumber );
  1072. for ( uint i = 0; i < globalCategorFeats.size(); i++ )
  1073. {
  1074. globalCategorFeats[i]->setDim ( uniquenumber );
  1075. globalCategorFeats[i]->normalize();
  1076. }
  1077. map<int,Vector> ys;
  1078. int cCounter = 0;
  1079. for ( map<int,int>::iterator it = labelmap.begin(); it != labelmap.end(); it++, cCounter++ )
  1080. {
  1081. ys[cCounter] = Vector ( globalCategorFeats.size() );
  1082. for ( int i = 0; i < imgcounter; i++ )
  1083. {
  1084. if ( classesPerImage[i].find ( it->first ) != classesPerImage[i].end() )
  1085. {
  1086. ys[cCounter][i] = 1;
  1087. }
  1088. else
  1089. {
  1090. ys[cCounter][i] = -1;
  1091. }
  1092. }
  1093. }
  1094. fasthik->train ( globalCategorFeats, ys );
  1095. }
  1096. #ifdef DEBUG
  1097. for ( int tree = 0; tree < nbTrees; tree++ )
  1098. {
  1099. int t = ( int ) forest[tree].size();
  1100. for ( int i = 0; i < t; i++ )
  1101. {
  1102. printf ( "tree[%i]: left: %i, right: %i", i, forest[tree][i].left, forest[tree][i].right );
  1103. if ( !forest[tree][i].isleaf && forest[tree][i].left != -1 )
  1104. {
  1105. cout << ", feat: " << forest[tree][i].feat->writeInfos() << " ";
  1106. opOverview[forest[tree][i].feat->getOps() ]++;
  1107. contextOverview[forest[tree][i].depth][ ( int ) forest[tree][i].feat->getContext() ]++;
  1108. }
  1109. for ( int d = 0; d < ( int ) forest[tree][i].dist.size(); d++ )
  1110. {
  1111. cout << " " << forest[tree][i].dist[d];
  1112. }
  1113. cout << endl;
  1114. }
  1115. }
  1116. std::map<int, int> featTypeCounter;
  1117. for ( int tree = 0; tree < nbTrees; tree++ )
  1118. {
  1119. int t = ( int ) forest[tree].size();
  1120. for ( int i = 0; i < t; i++ )
  1121. {
  1122. if ( !forest[tree][i].isleaf && forest[tree][i].left != -1 )
  1123. {
  1124. featTypeCounter[forest[tree][i].feat->getFeatType() ] += 1;
  1125. }
  1126. }
  1127. }
  1128. cout << "evaluation of featuretypes" << endl;
  1129. for ( map<int, int>::const_iterator it = featTypeCounter.begin(); it != featTypeCounter.end(); it++ )
  1130. {
  1131. cerr << it->first << ": " << it->second << endl;
  1132. }
  1133. for ( uint c = 0; c < ops.size(); c++ )
  1134. {
  1135. for ( int t = 0; t < ( int ) ops[c].size(); t++ )
  1136. {
  1137. cout << ops[c][t]->writeInfos() << ": " << opOverview[ops[c][t]->getOps() ] << endl;
  1138. }
  1139. }
  1140. for ( int d = 0; d < maxDepth; d++ )
  1141. {
  1142. double sum = contextOverview[d][0] + contextOverview[d][1];
  1143. if ( sum == 0 )
  1144. sum = 1;
  1145. contextOverview[d][0] /= sum;
  1146. contextOverview[d][1] /= sum;
  1147. cout << "depth: " << d << " woContext: " << contextOverview[d][0] << " wContext: " << contextOverview[d][1] << endl;
  1148. }
  1149. #endif
  1150. timer.stop();
  1151. cerr << "rest finished in: " << timer.getLastAbsolute() << " seconds" << endl;
  1152. timer.start();
  1153. }
  1154. void SemSegContextTree::extractBasicFeatures ( NICE::MultiChannelImage3DT<double> &feats, const NICE::MultiChannelImage3DT<double> &imgData, const vector<string> &filelist, int &amountRegions )
  1155. {
  1156. int xsize = imgData.width();
  1157. int ysize = imgData.height();
  1158. int zsize = imgData.depth();
  1159. //TODO: resize image?!
  1160. amountRegions = 0;
  1161. feats.reInit ( xsize, ysize, zsize, imgData.channels() );
  1162. feats.setAll ( 0 );
  1163. for ( int z = 0; z < zsize; z++ )
  1164. {
  1165. NICE::MultiChannelImageT<double> feats_tmp;
  1166. feats_tmp.reInit ( xsize, ysize, 3 );
  1167. if ( imagetype == IMAGETYPE_RGB )
  1168. {
  1169. NICE::ColorImage img = imgData.getColor ( z );
  1170. for ( int x = 0; x < xsize; x++ )
  1171. {
  1172. for ( int y = 0; y < ysize; y++ )
  1173. {
  1174. for ( int r = 0; r < 3; r++ )
  1175. {
  1176. feats_tmp.set ( x, y, img.getPixel ( x, y, r ), ( uint ) r );
  1177. }
  1178. }
  1179. }
  1180. }
  1181. else
  1182. {
  1183. NICE::ImageT<double> img = imgData.getChannelT ( z,0 );
  1184. for ( int x = 0; x < xsize; x++ )
  1185. {
  1186. for ( int y = 0; y < ysize; y++ )
  1187. {
  1188. feats_tmp.set ( x, y, img.getPixel ( x, y ), 0 );
  1189. }
  1190. }
  1191. }
  1192. if ( imagetype == IMAGETYPE_RGB )
  1193. feats_tmp = ColorSpace::rgbtolab ( feats_tmp );
  1194. for ( int x = 0; x < xsize; x++ )
  1195. {
  1196. for ( int y = 0; y < ysize; y++ )
  1197. {
  1198. if ( imagetype == IMAGETYPE_RGB )
  1199. {
  1200. for ( uint r = 0; r < 3; r++ )
  1201. {
  1202. feats.set ( x, y, z, feats_tmp.get ( x, y, r ), r );
  1203. }
  1204. }
  1205. else
  1206. {
  1207. feats.set ( x, y, z, feats_tmp.get ( x, y, 0 ), 0 );
  1208. }
  1209. }
  1210. }
  1211. if ( useGradient )
  1212. {
  1213. int currentsize = feats_tmp.channels();
  1214. feats_tmp.addChannel ( currentsize );
  1215. for ( int c = 0; c < currentsize; c++ )
  1216. {
  1217. ImageT<double> tmp = feats_tmp[c];
  1218. ImageT<double> tmp2 = feats_tmp[c+currentsize];
  1219. NICE::FilterT<double, double, double>::gradientStrength ( tmp, tmp2 );
  1220. }
  1221. }
  1222. if ( useWeijer )
  1223. {
  1224. if ( imagetype == IMAGETYPE_RGB )
  1225. {
  1226. NICE::ColorImage img = imgData.getColor ( z );
  1227. NICE::MultiChannelImageT<double> cfeats;
  1228. lfcw->getFeats ( img, cfeats );
  1229. feats_tmp.addChannel ( cfeats );
  1230. }
  1231. else
  1232. {
  1233. cerr << "Can't compute weijer features of a grayscale image." << endl;
  1234. }
  1235. }
  1236. if ( useGaussian )
  1237. {
  1238. vector<string> list;
  1239. StringTools::split ( filelist[z], '/', list );
  1240. string gaussPath = StringTools::trim ( filelist[z], list.back() ) + "gaussmap/" + list.back();
  1241. NICE::Image gauss ( gaussPath );
  1242. feats_tmp.addChannel ( gauss );
  1243. //cout << "Added file " << gaussPath << " to feature stack " << endl;
  1244. }
  1245. // read the geometric cues produced by Hoiem et al.
  1246. if ( useHoiemFeatures )
  1247. {
  1248. // we could also give the following set as a config option
  1249. string hoiemClasses_s = "sky 000 090-045 090-090 090-135 090 090-por 090-sol";
  1250. vector<string> hoiemClasses;
  1251. StringTools::split ( hoiemClasses_s, ' ', hoiemClasses );
  1252. // Now we have to do some fancy regular expressions :)
  1253. // Original image filename: basel_000083.jpg
  1254. // hoiem result: basel_000083_c_sky.png
  1255. // Fancy class of Ferid which supports string handling especially for filenames
  1256. FileName fn ( filelist[z] );
  1257. fn.removeExtension();
  1258. FileName fnBase = fn.extractFileName();
  1259. // counter for the channel index, starts with the current size of the destination multi-channel image
  1260. int currentChannel = feats_tmp.channels();
  1261. // add a channel for each feature in advance
  1262. feats_tmp.addChannel ( hoiemClasses.size() );
  1263. // loop through all geometric categories and add the images
  1264. for ( vector<string>::const_iterator i = hoiemClasses.begin(); i != hoiemClasses.end(); i++, currentChannel++ )
  1265. {
  1266. string hoiemClass = *i;
  1267. FileName fnConfidenceImage ( hoiemDirectory + fnBase.str() + "_c_" + hoiemClass + ".png" );
  1268. if ( ! fnConfidenceImage.fileExists() )
  1269. {
  1270. fthrow ( Exception, "Unable to read the Hoiem geometric confidence image: " << fnConfidenceImage.str() << " (original image is " << filelist[z] << ")" );
  1271. }
  1272. else
  1273. {
  1274. Image confidenceImage ( fnConfidenceImage.str() );
  1275. // check whether the image size is consistent
  1276. if ( confidenceImage.width() != feats_tmp.width() || confidenceImage.height() != feats_tmp.height() )
  1277. {
  1278. fthrow ( Exception, "The size of the geometric confidence image does not match with the original image size: " << fnConfidenceImage.str() );
  1279. }
  1280. ImageT<double> dst = feats_tmp[currentChannel];
  1281. // copy standard image to double image
  1282. for ( uint y = 0 ; y < ( uint ) confidenceImage.height(); y++ )
  1283. for ( uint x = 0 ; x < ( uint ) confidenceImage.width(); x++ )
  1284. feats_tmp ( x, y, currentChannel ) = ( double ) confidenceImage ( x, y );
  1285. }
  1286. }
  1287. }
  1288. uint oldChannels = feats.channels();
  1289. if ( feats.channels() < feats_tmp.channels() )
  1290. feats.addChannel ( feats_tmp.channels()-feats.channels() );
  1291. for ( int x = 0; x < xsize; x++ )
  1292. {
  1293. for ( int y = 0; y < ysize; y++ )
  1294. {
  1295. for ( uint r = oldChannels; r < ( uint ) feats_tmp.channels(); r++ )
  1296. {
  1297. feats.set ( x, y, z, feats_tmp.get ( x, y, r ), r );
  1298. }
  1299. }
  1300. }
  1301. }
  1302. if ( useRegionFeature )
  1303. {
  1304. //using segmentation
  1305. MultiChannelImageT<int> regions;
  1306. regions.reInit( xsize, ysize, zsize );
  1307. vector<int> chanSelect;
  1308. for ( int i=0; i<3; i++ )
  1309. chanSelect.push_back ( i );
  1310. amountRegions = segmentation->segRegions ( imgData, regions, chanSelect );
  1311. int cchannel = feats.channels();
  1312. feats.addChannel ( 1 );
  1313. for ( int z = 0; z < ( int ) regions.channels(); z++ )
  1314. {
  1315. for ( int y = 0; y < regions.height(); y++ )
  1316. {
  1317. for ( int x = 0; x < regions.width(); x++ )
  1318. {
  1319. feats.set ( x, y, z, regions ( x, y, ( uint ) z ), cchannel );
  1320. }
  1321. }
  1322. }
  1323. }
  1324. if ( useVariance )
  1325. {
  1326. int cchannel = feats.channels();
  1327. if (imagetype = IMAGETYPE_RGB)
  1328. {
  1329. feats.addChannel( 3 );
  1330. for (int c = 0; c < 3; c++)
  1331. {
  1332. feats.calcVariance( c, cchannel+c );
  1333. }
  1334. }
  1335. else
  1336. {
  1337. feats.addChannel( 1 );
  1338. feats.calcVariance( 0, cchannel );
  1339. }
  1340. }
  1341. }
  1342. void SemSegContextTree::semanticseg ( NICE::MultiChannelImage3DT<double> & imgData,
  1343. NICE::MultiChannelImageT<double> & segresult,
  1344. NICE::MultiChannelImage3DT<double> & probabilities,
  1345. const std::vector<std::string> & filelist )
  1346. {
  1347. int xsize = imgData.width();
  1348. int ysize = imgData.height();
  1349. int zsize = imgData.depth();
  1350. firstiteration = true;
  1351. int classes = labelmapback.size();
  1352. int numClasses = classNames->numClasses();
  1353. fprintf ( stderr, "ContextTree classification !\n" );
  1354. probabilities.reInit ( xsize, ysize, zsize, numClasses );
  1355. probabilities.setAll ( 0 );
  1356. SparseVector *globalCategorFeat = new SparseVector();
  1357. MultiChannelImage3DT<double> feats;
  1358. // Basic Features
  1359. int amountRegions;
  1360. extractBasicFeatures ( feats, imgData, filelist, amountRegions ); //read image and do some simple transformations
  1361. vector<int> rSize;
  1362. if ( useRegionFeature )
  1363. {
  1364. rSize = vector<int> ( amountRegions, 0 );
  1365. for ( int z = 0; z < zsize; z++ )
  1366. {
  1367. for ( int y = 0; y < ysize; y++ )
  1368. {
  1369. for ( int x = 0; x < xsize; x++ )
  1370. {
  1371. rSize[feats ( x, y, z, rawChannels ) ]++;
  1372. }
  1373. }
  1374. }
  1375. }
  1376. bool allleaf = false;
  1377. MultiChannelImage3DT<unsigned short int> currentfeats ( xsize, ysize, zsize, nbTrees );
  1378. currentfeats.setAll ( 0 );
  1379. depth = 0;
  1380. vector<vector<double> > regionProbs;
  1381. if ( useRegionFeature )
  1382. {
  1383. regionProbs = vector<vector<double> > ( amountRegions, vector<double> ( classes, 0.0 ) );
  1384. }
  1385. for ( int d = 0; d < maxDepth && !allleaf; d++ )
  1386. {
  1387. depth++;
  1388. vector<vector<double> > lastRegionProbs = regionProbs;
  1389. if ( useRegionFeature )
  1390. {
  1391. int b_max = ( int ) regionProbs.size();
  1392. for ( int b = 0; b < b_max; b++ )
  1393. {
  1394. int c_max = ( int ) regionProbs[b].size();
  1395. for ( int c = 0; c < c_max; c++ )
  1396. {
  1397. regionProbs[b][c] = 0.0;
  1398. }
  1399. }
  1400. }
  1401. double weight = computeWeight ( depth, maxDepth ) - computeWeight ( depth - 1, maxDepth );
  1402. if ( depth == 1 )
  1403. {
  1404. weight = computeWeight ( 1, maxDepth );
  1405. }
  1406. allleaf = true;
  1407. MultiChannelImage3DT<unsigned short int> lastfeats = currentfeats;
  1408. int tree;
  1409. #pragma omp parallel for private(tree)
  1410. for ( tree = 0; tree < nbTrees; tree++ )
  1411. {
  1412. for ( int x = 0; x < xsize; x++ )
  1413. {
  1414. for ( int y = 0; y < ysize; y++ )
  1415. {
  1416. for ( int z = 0; z < zsize; z++ )
  1417. {
  1418. int t = currentfeats.get ( x, y, z, tree );
  1419. if ( forest[tree][t].left > 0 )
  1420. {
  1421. allleaf = false;
  1422. Features feat;
  1423. feat.feats = &feats;
  1424. feat.cfeats = &lastfeats;
  1425. feat.cTree = tree;
  1426. feat.tree = &forest[tree];
  1427. feat.rProbs = &lastRegionProbs;
  1428. double val = forest[tree][t].feat->getVal ( feat, x, y, z );
  1429. if ( !isfinite ( val ) )
  1430. {
  1431. val = 0.0;
  1432. }
  1433. if ( val < forest[tree][t].decision )
  1434. {
  1435. currentfeats.set ( x, y, z, forest[tree][t].left, tree );
  1436. #pragma omp critical
  1437. {
  1438. if ( fasthik != NULL && useCategorization && forest[tree][forest[tree][t].left].nodeNumber < uniquenumber )
  1439. ( *globalCategorFeat ) [forest[tree][forest[tree][t].left].nodeNumber] += weight;
  1440. }
  1441. }
  1442. else
  1443. {
  1444. currentfeats.set ( x, y, z, forest[tree][t].right, tree );
  1445. #pragma omp critical
  1446. {
  1447. if ( fasthik != NULL && useCategorization && forest[tree][forest[tree][t].right].nodeNumber < uniquenumber )
  1448. ( *globalCategorFeat ) [forest[tree][forest[tree][t].right].nodeNumber] += weight;
  1449. }
  1450. }
  1451. }
  1452. }
  1453. }
  1454. }
  1455. }
  1456. if ( useRegionFeature )
  1457. {
  1458. int xsize = currentfeats.width();
  1459. int ysize = currentfeats.height();
  1460. int zsize = currentfeats.depth();
  1461. #pragma omp parallel for
  1462. for ( int x = 0; x < xsize; x++ )
  1463. {
  1464. for ( int y = 0; y < ysize; y++ )
  1465. {
  1466. for ( int z = 0; z < zsize; z++ )
  1467. {
  1468. for ( int tree = 0; tree < nbTrees; tree++ )
  1469. {
  1470. int node = currentfeats.get ( x, y, z, tree );
  1471. for ( uint d = 0; d < forest[tree][node].dist.size(); d++ )
  1472. {
  1473. regionProbs[ ( int ) ( feats ( x, y, z, rawChannels ) ) ][d] += forest[tree][node].dist[d];
  1474. }
  1475. }
  1476. }
  1477. }
  1478. }
  1479. for ( int b = 0; b < ( int ) regionProbs.size(); b++ )
  1480. {
  1481. for ( int c = 0; c < ( int ) regionProbs[b].size(); c++ )
  1482. {
  1483. regionProbs[b][c] /= ( double ) ( rSize[b] );
  1484. }
  1485. }
  1486. }
  1487. if ( depth < maxDepth )
  1488. {
  1489. //compute integral images
  1490. if ( firstiteration )
  1491. {
  1492. feats.addChannel ( classes + rawChannels );
  1493. }
  1494. computeIntegralImage ( currentfeats, feats, channelType.size() - classes );
  1495. if ( firstiteration )
  1496. {
  1497. firstiteration = false;
  1498. }
  1499. }
  1500. }
  1501. int allClasses = ( int ) probabilities.channels();
  1502. vector<int> useclass ( allClasses, 1 );
  1503. vector<int> classesInImg;
  1504. if ( useCategorization )
  1505. {
  1506. if ( cndir != "" )
  1507. {
  1508. for ( int z = 0; z < zsize; z++ )
  1509. {
  1510. std::vector< std::string > list;
  1511. StringTools::split ( filelist[z], '/', list );
  1512. string orgname = list.back();
  1513. ifstream infile ( ( cndir + "/" + orgname + ".dat" ).c_str() );
  1514. while ( !infile.eof() && infile.good() )
  1515. {
  1516. int tmp;
  1517. infile >> tmp;
  1518. assert ( tmp >= 0 && tmp < allClasses );
  1519. classesInImg.push_back ( tmp );
  1520. }
  1521. }
  1522. }
  1523. else
  1524. {
  1525. globalCategorFeat->setDim ( uniquenumber );
  1526. globalCategorFeat->normalize();
  1527. ClassificationResult cr = fasthik->classify ( globalCategorFeat );
  1528. for ( uint i = 0; i < ( uint ) classes; i++ )
  1529. {
  1530. cerr << cr.scores[i] << " ";
  1531. if ( cr.scores[i] > 0.0/*-0.3*/ )
  1532. {
  1533. classesInImg.push_back ( i );
  1534. }
  1535. }
  1536. }
  1537. cerr << "amount of classes: " << classes << " used classes: " << classesInImg.size() << endl;
  1538. }
  1539. if ( classesInImg.size() == 0 )
  1540. {
  1541. for ( uint i = 0; i < ( uint ) classes; i++ )
  1542. {
  1543. classesInImg.push_back ( i );
  1544. }
  1545. }
  1546. if ( pixelWiseLabeling )
  1547. {
  1548. //finales labeln:
  1549. //long int offset = 0;
  1550. for ( int x = 0; x < xsize; x++ )
  1551. {
  1552. for ( int y = 0; y < ysize; y++ )
  1553. {
  1554. for ( int z = 0; z < zsize; z++ )
  1555. {
  1556. double maxvalue = - numeric_limits<double>::max(); //TODO: das kann auch nur pro knoten gemacht werden, nicht pro pixel
  1557. int maxindex = 0;
  1558. for ( uint c = 0; c < classesInImg.size(); c++ )
  1559. {
  1560. int i = classesInImg[c];
  1561. int currentclass = labelmapback[i];
  1562. if ( useclass[currentclass] )
  1563. {
  1564. probabilities ( x, y, z, currentclass ) = getMeanProb ( x, y, z, i, currentfeats );
  1565. if ( probabilities ( x, y, z, currentclass ) > maxvalue )
  1566. {
  1567. maxvalue = probabilities ( x, y, z, currentclass );
  1568. maxindex = currentclass;
  1569. }
  1570. }
  1571. }
  1572. segresult.set ( x, y, maxindex, ( uint ) z );
  1573. if ( maxvalue > 1 )
  1574. cout << "maxvalue: " << maxvalue << endl;
  1575. }
  1576. }
  1577. }
  1578. #undef VISUALIZE
  1579. #ifdef VISUALIZE
  1580. for ( int z = 0; z < zsize; z++ )
  1581. {
  1582. for ( int j = 0 ; j < ( int ) probabilities.numChannels; j++ )
  1583. {
  1584. //cout << "class: " << j << endl;//" " << cn.text (j) << endl;
  1585. NICE::Matrix tmp ( probabilities.height(), probabilities.width() );
  1586. double maxval = -numeric_limits<double>::max();
  1587. double minval = numeric_limits<double>::max();
  1588. for ( int y = 0; y < probabilities.height(); y++ )
  1589. for ( int x = 0; x < probabilities.width(); x++ )
  1590. {
  1591. double val = probabilities ( x, y, z, j );
  1592. tmp ( y, x ) = val;
  1593. maxval = std::max ( val, maxval );
  1594. minval = std::min ( val, minval );
  1595. }
  1596. tmp ( 0, 0 ) = 1.0;
  1597. tmp ( 0, 1 ) = 0.0;
  1598. NICE::ColorImage imgrgb ( probabilities.width(), probabilities.height() );
  1599. ICETools::convertToRGB ( tmp, imgrgb );
  1600. cout << "maxval = " << maxval << " minval: " << minval << " for class " << j << endl; //cn.text (j) << endl;
  1601. std::string s;
  1602. std::stringstream out;
  1603. out << "tmpprebmap" << j << ".ppm";
  1604. s = out.str();
  1605. imgrgb.write ( s );
  1606. //showImage(imgrgb, "Ergebnis");
  1607. //getchar();
  1608. }
  1609. }
  1610. cout << "fertsch" << endl;
  1611. getchar();
  1612. cout << "weiter gehtsch" << endl;
  1613. #endif
  1614. }
  1615. else
  1616. {
  1617. //using segmentation
  1618. NICE::MultiChannelImageT<int> regions;
  1619. int xsize = feats.width();
  1620. int ysize = feats.height();
  1621. int zsize = feats.depth();
  1622. regions.reInit ( xsize, ysize, zsize );
  1623. if ( useRegionFeature )
  1624. {
  1625. int rchannel = -1;
  1626. for ( uint i = 0; i < channelType.size(); i++ )
  1627. {
  1628. if ( channelType[i] == 1 )
  1629. {
  1630. rchannel = i;
  1631. break;
  1632. }
  1633. }
  1634. assert ( rchannel > -1 );
  1635. for ( int z = 0; z < zsize; z++ )
  1636. {
  1637. for ( int y = 0; y < ysize; y++ )
  1638. {
  1639. for ( int x = 0; x < xsize; x++ )
  1640. {
  1641. regions.set ( x, y, feats ( x, y, z, rchannel ), ( uint ) z );
  1642. }
  1643. }
  1644. }
  1645. }
  1646. else
  1647. {
  1648. amountRegions = 0;
  1649. vector<int> chanSelect;
  1650. for ( int i=0; i<3; i++ )
  1651. chanSelect.push_back ( i );
  1652. amountRegions = segmentation->segRegions ( imgData, regions, chanSelect );
  1653. #ifdef DEBUG
  1654. for ( unsigned int z = 0; z < ( uint ) zsize; z++ )
  1655. {
  1656. NICE::Matrix regmask;
  1657. NICE::ColorImage colorimg ( xsize, ysize );
  1658. NICE::ColorImage marked ( xsize, ysize );
  1659. regmask.resize ( xsize, ysize );
  1660. for ( int y = 0; y < ysize; y++ )
  1661. {
  1662. for ( int x = 0; x < xsize; x++ )
  1663. {
  1664. regmask ( x,y ) = regions ( x,y,z );
  1665. colorimg.setPixelQuick ( x, y, 0, imgData.get ( x,y,z,0 ) );
  1666. colorimg.setPixelQuick ( x, y, 1, imgData.get ( x,y,z,0 ) );
  1667. colorimg.setPixelQuick ( x, y, 2, imgData.get ( x,y,z,0 ) );
  1668. }
  1669. }
  1670. vector<int> colorvals;
  1671. colorvals.push_back ( 255 );
  1672. colorvals.push_back ( 0 );
  1673. colorvals.push_back ( 0 );
  1674. segmentation->markContours ( colorimg, regmask, colorvals, marked );
  1675. std::vector<string> list;
  1676. StringTools::split ( filelist[z], '/', list );
  1677. string savePath = StringTools::trim ( filelist[z], list.back() ) + "marked/" + list.back();
  1678. marked.write ( savePath );
  1679. }
  1680. #endif
  1681. }
  1682. regionProbs.clear();
  1683. regionProbs = vector<vector<double> > ( amountRegions, vector<double> ( classes, 0.0 ) );
  1684. vector<int> bestlabels ( amountRegions, labelmapback[classesInImg[0]] );
  1685. for ( int z = 0; z < zsize; z++ )
  1686. {
  1687. for ( int y = 0; y < ysize; y++ )
  1688. {
  1689. for ( int x = 0; x < xsize; x++ )
  1690. {
  1691. int cregion = regions ( x, y, ( uint ) z );
  1692. for ( uint c = 0; c < classesInImg.size(); c++ )
  1693. {
  1694. int d = classesInImg[c];
  1695. regionProbs[cregion][d] += getMeanProb ( x, y, z, d, currentfeats );
  1696. }
  1697. }
  1698. }
  1699. }
  1700. for ( int r = 0; r < amountRegions; r++ )
  1701. {
  1702. double maxval = regionProbs[r][classesInImg[0]];
  1703. bestlabels[r] = classesInImg[0];
  1704. for ( int d = 1; d < classes; d++ )
  1705. {
  1706. if ( maxval < regionProbs[r][d] )
  1707. {
  1708. maxval = regionProbs[r][d];
  1709. bestlabels[r] = d;
  1710. }
  1711. }
  1712. bestlabels[r] = labelmapback[bestlabels[r]];
  1713. }
  1714. for ( int z = 0; z < zsize; z++ )
  1715. {
  1716. for ( int y = 0; y < ysize; y++ )
  1717. {
  1718. for ( int x = 0; x < xsize; x++ )
  1719. {
  1720. segresult.set ( x, y, bestlabels[regions ( x,y, ( uint ) z ) ], ( uint ) z );
  1721. }
  1722. }
  1723. }
  1724. //#define WRITEREGIONS
  1725. #ifdef WRITEREGIONS
  1726. for ( int z = 0; z < zsize; z++ )
  1727. {
  1728. RegionGraph rg;
  1729. NICE::ColorImage img ( xsize,ysize );
  1730. if ( imagetype == IMAGETYPE_RGB )
  1731. {
  1732. img = imgData.getColor ( z );
  1733. }
  1734. else
  1735. {
  1736. NICE::Image gray = imgData.getChannel ( z );
  1737. for ( int y = 0; y < ysize; y++ )
  1738. {
  1739. for ( int x = 0; x < xsize; x++ )
  1740. {
  1741. int val = gray.getPixelQuick ( x,y );
  1742. img.setPixelQuick ( x, y, val, val, val );
  1743. }
  1744. }
  1745. }
  1746. Matrix regions_tmp ( xsize,ysize );
  1747. for ( int y = 0; y < ysize; y++ )
  1748. {
  1749. for ( int x = 0; x < xsize; x++ )
  1750. {
  1751. regions_tmp ( x,y ) = regions ( x,y, ( uint ) z );
  1752. }
  1753. }
  1754. segmentation->getGraphRepresentation ( img, regions_tmp, rg );
  1755. for ( uint pos = 0; pos < regionProbs.size(); pos++ )
  1756. {
  1757. rg[pos]->setProbs ( regionProbs[pos] );
  1758. }
  1759. std::string s;
  1760. std::stringstream out;
  1761. std::vector< std::string > list;
  1762. StringTools::split ( filelist[z], '/', list );
  1763. out << "rgout/" << list.back() << ".graph";
  1764. string writefile = out.str();
  1765. rg.write ( writefile );
  1766. }
  1767. #endif
  1768. }
  1769. cout << "segmentation finished" << endl;
  1770. }
  1771. void SemSegContextTree::store ( std::ostream & os, int format ) const
  1772. {
  1773. os.precision ( numeric_limits<double>::digits10 + 1 );
  1774. os << nbTrees << endl;
  1775. classnames.store ( os );
  1776. map<int, int>::const_iterator it;
  1777. os << labelmap.size() << endl;
  1778. for ( it = labelmap.begin() ; it != labelmap.end(); it++ )
  1779. os << ( *it ).first << " " << ( *it ).second << endl;
  1780. os << labelmapback.size() << endl;
  1781. for ( it = labelmapback.begin() ; it != labelmapback.end(); it++ )
  1782. os << ( *it ).first << " " << ( *it ).second << endl;
  1783. int trees = forest.size();
  1784. os << trees << endl;
  1785. for ( int t = 0; t < trees; t++ )
  1786. {
  1787. int nodes = forest[t].size();
  1788. os << nodes << endl;
  1789. for ( int n = 0; n < nodes; n++ )
  1790. {
  1791. 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;
  1792. os << forest[t][n].dist << endl;
  1793. if ( forest[t][n].feat == NULL )
  1794. os << -1 << endl;
  1795. else
  1796. {
  1797. os << forest[t][n].feat->getOps() << endl;
  1798. forest[t][n].feat->store ( os );
  1799. }
  1800. }
  1801. }
  1802. os << channelType.size() << endl;
  1803. for ( int i = 0; i < ( int ) channelType.size(); i++ )
  1804. {
  1805. os << channelType[i] << " ";
  1806. }
  1807. os << endl;
  1808. os << integralMap.size() << endl;
  1809. for ( int i = 0; i < ( int ) integralMap.size(); i++ )
  1810. {
  1811. os << integralMap[i].first << " " << integralMap[i].second << endl;
  1812. }
  1813. os << rawChannels << endl;
  1814. os << uniquenumber << endl;
  1815. }
  1816. void SemSegContextTree::restore ( std::istream & is, int format )
  1817. {
  1818. is >> nbTrees;
  1819. classnames.restore ( is );
  1820. int lsize;
  1821. is >> lsize;
  1822. labelmap.clear();
  1823. for ( int l = 0; l < lsize; l++ )
  1824. {
  1825. int first, second;
  1826. is >> first;
  1827. is >> second;
  1828. labelmap[first] = second;
  1829. }
  1830. is >> lsize;
  1831. labelmapback.clear();
  1832. for ( int l = 0; l < lsize; l++ )
  1833. {
  1834. int first, second;
  1835. is >> first;
  1836. is >> second;
  1837. labelmapback[first] = second;
  1838. }
  1839. int trees;
  1840. is >> trees;
  1841. forest.clear();
  1842. for ( int t = 0; t < trees; t++ )
  1843. {
  1844. vector<TreeNode> tmptree;
  1845. forest.push_back ( tmptree );
  1846. int nodes;
  1847. is >> nodes;
  1848. for ( int n = 0; n < nodes; n++ )
  1849. {
  1850. TreeNode tmpnode;
  1851. forest[t].push_back ( tmpnode );
  1852. is >> forest[t][n].left;
  1853. is >> forest[t][n].right;
  1854. is >> forest[t][n].decision;
  1855. is >> forest[t][n].isleaf;
  1856. is >> forest[t][n].depth;
  1857. is >> forest[t][n].featcounter;
  1858. is >> forest[t][n].nodeNumber;
  1859. is >> forest[t][n].dist;
  1860. int feattype;
  1861. is >> feattype;
  1862. assert ( feattype < NBOPERATIONS );
  1863. forest[t][n].feat = NULL;
  1864. if ( feattype >= 0 )
  1865. {
  1866. for ( uint o = 0; o < ops.size(); o++ )
  1867. {
  1868. for ( uint o2 = 0; o2 < ops[o].size(); o2++ )
  1869. {
  1870. if ( forest[t][n].feat == NULL )
  1871. {
  1872. for ( uint c = 0; c < ops[o].size(); c++ )
  1873. {
  1874. if ( ops[o][o2]->getOps() == feattype )
  1875. {
  1876. forest[t][n].feat = ops[o][o2]->clone();
  1877. break;
  1878. }
  1879. }
  1880. }
  1881. }
  1882. }
  1883. assert ( forest[t][n].feat != NULL );
  1884. forest[t][n].feat->restore ( is );
  1885. }
  1886. }
  1887. }
  1888. channelType.clear();
  1889. int ctsize;
  1890. is >> ctsize;
  1891. for ( int i = 0; i < ctsize; i++ )
  1892. {
  1893. int tmp;
  1894. is >> tmp;
  1895. channelType.push_back ( tmp );
  1896. }
  1897. integralMap.clear();
  1898. int iMapSize;
  1899. is >> iMapSize;
  1900. for ( int i = 0; i < iMapSize; i++ )
  1901. {
  1902. int first;
  1903. int second;
  1904. is >> first;
  1905. is >> second;
  1906. integralMap.push_back ( pair<int, int> ( first, second ) );
  1907. }
  1908. is >> rawChannels;
  1909. is >> uniquenumber;
  1910. }