SemSegContextTree.cpp 59 KB

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