SemSegContextTree.cpp 44 KB

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