SemSegContextTree.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. #include "SemSegContextTree.h"
  2. #include "vislearning/baselib/Globals.h"
  3. #include "vislearning/baselib/ProgressBar.h"
  4. #include "core/basics/StringTools.h"
  5. #include "vislearning/cbaselib/CachedExample.h"
  6. #include "vislearning/cbaselib/PascalResults.h"
  7. #include "vislearning/baselib/ColorSpace.h"
  8. #include "objrec/segmentation/RSMeanShift.h"
  9. #include "objrec/segmentation/RSGraphBased.h"
  10. #include "core/basics/numerictools.h"
  11. #include "core/basics/Timer.h"
  12. #include "core/basics/vectorio.h"
  13. #include <omp.h>
  14. #include <iostream>
  15. #define BOUND(x,min,max) (((x)<(min))?(min):((x)>(max)?(max):(x)))
  16. #undef LOCALFEATS
  17. //#define LOCALFEATS
  18. using namespace OBJREC;
  19. using namespace std;
  20. using namespace NICE;
  21. class MCImageAccess: public ValueAccess
  22. {
  23. public:
  24. virtual double getVal ( const Features &feats, const int &x, const int &y, const int &channel )
  25. {
  26. return feats.feats->get ( x, y, channel );
  27. }
  28. virtual string writeInfos()
  29. {
  30. return "raw";
  31. }
  32. virtual ValueTypes getType()
  33. {
  34. return RAWFEAT;
  35. }
  36. };
  37. class ClassificationResultAcess: public ValueAccess
  38. {
  39. public:
  40. virtual double getVal ( const Features &feats, const int &x, const int &y, const int &channel )
  41. {
  42. return ( *feats.tree ) [feats.cfeats->get ( x,y,feats.cTree ) ].dist[channel];
  43. }
  44. virtual string writeInfos()
  45. {
  46. return "context";
  47. }
  48. virtual ValueTypes getType()
  49. {
  50. return CONTEXT;
  51. }
  52. };
  53. void Operation::restore ( std::istream &is )
  54. {
  55. is >> x1;
  56. is >> x2;
  57. is >> y1;
  58. is >> y2;
  59. is >> channel1;
  60. is >> channel2;
  61. int tmp;
  62. is >> tmp;
  63. if ( tmp >= 0 )
  64. {
  65. if ( tmp == RAWFEAT )
  66. {
  67. values = new MCImageAccess();
  68. }
  69. else if ( tmp == CONTEXT )
  70. {
  71. values = new ClassificationResultAcess();
  72. }
  73. else
  74. {
  75. throw ( "no valid ValueAccess" );
  76. }
  77. }
  78. else
  79. {
  80. values = NULL;
  81. }
  82. }
  83. std::string Operation::writeInfos()
  84. {
  85. std::stringstream ss;
  86. ss << " x1: " << x1 << " y1: " << y1 << " x2: " << x2 << " y2: " << y2 << " c1: " << channel1 << " c2: " << channel2;
  87. return ss.str();
  88. }
  89. class Minus: public Operation
  90. {
  91. public:
  92. virtual double getVal ( const Features &feats, const int &x, const int &y )
  93. {
  94. int xsize, ysize;
  95. getXY ( feats, xsize, ysize );
  96. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  97. double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel2 );
  98. return v1 -v2;
  99. }
  100. virtual Operation* clone()
  101. {
  102. return new Minus();
  103. }
  104. virtual string writeInfos()
  105. {
  106. string out = "Minus";
  107. if ( values != NULL )
  108. out += values->writeInfos();
  109. return out+Operation::writeInfos();
  110. }
  111. virtual OperationTypes getOps()
  112. {
  113. return MINUS;
  114. }
  115. };
  116. class MinusAbs: public Operation
  117. {
  118. public:
  119. virtual double getVal ( const Features &feats, const int &x, const int &y )
  120. {
  121. int xsize, ysize;
  122. getXY ( feats, xsize, ysize );
  123. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  124. double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel2 );
  125. return abs ( v1 -v2 );
  126. }
  127. virtual Operation* clone()
  128. {
  129. return new MinusAbs();
  130. };
  131. virtual string writeInfos()
  132. {
  133. string out = "MinusAbs";
  134. if ( values != NULL )
  135. out += values->writeInfos();
  136. return out;
  137. }
  138. virtual OperationTypes getOps()
  139. {
  140. return MINUSABS;
  141. }
  142. };
  143. class Addition: public Operation
  144. {
  145. public:
  146. virtual double getVal ( const Features &feats, const int &x, const int &y )
  147. {
  148. int xsize, ysize;
  149. getXY ( feats, xsize, ysize );
  150. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  151. double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize -
  152. 1 ), channel2 );
  153. return v1 + v2;
  154. }
  155. virtual Operation* clone()
  156. {
  157. return new Addition();
  158. }
  159. virtual string writeInfos()
  160. {
  161. string out = "Addition";
  162. if ( values != NULL )
  163. out += values->writeInfos();
  164. return out+Operation::writeInfos();
  165. }
  166. virtual OperationTypes getOps()
  167. {
  168. return ADDITION;
  169. }
  170. };
  171. class Only1: public Operation
  172. {
  173. public:
  174. virtual double getVal ( const Features &feats, const int &x, const int &y )
  175. {
  176. int xsize, ysize;
  177. getXY ( feats, xsize, ysize );
  178. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  179. return v1;
  180. }
  181. virtual Operation* clone()
  182. {
  183. return new Only1();
  184. }
  185. virtual string writeInfos()
  186. {
  187. string out = "Only1";
  188. if ( values != NULL )
  189. out += values->writeInfos();
  190. return out+Operation::writeInfos();
  191. }
  192. virtual OperationTypes getOps()
  193. {
  194. return ONLY1;
  195. }
  196. };
  197. class RelativeXPosition: public Operation
  198. {
  199. public:
  200. virtual double getVal ( const Features &feats, const int &x, const int &y )
  201. {
  202. int xsize, ysize;
  203. getXY ( feats, xsize, ysize );
  204. return ( double ) x / ( double ) xsize;
  205. }
  206. virtual Operation* clone()
  207. {
  208. return new RelativeXPosition();
  209. }
  210. virtual string writeInfos()
  211. {
  212. return "RelativeXPosition"+Operation::writeInfos();
  213. }
  214. virtual OperationTypes getOps()
  215. {
  216. return RELATIVEXPOSITION;
  217. }
  218. };
  219. class RelativeYPosition: public Operation
  220. {
  221. public:
  222. virtual double getVal ( const Features &feats, const int &x, const int &y )
  223. {
  224. int xsize, ysize;
  225. getXY ( feats, xsize, ysize );
  226. return ( double ) x / ( double ) xsize;
  227. }
  228. virtual Operation* clone()
  229. {
  230. return new RelativeYPosition();
  231. }
  232. virtual string writeInfos()
  233. {
  234. return "RelativeYPosition"+Operation::writeInfos();
  235. }
  236. virtual OperationTypes getOps()
  237. {
  238. return RELATIVEYPOSITION;
  239. }
  240. };
  241. // uses mean of classification in window given by (x1,y1) (x2,y2)
  242. class IntegralOps: public Operation
  243. {
  244. public:
  245. virtual void set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  246. {
  247. x1 = min ( _x1, _x2 );
  248. y1 = min ( _y1, _y2 );
  249. x2 = max ( _x1, _x2 );
  250. y2 = max ( _y1, _y2 );
  251. channel1 = _channel1;
  252. channel2 = _channel2;
  253. values = _values;
  254. }
  255. virtual double getVal ( const Features &feats, const int &x, const int &y )
  256. {
  257. int xsize, ysize;
  258. getXY ( feats, xsize, ysize );
  259. return computeMean ( *feats.integralImg, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel1 );
  260. }
  261. inline double computeMean ( const NICE::MultiChannelImageT<double> &intImg, const int &uLx, const int &uLy, const int &lRx, const int &lRy, const int &chan )
  262. {
  263. double val1 = intImg.get ( uLx, uLy, chan );
  264. double val2 = intImg.get ( lRx, uLy, chan );
  265. double val3 = intImg.get ( uLx, lRy, chan );
  266. double val4 = intImg.get ( lRx, lRy, chan );
  267. double area = ( lRx - uLx ) * ( lRy - uLy );
  268. if ( area == 0 )
  269. return 0.0;
  270. return ( val1 + val4 - val2 - val3 ) / area;
  271. }
  272. virtual Operation* clone()
  273. {
  274. return new IntegralOps();
  275. }
  276. virtual string writeInfos()
  277. {
  278. return "IntegralOps"+Operation::writeInfos();
  279. }
  280. virtual OperationTypes getOps()
  281. {
  282. return INTEGRAL;
  283. }
  284. };
  285. //like a global bag of words to model the current appearance of classes in an image without local context
  286. class GlobalFeats: public IntegralOps
  287. {
  288. public:
  289. virtual double getVal ( const Features &feats, const int &x, const int &y )
  290. {
  291. int xsize, ysize;
  292. getXY ( feats, xsize, ysize );
  293. return computeMean ( *feats.integralImg, 0, 0, xsize - 1, ysize - 1, channel1 );
  294. }
  295. virtual Operation* clone()
  296. {
  297. return new GlobalFeats();
  298. }
  299. virtual string writeInfos()
  300. {
  301. return "GlobalFeats"+Operation::writeInfos();
  302. }
  303. virtual OperationTypes getOps()
  304. {
  305. return GLOBALFEATS;
  306. }
  307. };
  308. //uses mean of Integral image given by x1, y1 with current pixel as center
  309. class IntegralCenteredOps: public IntegralOps
  310. {
  311. public:
  312. virtual void set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  313. {
  314. x1 = abs ( _x1 );
  315. y1 = abs ( _y1 );
  316. x2 = abs ( _x2 );
  317. y2 = abs ( _y2 );
  318. channel1 = _channel1;
  319. channel2 = _channel2;
  320. values = _values;
  321. }
  322. virtual double getVal ( const Features &feats, const int &x, const int &y )
  323. {
  324. int xsize, ysize;
  325. getXY ( feats, xsize, ysize );
  326. return computeMean ( *feats.integralImg, BOUND ( x - x1, 0, xsize - 1 ), BOUND ( y - y1, 0, ysize - 1 ), BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  327. }
  328. virtual Operation* clone()
  329. {
  330. return new IntegralCenteredOps();
  331. }
  332. virtual string writeInfos()
  333. {
  334. return "IntegralCenteredOps"+Operation::writeInfos();
  335. }
  336. virtual OperationTypes getOps()
  337. {
  338. return INTEGRALCENT;
  339. }
  340. };
  341. //uses different of mean of Integral image given by two windows, where (x1,y1) is the width and height of window1 and (x2,y2) of window 2
  342. class BiIntegralCenteredOps: public IntegralCenteredOps
  343. {
  344. public:
  345. virtual void set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  346. {
  347. x1 = min ( abs ( _x1 ), abs ( _x2 ) );
  348. y1 = min ( abs ( _y1 ), abs ( _y2 ) );
  349. x2 = max ( abs ( _x1 ), abs ( _x2 ) );
  350. y2 = max ( abs ( _y1 ), abs ( _y2 ) );
  351. channel1 = _channel1;
  352. channel2 = _channel2;
  353. values = _values;
  354. }
  355. virtual double getVal ( const Features &feats, const int &x, const int &y )
  356. {
  357. int xsize, ysize;
  358. getXY ( feats, xsize, ysize );
  359. return computeMean ( *feats.integralImg, BOUND ( x - x1, 0, xsize - 1 ), BOUND ( y - y1, 0, ysize - 1 ), BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 ) - computeMean ( *feats.integralImg, BOUND ( x - x2, 0, xsize - 1 ), BOUND ( y - y2, 0, ysize - 1 ), BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel1 );
  360. }
  361. virtual Operation* clone()
  362. {
  363. return new BiIntegralCenteredOps();
  364. }
  365. virtual string writeInfos()
  366. {
  367. return "BiIntegralCenteredOps"+Operation::writeInfos();
  368. }
  369. virtual OperationTypes getOps()
  370. {
  371. return BIINTEGRALCENT;
  372. }
  373. };
  374. /** horizontal Haar features
  375. * ++
  376. * --
  377. */
  378. class HaarHorizontal: public IntegralCenteredOps
  379. {
  380. virtual double getVal ( const Features &feats, const int &x, const int &y )
  381. {
  382. int xsize, ysize;
  383. getXY ( feats, xsize, ysize );
  384. int tlx = BOUND ( x - x1, 0, xsize - 1 );
  385. int tly = BOUND ( y - y1, 0, ysize - 1 );
  386. int lrx = BOUND ( x + x1, 0, xsize - 1 );
  387. int lry = BOUND ( y + y1, 0, ysize - 1 );
  388. return computeMean ( *feats.integralImg, tlx, tly, lrx, y, channel1 ) - computeMean ( *feats.integralImg, tlx, y, lrx, lry, channel1 );
  389. }
  390. virtual Operation* clone()
  391. {
  392. return new HaarHorizontal();
  393. }
  394. virtual string writeInfos()
  395. {
  396. return "HaarHorizontal"+Operation::writeInfos();
  397. }
  398. virtual OperationTypes getOps()
  399. {
  400. return HAARHORIZ;
  401. }
  402. };
  403. /** vertical Haar features
  404. * +-
  405. * +-
  406. */
  407. class HaarVertical: public IntegralCenteredOps
  408. {
  409. virtual double getVal ( const Features &feats, const int &x, const int &y )
  410. {
  411. int xsize, ysize;
  412. getXY ( feats, xsize, ysize );
  413. int tlx = BOUND ( x - x1, 0, xsize - 1 );
  414. int tly = BOUND ( y - y1, 0, ysize - 1 );
  415. int lrx = BOUND ( x + x1, 0, xsize - 1 );
  416. int lry = BOUND ( y + y1, 0, ysize - 1 );
  417. return computeMean ( *feats.integralImg, tlx, tly, x, lry, channel1 ) - computeMean ( *feats.integralImg, x, tly, lrx, lry, channel1 );
  418. }
  419. virtual Operation* clone()
  420. {
  421. return new HaarVertical();
  422. }
  423. virtual string writeInfos()
  424. {
  425. return "HaarVertical"+Operation::writeInfos();
  426. }
  427. virtual OperationTypes getOps()
  428. {
  429. return HAARVERT;
  430. }
  431. };
  432. /** vertical Haar features
  433. * +-
  434. * -+
  435. */
  436. class HaarDiag: public IntegralCenteredOps
  437. {
  438. virtual double getVal ( const Features &feats, const int &x, const int &y )
  439. {
  440. int xsize, ysize;
  441. getXY ( feats, xsize, ysize );
  442. int tlx = BOUND ( x - x1, 0, xsize - 1 );
  443. int tly = BOUND ( y - y1, 0, ysize - 1 );
  444. int lrx = BOUND ( x + x1, 0, xsize - 1 );
  445. int lry = BOUND ( y + y1, 0, ysize - 1 );
  446. return computeMean ( *feats.integralImg, tlx, tly, x, y, channel1 ) + computeMean ( *feats.integralImg, x, y, lrx, lry, channel1 ) - computeMean ( *feats.integralImg, tlx, y, x, lry, channel1 ) - computeMean ( *feats.integralImg, x, tly, lrx, y, channel1 );
  447. }
  448. virtual Operation* clone()
  449. {
  450. return new HaarDiag();
  451. }
  452. virtual string writeInfos()
  453. {
  454. return "HaarDiag"+Operation::writeInfos();
  455. }
  456. virtual OperationTypes getOps()
  457. {
  458. return HAARDIAG;
  459. }
  460. };
  461. /** horizontal Haar features
  462. * +++
  463. * ---
  464. * +++
  465. */
  466. class Haar3Horiz: public BiIntegralCenteredOps
  467. {
  468. virtual double getVal ( const Features &feats, const int &x, const int &y )
  469. {
  470. int xsize, ysize;
  471. getXY ( feats, xsize, ysize );
  472. int tlx = BOUND ( x - x2, 0, xsize - 1 );
  473. int tly = BOUND ( y - y2, 0, ysize - 1 );
  474. int mtly = BOUND ( y - y1, 0, ysize - 1 );
  475. int mlry = BOUND ( y + y1, 0, ysize - 1 );
  476. int lrx = BOUND ( x + x2, 0, xsize - 1 );
  477. int lry = BOUND ( y + y2, 0, ysize - 1 );
  478. return computeMean ( *feats.integralImg, tlx, tly, lrx, mtly, channel1 ) - computeMean ( *feats.integralImg, tlx, mtly, lrx, mlry, channel1 ) + computeMean ( *feats.integralImg, tlx, mlry, lrx, lry, channel1 );
  479. }
  480. virtual Operation* clone()
  481. {
  482. return new Haar3Horiz();
  483. }
  484. virtual string writeInfos()
  485. {
  486. return "Haar3Horiz"+Operation::writeInfos();
  487. }
  488. virtual OperationTypes getOps()
  489. {
  490. return HAAR3HORIZ;
  491. }
  492. };
  493. /** vertical Haar features
  494. * +-+
  495. * +-+
  496. * +-+
  497. */
  498. class Haar3Vert: public BiIntegralCenteredOps
  499. {
  500. virtual double getVal ( const Features &feats, const int &x, const int &y )
  501. {
  502. int xsize, ysize;
  503. getXY ( feats, xsize, ysize );
  504. int tlx = BOUND ( x - x2, 0, xsize - 1 );
  505. int tly = BOUND ( y - y2, 0, ysize - 1 );
  506. int mtlx = BOUND ( x - x1, 0, xsize - 1 );
  507. int mlrx = BOUND ( x + x1, 0, xsize - 1 );
  508. int lrx = BOUND ( x + x2, 0, xsize - 1 );
  509. int lry = BOUND ( y + y2, 0, ysize - 1 );
  510. return computeMean ( *feats.integralImg, tlx, tly, mtlx, lry, channel1 ) - computeMean ( *feats.integralImg, mtlx, tly, mlrx, lry, channel1 ) + computeMean ( *feats.integralImg, mlrx, tly, lrx, lry, channel1 );
  511. }
  512. virtual Operation* clone()
  513. {
  514. return new Haar3Vert();
  515. }
  516. virtual string writeInfos()
  517. {
  518. return "Haar3Vert"+Operation::writeInfos();
  519. }
  520. virtual OperationTypes getOps()
  521. {
  522. return HAAR3VERT;
  523. }
  524. };
  525. SemSegContextTree::SemSegContextTree ( const Config *conf, const MultiDataset *md )
  526. : SemanticSegmentation ( conf, & ( md->getClassNames ( "train" ) ) )
  527. {
  528. this->conf = conf;
  529. string section = "SSContextTree";
  530. lfcw = new LFColorWeijer ( conf );
  531. grid = conf->gI ( section, "grid", 10 );
  532. maxSamples = conf->gI ( section, "max_samples", 2000 );
  533. minFeats = conf->gI ( section, "min_feats", 50 );
  534. maxDepth = conf->gI ( section, "max_depth", 10 );
  535. windowSize = conf->gI ( section, "window_size", 16 );
  536. featsPerSplit = conf->gI ( section, "feats_per_split", 200 );
  537. useShannonEntropy = conf->gB ( section, "use_shannon_entropy", true );
  538. nbTrees = conf->gI ( section, "amount_trees", 1 );
  539. string segmentationtype = conf->gS ( section, "segmentation_type", "meanshift" );
  540. useGaussian = conf->gB ( section, "use_gaussian", true );
  541. randomTests = conf->gI ( section, "random_tests", 10 );
  542. bool saveLoadData = conf->gB ( "debug", "save_load_data", false );
  543. string fileLocation = conf->gS ( "debug", "datafile", "tmp.txt" );
  544. if ( useGaussian )
  545. throw ( "there something wrong with using gaussian! first fix it!" );
  546. pixelWiseLabeling = false;
  547. if ( segmentationtype == "meanshift" )
  548. segmentation = new RSMeanShift ( conf );
  549. else if ( segmentationtype == "none" )
  550. {
  551. segmentation = NULL;
  552. pixelWiseLabeling = true;
  553. }
  554. else if ( segmentationtype == "felzenszwalb" )
  555. segmentation = new RSGraphBased ( conf );
  556. else
  557. throw ( "no valid segmenation_type\n please choose between none, meanshift and felzenszwalb\n" );
  558. ftypes = conf->gI ( section, "features", 2 );;
  559. string featsec = "Features";
  560. if ( conf->gB ( featsec, "minus", true ) )
  561. ops.push_back ( new Minus() );
  562. if ( conf->gB ( featsec, "minus_abs", true ) )
  563. ops.push_back ( new MinusAbs() );
  564. if ( conf->gB ( featsec, "addition", true ) )
  565. ops.push_back ( new Addition() );
  566. if ( conf->gB ( featsec, "only1", true ) )
  567. ops.push_back ( new Only1() );
  568. if ( conf->gB ( featsec, "rel_x", true ) )
  569. ops.push_back ( new RelativeXPosition() );
  570. if ( conf->gB ( featsec, "rel_y", true ) )
  571. ops.push_back ( new RelativeYPosition() );
  572. if ( conf->gB ( featsec, "bi_int_cent", true ) )
  573. cops.push_back ( new BiIntegralCenteredOps() );
  574. if ( conf->gB ( featsec, "int_cent", true ) )
  575. cops.push_back ( new IntegralCenteredOps() );
  576. if ( conf->gB ( featsec, "int", true ) )
  577. cops.push_back ( new IntegralOps() );
  578. if ( conf->gB ( featsec, "haar_horz", true ) )
  579. cops.push_back ( new HaarHorizontal() );
  580. if ( conf->gB ( featsec, "haar_vert", true ) )
  581. cops.push_back ( new HaarVertical() );
  582. if ( conf->gB ( featsec, "haar_diag", true ) )
  583. cops.push_back ( new HaarDiag() );
  584. if ( conf->gB ( featsec, "haar3_horz", true ) )
  585. cops.push_back ( new Haar3Horiz() );
  586. if ( conf->gB ( featsec, "haar3_vert", true ) )
  587. cops.push_back ( new Haar3Vert() );
  588. if ( conf->gB ( featsec, "glob", true ) )
  589. cops.push_back ( new GlobalFeats() );
  590. opOverview = vector<int> ( NBOPERATIONS, 0 );
  591. contextOverview = vector<vector<double> > ( maxDepth, vector<double> ( 2, 0.0 ) );
  592. calcVal.push_back ( new MCImageAccess() );
  593. calcVal.push_back ( new ClassificationResultAcess() );
  594. classnames = md->getClassNames ( "train" );
  595. ///////////////////////////////////
  596. // Train Segmentation Context Trees
  597. ///////////////////////////////////
  598. if ( saveLoadData )
  599. {
  600. if ( FileMgt::fileExists ( fileLocation ) )
  601. read ( fileLocation );
  602. else
  603. {
  604. train ( md );
  605. write ( fileLocation );
  606. }
  607. }
  608. else
  609. {
  610. train ( md );
  611. }
  612. }
  613. SemSegContextTree::~SemSegContextTree()
  614. {
  615. }
  616. double SemSegContextTree::getBestSplit ( std::vector<NICE::MultiChannelImageT<double> > &feats, std::vector<NICE::MultiChannelImageT<unsigned short int> > &currentfeats, std::vector<NICE::MultiChannelImageT<double> > &integralImgs, const std::vector<NICE::MatrixT<int> > &labels, int node, Operation *&splitop, double &splitval, const int &tree )
  617. {
  618. Timer t;
  619. t.start();
  620. int imgCount = 0, featdim = 0;
  621. try
  622. {
  623. imgCount = ( int ) feats.size();
  624. featdim = feats[0].channels();
  625. }
  626. catch ( Exception )
  627. {
  628. cerr << "no features computed?" << endl;
  629. }
  630. double bestig = -numeric_limits< double >::max();
  631. splitop = NULL;
  632. splitval = -1.0;
  633. set<vector<int> >selFeats;
  634. map<int, int> e;
  635. int featcounter = forest[tree][node].featcounter;
  636. if ( featcounter < minFeats )
  637. {
  638. //cout << "only " << featcounter << " feats in current node -> it's a leaf" << endl;
  639. return 0.0;
  640. }
  641. vector<double> fraction ( a.size(), 0.0 );
  642. for ( uint i = 0; i < fraction.size(); i++ )
  643. {
  644. if ( forbidden_classes.find ( labelmapback[i] ) != forbidden_classes.end() )
  645. fraction[i] = 0;
  646. else
  647. fraction[i] = ( ( double ) maxSamples ) / ( ( double ) featcounter * a[i] * a.size() );
  648. //cout << "fraction["<<i<<"]: "<< fraction[i] << " a[" << i << "]: " << a[i] << endl;
  649. }
  650. featcounter = 0;
  651. for ( int iCounter = 0; iCounter < imgCount; iCounter++ )
  652. {
  653. int xsize = ( int ) currentfeats[iCounter].width();
  654. int ysize = ( int ) currentfeats[iCounter].height();
  655. for ( int x = 0; x < xsize; x++ )
  656. {
  657. for ( int y = 0; y < ysize; y++ )
  658. {
  659. if ( currentfeats[iCounter].get ( x, y, tree ) == node )
  660. {
  661. int cn = labels[iCounter] ( x, y );
  662. double randD = ( double ) rand() / ( double ) RAND_MAX;
  663. if(labelmap.find(cn) == labelmap.end())
  664. continue;
  665. if ( randD < fraction[labelmap[cn]] )
  666. {
  667. vector<int> tmp ( 3, 0 );
  668. tmp[0] = iCounter;
  669. tmp[1] = x;
  670. tmp[2] = y;
  671. featcounter++;
  672. selFeats.insert ( tmp );
  673. e[cn]++;
  674. }
  675. }
  676. }
  677. }
  678. }
  679. //cout << "size: " << selFeats.size() << endl;
  680. //getchar();
  681. map<int, int>::iterator mapit;
  682. double globent = 0.0;
  683. for ( mapit = e.begin() ; mapit != e.end(); mapit++ )
  684. {
  685. //cout << "class: " << mapit->first << ": " << mapit->second << endl;
  686. double p = ( double ) ( *mapit ).second / ( double ) featcounter;
  687. globent += p * log2 ( p );
  688. }
  689. globent = -globent;
  690. if ( globent < 0.5 )
  691. {
  692. //cout << "globent to small: " << globent << endl;
  693. return 0.0;
  694. }
  695. int classes = ( int ) forest[tree][0].dist.size();
  696. featsel.clear();
  697. for ( int i = 0; i < featsPerSplit; i++ )
  698. {
  699. int x1, x2, y1, y2;
  700. int ft = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) ftypes );
  701. int tmpws = windowSize;
  702. if ( integralImgs[0].width() == 0 )
  703. ft = 0;
  704. if ( ft > 0 )
  705. {
  706. tmpws *= 4;
  707. }
  708. if ( useGaussian )
  709. {
  710. double sigma = ( double ) tmpws / 2.0;
  711. x1 = randGaussDouble ( sigma ) * ( double ) tmpws;
  712. x2 = randGaussDouble ( sigma ) * ( double ) tmpws;
  713. y1 = randGaussDouble ( sigma ) * ( double ) tmpws;
  714. y2 = randGaussDouble ( sigma ) * ( double ) tmpws;
  715. }
  716. else
  717. {
  718. x1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  719. x2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  720. y1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  721. y2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  722. }
  723. if ( ft == 0 )
  724. {
  725. int f1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) featdim );
  726. int f2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) featdim );
  727. int o = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) ops.size() );
  728. Operation *op = ops[o]->clone();
  729. op->set ( x1, y1, x2, y2, f1, f2, calcVal[ft] );
  730. op->setContext ( false );
  731. featsel.push_back ( op );
  732. }
  733. else if ( ft == 1 )
  734. {
  735. int opssize = ( int ) ops.size();
  736. //opssize = 0;
  737. int o = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( ( ( double ) cops.size() ) + ( double ) opssize ) );
  738. Operation *op;
  739. if ( o < opssize )
  740. {
  741. int chans = ( int ) forest[0][0].dist.size();
  742. int f1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) chans );
  743. int f2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) chans );
  744. op = ops[o]->clone();
  745. op->set ( x1, y1, x2, y2, f1, f2, calcVal[ft] );
  746. op->setContext ( true );
  747. }
  748. else
  749. {
  750. int chans = integralImgs[0].channels();
  751. int f1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) chans );
  752. int f2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) chans );
  753. o -= opssize;
  754. op = cops[o]->clone();
  755. op->set ( x1, y1, x2, y2, f1, f2, calcVal[ft] );
  756. if ( f1 < forest[0][0].dist.size() )
  757. op->setContext ( true );
  758. else
  759. op->setContext ( false );
  760. }
  761. featsel.push_back ( op );
  762. }
  763. }
  764. #pragma omp parallel for private(mapit)
  765. for ( int f = 0; f < featsPerSplit; f++ )
  766. {
  767. double l_bestig = -numeric_limits< double >::max();
  768. double l_splitval = -1.0;
  769. set<vector<int> >::iterator it;
  770. vector<double> vals;
  771. double maxval = -numeric_limits<double>::max();
  772. double minval = numeric_limits<double>::max();
  773. for ( it = selFeats.begin() ; it != selFeats.end(); it++ )
  774. {
  775. Features feat;
  776. feat.feats = &feats[ ( *it ) [0]];
  777. feat.cfeats = &currentfeats[ ( *it ) [0]];
  778. feat.cTree = tree;
  779. feat.tree = &forest[tree];
  780. feat.integralImg = &integralImgs[ ( *it ) [0]];
  781. double val = featsel[f]->getVal ( feat, ( *it ) [1], ( *it ) [2] );
  782. vals.push_back ( val );
  783. maxval = std::max ( val, maxval );
  784. minval = std::min ( val, minval );
  785. }
  786. if ( minval == maxval )
  787. continue;
  788. double scale = maxval - minval;
  789. vector<double> splits;
  790. for ( int r = 0; r < randomTests; r++ )
  791. {
  792. splits.push_back ( ( ( double ) rand() / ( double ) RAND_MAX*scale ) + minval );
  793. }
  794. for ( int run = 0 ; run < randomTests; run++ )
  795. {
  796. set<vector<int> >::iterator it2;
  797. double val = splits[run];
  798. map<int, int> eL, eR;
  799. int counterL = 0, counterR = 0;
  800. int counter2 = 0;
  801. for ( it2 = selFeats.begin() ; it2 != selFeats.end(); it2++, counter2++ )
  802. {
  803. int cn = labels[ ( *it2 ) [0]] ( ( *it2 ) [1], ( *it2 ) [2] );
  804. //cout << "vals[counter2] " << vals[counter2] << " val: " << val << endl;
  805. if ( vals[counter2] < val )
  806. {
  807. //left entropie:
  808. eL[cn] = eL[cn] + 1;
  809. counterL++;
  810. }
  811. else
  812. {
  813. //right entropie:
  814. eR[cn] = eR[cn] + 1;
  815. counterR++;
  816. }
  817. }
  818. double leftent = 0.0;
  819. for ( mapit = eL.begin() ; mapit != eL.end(); mapit++ )
  820. {
  821. double p = ( double ) ( *mapit ).second / ( double ) counterL;
  822. leftent -= p * log2 ( p );
  823. }
  824. double rightent = 0.0;
  825. for ( mapit = eR.begin() ; mapit != eR.end(); mapit++ )
  826. {
  827. double p = ( double ) ( *mapit ).second / ( double ) counterR;
  828. rightent -= p * log2 ( p );
  829. }
  830. //cout << "rightent: " << rightent << " leftent: " << leftent << endl;
  831. double pl = ( double ) counterL / ( double ) ( counterL + counterR );
  832. double ig = globent - ( 1.0 - pl ) * rightent - pl * leftent;
  833. //double ig = globent - rightent - leftent;
  834. if ( useShannonEntropy )
  835. {
  836. double esplit = - ( pl * log ( pl ) + ( 1 - pl ) * log ( 1 - pl ) );
  837. ig = 2 * ig / ( globent + esplit );
  838. }
  839. if ( ig > l_bestig )
  840. {
  841. l_bestig = ig;
  842. l_splitval = val;
  843. }
  844. }
  845. #pragma omp critical
  846. {
  847. //cout << "globent: " << globent << " bestig " << bestig << " splitfeat: " << splitfeat << " splitval: " << splitval << endl;
  848. //cout << "globent: " << globent << " l_bestig " << l_bestig << " f: " << p << " l_splitval: " << l_splitval << endl;
  849. //cout << "p: " << featsubset[f] << endl;
  850. if ( l_bestig > bestig )
  851. {
  852. bestig = l_bestig;
  853. splitop = featsel[f];
  854. splitval = l_splitval;
  855. }
  856. }
  857. }
  858. //getchar();
  859. //splitop->writeInfos();
  860. //cout<< "ig: " << bestig << endl;
  861. //FIXME: delete all features!
  862. /*for(int i = 0; i < featsPerSplit; i++)
  863. {
  864. if(featsel[i] != splitop)
  865. delete featsel[i];
  866. }*/
  867. #ifdef debug
  868. cout << "globent: " << globent << " bestig " << bestig << " splitval: " << splitval << endl;
  869. #endif
  870. return bestig;
  871. }
  872. inline double SemSegContextTree::getMeanProb ( const int &x, const int &y, const int &channel, const MultiChannelImageT<unsigned short int> &currentfeats )
  873. {
  874. double val = 0.0;
  875. for ( int tree = 0; tree < nbTrees; tree++ )
  876. {
  877. val += forest[tree][currentfeats.get ( x,y,tree ) ].dist[channel];
  878. }
  879. return val / ( double ) nbTrees;
  880. }
  881. void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImageT<SparseVector> &infeats, NICE::MultiChannelImageT<SparseVector> &integralImage )
  882. {
  883. int xsize = infeats.width();
  884. int ysize = infeats.height();
  885. integralImage ( 0, 0 ).add ( infeats.get ( 0, 0 ) );
  886. //first column
  887. for ( int y = 1; y < ysize; y++ )
  888. {
  889. integralImage ( 0, y ).add ( infeats.get ( 0, y ) );
  890. integralImage ( 0, y ).add ( integralImage ( 0, y - 1 ) );
  891. }
  892. //first row
  893. for ( int x = 1; x < xsize; x++ )
  894. {
  895. integralImage ( x, 0 ).add ( infeats.get ( x, 0 ) );
  896. integralImage ( x, 0 ).add ( integralImage ( x - 1, 0 ) );
  897. }
  898. //rest
  899. for ( int y = 1; y < ysize; y++ )
  900. {
  901. for ( int x = 1; x < xsize; x++ )
  902. {
  903. integralImage ( x, y ).add ( infeats.get ( x, y ) );
  904. integralImage ( x, y ).add ( integralImage ( x, y - 1 ) );
  905. integralImage ( x, y ).add ( integralImage ( x - 1, y ) );
  906. integralImage ( x, y ).sub ( integralImage ( x - 1, y - 1 ) );
  907. }
  908. }
  909. }
  910. void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, const NICE::MultiChannelImageT<double> &lfeats, NICE::MultiChannelImageT<double> &integralImage )
  911. {
  912. int xsize = currentfeats.width();
  913. int ysize = currentfeats.height();
  914. int channels = ( int ) forest[0][0].dist.size();
  915. #pragma omp parallel for
  916. for ( int c = 0; c < channels; c++ )
  917. {
  918. integralImage.set ( 0, 0, getMeanProb ( 0, 0, c, currentfeats ), c );
  919. //first column
  920. for ( int y = 1; y < ysize; y++ )
  921. {
  922. integralImage.set ( 0, y, getMeanProb ( 0, y, c, currentfeats ) + integralImage.get ( 0, y - 1, c ), c );
  923. }
  924. //first row
  925. for ( int x = 1; x < xsize; x++ )
  926. {
  927. integralImage.set ( x, 0, getMeanProb ( x, 0, c, currentfeats ) + integralImage.get ( x - 1, 0, c ), c );
  928. }
  929. //rest
  930. for ( int y = 1; y < ysize; y++ )
  931. {
  932. for ( int x = 1; x < xsize; x++ )
  933. {
  934. double val = getMeanProb ( x, y, c, currentfeats ) + integralImage.get ( x, y - 1, c ) + integralImage.get ( x - 1, y, c ) - integralImage.get ( x - 1, y - 1, c );
  935. integralImage.set ( x, y, val, c );
  936. }
  937. }
  938. }
  939. int channels2 = ( int ) lfeats.channels();
  940. xsize = lfeats.width();
  941. ysize = lfeats.height();
  942. if ( integralImage.get ( xsize - 1, ysize - 1, channels ) == 0.0 )
  943. {
  944. #pragma omp parallel for
  945. for ( int c1 = 0; c1 < channels2; c1++ )
  946. {
  947. int c = channels + c1;
  948. integralImage.set ( 0, 0, lfeats.get ( 0, 0, c1 ), c );
  949. //first column
  950. for ( int y = 1; y < ysize; y++ )
  951. {
  952. integralImage.set ( 0, y, lfeats.get ( 0, y, c1 ) + integralImage.get ( 0, y, c ), c );
  953. }
  954. //first row
  955. for ( int x = 1; x < xsize; x++ )
  956. {
  957. integralImage.set ( x, 0, lfeats.get ( x, 0, c1 ) + integralImage.get ( x, 0, c ), c );
  958. }
  959. //rest
  960. for ( int y = 1; y < ysize; y++ )
  961. {
  962. for ( int x = 1; x < xsize; x++ )
  963. {
  964. double val = lfeats.get ( x, y, c1 ) + integralImage.get ( x, y - 1, c ) + integralImage.get ( x - 1, y, c ) - integralImage.get ( x - 1, y - 1, c );
  965. integralImage.set ( x, y, val, c );
  966. }
  967. }
  968. }
  969. }
  970. }
  971. void SemSegContextTree::train ( const MultiDataset *md )
  972. {
  973. const LabeledSet train = * ( *md ) ["train"];
  974. const LabeledSet *trainp = &train;
  975. ProgressBar pb ( "compute feats" );
  976. pb.show();
  977. //TODO: Speichefresser!, lohnt sich sparse?
  978. vector<MultiChannelImageT<double> > allfeats;
  979. vector<MultiChannelImageT<unsigned short int> > currentfeats;
  980. vector<MatrixT<int> > labels;
  981. vector<MultiChannelImageT<SparseVector> > textonMap;
  982. vector<MultiChannelImageT<SparseVector> > integralTexton;
  983. std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
  984. if ( forbidden_classes_s == "" )
  985. {
  986. forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  987. }
  988. classnames.getSelection ( forbidden_classes_s, forbidden_classes );
  989. int imgcounter = 0;
  990. int amountPixels = 0;
  991. LOOP_ALL_S ( *trainp )
  992. {
  993. EACH_INFO ( classno, info );
  994. NICE::ColorImage img;
  995. std::string currentFile = info.img();
  996. CachedExample *ce = new CachedExample ( currentFile );
  997. const LocalizationResult *locResult = info.localization();
  998. if ( locResult->size() <= 0 )
  999. {
  1000. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  1001. currentFile.c_str() );
  1002. continue;
  1003. }
  1004. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n", currentFile.c_str() );
  1005. int xsize, ysize;
  1006. ce->getImageSize ( xsize, ysize );
  1007. amountPixels += xsize * ysize;
  1008. MatrixT<int> tmpMat ( xsize, ysize );
  1009. currentfeats.push_back ( MultiChannelImageT<unsigned short int> ( xsize, ysize, nbTrees ) );
  1010. currentfeats[imgcounter].setAll ( 0 );
  1011. textonMap.push_back ( MultiChannelImageT<SparseVector> ( xsize / grid + 1, ysize / grid + 1, 1 ) );
  1012. integralTexton.push_back ( MultiChannelImageT<SparseVector> ( xsize / grid + 1, ysize / grid + 1, 1 ) );
  1013. labels.push_back ( tmpMat );
  1014. try {
  1015. img = ColorImage ( currentFile );
  1016. } catch ( Exception ) {
  1017. cerr << "SemSeg: error opening image file <" << currentFile << ">" << endl;
  1018. continue;
  1019. }
  1020. Globals::setCurrentImgFN ( currentFile );
  1021. //TODO: resize image?!
  1022. MultiChannelImageT<double> feats;
  1023. allfeats.push_back ( feats );
  1024. #ifdef LOCALFEATS
  1025. lfcw->getFeats ( img, allfeats[imgcounter] );
  1026. #else
  1027. allfeats[imgcounter].reInit ( xsize, ysize, 3, true );
  1028. for ( int x = 0; x < xsize; x++ )
  1029. {
  1030. for ( int y = 0; y < ysize; y++ )
  1031. {
  1032. for ( int r = 0; r < 3; r++ )
  1033. {
  1034. allfeats[imgcounter].set ( x, y, img.getPixel ( x, y, r ), r );
  1035. }
  1036. }
  1037. }
  1038. allfeats[imgcounter] = ColorSpace::rgbtolab ( allfeats[imgcounter] );
  1039. #endif
  1040. // getting groundtruth
  1041. NICE::Image pixelLabels ( xsize, ysize );
  1042. pixelLabels.set ( 0 );
  1043. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  1044. for ( int x = 0; x < xsize; x++ )
  1045. {
  1046. for ( int y = 0; y < ysize; y++ )
  1047. {
  1048. classno = pixelLabels.getPixel ( x, y );
  1049. labels[imgcounter] ( x, y ) = classno;
  1050. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  1051. continue;
  1052. labelcounter[classno]++;
  1053. }
  1054. }
  1055. imgcounter++;
  1056. pb.update ( trainp->count() );
  1057. delete ce;
  1058. }
  1059. pb.hide();
  1060. map<int, int>::iterator mapit;
  1061. int classes = 0;
  1062. for ( mapit = labelcounter.begin(); mapit != labelcounter.end(); mapit++ )
  1063. {
  1064. labelmap[mapit->first] = classes;
  1065. labelmapback[classes] = mapit->first;
  1066. classes++;
  1067. }
  1068. //balancing
  1069. int featcounter = 0;
  1070. a = vector<double> ( classes, 0.0 );
  1071. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  1072. {
  1073. int xsize = ( int ) currentfeats[iCounter].width();
  1074. int ysize = ( int ) currentfeats[iCounter].height();
  1075. for ( int x = 0; x < xsize; x++ )
  1076. {
  1077. for ( int y = 0; y < ysize; y++ )
  1078. {
  1079. featcounter++;
  1080. int cn = labels[iCounter] ( x, y );
  1081. if(labelmap.find(cn) == labelmap.end())
  1082. continue;
  1083. a[labelmap[cn]] ++;
  1084. }
  1085. }
  1086. }
  1087. for ( int i = 0; i < ( int ) a.size(); i++ )
  1088. {
  1089. a[i] /= ( double ) featcounter;
  1090. }
  1091. #ifdef DEBUG
  1092. for ( int i = 0; i < ( int ) a.size(); i++ )
  1093. {
  1094. cout << "a[" << i << "]: " << a[i] << endl;
  1095. }
  1096. cout << "a.size: " << a.size() << endl;
  1097. #endif
  1098. depth = 0;
  1099. int uniquenumber = 0;
  1100. for ( int t = 0; t < nbTrees; t++ )
  1101. {
  1102. vector<TreeNode> tree;
  1103. tree.push_back ( TreeNode() );
  1104. tree[0].dist = vector<double> ( classes, 0.0 );
  1105. tree[0].depth = depth;
  1106. tree[0].featcounter = amountPixels;
  1107. tree[0].nodeNumber = uniquenumber;
  1108. uniquenumber++;
  1109. forest.push_back ( tree );
  1110. }
  1111. vector<int> startnode ( nbTrees, 0 );
  1112. bool allleaf = false;
  1113. //int baseFeatSize = allfeats[0].size();
  1114. vector<MultiChannelImageT<double> > integralImgs ( imgcounter, MultiChannelImageT<double>() );
  1115. while ( !allleaf && depth < maxDepth )
  1116. {
  1117. #ifdef DEBUG
  1118. cout << "depth: " << depth << endl;
  1119. #endif
  1120. allleaf = true;
  1121. vector<MultiChannelImageT<unsigned short int> > lastfeats = currentfeats;
  1122. #if 1
  1123. Timer timer;
  1124. timer.start();
  1125. #endif
  1126. for ( int tree = 0; tree < nbTrees; tree++ )
  1127. {
  1128. int t = ( int ) forest[tree].size();
  1129. int s = startnode[tree];
  1130. startnode[tree] = t;
  1131. //TODO vielleicht parallel wenn nächste schleife trotzdem noch parallelsiert würde, die hat mehr gewicht
  1132. //#pragma omp parallel for
  1133. for ( int i = s; i < t; i++ )
  1134. {
  1135. if ( !forest[tree][i].isleaf && forest[tree][i].left < 0 )
  1136. {
  1137. #if 0
  1138. timer.stop();
  1139. cout << "time 1: " << timer.getLast() << endl;
  1140. timer.start();
  1141. #endif
  1142. Operation *splitfeat = NULL;
  1143. double splitval;
  1144. double bestig = getBestSplit ( allfeats, lastfeats, integralImgs, labels, i, splitfeat, splitval, tree );
  1145. #if 0
  1146. timer.stop();
  1147. double tl = timer.getLast();
  1148. if ( tl > 10.0 )
  1149. {
  1150. cout << "time 2: " << tl << endl;
  1151. cout << "slow split: " << splitfeat->writeInfos() << endl;
  1152. getchar();
  1153. }
  1154. timer.start();
  1155. #endif
  1156. forest[tree][i].feat = splitfeat;
  1157. forest[tree][i].decision = splitval;
  1158. if ( splitfeat != NULL )
  1159. {
  1160. allleaf = false;
  1161. int left = forest[tree].size();
  1162. forest[tree].push_back ( TreeNode() );
  1163. forest[tree].push_back ( TreeNode() );
  1164. int right = left + 1;
  1165. forest[tree][i].left = left;
  1166. forest[tree][i].right = right;
  1167. forest[tree][left].dist = vector<double> ( classes, 0.0 );
  1168. forest[tree][right].dist = vector<double> ( classes, 0.0 );
  1169. forest[tree][left].depth = depth + 1;
  1170. forest[tree][right].depth = depth + 1;
  1171. forest[tree][left].featcounter = 0;
  1172. forest[tree][right].featcounter = 0;
  1173. forest[tree][left].nodeNumber = uniquenumber;
  1174. int leftu = uniquenumber;
  1175. uniquenumber++;
  1176. forest[tree][right].nodeNumber = uniquenumber;
  1177. int rightu = uniquenumber;
  1178. uniquenumber++;
  1179. forest[tree][right].featcounter = 0;
  1180. #if 0
  1181. timer.stop();
  1182. cout << "time 3: " << timer.getLast() << endl;
  1183. timer.start();
  1184. #endif
  1185. #pragma omp parallel for
  1186. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  1187. {
  1188. int xsize = currentfeats[iCounter].width();
  1189. int ysize = currentfeats[iCounter].height();
  1190. for ( int x = 0; x < xsize; x++ )
  1191. {
  1192. for ( int y = 0; y < ysize; y++ )
  1193. {
  1194. if ( currentfeats[iCounter].get ( x, y, tree ) == i )
  1195. {
  1196. Features feat;
  1197. feat.feats = &allfeats[iCounter];
  1198. feat.cfeats = &lastfeats[iCounter];
  1199. feat.cTree = tree;
  1200. feat.tree = &forest[tree];
  1201. feat.integralImg = &integralImgs[iCounter];
  1202. double val = splitfeat->getVal ( feat, x, y );
  1203. int subx = x / grid;
  1204. int suby = y / grid;
  1205. #pragma omp critical
  1206. if ( val < splitval )
  1207. {
  1208. currentfeats[iCounter].set ( x, y, left, tree );
  1209. if(labelmap.find(labels[iCounter] ( x, y )) != labelmap.end())
  1210. forest[tree][left].dist[labelmap[labels[iCounter] ( x, y ) ]]++;
  1211. forest[tree][left].featcounter++;
  1212. SparseVector v;
  1213. v.insert ( pair<int, double> ( leftu, 1.0 ) );
  1214. textonMap[iCounter] ( subx, suby ).add ( v );
  1215. }
  1216. else
  1217. {
  1218. currentfeats[iCounter].set ( x, y, right, tree );
  1219. if(labelmap.find(labels[iCounter] ( x, y )) != labelmap.end())
  1220. forest[tree][right].dist[labelmap[labels[iCounter] ( x, y ) ]]++;
  1221. forest[tree][right].featcounter++;
  1222. //feld im subsampled finden und in diesem rechts hochzählen
  1223. SparseVector v;
  1224. v.insert ( pair<int, double> ( rightu, 1.0 ) );
  1225. textonMap[iCounter] ( subx, suby ).add ( v );
  1226. }
  1227. }
  1228. }
  1229. }
  1230. }
  1231. #if 0
  1232. timer.stop();
  1233. cout << "time 4: " << timer.getLast() << endl;
  1234. timer.start();
  1235. #endif
  1236. // forest[tree][right].featcounter = forest[tree][i].featcounter - forest[tree][left].featcounter;
  1237. double lcounter = 0.0, rcounter = 0.0;
  1238. for ( uint d = 0; d < forest[tree][left].dist.size(); d++ )
  1239. {
  1240. if ( forbidden_classes.find ( labelmapback[d] ) != forbidden_classes.end() )
  1241. {
  1242. forest[tree][left].dist[d] = 0;
  1243. forest[tree][right].dist[d] = 0;
  1244. }
  1245. else
  1246. {
  1247. forest[tree][left].dist[d] /= a[d];
  1248. lcounter += forest[tree][left].dist[d];
  1249. forest[tree][right].dist[d] /= a[d];
  1250. rcounter += forest[tree][right].dist[d];
  1251. }
  1252. }
  1253. #if 0
  1254. timer.stop();
  1255. cout << "time 5: " << timer.getLast() << endl;
  1256. timer.start();
  1257. #endif
  1258. if ( lcounter <= 0 || rcounter <= 0 )
  1259. {
  1260. cout << "lcounter : " << lcounter << " rcounter: " << rcounter << endl;
  1261. cout << "splitval: " << splitval << " splittype: " << splitfeat->writeInfos() << endl;
  1262. cout << "bestig: " << bestig << endl;
  1263. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  1264. {
  1265. int xsize = currentfeats[iCounter].width();
  1266. int ysize = currentfeats[iCounter].height();
  1267. int counter = 0;
  1268. for ( int x = 0; x < xsize; x++ )
  1269. {
  1270. for ( int y = 0; y < ysize; y++ )
  1271. {
  1272. if ( lastfeats[iCounter].get ( x, y, tree ) == i )
  1273. {
  1274. if ( ++counter > 30 )
  1275. break;
  1276. Features feat;
  1277. feat.feats = &allfeats[iCounter];
  1278. feat.cfeats = &lastfeats[iCounter];
  1279. feat.cTree = tree;
  1280. feat.tree = &forest[tree];
  1281. feat.integralImg = &integralImgs[iCounter];
  1282. double val = splitfeat->getVal ( feat, x, y );
  1283. cout << "splitval: " << splitval << " val: " << val << endl;
  1284. }
  1285. }
  1286. }
  1287. }
  1288. assert ( lcounter > 0 && rcounter > 0 );
  1289. }
  1290. for ( uint d = 0; d < forest[tree][left].dist.size(); d++ )
  1291. {
  1292. forest[tree][left].dist[d] /= lcounter;
  1293. forest[tree][right].dist[d] /= rcounter;
  1294. }
  1295. }
  1296. else
  1297. {
  1298. forest[tree][i].isleaf = true;
  1299. }
  1300. }
  1301. }
  1302. #if 0
  1303. timer.stop();
  1304. cout << "time after tree: " << timer.getLast() << endl;
  1305. timer.start();
  1306. #endif
  1307. }
  1308. //compute integral image
  1309. int channels = classes + allfeats[0].channels();
  1310. #if 0
  1311. timer.stop();
  1312. cout << "time for part0: " << timer.getLast() << endl;
  1313. timer.start();
  1314. #endif
  1315. if ( integralImgs[0].width() == 0 )
  1316. {
  1317. for ( int i = 0; i < imgcounter; i++ )
  1318. {
  1319. int xsize = allfeats[i].width();
  1320. int ysize = allfeats[i].height();
  1321. integralImgs[i].reInit ( xsize, ysize, channels );
  1322. integralImgs[i].setAll ( 0.0 );
  1323. }
  1324. }
  1325. #if 0
  1326. timer.stop();
  1327. cout << "time for part1: " << timer.getLast() << endl;
  1328. timer.start();
  1329. #endif
  1330. #pragma omp parallel for
  1331. for ( int i = 0; i < imgcounter; i++ )
  1332. {
  1333. computeIntegralImage ( currentfeats[i], allfeats[i], integralImgs[i] );
  1334. computeIntegralImage ( textonMap[i], integralTexton[i] );
  1335. }
  1336. #if 1
  1337. timer.stop();
  1338. cout << "time for depth " << depth << ": " << timer.getLast() << endl;
  1339. #endif
  1340. depth++;
  1341. }
  1342. #ifdef DEBUG
  1343. for ( int tree = 0; tree < nbTrees; tree++ )
  1344. {
  1345. int t = ( int ) forest[tree].size();
  1346. for ( int i = 0; i < t; i++ )
  1347. {
  1348. printf ( "tree[%i]: left: %i, right: %i", i, forest[tree][i].left, forest[tree][i].right );
  1349. if ( !forest[tree][i].isleaf && forest[tree][i].left != -1 )
  1350. {
  1351. cout << ", feat: " << forest[tree][i].feat->writeInfos() << " ";
  1352. opOverview[forest[tree][i].feat->getOps() ]++;
  1353. contextOverview[forest[tree][i].depth][ ( int ) forest[tree][i].feat->getContext() ]++;
  1354. }
  1355. for ( int d = 0; d < ( int ) forest[tree][i].dist.size(); d++ )
  1356. {
  1357. cout << " " << forest[tree][i].dist[d];
  1358. }
  1359. cout << endl;
  1360. }
  1361. }
  1362. for ( uint c = 0; c < ops.size(); c++ )
  1363. {
  1364. cout << ops[c]->writeInfos() << ": " << opOverview[ops[c]->getOps() ] << endl;
  1365. }
  1366. for ( uint c = 0; c < cops.size(); c++ )
  1367. {
  1368. cout << cops[c]->writeInfos() << ": " << opOverview[cops[c]->getOps() ] << endl;
  1369. }
  1370. for ( int d = 0; d < maxDepth; d++ )
  1371. {
  1372. double sum = contextOverview[d][0] + contextOverview[d][1];
  1373. contextOverview[d][0] /= sum;
  1374. contextOverview[d][1] /= sum;
  1375. cout << "depth: " << d << " woContext: " << contextOverview[d][0] << " wContext: " << contextOverview[d][1] << endl;
  1376. }
  1377. #endif
  1378. }
  1379. void SemSegContextTree::semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities )
  1380. {
  1381. int xpos = 8;
  1382. //int xpos = 15;
  1383. int ypos = 78;
  1384. int xsize;
  1385. int ysize;
  1386. ce->getImageSize ( xsize, ysize );
  1387. int numClasses = classNames->numClasses();
  1388. fprintf ( stderr, "ContextTree classification !\n" );
  1389. probabilities.reInit ( xsize, ysize, numClasses, true );
  1390. probabilities.setAll ( 0 );
  1391. NICE::ColorImage img;
  1392. std::string currentFile = Globals::getCurrentImgFN();
  1393. try {
  1394. img = ColorImage ( currentFile );
  1395. } catch ( Exception ) {
  1396. cerr << "SemSeg: error opening image file <" << currentFile << ">" << endl;
  1397. return;
  1398. }
  1399. //TODO: resize image?!
  1400. MultiChannelImageT<double> feats;
  1401. MultiChannelImageT<SparseVector> textonMap ( xsize / grid + 1, ysize / grid + 1, 1 );
  1402. MultiChannelImageT<SparseVector> integralTexton ( xsize / grid + 1, ysize / grid + 1, 1 );
  1403. #ifdef LOCALFEATS
  1404. lfcw->getFeats ( img, feats );
  1405. #else
  1406. feats.reInit ( xsize, ysize, 3, true );
  1407. for ( int x = 0; x < xsize; x++ )
  1408. {
  1409. for ( int y = 0; y < ysize; y++ )
  1410. {
  1411. for ( int r = 0; r < 3; r++ )
  1412. {
  1413. feats.set ( x, y, img.getPixel ( x, y, r ), r );
  1414. }
  1415. }
  1416. }
  1417. feats = ColorSpace::rgbtolab ( feats );
  1418. #endif
  1419. bool allleaf = false;
  1420. MultiChannelImageT<double> integralImg;
  1421. MultiChannelImageT<unsigned short int> currentfeats ( xsize, ysize, nbTrees );
  1422. currentfeats.setAll ( 0 );
  1423. depth = 0;
  1424. for ( int d = 0; d < maxDepth && !allleaf; d++ )
  1425. {
  1426. allleaf = true;
  1427. //TODO vielleicht parallel wenn nächste schleife auch noch parallelsiert würde, die hat mehr gewicht
  1428. //#pragma omp parallel for
  1429. MultiChannelImageT<unsigned short int> lastfeats = currentfeats;
  1430. for ( int tree = 0; tree < nbTrees; tree++ )
  1431. {
  1432. for ( int x = 0; x < xsize; x++ )
  1433. {
  1434. for ( int y = 0; y < ysize; y++ )
  1435. {
  1436. int t = currentfeats.get ( x, y, tree );
  1437. if ( forest[tree][t].left > 0 )
  1438. {
  1439. allleaf = false;
  1440. Features feat;
  1441. feat.feats = &feats;
  1442. feat.cfeats = &lastfeats;
  1443. feat.cTree = tree;
  1444. feat.tree = &forest[tree];
  1445. feat.integralImg = &integralImg;
  1446. double val = forest[tree][t].feat->getVal ( feat, x, y );
  1447. int subx = x / grid;
  1448. int suby = y / grid;
  1449. if ( val < forest[tree][t].decision )
  1450. {
  1451. currentfeats.set ( x, y, forest[tree][t].left, tree );
  1452. SparseVector v;
  1453. v.insert ( pair<int, double> ( forest[tree][forest[tree][t].left].nodeNumber, 1.0 ) );
  1454. textonMap ( subx, suby ).add ( v );
  1455. }
  1456. else
  1457. {
  1458. currentfeats.set ( x, y, forest[tree][t].right, tree );
  1459. SparseVector v;
  1460. v.insert ( pair<int, double> ( forest[tree][forest[tree][t].right].nodeNumber, 1.0 ) );
  1461. textonMap ( subx, suby ).add ( v );
  1462. }
  1463. if(x == xpos && y == ypos)
  1464. {
  1465. cout << "val: " << val << " decision: " << forest[tree][t].decision << " details: " << forest[tree][t].feat->writeInfos() << endl;
  1466. }
  1467. }
  1468. }
  1469. }
  1470. //compute integral image
  1471. int channels = ( int ) labelmap.size() + feats.channels();
  1472. if ( integralImg.width() == 0 )
  1473. {
  1474. int xsize = feats.width();
  1475. int ysize = feats.height();
  1476. integralImg.reInit ( xsize, ysize, channels );
  1477. integralImgs[i].setAll ( 0.0 );
  1478. }
  1479. }
  1480. computeIntegralImage ( currentfeats, feats, integralImg );
  1481. computeIntegralImage ( textonMap, integralTexton );
  1482. depth++;
  1483. }
  1484. cout << forest[0][currentfeats.get ( xpos, ypos, 0 )].dist << endl;
  1485. if ( pixelWiseLabeling )
  1486. {
  1487. //finales labeln:
  1488. long int offset = 0;
  1489. for ( int x = 0; x < xsize; x++ )
  1490. {
  1491. for ( int y = 0; y < ysize; y++, offset++ )
  1492. {
  1493. double maxvalue = - numeric_limits<double>::max(); //TODO: das muss nur pro knoten gemacht werden, nicht pro pixel
  1494. int maxindex = 0;
  1495. uint s = forest[0][0].dist.size();
  1496. for ( uint i = 0; i < s; i++ )
  1497. {
  1498. probabilities.data[labelmapback[i]][offset] = getMeanProb ( x, y, i, currentfeats );
  1499. if ( probabilities.data[labelmapback[i]][offset] > maxvalue )
  1500. {
  1501. maxvalue = probabilities.data[labelmapback[i]][offset];
  1502. maxindex = labelmapback[i];
  1503. }
  1504. segresult.setPixel ( x, y, maxindex );
  1505. }
  1506. if ( maxvalue > 1 )
  1507. cout << "maxvalue: " << maxvalue << endl;
  1508. }
  1509. }
  1510. }
  1511. else
  1512. {
  1513. //final labeling using segmentation
  1514. Matrix regions;
  1515. //showImage(img);
  1516. int regionNumber = segmentation->segRegions ( img, regions );
  1517. cout << "regions: " << regionNumber << endl;
  1518. int dSize = forest[0][0].dist.size();
  1519. vector<vector<double> > regionProbs ( regionNumber, vector<double> ( dSize, 0.0 ) );
  1520. vector<int> bestlabels ( regionNumber, 0 );
  1521. /*
  1522. for(int r = 0; r < regionNumber; r++)
  1523. {
  1524. Image over(img.width(), img.height());
  1525. for(int y = 0; y < img.height(); y++)
  1526. {
  1527. for(int x = 0; x < img.width(); x++)
  1528. {
  1529. if(((int)regions(x,y)) == r)
  1530. over.setPixel(x,y,1);
  1531. else
  1532. over.setPixel(x,y,0);
  1533. }
  1534. }
  1535. cout << "r: " << r << endl;
  1536. showImageOverlay(img, over);
  1537. }
  1538. */
  1539. for ( int y = 0; y < img.height(); y++ )
  1540. {
  1541. for ( int x = 0; x < img.width(); x++ )
  1542. {
  1543. int cregion = regions ( x, y );
  1544. for ( int d = 0; d < dSize; d++ )
  1545. {
  1546. regionProbs[cregion][d] += getMeanProb ( x, y, d, currentfeats );
  1547. }
  1548. }
  1549. }
  1550. for ( int r = 0; r < regionNumber; r++ )
  1551. {
  1552. double maxval = regionProbs[r][0];
  1553. bestlabels[r] = 0;
  1554. for ( int d = 1; d < dSize; d++ )
  1555. {
  1556. if ( maxval < regionProbs[r][d] )
  1557. {
  1558. maxval = regionProbs[r][d];
  1559. bestlabels[r] = d;
  1560. }
  1561. }
  1562. bestlabels[r] = labelmapback[bestlabels[r]];
  1563. }
  1564. for ( int y = 0; y < img.height(); y++ )
  1565. {
  1566. for ( int x = 0; x < img.width(); x++ )
  1567. {
  1568. segresult.setPixel ( x, y, bestlabels[regions ( x,y ) ] );
  1569. }
  1570. }
  1571. }
  1572. cout << "segmentation finished" << endl;
  1573. }
  1574. void SemSegContextTree::store ( std::ostream & os, int format ) const
  1575. {
  1576. os << nbTrees << endl;
  1577. classnames.store ( os );
  1578. map<int, int>::const_iterator it;
  1579. os << labelmap.size() << endl;
  1580. for ( it = labelmap.begin() ; it != labelmap.end(); it++ )
  1581. os << ( *it ).first << " " << ( *it ).second << endl;
  1582. os << labelmapback.size() << endl;
  1583. for ( it = labelmapback.begin() ; it != labelmapback.end(); it++ )
  1584. os << ( *it ).first << " " << ( *it ).second << endl;
  1585. int trees = forest.size();
  1586. os << trees << endl;
  1587. for ( int t = 0; t < trees; t++ )
  1588. {
  1589. int nodes = forest[t].size();
  1590. os << nodes << endl;
  1591. for ( int n = 0; n < nodes; n++ )
  1592. {
  1593. 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;
  1594. os << forest[t][n].dist << endl;
  1595. if ( forest[t][n].feat == NULL )
  1596. os << -1 << endl;
  1597. else
  1598. {
  1599. os << forest[t][n].feat->getOps() << endl;
  1600. forest[t][n].feat->store ( os );
  1601. }
  1602. }
  1603. }
  1604. }
  1605. void SemSegContextTree::restore ( std::istream & is, int format )
  1606. {
  1607. is >> nbTrees;
  1608. classnames.restore ( is );
  1609. int lsize;
  1610. is >> lsize;
  1611. labelmap.clear();
  1612. for ( int l = 0; l < lsize; l++ )
  1613. {
  1614. int first, second;
  1615. is >> first;
  1616. is >> second;
  1617. labelmap[first] = second;
  1618. }
  1619. is >> lsize;
  1620. labelmapback.clear();
  1621. for ( int l = 0; l < lsize; l++ )
  1622. {
  1623. int first, second;
  1624. is >> first;
  1625. is >> second;
  1626. labelmapback[first] = second;
  1627. }
  1628. int trees;
  1629. is >> trees;
  1630. forest.clear();
  1631. for ( int t = 0; t < trees; t++ )
  1632. {
  1633. vector<TreeNode> tmptree;
  1634. forest.push_back ( tmptree );
  1635. int nodes;
  1636. is >> nodes;
  1637. //cout << "nodes: " << nodes << endl;
  1638. for ( int n = 0; n < nodes; n++ )
  1639. {
  1640. TreeNode tmpnode;
  1641. forest[t].push_back ( tmpnode );
  1642. is >> forest[t][n].left;
  1643. is >> forest[t][n].right;
  1644. is >> forest[t][n].decision;
  1645. is >> forest[t][n].isleaf;
  1646. is >> forest[t][n].depth;
  1647. is >> forest[t][n].featcounter;
  1648. is >> forest[t][n].nodeNumber;
  1649. is >> forest[t][n].dist;
  1650. int feattype;
  1651. is >> feattype;
  1652. assert ( feattype < NBOPERATIONS );
  1653. forest[t][n].feat = NULL;
  1654. if ( feattype >= 0 )
  1655. {
  1656. for ( int o = 0; o < ops.size(); o++ )
  1657. {
  1658. if ( ops[o]->getOps() == feattype )
  1659. {
  1660. forest[t][n].feat = ops[o]->clone();
  1661. break;
  1662. }
  1663. }
  1664. if ( forest[t][n].feat == NULL )
  1665. {
  1666. for ( int o = 0; o < cops.size(); o++ )
  1667. {
  1668. if ( cops[o]->getOps() == feattype )
  1669. {
  1670. forest[t][n].feat = cops[o]->clone();
  1671. break;
  1672. }
  1673. }
  1674. }
  1675. assert ( forest[t][n].feat != NULL );
  1676. forest[t][n].feat->restore ( is );
  1677. }
  1678. }
  1679. }
  1680. }