SemSegContextTree.cpp 56 KB

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