SemSegContextTree.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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 = 0;
  555. for ( int iCounter = 0; iCounter < imgCount; iCounter++ )
  556. {
  557. int xsize = ( int )currentfeats[iCounter].width();
  558. int ysize = ( int )currentfeats[iCounter].height();
  559. for ( int x = 0; x < xsize; x++ )
  560. {
  561. for ( int y = 0; y < ysize; y++ )
  562. {
  563. if ( currentfeats[iCounter].get( x, y, tree ) == node )
  564. {
  565. featcounter++;
  566. }
  567. }
  568. }
  569. }
  570. if ( featcounter < minFeats )
  571. {
  572. //cout << "only " << featcounter << " feats in current node -> it's a leaf" << endl;
  573. return 0.0;
  574. }
  575. vector<double> fraction( a.size(), 0.0 );
  576. for ( uint i = 0; i < fraction.size(); i++ )
  577. {
  578. if ( forbidden_classes.find( labelmapback[i] ) != forbidden_classes.end() )
  579. fraction[i] = 0;
  580. else
  581. fraction[i] = (( double )maxSamples ) / (( double )featcounter * a[i] * a.size() );
  582. //cout << "fraction["<<i<<"]: "<< fraction[i] << " a[" << i << "]: " << a[i] << endl;
  583. }
  584. //cout << "a.size(): " << a.size() << endl;
  585. //getchar();
  586. featcounter = 0;
  587. for ( int iCounter = 0; iCounter < imgCount; iCounter++ )
  588. {
  589. int xsize = ( int )currentfeats[iCounter].width();
  590. int ysize = ( int )currentfeats[iCounter].height();
  591. for ( int x = 0; x < xsize; x++ )
  592. {
  593. for ( int y = 0; y < ysize; y++ )
  594. {
  595. if ( currentfeats[iCounter].get( x, y, tree ) == node )
  596. {
  597. int cn = labels[iCounter]( x, y );
  598. double randD = ( double )rand() / ( double )RAND_MAX;
  599. if ( randD < fraction[labelmap[cn]] )
  600. {
  601. vector<int> tmp( 3, 0 );
  602. tmp[0] = iCounter;
  603. tmp[1] = x;
  604. tmp[2] = y;
  605. featcounter++;
  606. selFeats.insert( tmp );
  607. e[cn]++;
  608. }
  609. }
  610. }
  611. }
  612. }
  613. //cout << "size: " << selFeats.size() << endl;
  614. //getchar();
  615. map<int, int>::iterator mapit;
  616. double globent = 0.0;
  617. for ( mapit = e.begin() ; mapit != e.end(); mapit++ )
  618. {
  619. //cout << "class: " << mapit->first << ": " << mapit->second << endl;
  620. double p = ( double )( *mapit ).second / ( double )featcounter;
  621. globent += p * log2( p );
  622. }
  623. globent = -globent;
  624. if ( globent < 0.5 )
  625. {
  626. //cout << "globent to small: " << globent << endl;
  627. return 0.0;
  628. }
  629. int classes = ( int )forest[tree][0].dist.size();
  630. featsel.clear();
  631. for ( int i = 0; i < featsPerSplit; i++ )
  632. {
  633. int x1, x2, y1, y2;
  634. int ft = ( int )(( double )rand() / ( double )RAND_MAX * ( double )ftypes );
  635. int tmpws = windowSize;
  636. if ( integralImgs[0].width() == 0 )
  637. ft = 0;
  638. if ( ft > 0 )
  639. {
  640. tmpws *= 4;
  641. }
  642. if ( useGaussian )
  643. {
  644. double sigma = ( double )tmpws / 2.0;
  645. x1 = randGaussDouble( sigma ) * ( double )tmpws;
  646. x2 = randGaussDouble( sigma ) * ( double )tmpws;
  647. y1 = randGaussDouble( sigma ) * ( double )tmpws;
  648. y2 = randGaussDouble( sigma ) * ( double )tmpws;
  649. }
  650. else
  651. {
  652. x1 = ( int )(( double )rand() / ( double )RAND_MAX * ( double )tmpws ) - tmpws / 2;
  653. x2 = ( int )(( double )rand() / ( double )RAND_MAX * ( double )tmpws ) - tmpws / 2;
  654. y1 = ( int )(( double )rand() / ( double )RAND_MAX * ( double )tmpws ) - tmpws / 2;
  655. y2 = ( int )(( double )rand() / ( double )RAND_MAX * ( double )tmpws ) - tmpws / 2;
  656. }
  657. if ( ft == 0 )
  658. {
  659. int f1 = ( int )(( double )rand() / ( double )RAND_MAX * ( double )featdim );
  660. int f2 = ( int )(( double )rand() / ( double )RAND_MAX * ( double )featdim );
  661. int o = ( int )(( double )rand() / ( double )RAND_MAX * ( double )ops.size() );
  662. Operation *op = ops[o]->clone();
  663. op->set( x1, y1, x2, y2, f1, f2, calcVal[ft] );
  664. featsel.push_back( op );
  665. }
  666. else if ( ft == 1 )
  667. {
  668. int opssize = ( int )ops.size();
  669. //opssize = 0;
  670. int o = ( int )(( double )rand() / ( double )RAND_MAX * ((( double )cops.size() ) + ( double )opssize ) );
  671. Operation *op;
  672. if ( o < opssize )
  673. {
  674. int chans = ( int )forest[0][0].dist.size();
  675. int f1 = ( int )(( double )rand() / ( double )RAND_MAX * ( double )chans );
  676. int f2 = ( int )(( double )rand() / ( double )RAND_MAX * ( double )chans );
  677. op = ops[o]->clone();
  678. op->set( x1, y1, x2, y2, f1, f2, calcVal[ft] );
  679. }
  680. else
  681. {
  682. int chans = integralImgs[0].channels();
  683. int f1 = ( int )(( double )rand() / ( double )RAND_MAX * ( double )chans );
  684. int f2 = ( int )(( double )rand() / ( double )RAND_MAX * ( double )chans );
  685. o -= opssize;
  686. op = cops[o]->clone();
  687. op->set( x1, y1, x2, y2, f1, f2, calcVal[ft] );
  688. }
  689. featsel.push_back( op );
  690. }
  691. }
  692. #pragma omp parallel for private(mapit)
  693. for ( int f = 0; f < featsPerSplit; f++ )
  694. {
  695. double l_bestig = -numeric_limits< double >::max();
  696. double l_splitval = -1.0;
  697. set<vector<int> >::iterator it;
  698. vector<double> vals;
  699. for ( it = selFeats.begin() ; it != selFeats.end(); it++ )
  700. {
  701. Features feat;
  702. feat.feats = &feats[( *it )[0]];
  703. feat.cfeats = &currentfeats[( *it )[0]];
  704. feat.cTree = tree;
  705. feat.tree = &forest[tree];
  706. feat.integralImg = &integralImgs[( *it )[0]];
  707. vals.push_back( featsel[f]->getVal( feat, ( *it )[1], ( *it )[2] ) );
  708. }
  709. int counter = 0;
  710. for ( it = selFeats.begin() ; it != selFeats.end(); it++ , counter++ )
  711. {
  712. set<vector<int> >::iterator it2;
  713. double val = vals[counter];
  714. map<int, int> eL, eR;
  715. int counterL = 0, counterR = 0;
  716. int counter2 = 0;
  717. for ( it2 = selFeats.begin() ; it2 != selFeats.end(); it2++, counter2++ )
  718. {
  719. int cn = labels[( *it2 )[0]](( *it2 )[1], ( *it2 )[2] );
  720. //cout << "vals[counter2] " << vals[counter2] << " val: " << val << endl;
  721. if ( vals[counter2] < val )
  722. {
  723. //left entropie:
  724. eL[cn] = eL[cn] + 1;
  725. counterL++;
  726. }
  727. else
  728. {
  729. //right entropie:
  730. eR[cn] = eR[cn] + 1;
  731. counterR++;
  732. }
  733. }
  734. double leftent = 0.0;
  735. for ( mapit = eL.begin() ; mapit != eL.end(); mapit++ )
  736. {
  737. double p = ( double )( *mapit ).second / ( double )counterL;
  738. leftent -= p * log2( p );
  739. }
  740. double rightent = 0.0;
  741. for ( mapit = eR.begin() ; mapit != eR.end(); mapit++ )
  742. {
  743. double p = ( double )( *mapit ).second / ( double )counterR;
  744. rightent -= p * log2( p );
  745. }
  746. //cout << "rightent: " << rightent << " leftent: " << leftent << endl;
  747. double pl = ( double )counterL / ( double )( counterL + counterR );
  748. double ig = globent - ( 1.0 - pl ) * rightent - pl * leftent;
  749. //double ig = globent - rightent - leftent;
  750. if ( useShannonEntropy )
  751. {
  752. double esplit = - ( pl * log( pl ) + ( 1 - pl ) * log( 1 - pl ) );
  753. ig = 2 * ig / ( globent + esplit );
  754. }
  755. if ( ig > l_bestig )
  756. {
  757. l_bestig = ig;
  758. l_splitval = val;
  759. }
  760. }
  761. #pragma omp critical
  762. {
  763. //cout << "globent: " << globent << " bestig " << bestig << " splitfeat: " << splitfeat << " splitval: " << splitval << endl;
  764. //cout << "globent: " << globent << " l_bestig " << l_bestig << " f: " << p << " l_splitval: " << l_splitval << endl;
  765. //cout << "p: " << featsubset[f] << endl;
  766. if ( l_bestig > bestig )
  767. {
  768. bestig = l_bestig;
  769. splitop = featsel[f];
  770. splitval = l_splitval;
  771. }
  772. }
  773. }
  774. //splitop->writeInfos();
  775. //cout<< "ig: " << bestig << endl;
  776. //FIXME: delete all features!
  777. /*for(int i = 0; i < featsPerSplit; i++)
  778. {
  779. if(featsel[i] != splitop)
  780. delete featsel[i];
  781. }*/
  782. #ifdef debug
  783. cout << "globent: " << globent << " bestig " << bestig << " splitval: " << splitval << endl;
  784. #endif
  785. return bestig;
  786. }
  787. inline double SemSegContextTree::getMeanProb( const int &x, const int &y, const int &channel, const MultiChannelImageT<int> &currentfeats )
  788. {
  789. double val = 0.0;
  790. for ( int tree = 0; tree < nbTrees; tree++ )
  791. {
  792. val += forest[tree][currentfeats.get( x,y,tree )].dist[channel];
  793. }
  794. return val / ( double )nbTrees;
  795. }
  796. void SemSegContextTree::computeIntegralImage( const NICE::MultiChannelImageT<int> &currentfeats, const NICE::MultiChannelImageT<double> &lfeats, NICE::MultiChannelImageT<double> &integralImage )
  797. {
  798. int xsize = currentfeats.width();
  799. int ysize = currentfeats.height();
  800. int channels = ( int )forest[0][0].dist.size();
  801. #pragma omp parallel for
  802. for ( int c = 0; c < channels; c++ )
  803. {
  804. integralImage.set( 0, 0, getMeanProb( 0, 0, c, currentfeats ), c );
  805. //first column
  806. for ( int y = 1; y < ysize; y++ )
  807. {
  808. integralImage.set( 0, y, getMeanProb( 0, y, c, currentfeats ) + integralImage.get( 0, y, c ), c );
  809. }
  810. //first row
  811. for ( int x = 1; x < xsize; x++ )
  812. {
  813. integralImage.set( x, 0, getMeanProb( x, 0, c, currentfeats ) + integralImage.get( x, 0, c ), c );
  814. }
  815. //rest
  816. for ( int y = 1; y < ysize; y++ )
  817. {
  818. for ( int x = 1; x < xsize; x++ )
  819. {
  820. 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 );
  821. integralImage.set( x, y, val, c );
  822. }
  823. }
  824. }
  825. int channels2 = ( int )lfeats.channels();
  826. xsize = lfeats.width();
  827. ysize = lfeats.height();
  828. if ( integralImage.get( xsize - 1, ysize - 1, channels ) == 0.0 )
  829. {
  830. #pragma omp parallel for
  831. for ( int c1 = 0; c1 < channels2; c1++ )
  832. {
  833. int c = channels + c1;
  834. integralImage.set( 0, 0, lfeats.get( 0, 0, c1 ), c );
  835. //first column
  836. for ( int y = 1; y < ysize; y++ )
  837. {
  838. integralImage.set( 0, y, lfeats.get( 0, y, c1 ) + integralImage.get( 0, y, c ), c );
  839. }
  840. //first row
  841. for ( int x = 1; x < xsize; x++ )
  842. {
  843. integralImage.set( x, 0, lfeats.get( x, 0, c1 ) + integralImage.get( x, 0, c ), c );
  844. }
  845. //rest
  846. for ( int y = 1; y < ysize; y++ )
  847. {
  848. for ( int x = 1; x < xsize; x++ )
  849. {
  850. 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 );
  851. integralImage.set( x, y, val, c );
  852. }
  853. }
  854. }
  855. }
  856. }
  857. void SemSegContextTree::train( const MultiDataset *md )
  858. {
  859. const LabeledSet train = * ( *md )["train"];
  860. const LabeledSet *trainp = &train;
  861. ProgressBar pb( "compute feats" );
  862. pb.show();
  863. //TODO: Speichefresser!, lohnt sich sparse?
  864. vector<MultiChannelImageT<double> > allfeats;
  865. vector<MultiChannelImageT<int> > currentfeats;
  866. vector<MatrixT<int> > labels;
  867. std::string forbidden_classes_s = conf->gS( "analysis", "donttrain", "" );
  868. if ( forbidden_classes_s == "" )
  869. {
  870. forbidden_classes_s = conf->gS( "analysis", "forbidden_classes", "" );
  871. }
  872. classnames.getSelection( forbidden_classes_s, forbidden_classes );
  873. int imgcounter = 0;
  874. /*
  875. MultiChannelImageT<int> ttmp2(0,0,0);
  876. MultiChannelImageT<double> ttmp1(100,100,1);
  877. MultiChannelImageT<double> tint(100,100,1);
  878. ttmp1.setAll(1.0);
  879. tint.setAll(0.0);
  880. computeIntegralImage(ttmp2,ttmp1,tint);
  881. for(int i = 0; i < cops.size(); i++)
  882. {
  883. Features feats;
  884. feats.feats = &tint;
  885. feats.cfeats = &ttmp2;
  886. feats.cTree = 0;
  887. feats.tree = new vector<TreeNode>;
  888. feats.integralImg = &tint;
  889. cops[i]->set(-10, -6, 8, 9, 0, 0, new MCImageAccess());
  890. cout << "for: " << cops[i]->writeInfos() << endl;
  891. int y = 50;
  892. for(int x = 40; x < 44; x++)
  893. {
  894. cout << "x: " << x << " val: " << cops[i]->getVal(feats, x, y) << endl;
  895. }
  896. }
  897. getchar();*/
  898. LOOP_ALL_S( *trainp )
  899. {
  900. EACH_INFO( classno, info );
  901. NICE::ColorImage img;
  902. std::string currentFile = info.img();
  903. CachedExample *ce = new CachedExample( currentFile );
  904. const LocalizationResult *locResult = info.localization();
  905. if ( locResult->size() <= 0 )
  906. {
  907. fprintf( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  908. currentFile.c_str() );
  909. continue;
  910. }
  911. fprintf( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n", currentFile.c_str() );
  912. int xsize, ysize;
  913. ce->getImageSize( xsize, ysize );
  914. MatrixT<int> tmpMat( xsize, ysize );
  915. currentfeats.push_back( MultiChannelImageT<int>( xsize, ysize, nbTrees ) );
  916. currentfeats[imgcounter].setAll( 0 );
  917. labels.push_back( tmpMat );
  918. try {
  919. img = ColorImage( currentFile );
  920. } catch ( Exception ) {
  921. cerr << "SemSeg: error opening image file <" << currentFile << ">" << endl;
  922. continue;
  923. }
  924. Globals::setCurrentImgFN( currentFile );
  925. //TODO: resize image?!
  926. MultiChannelImageT<double> feats;
  927. allfeats.push_back( feats );
  928. #ifdef LOCALFEATS
  929. lfcw->getFeats( img, allfeats[imgcounter] );
  930. #else
  931. allfeats[imgcounter].reInit( xsize, ysize, 3, true );
  932. for ( int x = 0; x < xsize; x++ )
  933. {
  934. for ( int y = 0; y < ysize; y++ )
  935. {
  936. for ( int r = 0; r < 3; r++ )
  937. {
  938. allfeats[imgcounter].set( x, y, img.getPixel( x, y, r ), r );
  939. }
  940. }
  941. }
  942. allfeats[imgcounter] = ColorSpace::rgbtolab(allfeats[imgcounter]);
  943. #endif
  944. // getting groundtruth
  945. NICE::Image pixelLabels( xsize, ysize );
  946. pixelLabels.set( 0 );
  947. locResult->calcLabeledImage( pixelLabels, ( *classNames ).getBackgroundClass() );
  948. for ( int x = 0; x < xsize; x++ )
  949. {
  950. for ( int y = 0; y < ysize; y++ )
  951. {
  952. classno = pixelLabels.getPixel( x, y );
  953. labels[imgcounter]( x, y ) = classno;
  954. if ( forbidden_classes.find( classno ) != forbidden_classes.end() )
  955. continue;
  956. labelcounter[classno]++;
  957. }
  958. }
  959. imgcounter++;
  960. pb.update( trainp->count() );
  961. delete ce;
  962. }
  963. pb.hide();
  964. map<int, int>::iterator mapit;
  965. int classes = 0;
  966. for ( mapit = labelcounter.begin(); mapit != labelcounter.end(); mapit++ )
  967. {
  968. labelmap[mapit->first] = classes;
  969. labelmapback[classes] = mapit->first;
  970. classes++;
  971. }
  972. //balancing
  973. int featcounter = 0;
  974. a = vector<double>( classes, 0.0 );
  975. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  976. {
  977. int xsize = ( int )currentfeats[iCounter].width();
  978. int ysize = ( int )currentfeats[iCounter].height();
  979. for ( int x = 0; x < xsize; x++ )
  980. {
  981. for ( int y = 0; y < ysize; y++ )
  982. {
  983. featcounter++;
  984. int cn = labels[iCounter]( x, y );
  985. a[labelmap[cn]] ++;
  986. }
  987. }
  988. }
  989. for ( int i = 0; i < ( int )a.size(); i++ )
  990. {
  991. a[i] /= ( double )featcounter;
  992. }
  993. #ifdef DEBUG
  994. for ( int i = 0; i < ( int )a.size(); i++ )
  995. {
  996. cout << "a[" << i << "]: " << a[i] << endl;
  997. }
  998. cout << "a.size: " << a.size() << endl;
  999. #endif
  1000. int depth = 0;
  1001. for ( int t = 0; t < nbTrees; t++ )
  1002. {
  1003. vector<TreeNode> tree;
  1004. tree.push_back( TreeNode() );
  1005. tree[0].dist = vector<double>( classes, 0.0 );
  1006. tree[0].depth = depth;
  1007. forest.push_back( tree );
  1008. }
  1009. vector<int> startnode( nbTrees, 0 );
  1010. bool allleaf = false;
  1011. //int baseFeatSize = allfeats[0].size();
  1012. vector<MultiChannelImageT<double> > integralImgs( imgcounter, MultiChannelImageT<double>() );
  1013. while ( !allleaf && depth < maxDepth )
  1014. {
  1015. allleaf = true;
  1016. vector<MultiChannelImageT<int> > lastfeats = currentfeats;
  1017. #if 1
  1018. Timer timer;
  1019. timer.start();
  1020. #endif
  1021. for ( int tree = 0; tree < nbTrees; tree++ )
  1022. {
  1023. int t = ( int ) forest[tree].size();
  1024. int s = startnode[tree];
  1025. startnode[tree] = t;
  1026. //TODO vielleicht parallel wenn nächste schleife trotzdem noch parallelsiert würde, die hat mehr gewicht
  1027. //#pragma omp parallel for
  1028. for ( int i = s; i < t; i++ )
  1029. {
  1030. if ( !forest[tree][i].isleaf && forest[tree][i].left < 0 )
  1031. {
  1032. Operation *splitfeat = NULL;
  1033. double splitval;
  1034. double bestig = getBestSplit( allfeats, lastfeats, integralImgs, labels, i, splitfeat, splitval, tree );
  1035. forest[tree][i].feat = splitfeat;
  1036. forest[tree][i].decision = splitval;
  1037. if ( splitfeat != NULL )
  1038. {
  1039. allleaf = false;
  1040. int left = forest[tree].size();
  1041. forest[tree].push_back( TreeNode() );
  1042. forest[tree].push_back( TreeNode() );
  1043. int right = left + 1;
  1044. forest[tree][i].left = left;
  1045. forest[tree][i].right = right;
  1046. forest[tree][left].dist = vector<double>( classes, 0.0 );
  1047. forest[tree][right].dist = vector<double>( classes, 0.0 );
  1048. forest[tree][left].depth = depth + 1;
  1049. forest[tree][right].depth = depth + 1;
  1050. #pragma omp parallel for
  1051. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  1052. {
  1053. int xsize = currentfeats[iCounter].width();
  1054. int ysize = currentfeats[iCounter].height();
  1055. for ( int x = 0; x < xsize; x++ )
  1056. {
  1057. for ( int y = 0; y < ysize; y++ )
  1058. {
  1059. if ( currentfeats[iCounter].get( x, y, tree ) == i )
  1060. {
  1061. Features feat;
  1062. feat.feats = &allfeats[iCounter];
  1063. feat.cfeats = &lastfeats[iCounter];
  1064. feat.cTree = tree;
  1065. feat.tree = &forest[tree];
  1066. feat.integralImg = &integralImgs[iCounter];
  1067. double val = splitfeat->getVal( feat, x, y );
  1068. if ( val < splitval )
  1069. {
  1070. currentfeats[iCounter].set( x, y, left, tree );
  1071. forest[tree][left].dist[labelmap[labels[iCounter]( x, y )]]++;
  1072. }
  1073. else
  1074. {
  1075. currentfeats[iCounter].set( x, y, right, tree );
  1076. forest[tree][right].dist[labelmap[labels[iCounter]( x, y )]]++;
  1077. }
  1078. }
  1079. }
  1080. }
  1081. }
  1082. double lcounter = 0.0, rcounter = 0.0;
  1083. for ( uint d = 0; d < forest[tree][left].dist.size(); d++ )
  1084. {
  1085. if ( forbidden_classes.find( labelmapback[d] ) != forbidden_classes.end() )
  1086. {
  1087. forest[tree][left].dist[d] = 0;
  1088. forest[tree][right].dist[d] = 0;
  1089. }
  1090. else
  1091. {
  1092. forest[tree][left].dist[d] /= a[d];
  1093. lcounter += forest[tree][left].dist[d];
  1094. forest[tree][right].dist[d] /= a[d];
  1095. rcounter += forest[tree][right].dist[d];
  1096. }
  1097. }
  1098. if ( lcounter <= 0 || rcounter <= 0 )
  1099. {
  1100. cout << "lcounter : " << lcounter << " rcounter: " << rcounter << endl;
  1101. cout << "splitval: " << splitval << " splittype: " << splitfeat->writeInfos() << endl;
  1102. cout << "bestig: " << bestig << endl;
  1103. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  1104. {
  1105. int xsize = currentfeats[iCounter].width();
  1106. int ysize = currentfeats[iCounter].height();
  1107. int counter = 0;
  1108. for ( int x = 0; x < xsize; x++ )
  1109. {
  1110. for ( int y = 0; y < ysize; y++ )
  1111. {
  1112. if ( lastfeats[iCounter].get( x, y, tree ) == i )
  1113. {
  1114. if ( ++counter > 30 )
  1115. break;
  1116. Features feat;
  1117. feat.feats = &allfeats[iCounter];
  1118. feat.cfeats = &lastfeats[iCounter];
  1119. feat.cTree = tree;
  1120. feat.tree = &forest[tree];
  1121. feat.integralImg = &integralImgs[iCounter];
  1122. double val = splitfeat->getVal( feat, x, y );
  1123. cout << "splitval: " << splitval << " val: " << val << endl;
  1124. }
  1125. }
  1126. }
  1127. }
  1128. assert( lcounter > 0 && rcounter > 0 );
  1129. }
  1130. for ( uint d = 0; d < forest[tree][left].dist.size(); d++ )
  1131. {
  1132. forest[tree][left].dist[d] /= lcounter;
  1133. forest[tree][right].dist[d] /= rcounter;
  1134. }
  1135. }
  1136. else
  1137. {
  1138. forest[tree][i].isleaf = true;
  1139. }
  1140. }
  1141. }
  1142. }
  1143. //TODO: features neu berechnen!
  1144. //compute integral image
  1145. int channels = classes + allfeats[0].channels();
  1146. if ( integralImgs[0].width() == 0 )
  1147. {
  1148. for ( int i = 0; i < imgcounter; i++ )
  1149. {
  1150. int xsize = allfeats[i].width();
  1151. int ysize = allfeats[i].height();
  1152. integralImgs[i].reInit( xsize, ysize, channels );
  1153. integralImgs[i].setAll( 0.0 );
  1154. }
  1155. }
  1156. for ( int i = 0; i < imgcounter; i++ )
  1157. {
  1158. computeIntegralImage( currentfeats[i], allfeats[i], integralImgs[i] );
  1159. }
  1160. #if 1
  1161. timer.stop();
  1162. cout << "time for depth " << depth << ": " << timer.getLast() << endl;
  1163. #endif
  1164. depth++;
  1165. #ifdef DEBUG
  1166. cout << "depth: " << depth << endl;
  1167. #endif
  1168. }
  1169. #ifdef DEBUG
  1170. for ( int tree = 0; tree < nbTrees; tree++ )
  1171. {
  1172. int t = ( int ) forest[tree].size();
  1173. for ( int i = 0; i < t; i++ )
  1174. {
  1175. printf( "tree[%i]: left: %i, right: %i", i, forest[tree][i].left, forest[tree][i].right );
  1176. if ( !forest[tree][i].isleaf && forest[tree][i].left != -1 )
  1177. {
  1178. cout << ", feat: " << forest[tree][i].feat->writeInfos() << " ";
  1179. opOverview[forest[tree][i].feat->getOps()]++;
  1180. }
  1181. for ( int d = 0; d < ( int )forest[tree][i].dist.size(); d++ )
  1182. {
  1183. cout << " " << forest[tree][i].dist[d];
  1184. }
  1185. cout << endl;
  1186. }
  1187. }
  1188. for ( uint c = 0; c < ops.size(); c++ )
  1189. {
  1190. cout << ops[c]->writeInfos() << ": " << opOverview[ops[c]->getOps()] << endl;
  1191. }
  1192. for ( uint c = 0; c < cops.size(); c++ )
  1193. {
  1194. cout << cops[c]->writeInfos() << ": " << opOverview[cops[c]->getOps()] << endl;
  1195. }
  1196. #endif
  1197. }
  1198. void SemSegContextTree::semanticseg( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities )
  1199. {
  1200. int xsize;
  1201. int ysize;
  1202. ce->getImageSize( xsize, ysize );
  1203. int numClasses = classNames->numClasses();
  1204. fprintf( stderr, "ContextTree classification !\n" );
  1205. probabilities.reInit( xsize, ysize, numClasses, true );
  1206. probabilities.setAll( 0 );
  1207. NICE::ColorImage img;
  1208. std::string currentFile = Globals::getCurrentImgFN();
  1209. try {
  1210. img = ColorImage( currentFile );
  1211. } catch ( Exception ) {
  1212. cerr << "SemSeg: error opening image file <" << currentFile << ">" << endl;
  1213. return;
  1214. }
  1215. //TODO: resize image?!
  1216. MultiChannelImageT<double> feats;
  1217. #ifdef LOCALFEATS
  1218. lfcw->getFeats( img, feats );
  1219. #else
  1220. feats.reInit( xsize, ysize, 3, true );
  1221. for ( int x = 0; x < xsize; x++ )
  1222. {
  1223. for ( int y = 0; y < ysize; y++ )
  1224. {
  1225. for ( int r = 0; r < 3; r++ )
  1226. {
  1227. feats.set( x, y, img.getPixel( x, y, r ), r );
  1228. }
  1229. }
  1230. }
  1231. feats = ColorSpace::rgbtolab(feats);
  1232. #endif
  1233. bool allleaf = false;
  1234. MultiChannelImageT<double> integralImg;
  1235. MultiChannelImageT<int> currentfeats( xsize, ysize, nbTrees );
  1236. currentfeats.setAll( 0 );
  1237. int depth = 0;
  1238. while ( !allleaf )
  1239. {
  1240. allleaf = true;
  1241. //TODO vielleicht parallel wenn nächste schleife auch noch parallelsiert würde, die hat mehr gewicht
  1242. //#pragma omp parallel for
  1243. MultiChannelImageT<int> lastfeats = currentfeats;
  1244. for ( int tree = 0; tree < nbTrees; tree++ )
  1245. {
  1246. for ( int x = 0; x < xsize; x++ )
  1247. {
  1248. for ( int y = 0; y < ysize; y++ )
  1249. {
  1250. int t = currentfeats.get( x, y, tree );
  1251. if ( forest[tree][t].left > 0 )
  1252. {
  1253. allleaf = false;
  1254. Features feat;
  1255. feat.feats = &feats;
  1256. feat.cfeats = &lastfeats;
  1257. feat.cTree = tree;
  1258. feat.tree = &forest[tree];
  1259. feat.integralImg = &integralImg;
  1260. double val = forest[tree][t].feat->getVal( feat, x, y );
  1261. if ( val < forest[tree][t].decision )
  1262. {
  1263. currentfeats.set( x, y, forest[tree][t].left, tree );
  1264. }
  1265. else
  1266. {
  1267. currentfeats.set( x, y, forest[tree][t].right, tree );
  1268. }
  1269. }
  1270. }
  1271. }
  1272. //compute integral image
  1273. int channels = ( int )labelmap.size() + feats.channels();
  1274. if ( integralImg.width() == 0 )
  1275. {
  1276. int xsize = feats.width();
  1277. int ysize = feats.height();
  1278. integralImg.reInit( xsize, ysize, channels );
  1279. }
  1280. }
  1281. computeIntegralImage( currentfeats, feats, integralImg );
  1282. depth++;
  1283. }
  1284. if ( pixelWiseLabeling )
  1285. {
  1286. //finales labeln:
  1287. long int offset = 0;
  1288. for ( int x = 0; x < xsize; x++ )
  1289. {
  1290. for ( int y = 0; y < ysize; y++, offset++ )
  1291. {
  1292. double maxvalue = - numeric_limits<double>::max(); //TODO: das muss nur pro knoten gemacht werden, nicht pro pixel
  1293. int maxindex = 0;
  1294. uint s = forest[0][0].dist.size();
  1295. for ( uint i = 0; i < s; i++ )
  1296. {
  1297. probabilities.data[labelmapback[i]][offset] = getMeanProb( x, y, i, currentfeats );
  1298. if ( probabilities.data[labelmapback[i]][offset] > maxvalue )
  1299. {
  1300. maxvalue = probabilities.data[labelmapback[i]][offset];
  1301. maxindex = labelmapback[i];
  1302. }
  1303. segresult.setPixel( x, y, maxindex );
  1304. }
  1305. if ( maxvalue > 1 )
  1306. cout << "maxvalue: " << maxvalue << endl;
  1307. }
  1308. }
  1309. }
  1310. else
  1311. {
  1312. //final labeling using segmentation
  1313. //TODO: segmentation
  1314. Matrix regions;
  1315. int regionNumber = segmentation->segRegions( img, regions );
  1316. cout << "regions: " << regionNumber << endl;
  1317. int dSize = forest[0][0].dist.size();
  1318. vector<vector<double> > regionProbs( regionNumber, vector<double>( dSize, 0.0 ) );
  1319. vector<int> bestlabels( regionNumber, 0 );
  1320. /*
  1321. for(int r = 0; r < regionNumber; r++)
  1322. {
  1323. Image over(img.width(), img.height());
  1324. for(int y = 0; y < img.height(); y++)
  1325. {
  1326. for(int x = 0; x < img.width(); x++)
  1327. {
  1328. if(((int)regions(x,y)) == r)
  1329. over.setPixel(x,y,1);
  1330. else
  1331. over.setPixel(x,y,0);
  1332. }
  1333. }
  1334. cout << "r: " << r << endl;
  1335. showImageOverlay(img, over);
  1336. }
  1337. */
  1338. for ( int y = 0; y < img.height(); y++ )
  1339. {
  1340. for ( int x = 0; x < img.width(); x++ )
  1341. {
  1342. int cregion = regions( x, y );
  1343. for ( int d = 0; d < dSize; d++ )
  1344. {
  1345. regionProbs[cregion][d] += getMeanProb( x, y, d, currentfeats );
  1346. }
  1347. }
  1348. }
  1349. int roi = 38;
  1350. for ( int r = 0; r < regionNumber; r++ )
  1351. {
  1352. double maxval = regionProbs[r][0];
  1353. bestlabels[r] = 0;
  1354. if ( roi == r )
  1355. {
  1356. cout << "r: " << r << endl;
  1357. cout << "0: " << regionProbs[r][0] << endl;
  1358. }
  1359. for ( int d = 1; d < dSize; d++ )
  1360. {
  1361. if ( maxval < regionProbs[r][d] )
  1362. {
  1363. maxval = regionProbs[r][d];
  1364. bestlabels[r] = d;
  1365. }
  1366. if ( roi == r )
  1367. {
  1368. cout << d << ": " << regionProbs[r][d] << endl;
  1369. }
  1370. }
  1371. if ( roi == r )
  1372. {
  1373. cout << "bestlabel: " << bestlabels[r] << " danach: " << labelmapback[bestlabels[r]] << endl;
  1374. }
  1375. bestlabels[r] = labelmapback[bestlabels[r]];
  1376. }
  1377. for ( int y = 0; y < img.height(); y++ )
  1378. {
  1379. for ( int x = 0; x < img.width(); x++ )
  1380. {
  1381. segresult.setPixel( x, y, bestlabels[regions( x,y )] );
  1382. }
  1383. }
  1384. }
  1385. }