SemSegContextTree.cpp 56 KB

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