SemSegContextTree.cpp 43 KB

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