SemSegContextTree.cpp 59 KB

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