SemSegContextTree.cpp 54 KB

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