SemSegCsurka.cpp 51 KB

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