SemSegContextTree.cpp 57 KB

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