SemSegContextTree.cpp 44 KB

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