SemSegCsurka.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  1. #include <iostream>
  2. #include "SemSegCsurka.h"
  3. #include "vislearning/baselib/ICETools.h"
  4. #include "core/image/Filter.h"
  5. #include <sstream>
  6. using namespace std;
  7. using namespace NICE;
  8. using namespace OBJREC;
  9. #undef DEBUG_CSURK
  10. SemSegCsurka::SemSegCsurka ( const Config *conf,
  11. const MultiDataset *md )
  12. : SemanticSegmentation ( conf, & ( md->getClassNames ( "train" ) ) )
  13. {
  14. this->conf = conf;
  15. opSiftImpl = conf->gS ( "Descriptor", "implementation", "VANDESANDE" );
  16. readfeat = conf->gB ( "Descriptor", "read", true );
  17. writefeat = conf->gB ( "Descriptor", "write", true );
  18. #ifdef DEBUG_CSURK
  19. clog << "[log] SemSegCsurka::SemSegCsurka: OppenentSift implemenation: " << opSiftImpl << endl;
  20. #endif
  21. save_cache = conf->gB ( "FPCPixel", "save_cache", true );
  22. read_cache = conf->gB ( "FPCPixel", "read_cache", false );
  23. cache = conf->gS ( "cache", "root", "" );
  24. sigmaweight = conf->gD ( "SemSegCsurka", "sigmaweight", 0.6 );
  25. dim = conf->gI ( "SemSegCsurka", "pcadim", 50 );
  26. usepca = conf->gB ( "SemSegCsurka", "usepca", true );
  27. calcpca = conf->gB ( "SemSegCsurka", "calcpca", false );
  28. usegmm = conf->gB ( "SemSegCsurka", "usegmm", false );
  29. norm = conf->gB ( "SemSegCsurka", "normalize", false );
  30. usefisher = conf->gB ( "SemSegCsurka", "usefisher", false );
  31. dogmm = conf->gB ( "SemSegCsurka", "dogmm", false );
  32. gaussians = conf->gI ( "SemSegCsurka", "gaussians", 50 );
  33. usekmeans = conf->gB ( "SemSegCsurka", "usekmeans", false );
  34. kmeansfeat = conf->gI ( "SemSegCsurka", "kmeansfeat", 50 );
  35. kmeanshard = conf->gB ( "SemSegCsurka", "kmeanshard", false );
  36. cname = conf->gS ( "SemSegCsurka", "classifier", "RandomForests" );
  37. anteil = conf->gD ( "SemSegCsurka", "anteil", 1.0 );
  38. userellocprior = conf->gB ( "SemSegCsurka", "rellocfeat", false );
  39. bool usesrg = conf->gB ( "SemSegCsurka", "usesrg", false );
  40. useregions = conf->gB ( "SemSegCsurka", "useregions", true );
  41. savesteps = conf->gB ( "SemSegCsurka", "savesteps", true );
  42. bool usegcopt = conf->gB ( "SemSegCsurka", "usegcopt", false );
  43. bestclasses = conf->gI ( "SemSegCsurka", "bestclasses", 0 );
  44. smoothhl = conf->gB ( "SemSegCsurka", "smoothhl", false );
  45. smoothfactor = conf->gD ( "SemSegCsurka", "smoothfactor", 1.0 );
  46. usecolorfeats = conf->gB ( "SemSegCsurka", "usecolorfeats", false );
  47. string rsMethod = conf->gS ( "SemSegCsurka", "segmentation", "meanshift" );
  48. g = NULL;
  49. k = NULL;
  50. relloc = NULL;
  51. srg = NULL;
  52. gcopt = NULL;
  53. if ( !useregions && ( userellocprior || usesrg ) )
  54. {
  55. cerr << "relative location priors and super region growing are just supported in combination with useregions" << endl;
  56. exit ( 1 );
  57. }
  58. if ( usepca )
  59. pca = PCA ( dim );
  60. RegionSegmentationMethod * tmpseg;
  61. if ( rsMethod == "meanshift" )
  62. tmpseg = new RSMeanShift ( conf );
  63. else
  64. tmpseg = new RSGraphBased ( conf );
  65. if ( save_cache )
  66. seg = new RSCache ( conf, tmpseg );
  67. else
  68. seg = tmpseg;
  69. if ( userellocprior )
  70. relloc = new RelativeLocationPrior ( conf );
  71. else
  72. relloc = NULL;
  73. #ifdef NICE_USELIB_ICE
  74. if ( usesrg )
  75. srg = new PPSuperregion ( conf );
  76. else
  77. srg = NULL;
  78. #else
  79. srg = NULL;
  80. #endif
  81. if ( usegcopt )
  82. gcopt = new PPGraphCut ( conf );
  83. else
  84. gcopt = NULL;
  85. classifier = NULL;
  86. vclassifier = NULL;
  87. if ( cname == "RandomForests" )
  88. classifier = new FPCRandomForests ( conf, "ClassifierForest" );
  89. else if ( cname == "SMLR" )
  90. classifier = new FPCSMLR ( conf, "ClassifierSMLR" );
  91. else
  92. vclassifier = CSGeneric::selectVecClassifier ( conf, "main" );
  93. //classifier = new FPCSparseMultinomialLogisticRegression(conf, "ClassifierSMLR");
  94. if ( classifier != NULL )
  95. classifier->setMaxClassNo ( classNames->getMaxClassno() );
  96. else
  97. vclassifier->setMaxClassNo ( classNames->getMaxClassno() );
  98. cn = md->getClassNames ( "train" );
  99. if ( read_cache )
  100. {
  101. fprintf ( stderr, "SemSegCsurka:: Reading classifier data from %s\n", ( cache + "/fpcrf.data" ).c_str() );
  102. if ( classifier != NULL )
  103. classifier->read ( cache + "/fpcrf.data" );
  104. else
  105. vclassifier->read ( cache + "/veccl.data" );
  106. if ( usepca )
  107. {
  108. std::string filename = cache + "/pca";
  109. pca.read ( filename );
  110. }
  111. if ( usegmm )
  112. {
  113. g = new GMM ( conf, gaussians );
  114. if ( !g->loadData ( cache + "/gmm" ) )
  115. {
  116. cerr << "SemSegCsurka:: no gmm file found" << endl;
  117. exit ( -1 );
  118. }
  119. }
  120. else {
  121. g = NULL;
  122. }
  123. if ( usekmeans )
  124. {
  125. k = new KMeansOnline ( gaussians );
  126. }
  127. fprintf ( stderr, "SemSegCsurka:: successfully read\n" );
  128. std::string filename = cache + "/rlp";
  129. FILE *value;
  130. value = fopen ( filename.c_str(), "r" );
  131. if ( value == NULL )
  132. {
  133. trainpostprocess ( md );
  134. }
  135. else
  136. {
  137. if ( userellocprior )
  138. {
  139. relloc->read ( filename );
  140. }
  141. }
  142. filename = cache + "/srg";
  143. value = fopen ( filename.c_str(), "r" );
  144. if ( value == NULL )
  145. {
  146. trainpostprocess ( md );
  147. }
  148. else
  149. {
  150. if ( srg != NULL )
  151. {
  152. srg->read ( filename );
  153. }
  154. }
  155. }
  156. else
  157. {
  158. train ( md );
  159. }
  160. }
  161. SemSegCsurka::~SemSegCsurka()
  162. {
  163. // clean-up
  164. if ( classifier != NULL )
  165. delete classifier;
  166. if ( vclassifier != NULL )
  167. delete vclassifier;
  168. if ( seg != NULL )
  169. delete seg;
  170. g = NULL;
  171. if ( g != NULL )
  172. delete g;
  173. }
  174. void SemSegCsurka::normalize ( Examples &ex )
  175. {
  176. assert ( ex.size() > 0 );
  177. if ( vecmin.size() == 0 )
  178. {
  179. for ( int j = 0; j < ( int ) ex[0].second.vec->size(); j++ )
  180. {
  181. double maxv = -numeric_limits<int>::max();
  182. double minv = numeric_limits<int>::max();
  183. for ( int i = 0; i < ( int ) ex.size(); i++ )
  184. {
  185. maxv = std::max ( maxv, ( *ex[i].second.vec ) [j] );
  186. minv = std::min ( minv, ( *ex[i].second.vec ) [j] );
  187. }
  188. vecmin.push_back ( minv );
  189. vecmax.push_back ( maxv );
  190. }
  191. }
  192. for ( int i = 0; i < ( int ) ex.size(); i++ )
  193. {
  194. for ( int j = 0; j < ( int ) ex[i].second.vec->size(); j++ )
  195. {
  196. ( *ex[i].second.vec ) [j] = ( ( *ex[i].second.vec ) [j] - vecmin[j] ) / ( vecmax[j] - vecmin[j] );
  197. }
  198. }
  199. return;
  200. }
  201. void SemSegCsurka::convertLowToHigh ( Examples &ex, double reduce )
  202. {
  203. cout << "converting low-level features to high-level features" << endl;
  204. if ( reduce >= 1.0 )
  205. {
  206. for ( int i = 0; i < ( int ) ex.size(); i++ )
  207. {
  208. SparseVector *f = new SparseVector();
  209. if ( usekmeans )
  210. {
  211. k->getDist ( *ex[i].second.vec, *f, kmeansfeat, kmeanshard );
  212. }
  213. else
  214. {
  215. if ( usefisher )
  216. g->getFisher ( *ex[i].second.vec, *f );
  217. else
  218. g->getProbs ( *ex[i].second.vec, *f );
  219. }
  220. delete ex[i].second.vec;
  221. ex[i].second.vec = NULL;
  222. ex[i].second.svec = f;
  223. }
  224. }
  225. else
  226. {
  227. srand ( time ( NULL ) );
  228. vector<bool> del ( ex.size(), false );
  229. cout << "Example size old " << ex.size() << endl;
  230. //#pragma omp parallel for
  231. for ( int i = 0; i < ( int ) ex.size(); i++ )
  232. {
  233. double rval = ( double ) rand() / ( double ) RAND_MAX;
  234. if ( rval < reduce )
  235. {
  236. SparseVector *f = new SparseVector();
  237. if ( usekmeans )
  238. k->getDist ( *ex[i].second.vec, *f, kmeansfeat, kmeanshard );
  239. else
  240. {
  241. if ( usefisher )
  242. g->getFisher ( *ex[i].second.vec, *f );
  243. else
  244. g->getProbs ( *ex[i].second.vec, *f );
  245. }
  246. delete ex[i].second.vec;
  247. ex[i].second.vec = NULL;
  248. ex[i].second.svec = f;
  249. }
  250. else
  251. {
  252. del[i] = true;
  253. }
  254. }
  255. for ( int i = ( int ) del.size() - 1; i >= 0; i-- )
  256. {
  257. if ( del[i] )
  258. {
  259. ex.erase ( ex.begin() + i );
  260. }
  261. }
  262. cerr << "Example size new " << ex.size() << endl;
  263. }
  264. cerr << "converting low-level features to high-level features finished" << endl;
  265. }
  266. void SemSegCsurka::smoothHL ( Examples ex )
  267. {
  268. if ( !smoothhl )
  269. return;
  270. assert ( ex.size() > 1 );
  271. long long int minx = numeric_limits<long long int>::max();
  272. long long int miny = numeric_limits<long long int>::max();
  273. long long int maxx = -numeric_limits<long long int>::max();
  274. long long int maxy = -numeric_limits<long long int>::max();
  275. long long int distx = numeric_limits<long long int>::max();
  276. long long int disty = numeric_limits<long long int>::max();
  277. set<double> scales;
  278. for ( int i = 0; i < ( int ) ex.size(); i++ )
  279. {
  280. scales.insert ( ex[i].second.scale );
  281. }
  282. map<double, int> scalepos;
  283. int it = 0;
  284. for ( set<double>::const_iterator iter = scales.begin(); iter != scales.end(); ++iter, ++it )
  285. {
  286. scalepos.insert ( make_pair ( *iter, it ) );
  287. }
  288. for ( int i = 0; i < ( int ) ex.size(); i++ )
  289. {
  290. if ( minx < numeric_limits<int>::max() && ex[i].second.x - minx > 0 )
  291. distx = std::min ( distx, ex[i].second.x - minx );
  292. if ( miny < numeric_limits<int>::max() && ex[i].second.y - miny > 0 )
  293. disty = std::min ( disty, ex[i].second.y - miny );
  294. minx = std::min ( ( long long int ) ex[i].second.x, minx );
  295. maxx = std::max ( ( long long int ) ex[i].second.x, maxx );
  296. miny = std::min ( ( long long int ) ex[i].second.y, miny );
  297. maxy = std::max ( ( long long int ) ex[i].second.y, maxy );
  298. }
  299. distx = abs ( distx );
  300. int xsize = ( maxx - minx ) / distx + 1;
  301. int ysize = ( maxy - miny ) / disty + 1;
  302. double valx = ( ( double ) xsize - 1 ) / ( double ) ( maxx - minx );
  303. double valy = ( ( double ) ysize - 1 ) / ( double ) ( maxy - miny );
  304. //double sigma = smoothfactor;
  305. double sigma = std::max ( xsize, ysize ) * smoothfactor;
  306. //double sigma = 0.2;
  307. cout << "sigma1: " << sigma << endl;
  308. vector<NICE::FloatImage> imgv;
  309. vector<NICE::FloatImage> gaussImgv;
  310. for ( int i = 0; i < ( int ) scalepos.size(); i++ )
  311. {
  312. NICE::FloatImage img ( xsize, ysize );
  313. NICE::FloatImage gaussImg ( xsize, ysize );
  314. imgv.push_back ( img );
  315. gaussImgv.push_back ( gaussImg );
  316. }
  317. for ( int d = 0; d < ex[0].second.svec->getDim(); d++ )
  318. {
  319. //TODO: max und min dynamisches bestimmen
  320. for ( int i = 0; i < ( int ) scalepos.size(); i++ )
  321. {
  322. imgv[i].set ( 0.0 );
  323. gaussImgv[i].set ( 0.0 );
  324. }
  325. for ( int i = 0; i < ( int ) ex.size(); i++ )
  326. {
  327. int xpos = ( ex[i].second.x - minx ) * valx;
  328. int ypos = ( ex[i].second.y - miny ) * valy;
  329. double val = ex[i].second.svec->get ( d );
  330. imgv[scalepos[ex[i].second.scale]].setPixel ( xpos, ypos, val );
  331. }
  332. /*
  333. for(int y = 0; y < ysize; y++)
  334. {
  335. for(int x = 0; x < xsize; x++)
  336. {
  337. // refactor-nice.pl: check this substitution
  338. // old: double val = GetValD(img,x,y);
  339. double val = img.getPixel(x,y);
  340. double c = 0.0;
  341. if(val == 0.0)
  342. {
  343. if(x > 0)
  344. {
  345. // refactor-nice.pl: check this substitution
  346. // old: val+=GetValD(img,x-1,y);
  347. val+=img.getPixel(x-1,y);
  348. c+=1.0;
  349. }
  350. if(y > 0)
  351. {
  352. // refactor-nice.pl: check this substitution
  353. // old: val+=GetValD(img,x,y-1);
  354. val+=img.getPixel(x,y-1);
  355. c+=1.0;
  356. }
  357. if(x < xsize-1)
  358. {
  359. // refactor-nice.pl: check this substitution
  360. // old: val+=GetValD(img,x+1,y);
  361. val+=img.getPixel(x+1,y);
  362. c+=1.0;
  363. }
  364. if(y < ysize-1)
  365. {
  366. // refactor-nice.pl: check this substitution
  367. // old: val+=GetValD(img,x,y+1);
  368. val+=img.getPixel(x,y+1);
  369. c+=1.0;
  370. }
  371. // refactor-nice.pl: check this substitution
  372. // old: PutValD(img,x,y,val/c);
  373. img.setPixel(x,y,val/c);
  374. }
  375. }
  376. }*/
  377. for ( int i = 0; i < ( int ) imgv.size(); i++ )
  378. filterGaussSigmaApproximate<float, float, float> ( imgv[i], sigma, &gaussImgv[i] );
  379. for ( int i = 0; i < ( int ) ex.size(); i++ )
  380. {
  381. int xpos = ( ex[i].second.x - minx ) * valx;
  382. int ypos = ( ex[i].second.y - miny ) * valy;
  383. // refactor-nice.pl: check this substitution
  384. // old: double val = GetValD ( gaussImgv[scalepos[ex[i].second.scale]], xpos, ypos );
  385. double val = gaussImgv[scalepos[ex[i].second.scale]].getPixel ( xpos, ypos );
  386. if ( fabs ( val ) < 1e-7 )
  387. {
  388. if ( ex[i].second.svec->get ( d ) != 0.0 )
  389. {
  390. ex[i].second.svec->erase ( d );
  391. }
  392. }
  393. else
  394. {
  395. ( *ex[i].second.svec ) [d] = val;
  396. }
  397. }
  398. }
  399. }
  400. void SemSegCsurka::initializePCA ( Examples &ex )
  401. {
  402. #ifdef DEBUG
  403. cerr << "start computing pca" << endl;
  404. #endif
  405. std::string filename = cache + "/pca";
  406. FILE *value;
  407. value = fopen ( filename.c_str(), "r" );
  408. if ( value == NULL || calcpca )
  409. {
  410. srand ( time ( NULL ) );
  411. int featsize = ( int ) ex.size();
  412. int maxfeatures = dim * 10;
  413. int olddim = ex[0].second.vec->size();
  414. maxfeatures = std::min ( maxfeatures, featsize );
  415. NICE::Matrix features ( maxfeatures, olddim );
  416. for ( int i = 0; i < maxfeatures; i++ )
  417. {
  418. int k = rand() % featsize;
  419. int vsize = ( int ) ex[k].second.vec->size();
  420. for ( int j = 0; j < vsize; j++ )
  421. {
  422. features ( i, j ) = ( * ( ex[k].second.vec ) ) [j];
  423. }
  424. }
  425. pca.calculateBasis ( features, dim );
  426. if ( save_cache )
  427. pca.save ( filename );
  428. }
  429. else
  430. {
  431. cout << "readpca: " << filename << endl;
  432. pca.read ( filename );
  433. cout << "end" << endl;
  434. }
  435. #ifdef DEBUG
  436. cerr << "finished computing pca" << endl;
  437. #endif
  438. }
  439. void SemSegCsurka::doPCA ( Examples &ex )
  440. {
  441. cout << "converting features using pca starts" << endl;
  442. std::string savedir = cname = conf->gS ( "cache", "root", "/dev/null/" );
  443. std::string shortf = ex.filename;
  444. if ( string::npos != ex.filename.rfind ( "/" ) )
  445. shortf = ex.filename.substr ( ex.filename.rfind ( "/" ) );
  446. std::string filename = savedir + "/pcasave/" + shortf;
  447. std::string syscall = "mkdir " + savedir + "/pcasave";
  448. system ( syscall.c_str() );
  449. cout << "filename: " << filename << endl;
  450. if ( !FileMgt::fileExists ( filename ) || calcpca )
  451. {
  452. ofstream ofStream;
  453. //Opens the file binary
  454. ofStream.open ( filename.c_str(), fstream::out | fstream::binary );
  455. for ( int k = 0; k < ( int ) ex.size(); k++ )
  456. {
  457. NICE::Vector tmp = pca.getFeatureVector ( * ( ex[k].second.vec ), true );
  458. delete ex[k].second.vec;
  459. for ( int d = 0; d < ( int ) tmp.size(); d++ )
  460. ofStream.write ( ( char* ) &tmp[d], sizeof ( double ) );
  461. ex[k].second.vec = new NICE::Vector ( tmp );
  462. }
  463. ofStream.close();
  464. cout << endl;
  465. }
  466. else
  467. {
  468. ifstream ifStream;
  469. ifStream.open ( filename.c_str(), std::fstream::in | std::fstream::binary );
  470. for ( int k = 0; k < ( int ) ex.size(); k++ )
  471. {
  472. NICE::Vector tmp = NICE::Vector ( dim );
  473. delete ex[k].second.vec;
  474. for ( int d = 0; d < dim; d++ )
  475. ifStream.read ( ( char* ) &tmp[d], sizeof ( double ) );
  476. ex[k].second.vec = new NICE::Vector ( tmp );
  477. }
  478. ifStream.close();
  479. }
  480. cout << "converting features using pca finished" << endl;
  481. }
  482. void SemSegCsurka::train ( const MultiDataset *md )
  483. {
  484. /*die einzelnen Trainingsschritte
  485. 1. auf allen Trainingsbilder SIFT Merkmale an den Gitterpunkten bei allen Auflösungen bestimmen
  486. 2. PCA anwenden
  487. 3. aus diesen ein GMM erstellen
  488. 4. für jedes SIFT-Merkmal einen Vektor erstellen, der an der Stelle i die Wahrscheinlichkeit enthällt zur Verteilung i des GMM, Zur Zeit mit BoV-Alternative durch Moosman06 erledigt
  489. 5. diese Vektoren in einem diskriminitativen Klassifikator ( z.B. SLR oder Randomized Forests) zusammen mit ihrer Klassenzugehörigkeit anlernen
  490. */
  491. #ifdef DEBUG
  492. cerr << "SemSegCsurka:: training starts" << endl;
  493. #endif
  494. Examples examples;
  495. examples.filename = "training";
  496. // Welche Opponentsift Implementierung soll genutzt werden ?
  497. LocalFeatureRepresentation *cSIFT = NULL;
  498. LocalFeatureRepresentation *writeFeats = NULL;
  499. LocalFeatureRepresentation *readFeats = NULL;
  500. LocalFeatureRepresentation *getFeats = NULL;
  501. if ( opSiftImpl == "NICE" )
  502. {
  503. cSIFT = new LFonHSG ( conf, "HSGtrain" );
  504. }
  505. else if ( opSiftImpl == "VANDESANDE" )
  506. {
  507. // the used features
  508. cSIFT = new LFColorSande ( conf, "LFColorSandeTrain" );
  509. }
  510. else
  511. {
  512. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  513. }
  514. getFeats = cSIFT;
  515. if ( writefeat )
  516. {
  517. // write the features to a file, if there isn't any to read
  518. writeFeats = new LFWriteCache ( conf, cSIFT );
  519. getFeats = writeFeats;
  520. }
  521. if ( readfeat )
  522. {
  523. // read the features from a file
  524. if ( writefeat )
  525. {
  526. readFeats = new LFReadCache ( conf, writeFeats, -1 );
  527. }
  528. else
  529. {
  530. readFeats = new LFReadCache ( conf, cSIFT, -1 );
  531. }
  532. getFeats = readFeats;
  533. }
  534. // additional Colorfeatures
  535. LFColorWeijer lcw ( conf );
  536. int lfdimension = -1;
  537. const LabeledSet train = * ( *md ) ["train"];
  538. const LabeledSet *trainp = &train;
  539. ////////////////////////
  540. // Merkmale berechnen //
  541. ////////////////////////
  542. set<int> forbidden_classes;
  543. std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
  544. if ( forbidden_classes_s == "" )
  545. {
  546. forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  547. }
  548. cn.getSelection ( forbidden_classes_s, forbidden_classes );
  549. cerr << "forbidden: " << forbidden_classes_s << endl;
  550. ProgressBar pb ( "Local Feature Extraction" );
  551. pb.show();
  552. int imgnb = 0;
  553. LOOP_ALL_S ( *trainp )
  554. {
  555. //EACH_S(classno, currentFile);
  556. EACH_INFO ( classno, info );
  557. pb.update ( trainp->count() );
  558. NICE::ColorImage img;
  559. std::string currentFile = info.img();
  560. CachedExample *ce = new CachedExample ( currentFile );
  561. const LocalizationResult *locResult = info.localization();
  562. if ( locResult->size() <= 0 )
  563. {
  564. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  565. currentFile.c_str() );
  566. continue;
  567. }
  568. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  569. currentFile.c_str() );
  570. int xsize, ysize;
  571. ce->getImageSize ( xsize, ysize );
  572. NICE::Image pixelLabels ( xsize, ysize );
  573. pixelLabels.set ( 0 );
  574. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  575. try {
  576. img = ColorImage ( currentFile );
  577. } catch ( Exception ) {
  578. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  579. continue;
  580. }
  581. Globals::setCurrentImgFN ( currentFile );
  582. VVector features;
  583. VVector cfeatures;
  584. VVector positions;
  585. NICE::ColorImage cimg ( currentFile );
  586. getFeats->extractFeatures ( img, features, positions );
  587. #ifdef DEBUG_CSURK
  588. cout << "[log] SemSegCsruka::train -> " << currentFile << " an " << positions.size() << " Positionen wurden Features (Anz = " << features.size() << ") " << endl;
  589. cout << "mit einer Dimension von " << features[ 0].size() << " extrahiert." << endl;
  590. #endif
  591. if ( usecolorfeats )
  592. lcw.getDescriptors ( cimg, cfeatures, positions );
  593. int j = 0;
  594. for ( VVector::const_iterator i = features.begin();
  595. i != features.end();
  596. i++, j++ )
  597. {
  598. const NICE::Vector & x = *i;
  599. classno = pixelLabels.getPixel ( ( int ) positions[j][0], ( int ) positions[j][1] );
  600. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  601. continue;
  602. if ( lfdimension < 0 )
  603. lfdimension = ( int ) x.size();
  604. else
  605. assert ( lfdimension == ( int ) x.size() );
  606. NICE::Vector *v = new NICE::Vector ( x );
  607. if ( usecolorfeats && !usepca )
  608. v->append ( cfeatures[j] );
  609. Example example ( v );
  610. example.position = imgnb;
  611. examples.push_back (
  612. pair<int, Example> ( classno, example ) );
  613. }
  614. features.clear();
  615. positions.clear();
  616. delete ce;
  617. imgnb++;
  618. }
  619. pb.hide();
  620. //////////////////
  621. // PCA anwenden //
  622. //////////////////
  623. if ( usepca )
  624. {
  625. if ( !read_cache )
  626. {
  627. initializePCA ( examples );
  628. }
  629. doPCA ( examples );
  630. lfdimension = dim;
  631. }
  632. /////////////////////////////////////////////////////
  633. // Low-Level Features in High-Level transformieren //
  634. /////////////////////////////////////////////////////
  635. int hlfdimension = lfdimension;
  636. if ( norm )
  637. normalize ( examples );
  638. if ( usegmm )
  639. {
  640. if ( !usepca && !norm )
  641. normalize ( examples );
  642. g = new GMM ( conf, gaussians );
  643. if ( dogmm || !g->loadData ( cache + "/gmm" ) )
  644. {
  645. g->computeMixture ( examples );
  646. if ( save_cache )
  647. g->saveData ( cache + "/gmm" );
  648. }
  649. hlfdimension = gaussians;
  650. if ( usefisher )
  651. hlfdimension = gaussians * 2 * dim;
  652. }
  653. if ( usekmeans )
  654. {
  655. if ( !usepca || norm )
  656. normalize ( examples );
  657. k = new KMeansOnline ( gaussians );
  658. k->cluster ( examples );
  659. hlfdimension = gaussians;
  660. }
  661. if ( usekmeans || usegmm )
  662. {
  663. examples.clear();
  664. pb.reset ( "Local Feature Extraction" );
  665. lfdimension = -1;
  666. pb.update ( trainp->count() );
  667. LOOP_ALL_S ( *trainp )
  668. {
  669. EACH_INFO ( classno, info );
  670. pb.update ( trainp->count() );
  671. NICE::ColorImage img;
  672. std::string currentFile = info.img();
  673. CachedExample *ce = new CachedExample ( currentFile );
  674. const LocalizationResult *locResult = info.localization();
  675. if ( locResult->size() <= 0 )
  676. {
  677. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  678. currentFile.c_str() );
  679. continue;
  680. }
  681. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  682. currentFile.c_str() );
  683. int xsize, ysize;
  684. ce->getImageSize ( xsize, ysize );
  685. NICE::Image pixelLabels ( xsize, ysize );
  686. pixelLabels.set ( 0 );
  687. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  688. try {
  689. img = ColorImage ( currentFile );
  690. }
  691. catch ( Exception ) {
  692. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  693. continue;
  694. }
  695. Globals::setCurrentImgFN ( currentFile );
  696. VVector features;
  697. VVector cfeatures;
  698. VVector positions;
  699. NICE::ColorImage cimg ( currentFile );
  700. getFeats->extractFeatures ( img, features, positions );
  701. if ( usecolorfeats )
  702. lcw.getDescriptors ( cimg, cfeatures, positions );
  703. int j = 0;
  704. Examples tmpex;
  705. for ( VVector::const_iterator i = features.begin();
  706. i != features.end();
  707. i++, j++ )
  708. {
  709. const NICE::Vector & x = *i;
  710. classno = pixelLabels.getPixel ( ( int ) positions[j][0], ( int ) positions[j][1] );
  711. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  712. continue;
  713. if ( lfdimension < 0 )
  714. lfdimension = ( int ) x.size();
  715. else
  716. assert ( lfdimension == ( int ) x.size() );
  717. NICE::Vector *v = new NICE::Vector ( x );
  718. if ( usecolorfeats )
  719. v->append ( cfeatures[j] );
  720. Example example ( v );
  721. example.position = imgnb;
  722. example.x = ( int ) positions[j][0];
  723. example.y = ( int ) positions[j][1];
  724. example.scale = positions[j][2];
  725. tmpex.push_back ( pair<int, Example> ( classno, example ) );
  726. }
  727. tmpex.filename = currentFile;
  728. if ( usepca )
  729. {
  730. doPCA ( tmpex );
  731. }
  732. convertLowToHigh ( tmpex, anteil );
  733. smoothHL ( tmpex );
  734. for ( int i = 0; i < ( int ) tmpex.size(); i++ )
  735. {
  736. examples.push_back ( pair<int, Example> ( tmpex[i].first, tmpex[i].second ) );
  737. }
  738. tmpex.clear();
  739. features.clear();
  740. positions.clear();
  741. delete ce;
  742. imgnb++;
  743. }
  744. pb.hide();
  745. }
  746. ////////////////////////////
  747. // Klassifikator anlernen //
  748. ////////////////////////////
  749. FeaturePool fp;
  750. Feature *f;
  751. if ( usegmm || usekmeans )
  752. f = new SparseVectorFeature ( hlfdimension );
  753. else
  754. f = new VectorFeature ( hlfdimension );
  755. f->explode ( fp );
  756. delete f;
  757. if ( usecolorfeats && ! ( usekmeans || usegmm ) )
  758. {
  759. int dimension = hlfdimension + 11;
  760. for ( int i = hlfdimension ; i < dimension ; i++ )
  761. {
  762. VectorFeature *f = new VectorFeature ( dimension );
  763. f->feature_index = i;
  764. fp.addFeature ( f, 1.0 / dimension );
  765. }
  766. }
  767. /*
  768. cout << "train classifier" << endl;
  769. fp.store(cout);
  770. getchar();
  771. for(int z = 0; z < examples.size(); z++)
  772. {
  773. cout << "examples.size() " << examples.size() << endl;
  774. cout << "class: " << examples[z].first << endl;
  775. cout << *examples[z].second.vec << endl;
  776. getchar();
  777. }*/
  778. if ( classifier != NULL )
  779. classifier->train ( fp, examples );
  780. else
  781. {
  782. LabeledSetVector lvec;
  783. convertExamplesToLSet ( examples, lvec );
  784. vclassifier->teach ( lvec );
  785. if ( usegmm )
  786. convertLSetToSparseExamples ( examples, lvec );
  787. else
  788. convertLSetToExamples ( examples, lvec );
  789. vclassifier->finishTeaching();
  790. }
  791. fp.destroy();
  792. if ( save_cache )
  793. {
  794. if ( classifier != NULL )
  795. classifier->save ( cache + "/fpcrf.data" );
  796. else
  797. vclassifier->save ( cache + "/veccl.data" );
  798. }
  799. ////////////
  800. //clean up//
  801. ////////////
  802. for ( int i = 0; i < ( int ) examples.size(); i++ )
  803. {
  804. examples[i].second.clean();
  805. }
  806. examples.clear();
  807. if ( cSIFT != NULL )
  808. delete cSIFT;
  809. if ( writeFeats != NULL )
  810. delete writeFeats;
  811. if ( readFeats != NULL )
  812. delete readFeats;
  813. getFeats = NULL;
  814. trainpostprocess ( md );
  815. cerr << "SemSeg training finished" << endl;
  816. }
  817. void SemSegCsurka::trainpostprocess ( const MultiDataset *md )
  818. {
  819. cout << "start postprocess" << endl;
  820. ////////////////////////////
  821. // Postprocess trainieren //
  822. ////////////////////////////
  823. const LabeledSet train = * ( *md ) ["train"];
  824. const LabeledSet *trainp = &train;
  825. if ( userellocprior || srg != NULL || gcopt != NULL )
  826. {
  827. clog << "[log] SemSegCsurka::trainpostprocess: if ( userellocprior || srg != NULL || gcopt !=NULL )" << endl;
  828. if ( userellocprior )
  829. relloc->setClassNo ( cn.numClasses() );
  830. if ( gcopt != NULL )
  831. {
  832. gcopt->setClassNo ( cn.numClasses() );
  833. }
  834. ProgressBar pb ( "learn relative location prior maps" );
  835. pb.show();
  836. LOOP_ALL_S ( *trainp ) // für alle Bilder den ersten Klassifikationsschritt durchführen um den zweiten Klassifikator anzutrainieren
  837. {
  838. EACH_INFO ( classno, info );
  839. pb.update ( trainp->count() );
  840. NICE::ColorImage img;
  841. std::string currentFile = info.img();
  842. Globals::setCurrentImgFN ( currentFile );
  843. CachedExample *ce = new CachedExample ( currentFile );
  844. const LocalizationResult *locResult = info.localization();
  845. if ( locResult->size() <= 0 )
  846. {
  847. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  848. currentFile.c_str() );
  849. continue;
  850. }
  851. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  852. currentFile.c_str() );
  853. int xsize, ysize;
  854. ce->getImageSize ( xsize, ysize );
  855. NICE::Image pixelLabels ( xsize, ysize );
  856. pixelLabels.set ( 0 );
  857. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  858. try {
  859. img = ColorImage ( currentFile );
  860. }
  861. catch ( Exception )
  862. {
  863. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  864. continue;
  865. }
  866. //Regionen ermitteln
  867. NICE::Matrix mask;
  868. int regionsize = seg->segRegions ( img, mask );
  869. #ifdef DEBUG_CSURK
  870. Image overlay ( img.width(), img.height() );
  871. double maxval = 0.0;
  872. for ( int y = 0; y < img.height(); y++ )
  873. {
  874. for ( int x = 0; x < img.width(); x++ )
  875. {
  876. int val = ( ( int ) mask ( x, y ) + 1 ) % 256;
  877. overlay.setPixel ( x, y, val );
  878. maxval = std::max ( mask ( x, y ), maxval );
  879. }
  880. }
  881. cout << maxval << " different regions found" << endl;
  882. NICE::showImageOverlay ( img, overlay, "Segmentation Result" );
  883. #endif
  884. Examples regions;
  885. vector<vector<int> > hists;
  886. for ( int i = 0; i < regionsize; i++ )
  887. {
  888. Example tmp;
  889. regions.push_back ( pair<int, Example> ( 0, tmp ) );
  890. vector<int> hist ( cn.numClasses(), 0 );
  891. hists.push_back ( hist );
  892. }
  893. for ( int x = 0; x < xsize; x++ )
  894. {
  895. for ( int y = 0; y < ysize; y++ )
  896. {
  897. int numb = mask ( x, y );
  898. regions[numb].second.x += x;
  899. regions[numb].second.y += y;
  900. regions[numb].second.weight += 1.0;
  901. hists[numb][pixelLabels.getPixel ( x,y ) ]++;
  902. }
  903. }
  904. for ( int i = 0; i < regionsize; i++ )
  905. {
  906. regions[i].second.x /= ( int ) regions[i].second.weight;
  907. regions[i].second.y /= ( int ) regions[i].second.weight;
  908. int maxval = -numeric_limits<int>::max();
  909. int maxpos = -1;
  910. int secondpos = -1;
  911. for ( int k = 0; k < ( int ) hists[i].size(); k++ )
  912. {
  913. if ( maxval < hists[i][k] )
  914. {
  915. maxval = hists[i][k];
  916. secondpos = maxpos;
  917. maxpos = k;
  918. }
  919. }
  920. if ( cn.text ( maxpos ) == "various" )
  921. regions[i].first = secondpos;
  922. else
  923. regions[i].first = maxpos;
  924. }
  925. if ( userellocprior )
  926. relloc->trainPriorsMaps ( regions, xsize, ysize );
  927. if ( srg != NULL )
  928. srg->trainShape ( regions, mask );
  929. if ( gcopt != NULL )
  930. gcopt->trainImage ( regions, mask );
  931. delete ce;
  932. }
  933. pb.hide();
  934. if ( userellocprior )
  935. relloc->finishPriorsMaps ( cn );
  936. if ( srg != NULL )
  937. srg->finishShape ( cn );
  938. if ( gcopt != NULL )
  939. gcopt->finishPP ( cn );
  940. }
  941. if ( userellocprior )
  942. {
  943. clog << "[log] SemSegCsurka::trainpostprocess: if ( userellocprior )" << endl;
  944. ProgressBar pb ( "learn relative location classifier" );
  945. pb.show();
  946. int nummer = 0;
  947. LOOP_ALL_S ( *trainp ) // für alle Bilder den ersten Klassifikationsschritt durchführen um den zweiten Klassifikator anzutrainieren
  948. {
  949. //EACH_S(classno, currentFile);
  950. EACH_INFO ( classno, info );
  951. nummer++;
  952. pb.update ( trainp->count() );
  953. NICE::Image img;
  954. std::string currentFile = info.img();
  955. CachedExample *ce = new CachedExample ( currentFile );
  956. const LocalizationResult *locResult = info.localization();
  957. if ( locResult->size() <= 0 )
  958. {
  959. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  960. currentFile.c_str() );
  961. continue;
  962. }
  963. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  964. currentFile.c_str() );
  965. int xsize, ysize;
  966. ce->getImageSize ( xsize, ysize );
  967. NICE::Image pixelLabels ( xsize, ysize );
  968. pixelLabels.set ( 0 );
  969. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  970. try {
  971. img = Preprocess::ReadImgAdv ( currentFile.c_str() );
  972. }
  973. catch ( Exception )
  974. {
  975. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  976. continue;
  977. }
  978. Globals::setCurrentImgFN ( currentFile );
  979. NICE::Image segresult;
  980. NICE::MultiChannelImageT<double> probabilities ( xsize, ysize, classno, true );
  981. Examples regions;
  982. NICE::Matrix mask;
  983. if ( savesteps )
  984. {
  985. std::ostringstream s1;
  986. s1 << cache << "/rlpsave/" << nummer;
  987. std::string filename = s1.str();
  988. s1 << ".probs";
  989. std::string fn2 = s1.str();
  990. FILE *file;
  991. file = fopen ( filename.c_str(), "r" );
  992. if ( file == NULL )
  993. {
  994. //berechnen
  995. classifyregions ( ce, segresult, probabilities, regions, mask );
  996. //schreiben
  997. ofstream fout ( filename.c_str(), ios::app );
  998. fout << regions.size() << endl;
  999. for ( int i = 0; i < ( int ) regions.size(); i++ )
  1000. {
  1001. regions[i].second.store ( fout );
  1002. fout << regions[i].first << endl;
  1003. }
  1004. fout.close();
  1005. probabilities.store ( fn2 );
  1006. }
  1007. else
  1008. {
  1009. //lesen
  1010. ifstream fin ( filename.c_str() );
  1011. int size;
  1012. fin >> size;
  1013. for ( int i = 0; i < size; i++ )
  1014. {
  1015. Example ex;
  1016. ex.restore ( fin );
  1017. int tmp;
  1018. fin >> tmp;
  1019. regions.push_back ( pair<int, Example> ( tmp, ex ) );
  1020. }
  1021. fin.close();
  1022. probabilities.restore ( fn2 );
  1023. }
  1024. }
  1025. else
  1026. {
  1027. classifyregions ( ce, segresult, probabilities, regions, mask );
  1028. }
  1029. relloc->trainClassifier ( regions, probabilities );
  1030. delete ce;
  1031. }
  1032. relloc->finishClassifier();
  1033. pb.hide();
  1034. relloc->save ( cache + "/rlp" );
  1035. }
  1036. cout << "finished postprocess" << endl;
  1037. }
  1038. void SemSegCsurka::classifyregions ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities, Examples &Regionen, NICE::Matrix & mask )
  1039. {
  1040. /* die einzelnen Testschritte:
  1041. 1.x auf dem Testbild alle SIFT Merkmale an den Gitterpunkten bei allen Auflösungen bestimmen
  1042. 2.x für jedes SIFT-Merkmal einen Vektor erstellen, der an der Stelle i die Wahrscheinlichkeit enthällt zur Verteilung i des GMM
  1043. 3.x diese Vektoren klassifizieren, so dass für jede Klasse die Wahrscheinlichkeit gespeichert wird
  1044. 4.x für jeden Pixel die Wahrscheinlichkeiten mitteln aus allen Patches, in denen der Pixel vorkommt
  1045. 5.x das Originalbild in homogene Bereiche segmentieren
  1046. 6.x die homogenen Bereiche bekommen die gemittelten Wahrscheinlichkeiten ihrer Pixel
  1047. 7. (einzelne Klassen mit einem globalen Klassifikator ausschließen)
  1048. 8.x jeder Pixel bekommt die Klasse seiner Region zugeordnet
  1049. */
  1050. clog << "[log] SemSegCsruka::classifyregions" << endl;
  1051. int xsize, ysize;
  1052. ce->getImageSize ( xsize, ysize );
  1053. probabilities.reInit ( xsize, ysize, classNames->getMaxClassno() + 1, true/*allocMem*/ );
  1054. clog << "[log] SemSegCsruka::classifyregions: probabilities.numChannels = " << probabilities.numChannels << endl;
  1055. segresult.resize ( xsize, ysize );
  1056. Examples pce;
  1057. // Welche Opponentsift Implementierung soll genutzt werden ?
  1058. LocalFeatureRepresentation *cSIFT = NULL;
  1059. LocalFeatureRepresentation *writeFeats = NULL;
  1060. LocalFeatureRepresentation *readFeats = NULL;
  1061. LocalFeatureRepresentation *getFeats = NULL;
  1062. if ( opSiftImpl == "NICE" )
  1063. {
  1064. cSIFT = new LFonHSG ( conf, "HSGtrain" );
  1065. }
  1066. else if ( opSiftImpl == "VANDESANDE" )
  1067. {
  1068. // the used features
  1069. cSIFT = new LFColorSande ( conf, "LFColorSandeTrain" );
  1070. }
  1071. else
  1072. {
  1073. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  1074. }
  1075. getFeats = cSIFT;
  1076. if ( writefeat )
  1077. {
  1078. // write the features to a file, if there isn't any to read
  1079. writeFeats = new LFWriteCache ( conf, cSIFT );
  1080. getFeats = writeFeats;
  1081. }
  1082. if ( readfeat )
  1083. {
  1084. // read the features from a file
  1085. if ( writefeat )
  1086. {
  1087. readFeats = new LFReadCache ( conf, writeFeats, -1 );
  1088. }
  1089. else
  1090. {
  1091. readFeats = new LFReadCache ( conf, cSIFT, -1 );
  1092. }
  1093. getFeats = readFeats;
  1094. }
  1095. // additional Colorfeatures
  1096. LFColorWeijer lcw ( conf );
  1097. NICE::ColorImage img;
  1098. std::string currentFile = Globals::getCurrentImgFN();
  1099. try
  1100. {
  1101. img = ColorImage ( currentFile );
  1102. }
  1103. catch ( Exception )
  1104. {
  1105. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  1106. }
  1107. VVector features;
  1108. VVector cfeatures;
  1109. VVector positions;
  1110. getFeats->extractFeatures ( img, features, positions );
  1111. if ( usecolorfeats )
  1112. lcw.getDescriptors ( img, cfeatures, positions );
  1113. set<double> scales;
  1114. int j = 0;
  1115. int lfdimension = -1;
  1116. for ( VVector::const_iterator i = features.begin();
  1117. i != features.end();
  1118. i++, j++ )
  1119. {
  1120. const NICE::Vector & x = *i;
  1121. if ( lfdimension < 0 ) lfdimension = ( int ) x.size();
  1122. else assert ( lfdimension == ( int ) x.size() );
  1123. NICE::Vector *v = new NICE::Vector ( x );
  1124. if ( usecolorfeats )
  1125. v->append ( cfeatures[j] );
  1126. Example tmp = Example ( v );
  1127. tmp.x = ( int ) positions[j][0];
  1128. tmp.y = ( int ) positions[j][1];
  1129. tmp.width = ( int ) ( 16.0 * positions[j][2] );
  1130. tmp.height = tmp.width;
  1131. tmp.scale = positions[j][2];
  1132. scales.insert ( positions[j][2] );
  1133. pce.push_back ( pair<int, Example> ( 0, tmp ) );
  1134. }
  1135. //////////////////
  1136. // PCA anwenden //
  1137. //////////////////
  1138. pce.filename = currentFile;
  1139. if ( usepca )
  1140. {
  1141. doPCA ( pce );
  1142. lfdimension = dim;
  1143. }
  1144. //////////////////
  1145. // BoV anwenden //
  1146. //////////////////
  1147. if ( norm )
  1148. normalize ( pce );
  1149. if ( usegmm || usekmeans )
  1150. {
  1151. if ( !usepca && !norm )
  1152. normalize ( pce );
  1153. convertLowToHigh ( pce );
  1154. smoothHL ( pce );
  1155. lfdimension = gaussians;
  1156. }
  1157. /////////////////////////////////////////
  1158. // Wahrscheinlichkeitskarten erstellen //
  1159. /////////////////////////////////////////
  1160. int klassen = probabilities.numChannels;
  1161. NICE::MultiChannelImageT<double> preMap ( xsize, ysize, klassen*scales.size(), true );
  1162. long int offset = 0;
  1163. // initialisieren
  1164. for ( int y = 0 ; y < ysize ; y++ )
  1165. for ( int x = 0 ; x < xsize ; x++, offset++ )
  1166. {
  1167. // alles zum Hintergrund machen
  1168. segresult.setPixel ( x, y, 0 );
  1169. // Die Wahrscheinlichkeitsmaps auf 0 initialisieren
  1170. for ( int i = 0 ; i < ( int ) probabilities.numChannels; i++ )
  1171. {
  1172. probabilities.data[i][offset] = 0.0;
  1173. }
  1174. for ( int j = 0; j < ( int ) preMap.numChannels; j++ )
  1175. {
  1176. preMap.data[j][offset] = 0.0;
  1177. }
  1178. }
  1179. // Die Wahrscheinlichkeitsmaps mit den einzelnen Wahrscheinlichkeiten je Skalierung füllen
  1180. int scalesize = scales.size();
  1181. // Globale Häufigkeiten akkumulieren
  1182. FullVector fV ( ( int ) probabilities.numChannels );
  1183. for ( int i = 0; i < fV.size(); i++ )
  1184. fV[i] = 0.0;
  1185. // read allowed classes
  1186. string cndir = conf->gS ( "SemSegCsurka", "cndir", "" );
  1187. int classes = ( int ) probabilities.numChannels;
  1188. vector<int> useclass ( classes, 1 );
  1189. std::vector< std::string > list;
  1190. StringTools::split ( currentFile, '/', list );
  1191. string orgname = list.back();
  1192. if ( cndir != "" )
  1193. {
  1194. useclass = vector<int> ( classes, 0 );
  1195. ifstream infile ( ( cndir + "/" + orgname + ".dat" ).c_str() );
  1196. while ( !infile.eof() && infile.good() )
  1197. {
  1198. int tmp;
  1199. infile >> tmp;
  1200. if ( tmp >= 0 && tmp < classes )
  1201. {
  1202. useclass[tmp] = 1;
  1203. }
  1204. }
  1205. }
  1206. if ( classifier != NULL )
  1207. {
  1208. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen: classifier != NULL" << endl;
  1209. //#pragma omp parallel for
  1210. for ( int s = 0; s < scalesize; s++ )
  1211. {
  1212. //#pragma omp parallel for
  1213. for ( int i = s; i < ( int ) pce.size(); i += scalesize )
  1214. {
  1215. ClassificationResult r = classifier->classify ( pce[i].second );
  1216. for ( int j = 0 ; j < r.scores.size(); j++ )
  1217. {
  1218. if ( useclass[j] == 0 )
  1219. continue;
  1220. fV[j] += r.scores[j];
  1221. preMap.set ( pce[i].second.x, pce[i].second.y, r.scores[j], j + s*klassen );
  1222. }
  1223. }
  1224. }
  1225. }
  1226. else
  1227. {
  1228. ////#pragma omp parallel for
  1229. for ( int s = 0; s < scalesize; s++ )
  1230. {
  1231. ////#pragma omp parallel for
  1232. for ( int i = s; i < ( int ) pce.size(); i += scalesize )
  1233. {
  1234. ClassificationResult r = vclassifier->classify ( * ( pce[i].second.vec ) );
  1235. for ( int j = 0 ; j < ( int ) r.scores.size(); j++ )
  1236. {
  1237. if ( useclass[j] == 0 )
  1238. continue;
  1239. fV[j] += r.scores[j];
  1240. preMap.set ( pce[i].second.x, pce[i].second.y, r.scores[j], j + s*klassen );
  1241. }
  1242. }
  1243. }
  1244. }
  1245. vector<double> scalesVec;
  1246. for ( set<double>::const_iterator iter = scales.begin();
  1247. iter != scales.end();
  1248. ++iter )
  1249. {
  1250. scalesVec.push_back ( *iter );
  1251. }
  1252. // Gaußfiltern
  1253. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen -> Gaussfiltern" << endl;
  1254. for ( int s = 0; s < scalesize; s++ )
  1255. {
  1256. double sigma = sigmaweight * 16.0 * scalesVec[s];
  1257. cerr << "sigma: " << sigma << endl;
  1258. //#pragma omp parallel for
  1259. for ( int i = 0; i < klassen; i++ )
  1260. {
  1261. int pos = i + s * klassen;
  1262. double maxval = preMap.data[pos][0];
  1263. double minval = preMap.data[pos][0];
  1264. for ( int z = 1; z < xsize*ysize; z++ )
  1265. {
  1266. maxval = std::max ( maxval, preMap.data[pos][z] );
  1267. minval = std::min ( minval, preMap.data[pos][z] );
  1268. }
  1269. NICE::FloatImage dblImg ( xsize, ysize );
  1270. NICE::FloatImage gaussImg ( xsize, ysize );
  1271. long int offset2 = 0;
  1272. for ( int y = 0; y < ysize; y++ )
  1273. {
  1274. for ( int x = 0; x < xsize; x++, offset2++ )
  1275. {
  1276. dblImg.setPixel ( x, y, preMap.data[pos][offset2] );
  1277. }
  1278. }
  1279. filterGaussSigmaApproximate<float, float, float> ( dblImg, sigma, &gaussImg );
  1280. offset2 = 0;
  1281. for ( int y = 0; y < ysize; y++ )
  1282. {
  1283. for ( int x = 0; x < xsize; x++, offset2++ )
  1284. {
  1285. preMap.data[pos][offset2] = gaussImg.getPixel ( x, y );
  1286. }
  1287. }
  1288. }
  1289. }
  1290. // Zusammenfassen und auswerten
  1291. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen -> zusammenfassen und auswerten" << endl;
  1292. //#pragma omp parallel for
  1293. for ( int x = 0; x < xsize; x++ )
  1294. {
  1295. for ( int y = 0; y < ysize; y++ )
  1296. {
  1297. for ( int j = 0 ; j < ( int ) probabilities.numChannels; j++ )
  1298. {
  1299. double prob = 0.0;
  1300. for ( int s = 0; s < ( int ) scalesize; s++ )
  1301. {
  1302. prob += preMap.get ( x, y, j + s * klassen );
  1303. }
  1304. double val = prob / ( double ) ( scalesize );
  1305. probabilities.set ( x, y, val, j );
  1306. }
  1307. }
  1308. }
  1309. #undef VISSEMSEG
  1310. #ifdef VISSEMSEG
  1311. std::string s;
  1312. std::stringstream out;
  1313. std::vector< std::string > list;
  1314. StringTools::split ( Globals::getCurrentImgFN (), '/', list );
  1315. out << "probmaps/" << list.back() << ".probs";
  1316. s = out.str();
  1317. probabilities.store ( s );
  1318. s
  1319. for ( int j = 0 ; j < ( int ) probabilities.numChannels; j++ )
  1320. {
  1321. cout << "klasse: " << j << endl;//" " << cn.text ( j ) << endl;
  1322. NICE::Matrix tmp ( probabilities.ysize, probabilities.xsize );
  1323. double maxval = 0.0;
  1324. for ( int y = 0; y < probabilities.ysize; y++ )
  1325. for ( int x = 0; x < probabilities.xsize; x++ )
  1326. {
  1327. double val = probabilities.get ( x, y, j );
  1328. tmp ( y, x ) = val;
  1329. maxval = std::max ( val, maxval );
  1330. }
  1331. NICE::ColorImage imgrgb ( probabilities.xsize, probabilities.ysize );
  1332. ICETools::convertToRGB ( tmp, imgrgb );
  1333. cout << "maxval = " << maxval << " for class " << j << endl; //cn.text ( j ) << endl;
  1334. //Show ( ON, imgrgb, cn.text ( j ) );
  1335. //showImage(imgrgb, "Ergebnis");
  1336. std::string s;
  1337. std::stringstream out;
  1338. out << "tmp" << j << ".ppm";
  1339. s = out.str();
  1340. imgrgb.writePPM ( s );
  1341. //getchar();
  1342. }
  1343. #endif
  1344. if ( useregions )
  1345. {
  1346. if ( bestclasses > 0 )
  1347. {
  1348. PSSImageLevelPrior pss ( 0, bestclasses, 0.2 );
  1349. pss.setPrior ( fV );
  1350. pss.postprocess ( segresult, probabilities );
  1351. }
  1352. //Regionen ermitteln
  1353. int regionsize = seg->segRegions ( img, mask );
  1354. Regionen.clear();
  1355. vector<vector <double> > regionprob;
  1356. // Wahrscheinlichkeiten für jede Region initialisieren
  1357. for ( int i = 0; i < regionsize; i++ )
  1358. {
  1359. vector<double> tmp;
  1360. for ( int j = 0; j < ( int ) probabilities.numChannels; j++ )
  1361. {
  1362. tmp.push_back ( 0.0 );
  1363. }
  1364. regionprob.push_back ( tmp );
  1365. Regionen.push_back ( pair<int, Example> ( 0, Example() ) );
  1366. }
  1367. // Wahrscheinlichkeiten für Regionen bestimmen
  1368. for ( int x = 0; x < xsize; x++ )
  1369. {
  1370. for ( int y = 0; y < ysize; y++ )
  1371. {
  1372. for ( int j = 0 ; j < ( int ) probabilities.numChannels; j++ )
  1373. {
  1374. double val = probabilities.get ( x, y, j );
  1375. int pos = mask ( x, y );
  1376. Regionen[pos].second.weight += 1.0;
  1377. Regionen[pos].second.x += x;
  1378. Regionen[pos].second.y += y;
  1379. regionprob[pos][j] += val;
  1380. }
  1381. }
  1382. }
  1383. /*
  1384. cout << "regions: " << regionsize << endl;
  1385. cout << "outfeats: " << endl;
  1386. for(int j = 0; j < regionprob.size(); j++)
  1387. {
  1388. for(int i = 0; i < regionprob[j].size(); i++)
  1389. {
  1390. cout << regionprob[j][i] << " ";
  1391. }
  1392. cout << endl;
  1393. }
  1394. cout << endl;
  1395. getchar();*/
  1396. // beste Wahrscheinlichkeit je Region wählen
  1397. for ( int i = 0; i < regionsize; i++ )
  1398. {
  1399. if ( Regionen[i].second.weight > 0 )
  1400. {
  1401. Regionen[i].second.x /= ( int ) Regionen[i].second.weight;
  1402. Regionen[i].second.y /= ( int ) Regionen[i].second.weight;
  1403. }
  1404. double maxval = 0.0;
  1405. int maxpos = 0;
  1406. for ( int j = 0 ; j < ( int ) regionprob[i].size(); j++ )
  1407. {
  1408. regionprob[i][j] /= Regionen[i].second.weight;
  1409. if ( maxval < regionprob[i][j] )
  1410. {
  1411. maxval = regionprob[i][j];
  1412. maxpos = j;
  1413. }
  1414. probabilities.set ( Regionen[i].second.x, Regionen[i].second.y, regionprob[i][j], j );
  1415. }
  1416. Regionen[i].first = maxpos;
  1417. }
  1418. // Pixel jeder Region labeln
  1419. for ( int y = 0; y < ( int ) mask.cols(); y++ )
  1420. {
  1421. for ( int x = 0; x < ( int ) mask.rows(); x++ )
  1422. {
  1423. int pos = mask ( x, y );
  1424. segresult.setPixel ( x, y, Regionen[pos].first );
  1425. }
  1426. }
  1427. #define WRITEREGIONS
  1428. #ifdef WRITEREGIONS
  1429. RegionGraph rg;
  1430. seg->getGraphRepresentation ( img, mask, rg );
  1431. for ( uint pos = 0; pos < regionprob.size(); pos++ )
  1432. {
  1433. rg[pos]->setProbs ( regionprob[pos] );
  1434. }
  1435. std::string s;
  1436. std::stringstream out;
  1437. std::vector< std::string > list;
  1438. StringTools::split ( Globals::getCurrentImgFN (), '/', list );
  1439. out << "rgout/" << list.back() << ".graph";
  1440. string writefile = out.str();
  1441. rg.write ( writefile );
  1442. #endif
  1443. }
  1444. else
  1445. {
  1446. PSSImageLevelPrior pss ( 1, 4, 0.2 );
  1447. pss.setPrior ( fV );
  1448. pss.postprocess ( segresult, probabilities );
  1449. }
  1450. // Saubermachen:
  1451. clog << "[log] SemSegCsurka::classifyregions: sauber machen" << endl;
  1452. for ( int i = 0; i < ( int ) pce.size(); i++ )
  1453. {
  1454. pce[i].second.clean();
  1455. }
  1456. pce.clear();
  1457. if ( cSIFT != NULL )
  1458. delete cSIFT;
  1459. if ( writeFeats != NULL )
  1460. delete writeFeats;
  1461. if ( readFeats != NULL )
  1462. delete readFeats;
  1463. getFeats = NULL;
  1464. }
  1465. void SemSegCsurka::semanticseg ( CachedExample *ce,
  1466. NICE::Image & segresult,
  1467. NICE::MultiChannelImageT<double> & probabilities )
  1468. {
  1469. Examples regions;
  1470. NICE::Matrix regionmask;
  1471. classifyregions ( ce, segresult, probabilities, regions, regionmask );
  1472. if ( userellocprior || srg != NULL || gcopt != NULL )
  1473. {
  1474. if ( userellocprior )
  1475. relloc->postprocess ( regions, probabilities );
  1476. if ( srg != NULL )
  1477. srg->optimizeShape ( regions, regionmask, probabilities );
  1478. if ( gcopt != NULL )
  1479. gcopt->optimizeImage ( regions, regionmask, probabilities );
  1480. // Pixel jeder Region labeln
  1481. for ( int y = 0; y < ( int ) regionmask.cols(); y++ )
  1482. {
  1483. for ( int x = 0; x < ( int ) regionmask.rows(); x++ )
  1484. {
  1485. int pos = regionmask ( x, y );
  1486. segresult.setPixel ( x, y, regions[pos].first );
  1487. }
  1488. }
  1489. }
  1490. #ifndef NOVISUAL
  1491. #undef VISSEMSEG
  1492. #ifdef VISSEMSEG
  1493. // showImage(img);
  1494. for ( int j = 0 ; j < ( int ) probabilities.numChannels; j++ )
  1495. {
  1496. cout << "klasse: " << j << " " << cn.text ( j ) << endl;
  1497. NICE::Matrix tmp ( probabilities.ysize, probabilities.xsize );
  1498. double maxval = 0.0;
  1499. for ( int y = 0; y < probabilities.ysize; y++ )
  1500. for ( int x = 0; x < probabilities.xsize; x++ )
  1501. {
  1502. double val = probabilities.get ( x, y, j );
  1503. tmp ( y, x ) = val;
  1504. maxval = std::max ( val, maxval );
  1505. }
  1506. NICE::ColorImage imgrgb ( probabilities.xsize, probabilities.ysize );
  1507. ICETools::convertToRGB ( tmp, imgrgb );
  1508. cout << "maxval = " << maxval << " for class " << cn.text ( j ) << endl;
  1509. Show ( ON, imgrgb, cn.text ( j ) );
  1510. imgrgb.Write ( "tmp.ppm" );
  1511. getchar();
  1512. }
  1513. #endif
  1514. #endif
  1515. }