SemSegContextTree.cpp 50 KB

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