SemSegContextTree.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  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]-1 ) != 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 ( randD < fraction[labelmap[cn]-1] )
  664. {
  665. vector<int> tmp ( 3, 0 );
  666. tmp[0] = iCounter;
  667. tmp[1] = x;
  668. tmp[2] = y;
  669. featcounter++;
  670. selFeats.insert ( tmp );
  671. e[cn]++;
  672. }
  673. }
  674. }
  675. }
  676. }
  677. //cout << "size: " << selFeats.size() << endl;
  678. //getchar();
  679. map<int, int>::iterator mapit;
  680. double globent = 0.0;
  681. for ( mapit = e.begin() ; mapit != e.end(); mapit++ )
  682. {
  683. //cout << "class: " << mapit->first << ": " << mapit->second << endl;
  684. double p = ( double ) ( *mapit ).second / ( double ) featcounter;
  685. globent += p * log2 ( p );
  686. }
  687. globent = -globent;
  688. if ( globent < 0.5 )
  689. {
  690. //cout << "globent to small: " << globent << endl;
  691. return 0.0;
  692. }
  693. int classes = ( int ) forest[tree][0].dist.size();
  694. featsel.clear();
  695. for ( int i = 0; i < featsPerSplit; i++ )
  696. {
  697. int x1, x2, y1, y2;
  698. int ft = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) ftypes );
  699. int tmpws = windowSize;
  700. if ( integralImgs[0].width() == 0 )
  701. ft = 0;
  702. if ( ft > 0 )
  703. {
  704. tmpws *= 4;
  705. }
  706. if ( useGaussian )
  707. {
  708. double sigma = ( double ) tmpws / 2.0;
  709. x1 = randGaussDouble ( sigma ) * ( double ) tmpws;
  710. x2 = randGaussDouble ( sigma ) * ( double ) tmpws;
  711. y1 = randGaussDouble ( sigma ) * ( double ) tmpws;
  712. y2 = randGaussDouble ( sigma ) * ( double ) tmpws;
  713. }
  714. else
  715. {
  716. x1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  717. x2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  718. y1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  719. y2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) tmpws ) - tmpws / 2;
  720. }
  721. if ( ft == 0 )
  722. {
  723. int f1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) featdim );
  724. int f2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) featdim );
  725. int o = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) ops.size() );
  726. Operation *op = ops[o]->clone();
  727. op->set ( x1, y1, x2, y2, f1, f2, calcVal[ft] );
  728. op->setContext ( false );
  729. featsel.push_back ( op );
  730. }
  731. else if ( ft == 1 )
  732. {
  733. int opssize = ( int ) ops.size();
  734. //opssize = 0;
  735. int o = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( ( ( double ) cops.size() ) + ( double ) opssize ) );
  736. Operation *op;
  737. if ( o < opssize )
  738. {
  739. int chans = ( int ) forest[0][0].dist.size();
  740. int f1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) chans );
  741. int f2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) chans );
  742. op = ops[o]->clone();
  743. op->set ( x1, y1, x2, y2, f1, f2, calcVal[ft] );
  744. op->setContext ( true );
  745. }
  746. else
  747. {
  748. int chans = integralImgs[0].channels();
  749. int f1 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) chans );
  750. int f2 = ( int ) ( ( double ) rand() / ( double ) RAND_MAX * ( double ) chans );
  751. o -= opssize;
  752. op = cops[o]->clone();
  753. op->set ( x1, y1, x2, y2, f1, f2, calcVal[ft] );
  754. if ( f1 < forest[0][0].dist.size() )
  755. op->setContext ( true );
  756. else
  757. op->setContext ( false );
  758. }
  759. featsel.push_back ( op );
  760. }
  761. }
  762. #pragma omp parallel for private(mapit)
  763. for ( int f = 0; f < featsPerSplit; f++ )
  764. {
  765. double l_bestig = -numeric_limits< double >::max();
  766. double l_splitval = -1.0;
  767. set<vector<int> >::iterator it;
  768. vector<double> vals;
  769. double maxval = -numeric_limits<double>::max();
  770. double minval = numeric_limits<double>::max();
  771. for ( it = selFeats.begin() ; it != selFeats.end(); it++ )
  772. {
  773. Features feat;
  774. feat.feats = &feats[ ( *it ) [0]];
  775. feat.cfeats = &currentfeats[ ( *it ) [0]];
  776. feat.cTree = tree;
  777. feat.tree = &forest[tree];
  778. feat.integralImg = &integralImgs[ ( *it ) [0]];
  779. double val = featsel[f]->getVal ( feat, ( *it ) [1], ( *it ) [2] );
  780. vals.push_back ( val );
  781. maxval = std::max ( val, maxval );
  782. minval = std::min ( val, minval );
  783. }
  784. if ( minval == maxval )
  785. continue;
  786. double scale = maxval - minval;
  787. vector<double> splits;
  788. for ( int r = 0; r < randomTests; r++ )
  789. {
  790. splits.push_back ( ( ( double ) rand() / ( double ) RAND_MAX*scale ) + minval );
  791. }
  792. for ( int run = 0 ; run < randomTests; run++ )
  793. {
  794. set<vector<int> >::iterator it2;
  795. double val = splits[run];
  796. map<int, int> eL, eR;
  797. int counterL = 0, counterR = 0;
  798. int counter2 = 0;
  799. for ( it2 = selFeats.begin() ; it2 != selFeats.end(); it2++, counter2++ )
  800. {
  801. int cn = labels[ ( *it2 ) [0]] ( ( *it2 ) [1], ( *it2 ) [2] );
  802. //cout << "vals[counter2] " << vals[counter2] << " val: " << val << endl;
  803. if ( vals[counter2] < val )
  804. {
  805. //left entropie:
  806. eL[cn] = eL[cn] + 1;
  807. counterL++;
  808. }
  809. else
  810. {
  811. //right entropie:
  812. eR[cn] = eR[cn] + 1;
  813. counterR++;
  814. }
  815. }
  816. double leftent = 0.0;
  817. for ( mapit = eL.begin() ; mapit != eL.end(); mapit++ )
  818. {
  819. double p = ( double ) ( *mapit ).second / ( double ) counterL;
  820. leftent -= p * log2 ( p );
  821. }
  822. double rightent = 0.0;
  823. for ( mapit = eR.begin() ; mapit != eR.end(); mapit++ )
  824. {
  825. double p = ( double ) ( *mapit ).second / ( double ) counterR;
  826. rightent -= p * log2 ( p );
  827. }
  828. //cout << "rightent: " << rightent << " leftent: " << leftent << endl;
  829. double pl = ( double ) counterL / ( double ) ( counterL + counterR );
  830. double ig = globent - ( 1.0 - pl ) * rightent - pl * leftent;
  831. //double ig = globent - rightent - leftent;
  832. if ( useShannonEntropy )
  833. {
  834. double esplit = - ( pl * log ( pl ) + ( 1 - pl ) * log ( 1 - pl ) );
  835. ig = 2 * ig / ( globent + esplit );
  836. }
  837. if ( ig > l_bestig )
  838. {
  839. l_bestig = ig;
  840. l_splitval = val;
  841. }
  842. }
  843. #pragma omp critical
  844. {
  845. //cout << "globent: " << globent << " bestig " << bestig << " splitfeat: " << splitfeat << " splitval: " << splitval << endl;
  846. //cout << "globent: " << globent << " l_bestig " << l_bestig << " f: " << p << " l_splitval: " << l_splitval << endl;
  847. //cout << "p: " << featsubset[f] << endl;
  848. if ( l_bestig > bestig )
  849. {
  850. bestig = l_bestig;
  851. splitop = featsel[f];
  852. splitval = l_splitval;
  853. }
  854. }
  855. }
  856. //getchar();
  857. //splitop->writeInfos();
  858. //cout<< "ig: " << bestig << endl;
  859. //FIXME: delete all features!
  860. /*for(int i = 0; i < featsPerSplit; i++)
  861. {
  862. if(featsel[i] != splitop)
  863. delete featsel[i];
  864. }*/
  865. #ifdef debug
  866. cout << "globent: " << globent << " bestig " << bestig << " splitval: " << splitval << endl;
  867. #endif
  868. return bestig;
  869. }
  870. inline double SemSegContextTree::getMeanProb ( const int &x, const int &y, const int &channel, const MultiChannelImageT<unsigned short int> &currentfeats )
  871. {
  872. double val = 0.0;
  873. for ( int tree = 0; tree < nbTrees; tree++ )
  874. {
  875. val += forest[tree][currentfeats.get ( x,y,tree ) ].dist[channel];
  876. }
  877. return val / ( double ) nbTrees;
  878. }
  879. void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImageT<SparseVector> &infeats, NICE::MultiChannelImageT<SparseVector> &integralImage )
  880. {
  881. int xsize = infeats.width();
  882. int ysize = infeats.height();
  883. integralImage ( 0, 0 ).add ( infeats.get ( 0, 0 ) );
  884. //first column
  885. for ( int y = 1; y < ysize; y++ )
  886. {
  887. integralImage ( 0, y ).add ( infeats.get ( 0, y ) );
  888. integralImage ( 0, y ).add ( integralImage ( 0, y - 1 ) );
  889. }
  890. //first row
  891. for ( int x = 1; x < xsize; x++ )
  892. {
  893. integralImage ( x, 0 ).add ( infeats.get ( x, 0 ) );
  894. integralImage ( x, 0 ).add ( integralImage ( x - 1, 0 ) );
  895. }
  896. //rest
  897. for ( int y = 1; y < ysize; y++ )
  898. {
  899. for ( int x = 1; x < xsize; x++ )
  900. {
  901. integralImage ( x, y ).add ( infeats.get ( x, y ) );
  902. integralImage ( x, y ).add ( integralImage ( x, y - 1 ) );
  903. integralImage ( x, y ).add ( integralImage ( x - 1, y ) );
  904. integralImage ( x, y ).sub ( integralImage ( x - 1, y - 1 ) );
  905. }
  906. }
  907. }
  908. void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, const NICE::MultiChannelImageT<double> &lfeats, NICE::MultiChannelImageT<double> &integralImage )
  909. {
  910. int xsize = currentfeats.width();
  911. int ysize = currentfeats.height();
  912. int channels = ( int ) forest[0][0].dist.size();
  913. #pragma omp parallel for
  914. for ( int c = 0; c < channels; c++ )
  915. {
  916. integralImage.set ( 0, 0, getMeanProb ( 0, 0, c, currentfeats ), c );
  917. //first column
  918. for ( int y = 1; y < ysize; y++ )
  919. {
  920. integralImage.set ( 0, y, getMeanProb ( 0, y, c, currentfeats ) + integralImage.get ( 0, y - 1, c ), c );
  921. }
  922. //first row
  923. for ( int x = 1; x < xsize; x++ )
  924. {
  925. integralImage.set ( x, 0, getMeanProb ( x, 0, c, currentfeats ) + integralImage.get ( x - 1, 0, c ), c );
  926. }
  927. //rest
  928. for ( int y = 1; y < ysize; y++ )
  929. {
  930. for ( int x = 1; x < xsize; x++ )
  931. {
  932. 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 );
  933. integralImage.set ( x, y, val, c );
  934. }
  935. }
  936. }
  937. int channels2 = ( int ) lfeats.channels();
  938. xsize = lfeats.width();
  939. ysize = lfeats.height();
  940. if ( integralImage.get ( xsize - 1, ysize - 1, channels ) == 0.0 )
  941. {
  942. #pragma omp parallel for
  943. for ( int c1 = 0; c1 < channels2; c1++ )
  944. {
  945. int c = channels + c1;
  946. integralImage.set ( 0, 0, lfeats.get ( 0, 0, c1 ), c );
  947. //first column
  948. for ( int y = 1; y < ysize; y++ )
  949. {
  950. integralImage.set ( 0, y, lfeats.get ( 0, y, c1 ) + integralImage.get ( 0, y, c ), c );
  951. }
  952. //first row
  953. for ( int x = 1; x < xsize; x++ )
  954. {
  955. integralImage.set ( x, 0, lfeats.get ( x, 0, c1 ) + integralImage.get ( x, 0, c ), c );
  956. }
  957. //rest
  958. for ( int y = 1; y < ysize; y++ )
  959. {
  960. for ( int x = 1; x < xsize; x++ )
  961. {
  962. 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 );
  963. integralImage.set ( x, y, val, c );
  964. }
  965. }
  966. }
  967. }
  968. }
  969. void SemSegContextTree::train ( const MultiDataset *md )
  970. {
  971. const LabeledSet train = * ( *md ) ["train"];
  972. const LabeledSet *trainp = &train;
  973. ProgressBar pb ( "compute feats" );
  974. pb.show();
  975. //TODO: Speichefresser!, lohnt sich sparse?
  976. vector<MultiChannelImageT<double> > allfeats;
  977. vector<MultiChannelImageT<unsigned short int> > currentfeats;
  978. vector<MatrixT<int> > labels;
  979. vector<MultiChannelImageT<SparseVector> > textonMap;
  980. vector<MultiChannelImageT<SparseVector> > integralTexton;
  981. std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
  982. if ( forbidden_classes_s == "" )
  983. {
  984. forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  985. }
  986. classnames.getSelection ( forbidden_classes_s, forbidden_classes );
  987. int imgcounter = 0;
  988. int amountPixels = 0;
  989. LOOP_ALL_S ( *trainp )
  990. {
  991. EACH_INFO ( classno, info );
  992. NICE::ColorImage img;
  993. std::string currentFile = info.img();
  994. CachedExample *ce = new CachedExample ( currentFile );
  995. const LocalizationResult *locResult = info.localization();
  996. if ( locResult->size() <= 0 )
  997. {
  998. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  999. currentFile.c_str() );
  1000. continue;
  1001. }
  1002. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n", currentFile.c_str() );
  1003. int xsize, ysize;
  1004. ce->getImageSize ( xsize, ysize );
  1005. amountPixels += xsize * ysize;
  1006. MatrixT<int> tmpMat ( xsize, ysize );
  1007. currentfeats.push_back ( MultiChannelImageT<unsigned short int> ( xsize, ysize, nbTrees ) );
  1008. currentfeats[imgcounter].setAll ( 0 );
  1009. textonMap.push_back ( MultiChannelImageT<SparseVector> ( xsize / grid + 1, ysize / grid + 1, 1 ) );
  1010. integralTexton.push_back ( MultiChannelImageT<SparseVector> ( xsize / grid + 1, ysize / grid + 1, 1 ) );
  1011. labels.push_back ( tmpMat );
  1012. try {
  1013. img = ColorImage ( currentFile );
  1014. } catch ( Exception ) {
  1015. cerr << "SemSeg: error opening image file <" << currentFile << ">" << endl;
  1016. continue;
  1017. }
  1018. Globals::setCurrentImgFN ( currentFile );
  1019. //TODO: resize image?!
  1020. MultiChannelImageT<double> feats;
  1021. allfeats.push_back ( feats );
  1022. #ifdef LOCALFEATS
  1023. lfcw->getFeats ( img, allfeats[imgcounter] );
  1024. #else
  1025. allfeats[imgcounter].reInit ( xsize, ysize, 3, true );
  1026. for ( int x = 0; x < xsize; x++ )
  1027. {
  1028. for ( int y = 0; y < ysize; y++ )
  1029. {
  1030. for ( int r = 0; r < 3; r++ )
  1031. {
  1032. allfeats[imgcounter].set ( x, y, img.getPixel ( x, y, r ), r );
  1033. }
  1034. }
  1035. }
  1036. allfeats[imgcounter] = ColorSpace::rgbtolab ( allfeats[imgcounter] );
  1037. #endif
  1038. // getting groundtruth
  1039. NICE::Image pixelLabels ( xsize, ysize );
  1040. pixelLabels.set ( 0 );
  1041. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  1042. for ( int x = 0; x < xsize; x++ )
  1043. {
  1044. for ( int y = 0; y < ysize; y++ )
  1045. {
  1046. classno = pixelLabels.getPixel ( x, y );
  1047. labels[imgcounter] ( x, y ) = classno;
  1048. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  1049. continue;
  1050. labelcounter[classno]++;
  1051. }
  1052. }
  1053. imgcounter++;
  1054. pb.update ( trainp->count() );
  1055. delete ce;
  1056. }
  1057. pb.hide();
  1058. map<int, int>::iterator mapit;
  1059. int classes = 0;
  1060. for ( mapit = labelcounter.begin(); mapit != labelcounter.end(); mapit++ )
  1061. {
  1062. labelmap[mapit->first] = classes+1;
  1063. labelmapback[classes] = mapit->first;
  1064. classes++;
  1065. }
  1066. //balancing
  1067. int featcounter = 0;
  1068. a = vector<double> ( classes, 0.0 );
  1069. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  1070. {
  1071. int xsize = ( int ) currentfeats[iCounter].width();
  1072. int ysize = ( int ) currentfeats[iCounter].height();
  1073. for ( int x = 0; x < xsize; x++ )
  1074. {
  1075. for ( int y = 0; y < ysize; y++ )
  1076. {
  1077. featcounter++;
  1078. int cn = labels[iCounter] ( x, y );
  1079. a[labelmap[cn]-1] ++;
  1080. }
  1081. }
  1082. }
  1083. for ( int i = 0; i < ( int ) a.size(); i++ )
  1084. {
  1085. a[i] /= ( double ) featcounter;
  1086. }
  1087. #ifdef DEBUG
  1088. for ( int i = 0; i < ( int ) a.size(); i++ )
  1089. {
  1090. cout << "a[" << i << "]: " << a[i] << endl;
  1091. }
  1092. cout << "a.size: " << a.size() << endl;
  1093. #endif
  1094. depth = 0;
  1095. int uniquenumber = 0;
  1096. for ( int t = 0; t < nbTrees; t++ )
  1097. {
  1098. vector<TreeNode> tree;
  1099. tree.push_back ( TreeNode() );
  1100. tree[0].dist = vector<double> ( classes, 0.0 );
  1101. tree[0].depth = depth;
  1102. tree[0].featcounter = amountPixels;
  1103. tree[0].nodeNumber = uniquenumber;
  1104. uniquenumber++;
  1105. forest.push_back ( tree );
  1106. }
  1107. vector<int> startnode ( nbTrees, 0 );
  1108. bool allleaf = false;
  1109. //int baseFeatSize = allfeats[0].size();
  1110. vector<MultiChannelImageT<double> > integralImgs ( imgcounter, MultiChannelImageT<double>() );
  1111. while ( !allleaf && depth < maxDepth )
  1112. {
  1113. #ifdef DEBUG
  1114. cout << "depth: " << depth << endl;
  1115. #endif
  1116. allleaf = true;
  1117. vector<MultiChannelImageT<unsigned short int> > lastfeats = currentfeats;
  1118. #if 1
  1119. Timer timer;
  1120. timer.start();
  1121. #endif
  1122. for ( int tree = 0; tree < nbTrees; tree++ )
  1123. {
  1124. int t = ( int ) forest[tree].size();
  1125. int s = startnode[tree];
  1126. startnode[tree] = t;
  1127. //TODO vielleicht parallel wenn nächste schleife trotzdem noch parallelsiert würde, die hat mehr gewicht
  1128. //#pragma omp parallel for
  1129. for ( int i = s; i < t; i++ )
  1130. {
  1131. if ( !forest[tree][i].isleaf && forest[tree][i].left < 0 )
  1132. {
  1133. #if 0
  1134. timer.stop();
  1135. cout << "time 1: " << timer.getLast() << endl;
  1136. timer.start();
  1137. #endif
  1138. Operation *splitfeat = NULL;
  1139. double splitval;
  1140. double bestig = getBestSplit ( allfeats, lastfeats, integralImgs, labels, i, splitfeat, splitval, tree );
  1141. #if 0
  1142. timer.stop();
  1143. double tl = timer.getLast();
  1144. if ( tl > 10.0 )
  1145. {
  1146. cout << "time 2: " << tl << endl;
  1147. cout << "slow split: " << splitfeat->writeInfos() << endl;
  1148. getchar();
  1149. }
  1150. timer.start();
  1151. #endif
  1152. forest[tree][i].feat = splitfeat;
  1153. forest[tree][i].decision = splitval;
  1154. if ( splitfeat != NULL )
  1155. {
  1156. allleaf = false;
  1157. int left = forest[tree].size();
  1158. forest[tree].push_back ( TreeNode() );
  1159. forest[tree].push_back ( TreeNode() );
  1160. int right = left + 1;
  1161. forest[tree][i].left = left;
  1162. forest[tree][i].right = right;
  1163. forest[tree][left].dist = vector<double> ( classes, 0.0 );
  1164. forest[tree][right].dist = vector<double> ( classes, 0.0 );
  1165. forest[tree][left].depth = depth + 1;
  1166. forest[tree][right].depth = depth + 1;
  1167. forest[tree][left].featcounter = 0;
  1168. forest[tree][right].featcounter = 0;
  1169. forest[tree][left].nodeNumber = uniquenumber;
  1170. int leftu = uniquenumber;
  1171. uniquenumber++;
  1172. forest[tree][right].nodeNumber = uniquenumber;
  1173. int rightu = uniquenumber;
  1174. uniquenumber++;
  1175. forest[tree][right].featcounter = 0;
  1176. #if 0
  1177. timer.stop();
  1178. cout << "time 3: " << timer.getLast() << endl;
  1179. timer.start();
  1180. #endif
  1181. #pragma omp parallel for
  1182. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  1183. {
  1184. int xsize = currentfeats[iCounter].width();
  1185. int ysize = currentfeats[iCounter].height();
  1186. for ( int x = 0; x < xsize; x++ )
  1187. {
  1188. for ( int y = 0; y < ysize; y++ )
  1189. {
  1190. if ( currentfeats[iCounter].get ( x, y, tree ) == i )
  1191. {
  1192. Features feat;
  1193. feat.feats = &allfeats[iCounter];
  1194. feat.cfeats = &lastfeats[iCounter];
  1195. feat.cTree = tree;
  1196. feat.tree = &forest[tree];
  1197. feat.integralImg = &integralImgs[iCounter];
  1198. double val = splitfeat->getVal ( feat, x, y );
  1199. int subx = x / grid;
  1200. int suby = y / grid;
  1201. #pragma omp critical
  1202. if ( val < splitval )
  1203. {
  1204. currentfeats[iCounter].set ( x, y, left, tree );
  1205. if(labelmap[labels[iCounter] ( x, y ) ] > 0)
  1206. forest[tree][left].dist[labelmap[labels[iCounter] ( x, y )]-1]++;
  1207. forest[tree][left].featcounter++;
  1208. SparseVector v;
  1209. v.insert ( pair<int, double> ( leftu, 1.0 ) );
  1210. textonMap[iCounter] ( subx, suby ).add ( v );
  1211. }
  1212. else
  1213. {
  1214. currentfeats[iCounter].set ( x, y, right, tree );
  1215. if(labelmap[labels[iCounter] ( x, y ) ] > 0)
  1216. forest[tree][right].dist[labelmap[labels[iCounter] ( x, y )]-1]++;
  1217. forest[tree][right].featcounter++;
  1218. //feld im subsampled finden und in diesem rechts hochzählen
  1219. SparseVector v;
  1220. v.insert ( pair<int, double> ( rightu, 1.0 ) );
  1221. textonMap[iCounter] ( subx, suby ).add ( v );
  1222. }
  1223. }
  1224. }
  1225. }
  1226. }
  1227. #if 0
  1228. timer.stop();
  1229. cout << "time 4: " << timer.getLast() << endl;
  1230. timer.start();
  1231. #endif
  1232. // forest[tree][right].featcounter = forest[tree][i].featcounter - forest[tree][left].featcounter;
  1233. double lcounter = 0.0, rcounter = 0.0;
  1234. for ( uint d = 0; d < forest[tree][left].dist.size(); d++ )
  1235. {
  1236. if ( forbidden_classes.find ( labelmapback[d] ) != forbidden_classes.end() )
  1237. {
  1238. forest[tree][left].dist[d] = 0;
  1239. forest[tree][right].dist[d] = 0;
  1240. }
  1241. else
  1242. {
  1243. forest[tree][left].dist[d] /= a[d];
  1244. lcounter += forest[tree][left].dist[d];
  1245. forest[tree][right].dist[d] /= a[d];
  1246. rcounter += forest[tree][right].dist[d];
  1247. }
  1248. }
  1249. #if 0
  1250. timer.stop();
  1251. cout << "time 5: " << timer.getLast() << endl;
  1252. timer.start();
  1253. #endif
  1254. if ( lcounter <= 0 || rcounter <= 0 )
  1255. {
  1256. cout << "lcounter : " << lcounter << " rcounter: " << rcounter << endl;
  1257. cout << "splitval: " << splitval << " splittype: " << splitfeat->writeInfos() << endl;
  1258. cout << "bestig: " << bestig << endl;
  1259. for ( int iCounter = 0; iCounter < imgcounter; iCounter++ )
  1260. {
  1261. int xsize = currentfeats[iCounter].width();
  1262. int ysize = currentfeats[iCounter].height();
  1263. int counter = 0;
  1264. for ( int x = 0; x < xsize; x++ )
  1265. {
  1266. for ( int y = 0; y < ysize; y++ )
  1267. {
  1268. if ( lastfeats[iCounter].get ( x, y, tree ) == i )
  1269. {
  1270. if ( ++counter > 30 )
  1271. break;
  1272. Features feat;
  1273. feat.feats = &allfeats[iCounter];
  1274. feat.cfeats = &lastfeats[iCounter];
  1275. feat.cTree = tree;
  1276. feat.tree = &forest[tree];
  1277. feat.integralImg = &integralImgs[iCounter];
  1278. double val = splitfeat->getVal ( feat, x, y );
  1279. cout << "splitval: " << splitval << " val: " << val << endl;
  1280. }
  1281. }
  1282. }
  1283. }
  1284. assert ( lcounter > 0 && rcounter > 0 );
  1285. }
  1286. for ( uint d = 0; d < forest[tree][left].dist.size(); d++ )
  1287. {
  1288. forest[tree][left].dist[d] /= lcounter;
  1289. forest[tree][right].dist[d] /= rcounter;
  1290. }
  1291. }
  1292. else
  1293. {
  1294. forest[tree][i].isleaf = true;
  1295. }
  1296. }
  1297. }
  1298. #if 0
  1299. timer.stop();
  1300. cout << "time after tree: " << timer.getLast() << endl;
  1301. timer.start();
  1302. #endif
  1303. }
  1304. //compute integral image
  1305. int channels = classes + allfeats[0].channels();
  1306. #if 0
  1307. timer.stop();
  1308. cout << "time for part0: " << timer.getLast() << endl;
  1309. timer.start();
  1310. #endif
  1311. if ( integralImgs[0].width() == 0 )
  1312. {
  1313. for ( int i = 0; i < imgcounter; i++ )
  1314. {
  1315. int xsize = allfeats[i].width();
  1316. int ysize = allfeats[i].height();
  1317. integralImgs[i].reInit ( xsize, ysize, channels );
  1318. integralImgs[i].setAll ( 0.0 );
  1319. }
  1320. }
  1321. #if 0
  1322. timer.stop();
  1323. cout << "time for part1: " << timer.getLast() << endl;
  1324. timer.start();
  1325. #endif
  1326. #pragma omp parallel for
  1327. for ( int i = 0; i < imgcounter; i++ )
  1328. {
  1329. computeIntegralImage ( currentfeats[i], allfeats[i], integralImgs[i] );
  1330. computeIntegralImage ( textonMap[i], integralTexton[i] );
  1331. }
  1332. #if 1
  1333. timer.stop();
  1334. cout << "time for depth " << depth << ": " << timer.getLast() << endl;
  1335. #endif
  1336. depth++;
  1337. }
  1338. #ifdef DEBUG
  1339. for ( int tree = 0; tree < nbTrees; tree++ )
  1340. {
  1341. int t = ( int ) forest[tree].size();
  1342. for ( int i = 0; i < t; i++ )
  1343. {
  1344. printf ( "tree[%i]: left: %i, right: %i", i, forest[tree][i].left, forest[tree][i].right );
  1345. if ( !forest[tree][i].isleaf && forest[tree][i].left != -1 )
  1346. {
  1347. cout << ", feat: " << forest[tree][i].feat->writeInfos() << " ";
  1348. opOverview[forest[tree][i].feat->getOps() ]++;
  1349. contextOverview[forest[tree][i].depth][ ( int ) forest[tree][i].feat->getContext() ]++;
  1350. }
  1351. for ( int d = 0; d < ( int ) forest[tree][i].dist.size(); d++ )
  1352. {
  1353. cout << " " << forest[tree][i].dist[d];
  1354. }
  1355. cout << endl;
  1356. }
  1357. }
  1358. for ( uint c = 0; c < ops.size(); c++ )
  1359. {
  1360. cout << ops[c]->writeInfos() << ": " << opOverview[ops[c]->getOps() ] << endl;
  1361. }
  1362. for ( uint c = 0; c < cops.size(); c++ )
  1363. {
  1364. cout << cops[c]->writeInfos() << ": " << opOverview[cops[c]->getOps() ] << endl;
  1365. }
  1366. for ( int d = 0; d < maxDepth; d++ )
  1367. {
  1368. double sum = contextOverview[d][0] + contextOverview[d][1];
  1369. contextOverview[d][0] /= sum;
  1370. contextOverview[d][1] /= sum;
  1371. cout << "depth: " << d << " woContext: " << contextOverview[d][0] << " wContext: " << contextOverview[d][1] << endl;
  1372. }
  1373. #endif
  1374. }
  1375. void SemSegContextTree::semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities )
  1376. {
  1377. int xpos = 8;
  1378. //int xpos = 15;
  1379. int ypos = 78;
  1380. int xsize;
  1381. int ysize;
  1382. ce->getImageSize ( xsize, ysize );
  1383. int numClasses = classNames->numClasses();
  1384. fprintf ( stderr, "ContextTree classification !\n" );
  1385. probabilities.reInit ( xsize, ysize, numClasses, true );
  1386. probabilities.setAll ( 0 );
  1387. NICE::ColorImage img;
  1388. std::string currentFile = Globals::getCurrentImgFN();
  1389. try {
  1390. img = ColorImage ( currentFile );
  1391. } catch ( Exception ) {
  1392. cerr << "SemSeg: error opening image file <" << currentFile << ">" << endl;
  1393. return;
  1394. }
  1395. //TODO: resize image?!
  1396. MultiChannelImageT<double> feats;
  1397. MultiChannelImageT<SparseVector> textonMap ( xsize / grid + 1, ysize / grid + 1, 1 );
  1398. MultiChannelImageT<SparseVector> integralTexton ( xsize / grid + 1, ysize / grid + 1, 1 );
  1399. #ifdef LOCALFEATS
  1400. lfcw->getFeats ( img, feats );
  1401. #else
  1402. feats.reInit ( xsize, ysize, 3, true );
  1403. for ( int x = 0; x < xsize; x++ )
  1404. {
  1405. for ( int y = 0; y < ysize; y++ )
  1406. {
  1407. for ( int r = 0; r < 3; r++ )
  1408. {
  1409. feats.set ( x, y, img.getPixel ( x, y, r ), r );
  1410. }
  1411. }
  1412. }
  1413. feats = ColorSpace::rgbtolab ( feats );
  1414. #endif
  1415. bool allleaf = false;
  1416. MultiChannelImageT<double> integralImg;
  1417. MultiChannelImageT<unsigned short int> currentfeats ( xsize, ysize, nbTrees );
  1418. currentfeats.setAll ( 0 );
  1419. depth = 0;
  1420. for ( int d = 0; d < maxDepth && !allleaf; d++ )
  1421. {
  1422. allleaf = true;
  1423. //TODO vielleicht parallel wenn nächste schleife auch noch parallelsiert würde, die hat mehr gewicht
  1424. //#pragma omp parallel for
  1425. MultiChannelImageT<unsigned short int> lastfeats = currentfeats;
  1426. for ( int tree = 0; tree < nbTrees; tree++ )
  1427. {
  1428. for ( int x = 0; x < xsize; x++ )
  1429. {
  1430. for ( int y = 0; y < ysize; y++ )
  1431. {
  1432. int t = currentfeats.get ( x, y, tree );
  1433. if ( forest[tree][t].left > 0 )
  1434. {
  1435. allleaf = false;
  1436. Features feat;
  1437. feat.feats = &feats;
  1438. feat.cfeats = &lastfeats;
  1439. feat.cTree = tree;
  1440. feat.tree = &forest[tree];
  1441. feat.integralImg = &integralImg;
  1442. double val = forest[tree][t].feat->getVal ( feat, x, y );
  1443. int subx = x / grid;
  1444. int suby = y / grid;
  1445. if ( val < forest[tree][t].decision )
  1446. {
  1447. currentfeats.set ( x, y, forest[tree][t].left, tree );
  1448. SparseVector v;
  1449. v.insert ( pair<int, double> ( forest[tree][forest[tree][t].left].nodeNumber, 1.0 ) );
  1450. textonMap ( subx, suby ).add ( v );
  1451. }
  1452. else
  1453. {
  1454. currentfeats.set ( x, y, forest[tree][t].right, tree );
  1455. SparseVector v;
  1456. v.insert ( pair<int, double> ( forest[tree][forest[tree][t].right].nodeNumber, 1.0 ) );
  1457. textonMap ( subx, suby ).add ( v );
  1458. }
  1459. if(x == xpos && y == ypos)
  1460. {
  1461. cout << "val: " << val << " decision: " << forest[tree][t].decision << " details: " << forest[tree][t].feat->writeInfos() << endl;
  1462. }
  1463. }
  1464. }
  1465. }
  1466. //compute integral image
  1467. int channels = ( int ) labelmap.size() + feats.channels();
  1468. if ( integralImg.width() == 0 )
  1469. {
  1470. int xsize = feats.width();
  1471. int ysize = feats.height();
  1472. integralImg.reInit ( xsize, ysize, channels );
  1473. }
  1474. }
  1475. computeIntegralImage ( currentfeats, feats, integralImg );
  1476. computeIntegralImage ( textonMap, integralTexton );
  1477. depth++;
  1478. }
  1479. cout << forest[0][currentfeats.get ( xpos, ypos, 0 )].dist << endl;
  1480. if ( pixelWiseLabeling )
  1481. {
  1482. //finales labeln:
  1483. long int offset = 0;
  1484. for ( int x = 0; x < xsize; x++ )
  1485. {
  1486. for ( int y = 0; y < ysize; y++, offset++ )
  1487. {
  1488. double maxvalue = - numeric_limits<double>::max(); //TODO: das muss nur pro knoten gemacht werden, nicht pro pixel
  1489. int maxindex = 0;
  1490. uint s = forest[0][0].dist.size();
  1491. for ( uint i = 0; i < s; i++ )
  1492. {
  1493. probabilities.data[labelmapback[i]][offset] = getMeanProb ( x, y, i, currentfeats );
  1494. if ( probabilities.data[labelmapback[i]][offset] > maxvalue )
  1495. {
  1496. maxvalue = probabilities.data[labelmapback[i]][offset];
  1497. maxindex = labelmapback[i];
  1498. }
  1499. segresult.setPixel ( x, y, maxindex );
  1500. }
  1501. if ( maxvalue > 1 )
  1502. cout << "maxvalue: " << maxvalue << endl;
  1503. }
  1504. }
  1505. }
  1506. else
  1507. {
  1508. //final labeling using segmentation
  1509. Matrix regions;
  1510. //showImage(img);
  1511. int regionNumber = segmentation->segRegions ( img, regions );
  1512. cout << "regions: " << regionNumber << endl;
  1513. int dSize = forest[0][0].dist.size();
  1514. vector<vector<double> > regionProbs ( regionNumber, vector<double> ( dSize, 0.0 ) );
  1515. vector<int> bestlabels ( regionNumber, 0 );
  1516. /*
  1517. for(int r = 0; r < regionNumber; r++)
  1518. {
  1519. Image over(img.width(), img.height());
  1520. for(int y = 0; y < img.height(); y++)
  1521. {
  1522. for(int x = 0; x < img.width(); x++)
  1523. {
  1524. if(((int)regions(x,y)) == r)
  1525. over.setPixel(x,y,1);
  1526. else
  1527. over.setPixel(x,y,0);
  1528. }
  1529. }
  1530. cout << "r: " << r << endl;
  1531. showImageOverlay(img, over);
  1532. }
  1533. */
  1534. for ( int y = 0; y < img.height(); y++ )
  1535. {
  1536. for ( int x = 0; x < img.width(); x++ )
  1537. {
  1538. int cregion = regions ( x, y );
  1539. for ( int d = 0; d < dSize; d++ )
  1540. {
  1541. regionProbs[cregion][d] += getMeanProb ( x, y, d, currentfeats );
  1542. }
  1543. }
  1544. }
  1545. for ( int r = 0; r < regionNumber; r++ )
  1546. {
  1547. double maxval = regionProbs[r][0];
  1548. bestlabels[r] = 0;
  1549. for ( int d = 1; d < dSize; d++ )
  1550. {
  1551. if ( maxval < regionProbs[r][d] )
  1552. {
  1553. maxval = regionProbs[r][d];
  1554. bestlabels[r] = d;
  1555. }
  1556. }
  1557. bestlabels[r] = labelmapback[bestlabels[r]];
  1558. }
  1559. for ( int y = 0; y < img.height(); y++ )
  1560. {
  1561. for ( int x = 0; x < img.width(); x++ )
  1562. {
  1563. segresult.setPixel ( x, y, bestlabels[regions ( x,y ) ] );
  1564. }
  1565. }
  1566. }
  1567. cout << "segmentation finished" << endl;
  1568. }
  1569. void SemSegContextTree::store ( std::ostream & os, int format ) const
  1570. {
  1571. os << nbTrees << endl;
  1572. classnames.store ( os );
  1573. map<int, int>::const_iterator it;
  1574. os << labelmap.size() << endl;
  1575. for ( it = labelmap.begin() ; it != labelmap.end(); it++ )
  1576. os << ( *it ).first << " " << ( *it ).second << endl;
  1577. os << labelmapback.size() << endl;
  1578. for ( it = labelmapback.begin() ; it != labelmapback.end(); it++ )
  1579. os << ( *it ).first << " " << ( *it ).second << endl;
  1580. int trees = forest.size();
  1581. os << trees << endl;
  1582. for ( int t = 0; t < trees; t++ )
  1583. {
  1584. int nodes = forest[t].size();
  1585. os << nodes << endl;
  1586. for ( int n = 0; n < nodes; n++ )
  1587. {
  1588. 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;
  1589. os << forest[t][n].dist << endl;
  1590. if ( forest[t][n].feat == NULL )
  1591. os << -1 << endl;
  1592. else
  1593. {
  1594. os << forest[t][n].feat->getOps() << endl;
  1595. forest[t][n].feat->store ( os );
  1596. }
  1597. }
  1598. }
  1599. }
  1600. void SemSegContextTree::restore ( std::istream & is, int format )
  1601. {
  1602. is >> nbTrees;
  1603. classnames.restore ( is );
  1604. int lsize;
  1605. is >> lsize;
  1606. labelmap.clear();
  1607. for ( int l = 0; l < lsize; l++ )
  1608. {
  1609. int first, second;
  1610. is >> first;
  1611. is >> second;
  1612. labelmap[first] = second;
  1613. }
  1614. is >> lsize;
  1615. labelmapback.clear();
  1616. for ( int l = 0; l < lsize; l++ )
  1617. {
  1618. int first, second;
  1619. is >> first;
  1620. is >> second;
  1621. labelmapback[first] = second;
  1622. }
  1623. int trees;
  1624. is >> trees;
  1625. forest.clear();
  1626. for ( int t = 0; t < trees; t++ )
  1627. {
  1628. vector<TreeNode> tmptree;
  1629. forest.push_back ( tmptree );
  1630. int nodes;
  1631. is >> nodes;
  1632. //cout << "nodes: " << nodes << endl;
  1633. for ( int n = 0; n < nodes; n++ )
  1634. {
  1635. TreeNode tmpnode;
  1636. forest[t].push_back ( tmpnode );
  1637. is >> forest[t][n].left;
  1638. is >> forest[t][n].right;
  1639. is >> forest[t][n].decision;
  1640. is >> forest[t][n].isleaf;
  1641. is >> forest[t][n].depth;
  1642. is >> forest[t][n].featcounter;
  1643. is >> forest[t][n].nodeNumber;
  1644. is >> forest[t][n].dist;
  1645. int feattype;
  1646. is >> feattype;
  1647. assert ( feattype < NBOPERATIONS );
  1648. forest[t][n].feat = NULL;
  1649. if ( feattype >= 0 )
  1650. {
  1651. for ( int o = 0; o < ops.size(); o++ )
  1652. {
  1653. if ( ops[o]->getOps() == feattype )
  1654. {
  1655. forest[t][n].feat = ops[o]->clone();
  1656. break;
  1657. }
  1658. }
  1659. if ( forest[t][n].feat == NULL )
  1660. {
  1661. for ( int o = 0; o < cops.size(); o++ )
  1662. {
  1663. if ( cops[o]->getOps() == feattype )
  1664. {
  1665. forest[t][n].feat = cops[o]->clone();
  1666. break;
  1667. }
  1668. }
  1669. }
  1670. assert ( forest[t][n].feat != NULL );
  1671. forest[t][n].feat->restore ( is );
  1672. }
  1673. }
  1674. }
  1675. }