SemSegContextTree.cpp 60 KB

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