SemSegContextTree.cpp 49 KB

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