SemSegContextTree.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  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. for ( int tree = 0; tree < nbTrees; tree++ )
  1552. {
  1553. for ( int x = 0; x < xsize; x++ )
  1554. {
  1555. for ( int y = 0; y < ysize; y++ )
  1556. {
  1557. int t = currentfeats.get ( x, y, tree );
  1558. if ( forest[tree][t].left > 0 )
  1559. {
  1560. allleaf = false;
  1561. Features feat;
  1562. feat.feats = &feats;
  1563. feat.cfeats = &lastfeats;
  1564. feat.cTree = tree;
  1565. feat.tree = &forest[tree];
  1566. feat.integralImg = &integralImg;
  1567. double val = forest[tree][t].feat->getVal ( feat, x, y );
  1568. int subx = x / grid;
  1569. int suby = y / grid;
  1570. if ( val < forest[tree][t].decision )
  1571. {
  1572. currentfeats.set ( x, y, forest[tree][t].left, tree );
  1573. #ifdef TEXTONMAP
  1574. SparseVectorInt v;
  1575. v.insert ( pair<int, double> ( forest[tree][forest[tree][t].left].nodeNumber, weight ) );
  1576. textonMap ( subx, suby ).add ( v );
  1577. #endif
  1578. }
  1579. else
  1580. {
  1581. currentfeats.set ( x, y, forest[tree][t].right, tree );
  1582. #ifdef TEXTONMAP
  1583. SparseVectorInt v;
  1584. v.insert ( pair<int, double> ( forest[tree][forest[tree][t].right].nodeNumber, weight ) );
  1585. textonMap ( subx, suby ).add ( v );
  1586. #endif
  1587. }
  1588. /*if ( x == xpos && y == ypos )
  1589. {
  1590. cout << "val: " << val << " decision: " << forest[tree][t].decision << " details: " << forest[tree][t].feat->writeInfos() << endl;
  1591. }*/
  1592. }
  1593. }
  1594. }
  1595. //compute integral image
  1596. int channels = ( int ) labelmap.size() + feats.channels();
  1597. if ( integralImg.width() == 0 )
  1598. {
  1599. int xsize = feats.width();
  1600. int ysize = feats.height();
  1601. integralImg.reInit ( xsize, ysize, channels );
  1602. integralImg.setAll ( 0.0 );
  1603. }
  1604. }
  1605. computeIntegralImage ( currentfeats, feats, integralImg );
  1606. #ifdef TEXTONMAP
  1607. computeIntegralImage ( textonMap, integralTexton );
  1608. #endif
  1609. }
  1610. // cout << forest[0][currentfeats.get ( xpos, ypos, 0 ) ].dist << endl;
  1611. #ifdef WRITEGLOB
  1612. ofstream outstream("globtest.feat",ofstream::app);
  1613. outstream << 0 << endl;
  1614. integralTexton(integralTexton.width()-1, integralTexton.height()-1).store(outstream);
  1615. outstream.close();
  1616. #endif
  1617. string cndir = conf->gS ( "SSContextTree", "cndir", "" );
  1618. int classes = ( int ) probabilities.numChannels;
  1619. vector<int> useclass ( classes, 1 );
  1620. #ifdef WRITEGLOB
  1621. std::vector< std::string > list;
  1622. StringTools::split ( currentFile, '/', list );
  1623. string orgname = list.back();
  1624. ofstream ostream("filelist.txt",ofstream::app);
  1625. ostream << orgname << ".dat" << endl;
  1626. ostream.close();
  1627. if ( cndir != "" )
  1628. {
  1629. useclass = vector<int> ( classes, 0 );
  1630. ifstream infile ( ( cndir + "/" + orgname + ".dat" ).c_str() );
  1631. #undef OLD
  1632. #ifdef OLD
  1633. while ( !infile.eof() && infile.good() )
  1634. {
  1635. int tmp;
  1636. infile >> tmp;
  1637. assert(tmp >= 0 && tmp < classes);
  1638. useclass[tmp] = 1;
  1639. }
  1640. #else
  1641. int c = 0;
  1642. vector<double> probs(classes, 0.0);
  1643. while ( !infile.eof() && infile.good() )
  1644. {
  1645. infile >> probs[c];
  1646. c++;
  1647. }
  1648. vector<double> sorted = probs;
  1649. sort (sorted.begin(), sorted.end());
  1650. double thr = sorted[10];
  1651. if(thr < 0.0)
  1652. thr = 0.0;
  1653. for(int c = 0; c < classes; c++)
  1654. {
  1655. if(probs[c] < thr)
  1656. {
  1657. useclass[c] = 1;
  1658. }
  1659. }
  1660. #endif
  1661. for(int c = 0; c < classes; c++)
  1662. {
  1663. if(useclass[c] == 0)
  1664. {
  1665. probabilities.set(-numeric_limits< double >::max(), c);
  1666. }
  1667. }
  1668. }
  1669. #endif
  1670. if ( pixelWiseLabeling )
  1671. {
  1672. //finales labeln:
  1673. long int offset = 0;
  1674. for ( int x = 0; x < xsize; x++ )
  1675. {
  1676. for ( int y = 0; y < ysize; y++, offset++ )
  1677. {
  1678. double maxvalue = - numeric_limits<double>::max(); //TODO: das kann auch nur pro knoten gemacht werden, nicht pro pixel
  1679. int maxindex = 0;
  1680. uint s = forest[0][0].dist.size();
  1681. for ( uint i = 0; i < s; i++ )
  1682. {
  1683. int currentclass = labelmapback[i];
  1684. if(useclass[currentclass])
  1685. {
  1686. probabilities.data[currentclass][offset] = getMeanProb ( x, y, i, currentfeats );
  1687. if ( probabilities.data[currentclass][offset] > maxvalue )
  1688. {
  1689. maxvalue = probabilities.data[currentclass][offset];
  1690. maxindex = currentclass;
  1691. }
  1692. }
  1693. }
  1694. segresult.setPixel ( x, y, maxindex );
  1695. if ( maxvalue > 1 )
  1696. cout << "maxvalue: " << maxvalue << endl;
  1697. }
  1698. }
  1699. }
  1700. else
  1701. {
  1702. //final labeling using segmentation
  1703. Matrix regions;
  1704. //showImage(img);
  1705. int regionNumber = segmentation->segRegions ( img, regions );
  1706. cout << "regions: " << regionNumber << endl;
  1707. int dSize = forest[0][0].dist.size();
  1708. vector<vector<double> > regionProbs ( regionNumber, vector<double> ( dSize, 0.0 ) );
  1709. vector<int> bestlabels ( regionNumber, 0 );
  1710. /*
  1711. for(int r = 0; r < regionNumber; r++)
  1712. {
  1713. Image over(img.width(), img.height());
  1714. for(int y = 0; y < img.height(); y++)
  1715. {
  1716. for(int x = 0; x < img.width(); x++)
  1717. {
  1718. if(((int)regions(x,y)) == r)
  1719. over.setPixel(x,y,1);
  1720. else
  1721. over.setPixel(x,y,0);
  1722. }
  1723. }
  1724. cout << "r: " << r << endl;
  1725. showImageOverlay(img, over);
  1726. }
  1727. */
  1728. for ( int y = 0; y < img.height(); y++ )
  1729. {
  1730. for ( int x = 0; x < img.width(); x++ )
  1731. {
  1732. int cregion = regions ( x, y );
  1733. for ( int d = 0; d < dSize; d++ )
  1734. {
  1735. regionProbs[cregion][d] += getMeanProb ( x, y, d, currentfeats );
  1736. }
  1737. }
  1738. }
  1739. for ( int r = 0; r < regionNumber; r++ )
  1740. {
  1741. double maxval = regionProbs[r][0];
  1742. bestlabels[r] = 0;
  1743. for ( int d = 1; d < dSize; d++ )
  1744. {
  1745. if ( maxval < regionProbs[r][d] )
  1746. {
  1747. maxval = regionProbs[r][d];
  1748. bestlabels[r] = d;
  1749. }
  1750. }
  1751. bestlabels[r] = labelmapback[bestlabels[r]];
  1752. }
  1753. for ( int y = 0; y < img.height(); y++ )
  1754. {
  1755. for ( int x = 0; x < img.width(); x++ )
  1756. {
  1757. segresult.setPixel ( x, y, bestlabels[regions ( x,y ) ] );
  1758. }
  1759. }
  1760. }
  1761. cout << "segmentation finished" << endl;
  1762. }
  1763. void SemSegContextTree::store ( std::ostream & os, int format ) const
  1764. {
  1765. os << nbTrees << endl;
  1766. classnames.store ( os );
  1767. map<int, int>::const_iterator it;
  1768. os << labelmap.size() << endl;
  1769. for ( it = labelmap.begin() ; it != labelmap.end(); it++ )
  1770. os << ( *it ).first << " " << ( *it ).second << endl;
  1771. os << labelmapback.size() << endl;
  1772. for ( it = labelmapback.begin() ; it != labelmapback.end(); it++ )
  1773. os << ( *it ).first << " " << ( *it ).second << endl;
  1774. int trees = forest.size();
  1775. os << trees << endl;
  1776. for ( int t = 0; t < trees; t++ )
  1777. {
  1778. int nodes = forest[t].size();
  1779. os << nodes << endl;
  1780. for ( int n = 0; n < nodes; n++ )
  1781. {
  1782. 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;
  1783. os << forest[t][n].dist << endl;
  1784. if ( forest[t][n].feat == NULL )
  1785. os << -1 << endl;
  1786. else
  1787. {
  1788. os << forest[t][n].feat->getOps() << endl;
  1789. forest[t][n].feat->store ( os );
  1790. }
  1791. }
  1792. }
  1793. }
  1794. void SemSegContextTree::restore ( std::istream & is, int format )
  1795. {
  1796. is >> nbTrees;
  1797. classnames.restore ( is );
  1798. int lsize;
  1799. is >> lsize;
  1800. labelmap.clear();
  1801. for ( int l = 0; l < lsize; l++ )
  1802. {
  1803. int first, second;
  1804. is >> first;
  1805. is >> second;
  1806. labelmap[first] = second;
  1807. }
  1808. is >> lsize;
  1809. labelmapback.clear();
  1810. for ( int l = 0; l < lsize; l++ )
  1811. {
  1812. int first, second;
  1813. is >> first;
  1814. is >> second;
  1815. labelmapback[first] = second;
  1816. }
  1817. int trees;
  1818. is >> trees;
  1819. forest.clear();
  1820. for ( int t = 0; t < trees; t++ )
  1821. {
  1822. vector<TreeNode> tmptree;
  1823. forest.push_back ( tmptree );
  1824. int nodes;
  1825. is >> nodes;
  1826. //cout << "nodes: " << nodes << endl;
  1827. for ( int n = 0; n < nodes; n++ )
  1828. {
  1829. TreeNode tmpnode;
  1830. forest[t].push_back ( tmpnode );
  1831. is >> forest[t][n].left;
  1832. is >> forest[t][n].right;
  1833. is >> forest[t][n].decision;
  1834. is >> forest[t][n].isleaf;
  1835. is >> forest[t][n].depth;
  1836. is >> forest[t][n].featcounter;
  1837. is >> forest[t][n].nodeNumber;
  1838. is >> forest[t][n].dist;
  1839. int feattype;
  1840. is >> feattype;
  1841. assert ( feattype < NBOPERATIONS );
  1842. forest[t][n].feat = NULL;
  1843. if ( feattype >= 0 )
  1844. {
  1845. for ( uint o = 0; o < ops.size(); o++ )
  1846. {
  1847. if ( ops[o]->getOps() == feattype )
  1848. {
  1849. forest[t][n].feat = ops[o]->clone();
  1850. break;
  1851. }
  1852. }
  1853. if ( forest[t][n].feat == NULL )
  1854. {
  1855. for ( uint o = 0; o < cops.size(); o++ )
  1856. {
  1857. if ( cops[o]->getOps() == feattype )
  1858. {
  1859. forest[t][n].feat = cops[o]->clone();
  1860. break;
  1861. }
  1862. }
  1863. }
  1864. assert ( forest[t][n].feat != NULL );
  1865. forest[t][n].feat->restore ( is );
  1866. }
  1867. }
  1868. }
  1869. }