SemSegCsurka.cpp 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358
  1. #include <sstream>
  2. #include <iostream>
  3. #include "SemSegCsurka.h"
  4. #include "vislearning/baselib/ICETools.h"
  5. #include "core/image/Filter.h"
  6. #include "semseg/semseg/postsegmentation/PSSImageLevelPrior.h"
  7. using namespace std;
  8. using namespace NICE;
  9. using namespace OBJREC;
  10. #undef DEBUG_CSURK
  11. // #undef UNCERTAINTY
  12. SemSegCsurka::SemSegCsurka ( const Config *conf,
  13. const MultiDataset *md )
  14. : SemanticSegmentation ( conf, & ( md->getClassNames ( "train" ) ) )
  15. {
  16. this->conf = conf;
  17. opSiftImpl = conf->gS ( "Descriptor", "implementation", "VANDESANDE" );
  18. readfeat = conf->gB ( "Descriptor", "read", true );
  19. writefeat = conf->gB ( "Descriptor", "write", true );
  20. #ifdef DEBUG_CSURK
  21. clog << "[log] SemSegCsurka::SemSegCsurka: OppenentSift implemenation: " << opSiftImpl << endl;
  22. #endif
  23. save_cache = conf->gB ( "FPCPixel", "save_cache", true );
  24. read_cache = conf->gB ( "FPCPixel", "read_cache", false );
  25. cache = conf->gS ( "cache", "root", "" );
  26. sigmaweight = conf->gD ( "SemSegCsurka", "sigmaweight", 0.6 );
  27. dim = conf->gI ( "SemSegCsurka", "pcadim", 50 );
  28. usepca = conf->gB ( "SemSegCsurka", "usepca", true );
  29. calcpca = conf->gB ( "SemSegCsurka", "calcpca", false );
  30. usegmm = conf->gB ( "SemSegCsurka", "usegmm", false );
  31. norm = conf->gB ( "SemSegCsurka", "normalize", false );
  32. usefisher = conf->gB ( "SemSegCsurka", "usefisher", false );
  33. dogmm = conf->gB ( "SemSegCsurka", "dogmm", false );
  34. gaussians = conf->gI ( "SemSegCsurka", "gaussians", 50 );
  35. usekmeans = conf->gB ( "SemSegCsurka", "usekmeans", false );
  36. kmeansfeat = conf->gI ( "SemSegCsurka", "kmeansfeat", 50 );
  37. kmeanshard = conf->gB ( "SemSegCsurka", "kmeanshard", false );
  38. cname = conf->gS ( "SemSegCsurka", "classifier", "RandomForests" );
  39. anteil = conf->gD ( "SemSegCsurka", "anteil", 1.0 );
  40. userellocprior = conf->gB ( "SemSegCsurka", "rellocfeat", false );
  41. bool usesrg = conf->gB ( "SemSegCsurka", "usesrg", false );
  42. useregions = conf->gB ( "SemSegCsurka", "useregions", true );
  43. savesteps = conf->gB ( "SemSegCsurka", "savesteps", true );
  44. bool usegcopt = conf->gB ( "SemSegCsurka", "usegcopt", false );
  45. bestclasses = conf->gI ( "SemSegCsurka", "bestclasses", 0 );
  46. smoothhl = conf->gB ( "SemSegCsurka", "smoothhl", false );
  47. smoothfactor = conf->gD ( "SemSegCsurka", "smoothfactor", 1.0 );
  48. usecolorfeats = conf->gB ( "SemSegCsurka", "usecolorfeats", false );
  49. string rsMethod = conf->gS ( "SemSegCsurka", "segmentation", "meanshift" );
  50. g = NULL;
  51. k = NULL;
  52. relloc = NULL;
  53. srg = NULL;
  54. gcopt = NULL;
  55. if ( !useregions && ( userellocprior || usesrg ) )
  56. {
  57. cerr << "relative location priors and super region growing are just supported in combination with useregions" << endl;
  58. exit ( 1 );
  59. }
  60. if ( usepca )
  61. pca = PCA ( dim );
  62. RegionSegmentationMethod * tmpseg;
  63. if ( rsMethod == "meanshift" )
  64. tmpseg = new RSMeanShift ( conf );
  65. else
  66. tmpseg = new RSGraphBased ( conf );
  67. if ( save_cache )
  68. seg = new RSCache ( conf, tmpseg );
  69. else
  70. seg = tmpseg;
  71. if ( userellocprior )
  72. relloc = new RelativeLocationPrior ( conf );
  73. else
  74. relloc = NULL;
  75. #ifdef NICE_USELIB_ICE
  76. if ( usesrg )
  77. srg = new PPSuperregion ( conf );
  78. else
  79. srg = NULL;
  80. #else
  81. srg = NULL;
  82. #endif
  83. if ( usegcopt )
  84. gcopt = new PPGraphCut ( conf );
  85. else
  86. gcopt = NULL;
  87. classifier = NULL;
  88. vclassifier = NULL;
  89. if ( cname == "RandomForests" )
  90. classifier = new FPCRandomForests ( conf, "ClassifierForest" );
  91. else if ( cname == "SMLR" )
  92. classifier = new FPCSMLR ( conf, "ClassifierSMLR" );
  93. else if ( cname == "GPHIK" )
  94. classifier = new GPHIKClassifierNICE ( conf, "ClassiferGPHIK" );
  95. else
  96. vclassifier = GenericClassifierSelection::selectVecClassifier ( conf, "main" );
  97. //classifier = new FPCSparseMultinomialLogisticRegression(conf, "ClassifierSMLR");
  98. if ( classifier != NULL )
  99. classifier->setMaxClassNo ( classNames->getMaxClassno() );
  100. else
  101. vclassifier->setMaxClassNo ( classNames->getMaxClassno() );
  102. cn = md->getClassNames ( "train" );
  103. if ( read_cache )
  104. {
  105. fprintf ( stderr, "SemSegCsurka:: Reading classifier data from %s\n", ( cache + "/fpcrf.data" ).c_str() );
  106. if ( classifier != NULL )
  107. classifier->read ( cache + "/fpcrf.data" );
  108. else
  109. vclassifier->read ( cache + "/veccl.data" );
  110. if ( usepca )
  111. {
  112. std::string filename = cache + "/pca";
  113. pca.read ( filename );
  114. }
  115. if ( usegmm )
  116. {
  117. g = new GMM ( conf, gaussians );
  118. if ( !g->loadData ( cache + "/gmm" ) )
  119. {
  120. cerr << "SemSegCsurka:: no gmm file found" << endl;
  121. exit ( -1 );
  122. }
  123. }
  124. else {
  125. g = NULL;
  126. }
  127. if ( usekmeans )
  128. {
  129. k = new KMeansOnline ( gaussians );
  130. }
  131. fprintf ( stderr, "SemSegCsurka:: successfully read\n" );
  132. std::string filename = cache + "/rlp";
  133. FILE *value;
  134. value = fopen ( filename.c_str(), "r" );
  135. if ( value == NULL )
  136. {
  137. trainpostprocess ( md );
  138. }
  139. else
  140. {
  141. if ( userellocprior )
  142. {
  143. relloc->read ( filename );
  144. }
  145. }
  146. filename = cache + "/srg";
  147. value = fopen ( filename.c_str(), "r" );
  148. if ( value == NULL )
  149. {
  150. trainpostprocess ( md );
  151. }
  152. else
  153. {
  154. if ( srg != NULL )
  155. {
  156. srg->read ( filename );
  157. }
  158. }
  159. }
  160. else
  161. {
  162. train ( md );
  163. }
  164. }
  165. SemSegCsurka::~SemSegCsurka()
  166. {
  167. // clean-up
  168. if ( classifier != NULL )
  169. delete classifier;
  170. if ( vclassifier != NULL )
  171. delete vclassifier;
  172. if ( seg != NULL )
  173. delete seg;
  174. g = NULL;
  175. if ( g != NULL )
  176. delete g;
  177. }
  178. void SemSegCsurka::normalize ( Examples &ex )
  179. {
  180. assert ( ex.size() > 0 );
  181. if ( vecmin.size() == 0 )
  182. {
  183. for ( int j = 0; j < ( int ) ex[0].second.vec->size(); j++ )
  184. {
  185. double maxv = -numeric_limits<int>::max();
  186. double minv = numeric_limits<int>::max();
  187. for ( int i = 0; i < ( int ) ex.size(); i++ )
  188. {
  189. maxv = std::max ( maxv, ( *ex[i].second.vec ) [j] );
  190. minv = std::min ( minv, ( *ex[i].second.vec ) [j] );
  191. }
  192. vecmin.push_back ( minv );
  193. vecmax.push_back ( maxv );
  194. }
  195. }
  196. for ( int i = 0; i < ( int ) ex.size(); i++ )
  197. {
  198. for ( int j = 0; j < ( int ) ex[i].second.vec->size(); j++ )
  199. {
  200. ( *ex[i].second.vec ) [j] = ( ( *ex[i].second.vec ) [j] - vecmin[j] ) / ( vecmax[j] - vecmin[j] );
  201. }
  202. }
  203. return;
  204. }
  205. void SemSegCsurka::convertLowToHigh ( Examples &ex, double reduce )
  206. {
  207. cout << "converting low-level features to high-level features" << endl;
  208. if ( reduce >= 1.0 )
  209. {
  210. for ( int i = 0; i < ( int ) ex.size(); i++ )
  211. {
  212. SparseVector *f = new SparseVector();
  213. if ( usekmeans )
  214. {
  215. k->getDist ( *ex[i].second.vec, *f, kmeansfeat, kmeanshard );
  216. }
  217. else
  218. {
  219. if ( usefisher )
  220. g->getFisher ( *ex[i].second.vec, *f );
  221. else
  222. g->getProbs ( *ex[i].second.vec, *f );
  223. }
  224. delete ex[i].second.vec;
  225. ex[i].second.vec = NULL;
  226. ex[i].second.svec = f;
  227. }
  228. }
  229. else
  230. {
  231. srand ( time ( NULL ) );
  232. vector<bool> del ( ex.size(), false );
  233. cout << "Example size old " << ex.size() << endl;
  234. //#pragma omp parallel for
  235. for ( int i = 0; i < ( int ) ex.size(); i++ )
  236. {
  237. double rval = ( double ) rand() / ( double ) RAND_MAX;
  238. if ( rval < reduce )
  239. {
  240. SparseVector *f = new SparseVector();
  241. if ( usekmeans )
  242. k->getDist ( *ex[i].second.vec, *f, kmeansfeat, kmeanshard );
  243. else
  244. {
  245. if ( usefisher )
  246. g->getFisher ( *ex[i].second.vec, *f );
  247. else
  248. g->getProbs ( *ex[i].second.vec, *f );
  249. }
  250. delete ex[i].second.vec;
  251. ex[i].second.vec = NULL;
  252. ex[i].second.svec = f;
  253. }
  254. else
  255. {
  256. del[i] = true;
  257. }
  258. }
  259. for ( int i = ( int ) del.size() - 1; i >= 0; i-- )
  260. {
  261. if ( del[i] )
  262. {
  263. ex.erase ( ex.begin() + i );
  264. }
  265. }
  266. cerr << "Example size new " << ex.size() << endl;
  267. }
  268. cerr << "converting low-level features to high-level features finished" << endl;
  269. }
  270. void SemSegCsurka::smoothHL ( Examples ex )
  271. {
  272. if ( !smoothhl )
  273. return;
  274. assert ( ex.size() > 1 );
  275. long long int minx = numeric_limits<long long int>::max();
  276. long long int miny = numeric_limits<long long int>::max();
  277. long long int maxx = -numeric_limits<long long int>::max();
  278. long long int maxy = -numeric_limits<long long int>::max();
  279. long long int distx = numeric_limits<long long int>::max();
  280. long long int disty = numeric_limits<long long int>::max();
  281. set<double> scales;
  282. for ( int i = 0; i < ( int ) ex.size(); i++ )
  283. {
  284. scales.insert ( ex[i].second.scale );
  285. }
  286. map<double, int> scalepos;
  287. int it = 0;
  288. for ( set<double>::const_iterator iter = scales.begin(); iter != scales.end(); ++iter, ++it )
  289. {
  290. scalepos.insert ( make_pair ( *iter, it ) );
  291. }
  292. for ( int i = 0; i < ( int ) ex.size(); i++ )
  293. {
  294. if ( minx < numeric_limits<int>::max() && ex[i].second.x - minx > 0 )
  295. distx = std::min ( distx, ex[i].second.x - minx );
  296. if ( miny < numeric_limits<int>::max() && ex[i].second.y - miny > 0 )
  297. disty = std::min ( disty, ex[i].second.y - miny );
  298. minx = std::min ( ( long long int ) ex[i].second.x, minx );
  299. maxx = std::max ( ( long long int ) ex[i].second.x, maxx );
  300. miny = std::min ( ( long long int ) ex[i].second.y, miny );
  301. maxy = std::max ( ( long long int ) ex[i].second.y, maxy );
  302. }
  303. distx = abs ( distx );
  304. int xsize = ( maxx - minx ) / distx + 1;
  305. int ysize = ( maxy - miny ) / disty + 1;
  306. double valx = ( ( double ) xsize - 1 ) / ( double ) ( maxx - minx );
  307. double valy = ( ( double ) ysize - 1 ) / ( double ) ( maxy - miny );
  308. //double sigma = smoothfactor;
  309. double sigma = std::max ( xsize, ysize ) * smoothfactor;
  310. //double sigma = 0.2;
  311. cout << "sigma1: " << sigma << endl;
  312. vector<NICE::FloatImage> imgv;
  313. vector<NICE::FloatImage> gaussImgv;
  314. for ( int i = 0; i < ( int ) scalepos.size(); i++ )
  315. {
  316. NICE::FloatImage img ( xsize, ysize );
  317. NICE::FloatImage gaussImg ( xsize, ysize );
  318. imgv.push_back ( img );
  319. gaussImgv.push_back ( gaussImg );
  320. }
  321. for ( int d = 0; d < ex[0].second.svec->getDim(); d++ )
  322. {
  323. //TODO: max und min dynamisches bestimmen
  324. for ( int i = 0; i < ( int ) scalepos.size(); i++ )
  325. {
  326. imgv[i].set ( 0.0 );
  327. gaussImgv[i].set ( 0.0 );
  328. }
  329. for ( int i = 0; i < ( int ) ex.size(); i++ )
  330. {
  331. int xpos = ( ex[i].second.x - minx ) * valx;
  332. int ypos = ( ex[i].second.y - miny ) * valy;
  333. double val = ex[i].second.svec->get ( d );
  334. imgv[scalepos[ex[i].second.scale]].setPixel ( xpos, ypos, val );
  335. }
  336. /*
  337. for(int y = 0; y < ysize; y++)
  338. {
  339. for(int x = 0; x < xsize; x++)
  340. {
  341. // refactor-nice.pl: check this substitution
  342. // old: double val = GetValD(img,x,y);
  343. double val = img.getPixel(x,y);
  344. double c = 0.0;
  345. if(val == 0.0)
  346. {
  347. if(x > 0)
  348. {
  349. // refactor-nice.pl: check this substitution
  350. // old: val+=GetValD(img,x-1,y);
  351. val+=img.getPixel(x-1,y);
  352. c+=1.0;
  353. }
  354. if(y > 0)
  355. {
  356. // refactor-nice.pl: check this substitution
  357. // old: val+=GetValD(img,x,y-1);
  358. val+=img.getPixel(x,y-1);
  359. c+=1.0;
  360. }
  361. if(x < xsize-1)
  362. {
  363. // refactor-nice.pl: check this substitution
  364. // old: val+=GetValD(img,x+1,y);
  365. val+=img.getPixel(x+1,y);
  366. c+=1.0;
  367. }
  368. if(y < ysize-1)
  369. {
  370. // refactor-nice.pl: check this substitution
  371. // old: val+=GetValD(img,x,y+1);
  372. val+=img.getPixel(x,y+1);
  373. c+=1.0;
  374. }
  375. // refactor-nice.pl: check this substitution
  376. // old: PutValD(img,x,y,val/c);
  377. img.setPixel(x,y,val/c);
  378. }
  379. }
  380. }*/
  381. for ( int i = 0; i < ( int ) imgv.size(); i++ )
  382. filterGaussSigmaApproximate<float, float, float> ( imgv[i], sigma, &gaussImgv[i] );
  383. for ( int i = 0; i < ( int ) ex.size(); i++ )
  384. {
  385. int xpos = ( ex[i].second.x - minx ) * valx;
  386. int ypos = ( ex[i].second.y - miny ) * valy;
  387. // refactor-nice.pl: check this substitution
  388. // old: double val = GetValD ( gaussImgv[scalepos[ex[i].second.scale]], xpos, ypos );
  389. double val = gaussImgv[scalepos[ex[i].second.scale]].getPixel ( xpos, ypos );
  390. if ( fabs ( val ) < 1e-7 )
  391. {
  392. if ( ex[i].second.svec->get ( d ) != 0.0 )
  393. {
  394. ex[i].second.svec->erase ( d );
  395. }
  396. }
  397. else
  398. {
  399. ( *ex[i].second.svec ) [d] = val;
  400. }
  401. }
  402. }
  403. }
  404. void SemSegCsurka::initializePCA ( Examples &ex )
  405. {
  406. #ifdef DEBUG
  407. cerr << "start computing pca" << endl;
  408. #endif
  409. std::string filename = cache + "/pca";
  410. FILE *value;
  411. value = fopen ( filename.c_str(), "r" );
  412. if ( value == NULL || calcpca )
  413. {
  414. srand ( time ( NULL ) );
  415. int featsize = ( int ) ex.size();
  416. int maxfeatures = dim * 10;
  417. int olddim = ex[0].second.vec->size();
  418. maxfeatures = std::min ( maxfeatures, featsize );
  419. NICE::Matrix features ( maxfeatures, olddim );
  420. for ( int i = 0; i < maxfeatures; i++ )
  421. {
  422. int k = rand() % featsize;
  423. int vsize = ( int ) ex[k].second.vec->size();
  424. for ( int j = 0; j < vsize; j++ )
  425. {
  426. features ( i, j ) = ( * ( ex[k].second.vec ) ) [j];
  427. }
  428. }
  429. pca.calculateBasis ( features, dim );
  430. if ( save_cache )
  431. pca.save ( filename );
  432. }
  433. else
  434. {
  435. cout << "readpca: " << filename << endl;
  436. pca.read ( filename );
  437. cout << "end" << endl;
  438. }
  439. #ifdef DEBUG
  440. cerr << "finished computing pca" << endl;
  441. #endif
  442. }
  443. void SemSegCsurka::doPCA ( Examples &ex )
  444. {
  445. cout << "converting features using pca starts" << endl;
  446. std::string savedir = cname = conf->gS ( "cache", "root", "/dev/null/" );
  447. std::string shortf = ex.filename;
  448. if ( string::npos != ex.filename.rfind ( "/" ) )
  449. shortf = ex.filename.substr ( ex.filename.rfind ( "/" ) );
  450. std::string filename = savedir + "/pcasave/" + shortf;
  451. std::string syscall = "mkdir " + savedir + "/pcasave";
  452. system ( syscall.c_str() );
  453. cout << "filename: " << filename << endl;
  454. if ( !FileMgt::fileExists ( filename ) || calcpca )
  455. {
  456. ofstream ofStream;
  457. //Opens the file binary
  458. ofStream.open ( filename.c_str(), fstream::out | fstream::binary );
  459. for ( int k = 0; k < ( int ) ex.size(); k++ )
  460. {
  461. NICE::Vector tmp = pca.getFeatureVector ( * ( ex[k].second.vec ), true );
  462. delete ex[k].second.vec;
  463. for ( int d = 0; d < ( int ) tmp.size(); d++ )
  464. ofStream.write ( ( char* ) &tmp[d], sizeof ( double ) );
  465. ex[k].second.vec = new NICE::Vector ( tmp );
  466. }
  467. ofStream.close();
  468. cout << endl;
  469. }
  470. else
  471. {
  472. ifstream ifStream;
  473. ifStream.open ( filename.c_str(), std::fstream::in | std::fstream::binary );
  474. for ( int k = 0; k < ( int ) ex.size(); k++ )
  475. {
  476. NICE::Vector tmp = NICE::Vector ( dim );
  477. delete ex[k].second.vec;
  478. for ( int d = 0; d < dim; d++ )
  479. ifStream.read ( ( char* ) &tmp[d], sizeof ( double ) );
  480. ex[k].second.vec = new NICE::Vector ( tmp );
  481. }
  482. ifStream.close();
  483. }
  484. cout << "converting features using pca finished" << endl;
  485. }
  486. void SemSegCsurka::train ( const MultiDataset *md )
  487. {
  488. /*die einzelnen Trainingsschritte
  489. 1. auf allen Trainingsbilder SIFT Merkmale an den Gitterpunkten bei allen Auflösungen bestimmen
  490. 2. PCA anwenden
  491. 3. aus diesen ein GMM erstellen
  492. 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
  493. 5. diese Vektoren in einem diskriminitativen Klassifikator ( z.B. SLR oder Randomized Forests) zusammen mit ihrer Klassenzugehörigkeit anlernen
  494. */
  495. #ifdef DEBUG
  496. cerr << "SemSegCsurka:: training starts" << endl;
  497. #endif
  498. Examples examples;
  499. examples.filename = "training";
  500. // Welche Opponentsift Implementierung soll genutzt werden ?
  501. LocalFeatureRepresentation *cSIFT = NULL;
  502. LocalFeatureRepresentation *writeFeats = NULL;
  503. LocalFeatureRepresentation *readFeats = NULL;
  504. LocalFeatureRepresentation *getFeats = NULL;
  505. if ( opSiftImpl == "NICE" )
  506. {
  507. cSIFT = new LFonHSG ( conf, "HSGtrain" );
  508. }
  509. else if ( opSiftImpl == "VANDESANDE" )
  510. {
  511. // the used features
  512. cSIFT = new LFColorSande ( conf, "LFColorSandeTrain" );
  513. }
  514. else
  515. {
  516. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  517. }
  518. getFeats = cSIFT;
  519. if ( writefeat )
  520. {
  521. // write the features to a file, if there isn't any to read
  522. writeFeats = new LFWriteCache ( conf, cSIFT );
  523. getFeats = writeFeats;
  524. }
  525. if ( readfeat )
  526. {
  527. // read the features from a file
  528. if ( writefeat )
  529. {
  530. readFeats = new LFReadCache ( conf, writeFeats, -1 );
  531. }
  532. else
  533. {
  534. readFeats = new LFReadCache ( conf, cSIFT, -1 );
  535. }
  536. getFeats = readFeats;
  537. }
  538. // additional Colorfeatures
  539. LFColorWeijer lcw ( conf );
  540. int lfdimension = -1;
  541. const LabeledSet train = * ( *md ) ["train"];
  542. const LabeledSet *trainp = &train;
  543. ////////////////////////
  544. // Merkmale berechnen //
  545. ////////////////////////
  546. std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
  547. if ( forbidden_classes_s == "" )
  548. {
  549. forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  550. }
  551. cn.getSelection ( forbidden_classes_s, forbidden_classes );
  552. cerr << "forbidden: " << forbidden_classes_s << endl;
  553. ProgressBar pb ( "Local Feature Extraction" );
  554. pb.show();
  555. int imgnb = 0;
  556. LOOP_ALL_S ( *trainp )
  557. {
  558. //EACH_S(classno, currentFile);
  559. EACH_INFO ( classno, info );
  560. pb.update ( trainp->count() );
  561. NICE::ColorImage img;
  562. std::string currentFile = info.img();
  563. CachedExample *ce = new CachedExample ( currentFile );
  564. const LocalizationResult *locResult = info.localization();
  565. if ( locResult->size() <= 0 )
  566. {
  567. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  568. currentFile.c_str() );
  569. continue;
  570. }
  571. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  572. currentFile.c_str() );
  573. int xsize, ysize;
  574. ce->getImageSize ( xsize, ysize );
  575. NICE::Image pixelLabels ( xsize, ysize );
  576. pixelLabels.set ( 0 );
  577. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  578. try {
  579. img = ColorImage ( currentFile );
  580. } catch ( Exception ) {
  581. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  582. continue;
  583. }
  584. Globals::setCurrentImgFN ( currentFile );
  585. VVector features;
  586. VVector cfeatures;
  587. VVector positions;
  588. NICE::ColorImage cimg ( currentFile );
  589. getFeats->extractFeatures ( img, features, positions );
  590. #ifdef DEBUG_CSURK
  591. cout << "[log] SemSegCsruka::train -> " << currentFile << " an " << positions.size() << " Positionen wurden Features (Anz = " << features.size() << ") " << endl;
  592. cout << "mit einer Dimension von " << features[ 0].size() << " extrahiert." << endl;
  593. #endif
  594. if ( usecolorfeats )
  595. lcw.getDescriptors ( cimg, cfeatures, positions );
  596. int j = 0;
  597. for ( VVector::const_iterator i = features.begin();
  598. i != features.end();
  599. i++, j++ )
  600. {
  601. const NICE::Vector & x = *i;
  602. classno = pixelLabels.getPixel ( ( int ) positions[j][0], ( int ) positions[j][1] );
  603. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  604. continue;
  605. if ( lfdimension < 0 )
  606. lfdimension = ( int ) x.size();
  607. else
  608. assert ( lfdimension == ( int ) x.size() );
  609. NICE::Vector *v = new NICE::Vector ( x );
  610. if ( usecolorfeats && !usepca )
  611. v->append ( cfeatures[j] );
  612. Example example ( v );
  613. example.position = imgnb;
  614. examples.push_back (
  615. pair<int, Example> ( classno, example ) );
  616. }
  617. features.clear();
  618. positions.clear();
  619. delete ce;
  620. imgnb++;
  621. }
  622. pb.hide();
  623. //////////////////
  624. // PCA anwenden //
  625. //////////////////
  626. if ( usepca )
  627. {
  628. if ( !read_cache )
  629. {
  630. initializePCA ( examples );
  631. }
  632. doPCA ( examples );
  633. lfdimension = dim;
  634. }
  635. /////////////////////////////////////////////////////
  636. // Low-Level Features in High-Level transformieren //
  637. /////////////////////////////////////////////////////
  638. int hlfdimension = lfdimension;
  639. if ( norm )
  640. normalize ( examples );
  641. if ( usegmm )
  642. {
  643. if ( !usepca && !norm )
  644. normalize ( examples );
  645. g = new GMM ( conf, gaussians );
  646. if ( dogmm || !g->loadData ( cache + "/gmm" ) )
  647. {
  648. g->computeMixture ( examples );
  649. if ( save_cache )
  650. g->saveData ( cache + "/gmm" );
  651. }
  652. hlfdimension = gaussians;
  653. if ( usefisher )
  654. hlfdimension = gaussians * 2 * dim;
  655. }
  656. if ( usekmeans )
  657. {
  658. if ( !usepca || norm )
  659. normalize ( examples );
  660. k = new KMeansOnline ( gaussians );
  661. k->cluster ( examples );
  662. hlfdimension = gaussians;
  663. }
  664. if ( usekmeans || usegmm )
  665. {
  666. examples.clear();
  667. pb.reset ( "Local Feature Extraction" );
  668. lfdimension = -1;
  669. pb.update ( trainp->count() );
  670. LOOP_ALL_S ( *trainp )
  671. {
  672. EACH_INFO ( classno, info );
  673. pb.update ( trainp->count() );
  674. NICE::ColorImage img;
  675. std::string currentFile = info.img();
  676. CachedExample *ce = new CachedExample ( currentFile );
  677. const LocalizationResult *locResult = info.localization();
  678. if ( locResult->size() <= 0 )
  679. {
  680. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  681. currentFile.c_str() );
  682. continue;
  683. }
  684. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  685. currentFile.c_str() );
  686. int xsize, ysize;
  687. ce->getImageSize ( xsize, ysize );
  688. NICE::Image pixelLabels ( xsize, ysize );
  689. pixelLabels.set ( 0 );
  690. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  691. try {
  692. img = ColorImage ( currentFile );
  693. }
  694. catch ( Exception ) {
  695. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  696. continue;
  697. }
  698. Globals::setCurrentImgFN ( currentFile );
  699. VVector features;
  700. VVector cfeatures;
  701. VVector positions;
  702. NICE::ColorImage cimg ( currentFile );
  703. getFeats->extractFeatures ( img, features, positions );
  704. if ( usecolorfeats )
  705. lcw.getDescriptors ( cimg, cfeatures, positions );
  706. int j = 0;
  707. Examples tmpex;
  708. for ( VVector::const_iterator i = features.begin();
  709. i != features.end();
  710. i++, j++ )
  711. {
  712. const NICE::Vector & x = *i;
  713. classno = pixelLabels.getPixel ( ( int ) positions[j][0], ( int ) positions[j][1] );
  714. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  715. continue;
  716. if ( lfdimension < 0 )
  717. lfdimension = ( int ) x.size();
  718. else
  719. assert ( lfdimension == ( int ) x.size() );
  720. NICE::Vector *v = new NICE::Vector ( x );
  721. if ( usecolorfeats )
  722. v->append ( cfeatures[j] );
  723. Example example ( v );
  724. example.position = imgnb;
  725. example.x = ( int ) positions[j][0];
  726. example.y = ( int ) positions[j][1];
  727. example.scale = positions[j][2];
  728. tmpex.push_back ( pair<int, Example> ( classno, example ) );
  729. }
  730. tmpex.filename = currentFile;
  731. if ( usepca )
  732. {
  733. doPCA ( tmpex );
  734. }
  735. convertLowToHigh ( tmpex, anteil );
  736. smoothHL ( tmpex );
  737. for ( int i = 0; i < ( int ) tmpex.size(); i++ )
  738. {
  739. examples.push_back ( pair<int, Example> ( tmpex[i].first, tmpex[i].second ) );
  740. }
  741. tmpex.clear();
  742. features.clear();
  743. positions.clear();
  744. delete ce;
  745. imgnb++;
  746. }
  747. pb.hide();
  748. }
  749. ////////////////////////////
  750. // Klassifikator anlernen //
  751. ////////////////////////////
  752. FeaturePool fp;
  753. Feature *f;
  754. if ( usegmm || usekmeans )
  755. f = new SparseVectorFeature ( hlfdimension );
  756. else
  757. f = new VectorFeature ( hlfdimension );
  758. f->explode ( fp );
  759. delete f;
  760. if ( usecolorfeats && ! ( usekmeans || usegmm ) )
  761. {
  762. int dimension = hlfdimension + 11;
  763. for ( int i = hlfdimension ; i < dimension ; i++ )
  764. {
  765. VectorFeature *f = new VectorFeature ( dimension );
  766. f->feature_index = i;
  767. fp.addFeature ( f, 1.0 / dimension );
  768. }
  769. }
  770. /*
  771. cout << "train classifier" << endl;
  772. fp.store(cout);
  773. getchar();
  774. for(int z = 0; z < examples.size(); z++)
  775. {
  776. cout << "examples.size() " << examples.size() << endl;
  777. cout << "class: " << examples[z].first << endl;
  778. cout << *examples[z].second.vec << endl;
  779. getchar();
  780. }*/
  781. if ( classifier != NULL )
  782. classifier->train ( fp, examples );
  783. else
  784. {
  785. LabeledSetVector lvec;
  786. convertExamplesToLSet ( examples, lvec );
  787. vclassifier->teach ( lvec );
  788. if ( usegmm )
  789. convertLSetToSparseExamples ( examples, lvec );
  790. else
  791. convertLSetToExamples ( examples, lvec );
  792. vclassifier->finishTeaching();
  793. }
  794. fp.destroy();
  795. if ( save_cache )
  796. {
  797. if ( classifier != NULL )
  798. classifier->save ( cache + "/fpcrf.data" );
  799. else
  800. vclassifier->save ( cache + "/veccl.data" );
  801. }
  802. ////////////
  803. //clean up//
  804. ////////////
  805. for ( int i = 0; i < ( int ) examples.size(); i++ )
  806. {
  807. examples[i].second.clean();
  808. }
  809. examples.clear();
  810. if ( cSIFT != NULL )
  811. delete cSIFT;
  812. if ( writeFeats != NULL )
  813. delete writeFeats;
  814. if ( readFeats != NULL )
  815. delete readFeats;
  816. getFeats = NULL;
  817. trainpostprocess ( md );
  818. cerr << "SemSeg training finished" << endl;
  819. }
  820. void SemSegCsurka::trainpostprocess ( const MultiDataset *md )
  821. {
  822. cout << "start postprocess" << endl;
  823. ////////////////////////////
  824. // Postprocess trainieren //
  825. ////////////////////////////
  826. const LabeledSet train = * ( *md ) ["train"];
  827. const LabeledSet *trainp = &train;
  828. if ( userellocprior || srg != NULL || gcopt != NULL )
  829. {
  830. clog << "[log] SemSegCsurka::trainpostprocess: if ( userellocprior || srg != NULL || gcopt !=NULL )" << endl;
  831. if ( userellocprior )
  832. relloc->setClassNo ( cn.numClasses() );
  833. if ( gcopt != NULL )
  834. {
  835. gcopt->setClassNo ( cn.numClasses() );
  836. }
  837. ProgressBar pb ( "learn relative location prior maps" );
  838. pb.show();
  839. LOOP_ALL_S ( *trainp ) // für alle Bilder den ersten Klassifikationsschritt durchführen um den zweiten Klassifikator anzutrainieren
  840. {
  841. EACH_INFO ( classno, info );
  842. pb.update ( trainp->count() );
  843. NICE::ColorImage img;
  844. std::string currentFile = info.img();
  845. Globals::setCurrentImgFN ( currentFile );
  846. CachedExample *ce = new CachedExample ( currentFile );
  847. const LocalizationResult *locResult = info.localization();
  848. if ( locResult->size() <= 0 )
  849. {
  850. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  851. currentFile.c_str() );
  852. continue;
  853. }
  854. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  855. currentFile.c_str() );
  856. int xsize, ysize;
  857. ce->getImageSize ( xsize, ysize );
  858. NICE::Image pixelLabels ( xsize, ysize );
  859. pixelLabels.set ( 0 );
  860. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  861. try {
  862. img = ColorImage ( currentFile );
  863. }
  864. catch ( Exception )
  865. {
  866. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  867. continue;
  868. }
  869. //Regionen ermitteln
  870. NICE::Matrix mask;
  871. int regionsize = seg->segRegions ( img, mask );
  872. #ifdef DEBUG_CSURK
  873. Image overlay ( img.width(), img.height() );
  874. double maxval = -numeric_limits<double>::max();
  875. for ( int y = 0; y < img.height(); y++ )
  876. {
  877. for ( int x = 0; x < img.width(); x++ )
  878. {
  879. int val = ( ( int ) mask ( x, y ) + 1 ) % 256;
  880. overlay.setPixel ( x, y, val );
  881. maxval = std::max ( mask ( x, y ), maxval );
  882. }
  883. }
  884. cout << maxval << " different regions found" << endl;
  885. NICE::showImageOverlay ( img, overlay, "Segmentation Result" );
  886. #endif
  887. Examples regions;
  888. vector<vector<int> > hists;
  889. for ( int i = 0; i < regionsize; i++ )
  890. {
  891. Example tmp;
  892. regions.push_back ( pair<int, Example> ( 0, tmp ) );
  893. vector<int> hist ( cn.numClasses(), 0 );
  894. hists.push_back ( hist );
  895. }
  896. for ( int x = 0; x < xsize; x++ )
  897. {
  898. for ( int y = 0; y < ysize; y++ )
  899. {
  900. int numb = mask ( x, y );
  901. regions[numb].second.x += x;
  902. regions[numb].second.y += y;
  903. regions[numb].second.weight += 1.0;
  904. hists[numb][pixelLabels.getPixel ( x,y ) ]++;
  905. }
  906. }
  907. for ( int i = 0; i < regionsize; i++ )
  908. {
  909. regions[i].second.x /= ( int ) regions[i].second.weight;
  910. regions[i].second.y /= ( int ) regions[i].second.weight;
  911. int maxval = -numeric_limits<int>::max();
  912. int maxpos = -1;
  913. int secondpos = -1;
  914. for ( int k = 0; k < ( int ) hists[i].size(); k++ )
  915. {
  916. if ( maxval < hists[i][k] )
  917. {
  918. maxval = hists[i][k];
  919. secondpos = maxpos;
  920. maxpos = k;
  921. }
  922. }
  923. if ( cn.text ( maxpos ) == "various" )
  924. regions[i].first = secondpos;
  925. else
  926. regions[i].first = maxpos;
  927. }
  928. if ( userellocprior )
  929. relloc->trainPriorsMaps ( regions, xsize, ysize );
  930. if ( srg != NULL )
  931. srg->trainShape ( regions, mask );
  932. if ( gcopt != NULL )
  933. gcopt->trainImage ( regions, mask );
  934. delete ce;
  935. }
  936. pb.hide();
  937. if ( userellocprior )
  938. relloc->finishPriorsMaps ( cn );
  939. if ( srg != NULL )
  940. srg->finishShape ( cn );
  941. if ( gcopt != NULL )
  942. gcopt->finishPP ( cn );
  943. }
  944. if ( userellocprior )
  945. {
  946. clog << "[log] SemSegCsurka::trainpostprocess: if ( userellocprior )" << endl;
  947. ProgressBar pb ( "learn relative location classifier" );
  948. pb.show();
  949. int nummer = 0;
  950. LOOP_ALL_S ( *trainp ) // für alle Bilder den ersten Klassifikationsschritt durchführen um den zweiten Klassifikator anzutrainieren
  951. {
  952. //EACH_S(classno, currentFile);
  953. EACH_INFO ( classno, info );
  954. nummer++;
  955. pb.update ( trainp->count() );
  956. NICE::Image img;
  957. std::string currentFile = info.img();
  958. CachedExample *ce = new CachedExample ( currentFile );
  959. const LocalizationResult *locResult = info.localization();
  960. if ( locResult->size() <= 0 )
  961. {
  962. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  963. currentFile.c_str() );
  964. continue;
  965. }
  966. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  967. currentFile.c_str() );
  968. int xsize, ysize;
  969. ce->getImageSize ( xsize, ysize );
  970. NICE::Image pixelLabels ( xsize, ysize );
  971. pixelLabels.set ( 0 );
  972. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  973. try {
  974. img = Preprocess::ReadImgAdv ( currentFile.c_str() );
  975. }
  976. catch ( Exception )
  977. {
  978. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  979. continue;
  980. }
  981. Globals::setCurrentImgFN ( currentFile );
  982. NICE::Image segresult;
  983. NICE::MultiChannelImageT<double> probabilities ( xsize, ysize, classno );
  984. Examples regions;
  985. NICE::Matrix mask;
  986. if ( savesteps )
  987. {
  988. std::ostringstream s1;
  989. s1 << cache << "/rlpsave/" << nummer;
  990. std::string filename = s1.str();
  991. s1 << ".probs";
  992. std::string fn2 = s1.str();
  993. FILE *file;
  994. file = fopen ( filename.c_str(), "r" );
  995. if ( file == NULL )
  996. {
  997. //berechnen
  998. classifyregions ( ce, segresult, probabilities, regions, mask );
  999. //schreiben
  1000. ofstream fout ( filename.c_str(), ios::app );
  1001. fout << regions.size() << endl;
  1002. for ( int i = 0; i < ( int ) regions.size(); i++ )
  1003. {
  1004. regions[i].second.store ( fout );
  1005. fout << regions[i].first << endl;
  1006. }
  1007. fout.close();
  1008. probabilities.store ( fn2 );
  1009. }
  1010. else
  1011. {
  1012. //lesen
  1013. ifstream fin ( filename.c_str() );
  1014. int size;
  1015. fin >> size;
  1016. for ( int i = 0; i < size; i++ )
  1017. {
  1018. Example ex;
  1019. ex.restore ( fin );
  1020. int tmp;
  1021. fin >> tmp;
  1022. regions.push_back ( pair<int, Example> ( tmp, ex ) );
  1023. }
  1024. fin.close();
  1025. probabilities.restore ( fn2 );
  1026. }
  1027. }
  1028. else
  1029. {
  1030. classifyregions ( ce, segresult, probabilities, regions, mask );
  1031. }
  1032. relloc->trainClassifier ( regions, probabilities );
  1033. delete ce;
  1034. }
  1035. relloc->finishClassifier();
  1036. pb.hide();
  1037. relloc->save ( cache + "/rlp" );
  1038. }
  1039. cout << "finished postprocess" << endl;
  1040. }
  1041. void SemSegCsurka::classifyregions ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities, Examples &Regionen, NICE::Matrix & mask )
  1042. {
  1043. /* die einzelnen Testschritte:
  1044. 1.x auf dem Testbild alle SIFT Merkmale an den Gitterpunkten bei allen Auflösungen bestimmen
  1045. 2.x für jedes SIFT-Merkmal einen Vektor erstellen, der an der Stelle i die Wahrscheinlichkeit enthällt zur Verteilung i des GMM
  1046. 3.x diese Vektoren klassifizieren, so dass für jede Klasse die Wahrscheinlichkeit gespeichert wird
  1047. 4.x für jeden Pixel die Wahrscheinlichkeiten mitteln aus allen Patches, in denen der Pixel vorkommt
  1048. 5.x das Originalbild in homogene Bereiche segmentieren
  1049. 6.x die homogenen Bereiche bekommen die gemittelten Wahrscheinlichkeiten ihrer Pixel
  1050. 7. (einzelne Klassen mit einem globalen Klassifikator ausschließen)
  1051. 8.x jeder Pixel bekommt die Klasse seiner Region zugeordnet
  1052. */
  1053. clog << "[log] SemSegCsruka::classifyregions" << endl;
  1054. int xsize, ysize;
  1055. ce->getImageSize ( xsize, ysize );
  1056. probabilities.reInit ( xsize, ysize, classNames->getMaxClassno() + 1 );
  1057. clog << "[log] SemSegCsruka::classifyregions: probabilities.channels() = " << probabilities.channels() << endl;
  1058. segresult.resize ( xsize, ysize );
  1059. Examples pce;
  1060. // Welche Opponentsift Implementierung soll genutzt werden ?
  1061. LocalFeatureRepresentation *cSIFT = NULL;
  1062. LocalFeatureRepresentation *writeFeats = NULL;
  1063. LocalFeatureRepresentation *readFeats = NULL;
  1064. LocalFeatureRepresentation *getFeats = NULL;
  1065. if ( opSiftImpl == "NICE" )
  1066. {
  1067. cSIFT = new LFonHSG ( conf, "HSGtest" );
  1068. }
  1069. else if ( opSiftImpl == "VANDESANDE" )
  1070. {
  1071. // the used features
  1072. cSIFT = new LFColorSande ( conf, "LFColorSandeTrain" );
  1073. }
  1074. else
  1075. {
  1076. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  1077. }
  1078. getFeats = cSIFT;
  1079. if ( writefeat )
  1080. {
  1081. // write the features to a file, if there isn't any to read
  1082. writeFeats = new LFWriteCache ( conf, cSIFT );
  1083. getFeats = writeFeats;
  1084. }
  1085. if ( readfeat )
  1086. {
  1087. // read the features from a file
  1088. if ( writefeat )
  1089. {
  1090. readFeats = new LFReadCache ( conf, writeFeats, -1 );
  1091. }
  1092. else
  1093. {
  1094. readFeats = new LFReadCache ( conf, cSIFT, -1 );
  1095. }
  1096. getFeats = readFeats;
  1097. }
  1098. // additional Colorfeatures
  1099. LFColorWeijer lcw ( conf );
  1100. NICE::ColorImage img;
  1101. std::string currentFile = Globals::getCurrentImgFN();
  1102. try
  1103. {
  1104. img = ColorImage ( currentFile );
  1105. }
  1106. catch ( Exception )
  1107. {
  1108. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  1109. }
  1110. VVector features;
  1111. VVector cfeatures;
  1112. VVector positions;
  1113. getFeats->extractFeatures ( img, features, positions );
  1114. if ( usecolorfeats )
  1115. lcw.getDescriptors ( img, cfeatures, positions );
  1116. set<double> scales;
  1117. int j = 0;
  1118. int lfdimension = -1;
  1119. for ( VVector::const_iterator i = features.begin();
  1120. i != features.end();
  1121. i++, j++ )
  1122. {
  1123. const NICE::Vector & x = *i;
  1124. if ( lfdimension < 0 ) lfdimension = ( int ) x.size();
  1125. else assert ( lfdimension == ( int ) x.size() );
  1126. NICE::Vector *v = new NICE::Vector ( x );
  1127. if ( usecolorfeats )
  1128. v->append ( cfeatures[j] );
  1129. Example tmp = Example ( v );
  1130. tmp.x = ( int ) positions[j][0];
  1131. tmp.y = ( int ) positions[j][1];
  1132. tmp.width = ( int ) ( 16.0 * positions[j][2] );
  1133. tmp.height = tmp.width;
  1134. tmp.scale = positions[j][2];
  1135. scales.insert ( positions[j][2] );
  1136. pce.push_back ( pair<int, Example> ( 0, tmp ) );
  1137. }
  1138. //////////////////
  1139. // PCA anwenden //
  1140. //////////////////
  1141. pce.filename = currentFile;
  1142. if ( usepca )
  1143. {
  1144. doPCA ( pce );
  1145. lfdimension = dim;
  1146. }
  1147. //////////////////
  1148. // BoV anwenden //
  1149. //////////////////
  1150. if ( norm )
  1151. normalize ( pce );
  1152. if ( usegmm || usekmeans )
  1153. {
  1154. if ( !usepca && !norm )
  1155. normalize ( pce );
  1156. convertLowToHigh ( pce );
  1157. smoothHL ( pce );
  1158. lfdimension = gaussians;
  1159. }
  1160. /////////////////////////////////////////
  1161. // Wahrscheinlichkeitskarten erstellen //
  1162. /////////////////////////////////////////
  1163. int klassen = probabilities.channels();
  1164. NICE::MultiChannelImageT<double> preMap ( xsize, ysize, klassen*scales.size() );
  1165. // initialisieren
  1166. for ( int y = 0 ; y < ysize ; y++ )
  1167. for ( int x = 0 ; x < xsize ; x++ )
  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.channels(); i++ )
  1173. {
  1174. probabilities[i](x,y) = 0.0;
  1175. }
  1176. for ( int j = 0; j < ( int ) preMap.channels(); j++ )
  1177. {
  1178. preMap[j](x,y) = 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.channels() );
  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.channels();
  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. std::vector<FloatImage> uncert;
  1210. std::vector<FloatImage> gpUncertainty;
  1211. std::vector<FloatImage> gpMean;
  1212. std::vector<FloatImage> gpMeanRatio;
  1213. std::vector<FloatImage> gpWeightAll;
  1214. std::vector<FloatImage> gpWeightRatio;
  1215. // std::vector<FloatImage> gpImpactAll;
  1216. // std::vector<FloatImage> gpImpactRatio;
  1217. //pre-allocate storage -- one image per scale and method
  1218. for(int s = 0; s < scalesize; s++)
  1219. {
  1220. uncert.push_back(FloatImage(xsize, ysize));
  1221. uncert[s].set(0.0);
  1222. gpUncertainty.push_back(FloatImage(xsize, ysize));
  1223. gpMean.push_back(FloatImage(xsize, ysize));
  1224. gpMeanRatio.push_back(FloatImage(xsize, ysize));
  1225. gpWeightAll.push_back(FloatImage(xsize, ysize));
  1226. gpWeightRatio.push_back(FloatImage(xsize, ysize));
  1227. /* gpImpactAll.push_back(FloatImage(xsize, ysize));
  1228. gpImpactRatio.push_back(FloatImage(xsize, ysize)); */
  1229. gpUncertainty[s].set(0.0);
  1230. gpMean[s].set(0.0);
  1231. gpMeanRatio[s].set(0.0);
  1232. gpWeightAll[s].set(0.0);
  1233. gpWeightRatio[s].set(0.0);
  1234. // gpImpactAll[s].set(0.0);
  1235. // gpImpactRatio[s].set(0.0);
  1236. }
  1237. ColorImage imgrgb ( xsize, ysize );
  1238. std::string s;
  1239. std::stringstream out;
  1240. std::vector< std::string > list2;
  1241. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  1242. out << "uncertainty/" << list2.back();
  1243. double maxu = -numeric_limits<double>::max();
  1244. double minu = numeric_limits<double>::max();
  1245. double gpNoise = conf->gD("GPHIK", "noise", 0.01);
  1246. #endif
  1247. if ( classifier != NULL )
  1248. {
  1249. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen: classifier != NULL" << endl;
  1250. #pragma omp parallel for
  1251. for ( int s = 0; s < scalesize; s++ )
  1252. {
  1253. #pragma omp parallel for
  1254. for ( int i = s; i < ( int ) pce.size(); i += scalesize )
  1255. {
  1256. ClassificationResult r = classifier->classify ( pce[i].second );
  1257. #ifdef UNCERTAINTY
  1258. //we need this if we want to compute GP-AL-measure lateron
  1259. double minMeanAbs ( numeric_limits<double>::max() );
  1260. double maxMeanAbs ( 0.0 );
  1261. double sndMaxMeanAbs ( 0.0 );
  1262. #endif
  1263. for ( int j = 0 ; j < fV.size(); j++ )
  1264. {
  1265. if ( useclass[j] == 0 )
  1266. continue;
  1267. fV[j] += r.scores[j];
  1268. preMap.set ( pce[i].second.x, pce[i].second.y, r.scores[j], j + s*klassen );
  1269. #ifdef UNCERTAINTY
  1270. //check whether we found a class with higher smaller abs mean than the current minimum
  1271. if (abs(r.scores[j]) < minMeanAbs)
  1272. minMeanAbs = abs(r.scores[j]);
  1273. //check for larger abs mean as well
  1274. if (abs(r.scores[j]) > maxMeanAbs)
  1275. {
  1276. sndMaxMeanAbs = maxMeanAbs;
  1277. maxMeanAbs = abs(r.scores[j]);
  1278. }
  1279. // and also for the second highest mean of all classes
  1280. else if (abs(r.scores[j]) > sndMaxMeanAbs)
  1281. {
  1282. sndMaxMeanAbs = abs(r.scores[j]);
  1283. }
  1284. #endif
  1285. }
  1286. /*if(r.uncertainty < 0.0)
  1287. {
  1288. cerr << "uncertainty: " << r.uncertainty << endl;
  1289. pce[i].second.svec->store(cerr);
  1290. cerr << endl;
  1291. exit(-1);
  1292. }*/
  1293. #ifdef UNCERTAINTY
  1294. uncert[s] ( pce[i].second.x, pce[i].second.y ) = r.uncertainty;
  1295. maxu = std::max ( r.uncertainty, maxu );
  1296. minu = std::min ( r.uncertainty, minu );
  1297. double firstTerm (1.0 / sqrt(r.uncertainty+gpNoise));
  1298. //compute the heuristic GP-UNCERTAINTY, as proposed by Kapoor et al. in IJCV 2010
  1299. // GP-UNCERTAINTY : |mean| / sqrt(var^2 + gpnoise^2)
  1300. gpUncertainty[s] ( pce[i].second.x, pce[i].second.y ) = maxMeanAbs*firstTerm; //firstTerm = 1.0 / sqrt(r.uncertainty+gpNoise))
  1301. // compute results when we take the lowest mean value of all classes
  1302. gpMean[s] ( pce[i].second.x, pce[i].second.y ) = minMeanAbs;
  1303. //look at the difference in the absolut mean values for the most plausible class
  1304. // and the second most plausible class
  1305. gpMeanRatio[s] ( pce[i].second.x, pce[i].second.y ) = maxMeanAbs - sndMaxMeanAbs;
  1306. //compute the weight in the alpha-vector for every sample after assuming it to be
  1307. // added to the training set.
  1308. // Thereby, we measure its "importance" for the current model
  1309. //
  1310. //double firstTerm is already computed
  1311. //
  1312. //the second term is only needed when computing impacts
  1313. //double secondTerm; //this is the nasty guy :/
  1314. //--- compute the third term
  1315. // this is the difference between predicted label and GT label
  1316. std::vector<double> diffToPositive; diffToPositive.clear();
  1317. std::vector<double> diffToNegative; diffToNegative.clear();
  1318. double diffToNegativeSum(0.0);
  1319. for ( int j = 0 ; j < fV.size(); j++ )
  1320. {
  1321. if ( useclass[j] == 0 )
  1322. continue;
  1323. // look at the difference to plus 1
  1324. diffToPositive.push_back(abs(r.scores[j] - 1));
  1325. // look at the difference to -1
  1326. diffToNegative.push_back(abs(r.scores[j] + 1));
  1327. //sum up the difference to -1
  1328. diffToNegativeSum += abs(r.scores[j] - 1);
  1329. }
  1330. //let's subtract for every class its diffToNegative from the sum, add its diffToPositive,
  1331. //and use this as the third term for this specific class.
  1332. //the final value is obtained by minimizing over all classes
  1333. //
  1334. // originally, we minimize over all classes after building the final score
  1335. // however, the first and the second term do not depend on the choice of
  1336. // y*, therefore we minimize here already
  1337. double thirdTerm (numeric_limits<double>::max()) ;
  1338. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1339. {
  1340. double tmpVal ( diffToPositive[tmpCnt] + (diffToNegativeSum-diffToNegative[tmpCnt]) );
  1341. if (tmpVal < thirdTerm)
  1342. thirdTerm = tmpVal;
  1343. }
  1344. gpWeightAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm;
  1345. //now look on the ratio of the resulting weights for the most plausible
  1346. // against the second most plausible class
  1347. double thirdTermMostPlausible ( 0.0 ) ;
  1348. double thirdTermSecondMostPlausible ( 0.0 ) ;
  1349. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1350. {
  1351. if (diffToPositive[tmpCnt] > thirdTermMostPlausible)
  1352. {
  1353. thirdTermSecondMostPlausible = thirdTermMostPlausible;
  1354. thirdTermMostPlausible = diffToPositive[tmpCnt];
  1355. }
  1356. else if (diffToPositive[tmpCnt] > thirdTermSecondMostPlausible)
  1357. {
  1358. thirdTermSecondMostPlausible = diffToPositive[tmpCnt];
  1359. }
  1360. }
  1361. //compute the resulting score
  1362. gpWeightRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm;
  1363. //finally, look for this feature how it would affect to whole model (summarized by weight-vector alpha), if we would
  1364. //use it as an additional training example
  1365. //TODO this would be REALLY computational demanding. Do we really want to do this?
  1366. // gpImpactAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm*secondTerm;
  1367. // gpImpactRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm*secondTerm;
  1368. #endif
  1369. }
  1370. }
  1371. }
  1372. else
  1373. {
  1374. //#pragma omp parallel for
  1375. for ( int s = 0; s < scalesize; s++ )
  1376. {
  1377. //#pragma omp parallel for
  1378. for ( int i = s; i < ( int ) pce.size(); i += scalesize )
  1379. {
  1380. ClassificationResult r = vclassifier->classify ( * ( pce[i].second.vec ) );
  1381. #ifdef UNCERTAINTY
  1382. //we need this if we want to compute GP-AL-measure lateron
  1383. double minMeanAbs ( numeric_limits<double>::max() );
  1384. double maxMeanAbs ( 0.0 );
  1385. double sndMaxMeanAbs ( 0.0 );
  1386. #endif
  1387. for ( int j = 0 ; j < ( int ) fV.size(); j++ )
  1388. {
  1389. if ( useclass[j] == 0 )
  1390. continue;
  1391. fV[j] += r.scores[j];
  1392. preMap.set ( pce[i].second.x, pce[i].second.y, r.scores[j], j + s*klassen );
  1393. #ifdef UNCERTAINTY
  1394. //check whether we found a class with higher smaller abs mean than the current minimum
  1395. if (abs(r.scores[j]) < minMeanAbs)
  1396. minMeanAbs = abs(r.scores[j]);
  1397. //check for larger abs mean as well
  1398. if (abs(r.scores[j]) > maxMeanAbs)
  1399. {
  1400. sndMaxMeanAbs = maxMeanAbs;
  1401. maxMeanAbs = abs(r.scores[j]);
  1402. }
  1403. // and also for the second highest mean of all classes
  1404. else if (abs(r.scores[j]) > sndMaxMeanAbs)
  1405. {
  1406. sndMaxMeanAbs = abs(r.scores[j]);
  1407. }
  1408. #endif
  1409. }
  1410. #ifdef UNCERTAINTY
  1411. uncert[s] ( pce[i].second.x, pce[i].second.y ) = r.uncertainty;
  1412. maxu = std::max ( r.uncertainty, maxu );
  1413. minu = std::min ( r.uncertainty, minu );
  1414. double firstTerm (1.0 / sqrt(r.uncertainty+gpNoise));
  1415. //compute the heuristic GP-UNCERTAINTY, as proposed by Kapoor et al. in IJCV 2010
  1416. // GP-UNCERTAINTY : |mean| / sqrt(var^2 + gpnoise^2)
  1417. gpUncertainty[s] ( pce[i].second.x, pce[i].second.y ) = maxMeanAbs*firstTerm; //firstTerm = 1.0 / sqrt(r.uncertainty+gpNoise))
  1418. // compute results when we take the lowest mean value of all classes
  1419. gpMean[s] ( pce[i].second.x, pce[i].second.y ) = minMeanAbs;
  1420. //look at the difference in the absolut mean values for the most plausible class
  1421. // and the second most plausible class
  1422. gpMeanRatio[s] ( pce[i].second.x, pce[i].second.y ) = maxMeanAbs - sndMaxMeanAbs;
  1423. //compute the weight in the alpha-vector for every sample after assuming it to be
  1424. // added to the training set.
  1425. // Thereby, we measure its "importance" for the current model
  1426. //
  1427. //double firstTerm is already computed
  1428. //
  1429. //the second term is only needed when computing impacts
  1430. //double secondTerm; //this is the nasty guy :/
  1431. //--- compute the third term
  1432. // this is the difference between predicted label and GT label
  1433. std::vector<double> diffToPositive; diffToPositive.clear();
  1434. std::vector<double> diffToNegative; diffToNegative.clear();
  1435. double diffToNegativeSum(0.0);
  1436. for ( int j = 0 ; j < fV.size(); j++ )
  1437. {
  1438. if ( useclass[j] == 0 )
  1439. continue;
  1440. // look at the difference to plus 1
  1441. diffToPositive.push_back(abs(r.scores[j] - 1));
  1442. // look at the difference to -1
  1443. diffToNegative.push_back(abs(r.scores[j] + 1));
  1444. //sum up the difference to -1
  1445. diffToNegativeSum += abs(r.scores[j] - 1);
  1446. }
  1447. //let's subtract for every class its diffToNegative from the sum, add its diffToPositive,
  1448. //and use this as the third term for this specific class.
  1449. //the final value is obtained by minimizing over all classes
  1450. //
  1451. // originally, we minimize over all classes after building the final score
  1452. // however, the first and the second term do not depend on the choice of
  1453. // y*, therefore we minimize here already
  1454. double thirdTerm (numeric_limits<double>::max()) ;
  1455. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1456. {
  1457. double tmpVal ( diffToPositive[tmpCnt] + (diffToNegativeSum-diffToNegative[tmpCnt]) );
  1458. if (tmpVal < thirdTerm)
  1459. thirdTerm = tmpVal;
  1460. }
  1461. gpWeightAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm;
  1462. //now look on the ratio of the resulting weights for the most plausible
  1463. // against the second most plausible class
  1464. double thirdTermMostPlausible ( 0.0 ) ;
  1465. double thirdTermSecondMostPlausible ( 0.0 ) ;
  1466. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1467. {
  1468. if (diffToPositive[tmpCnt] > thirdTermMostPlausible)
  1469. {
  1470. thirdTermSecondMostPlausible = thirdTermMostPlausible;
  1471. thirdTermMostPlausible = diffToPositive[tmpCnt];
  1472. }
  1473. else if (diffToPositive[tmpCnt] > thirdTermSecondMostPlausible)
  1474. {
  1475. thirdTermSecondMostPlausible = diffToPositive[tmpCnt];
  1476. }
  1477. }
  1478. //compute the resulting score
  1479. gpWeightRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm;
  1480. //finally, look for this feature how it would affect to whole model (summarized by weight-vector alpha), if we would
  1481. //use it as an additional training example
  1482. //TODO this would be REALLY computational demanding. Do we really want to do this?
  1483. // gpImpactAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm*secondTerm;
  1484. // gpImpactRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm*secondTerm;
  1485. #endif
  1486. }
  1487. }
  1488. }
  1489. #ifdef UNCERTAINTY
  1490. cout << "maxvdirect: " << maxu << " minvdirect: " << minu << endl;
  1491. //pre-allocate the image for filtering lateron
  1492. FloatImage gaussUncert ( xsize, ysize );
  1493. //just store the first scale
  1494. ICETools::convertToRGB ( uncert[0], imgrgb );
  1495. imgrgb.write ( out.str() + "rough.png" );
  1496. //pre-allocate memory for filtering of scales
  1497. FloatImage gaussGPUncertainty ( xsize, ysize );
  1498. FloatImage gaussGPMean ( xsize, ysize );
  1499. FloatImage gaussGPMeanRatio( xsize, ysize );
  1500. FloatImage gaussGPWeightAll ( xsize, ysize );
  1501. FloatImage gaussGPWeightRatio ( xsize, ysize );
  1502. //just store the first scale for every method
  1503. ICETools::convertToRGB ( gpUncertainty[0], imgrgb );
  1504. imgrgb.write ( out.str() + "gpUncertainty.png" );
  1505. ICETools::convertToRGB ( gpMean[0], imgrgb );
  1506. imgrgb.write ( out.str() + "gpMean.png" );
  1507. ICETools::convertToRGB ( gpMeanRatio[0], imgrgb );
  1508. imgrgb.write ( out.str() + "gpMeanRatio.png" );
  1509. ICETools::convertToRGB ( gpWeightAll[0], imgrgb );
  1510. imgrgb.write ( out.str() + "gpWeightAll.png" );
  1511. ICETools::convertToRGB ( gpWeightRatio[0], imgrgb );
  1512. imgrgb.write ( out.str() + "gpWeightRatio.png" );
  1513. #endif
  1514. vector<double> scalesVec;
  1515. for ( set<double>::const_iterator iter = scales.begin();
  1516. iter != scales.end();
  1517. ++iter )
  1518. {
  1519. scalesVec.push_back ( *iter );
  1520. }
  1521. #undef VISSEMSEG
  1522. #ifdef VISSEMSEG
  1523. for ( int j = 0 ; j < ( int ) preMap.channels(); j++ )
  1524. {
  1525. cout << "klasse: " << j << endl;//" " << cn.text ( j ) << endl;
  1526. NICE::Matrix tmp ( preMap.ysize, preMap.xsize );
  1527. double maxval = 0.0;
  1528. for ( int y = 0; y < preMap.ysize; y++ )
  1529. for ( int x = 0; x < preMap.xsize; x++ )
  1530. {
  1531. double val = preMap.get ( x, y, j );
  1532. tmp ( y, x ) = val;
  1533. maxval = std::max ( val, maxval );
  1534. }
  1535. NICE::ColorImage imgrgb ( preMap.xsize, preMap.ysize );
  1536. ICETools::convertToRGB ( tmp, imgrgb );
  1537. cout << "maxval = " << maxval << " for class " << j << endl; //cn.text ( j ) << endl;
  1538. //Show ( ON, imgrgb, cn.text ( j ) );
  1539. //showImage(imgrgb, "Ergebnis");
  1540. std::string s;
  1541. std::stringstream out;
  1542. out << "tmpprebmap" << j << ".ppm";
  1543. s = out.str();
  1544. imgrgb.writePPM ( s );
  1545. //getchar();
  1546. }
  1547. #endif
  1548. // Gaußfiltern
  1549. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen -> Gaussfiltern" << endl;
  1550. for ( int s = 0; s < scalesize; s++ )
  1551. {
  1552. double sigma = sigmaweight * 16.0 * scalesVec[s];
  1553. cerr << "sigma: " << sigma << endl;
  1554. #pragma omp parallel for
  1555. for ( int i = 0; i < klassen; i++ )
  1556. {
  1557. if ( forbidden_classes.find ( i ) != forbidden_classes.end() )
  1558. {
  1559. continue;
  1560. }
  1561. int pos = i + s * klassen;
  1562. double maxval = preMap[pos](0,0);
  1563. double minval = maxval;
  1564. for ( int y = 0; y < ysize; y++ )
  1565. {
  1566. for ( int x = 0; x < xsize; x++ )
  1567. {
  1568. maxval = std::max ( maxval, preMap[pos](x,y) );
  1569. minval = std::min ( minval, preMap[pos](x,y) );
  1570. }
  1571. }
  1572. NICE::FloatImage dblImg ( xsize, ysize );
  1573. NICE::FloatImage gaussImg ( xsize, ysize );
  1574. for ( int y = 0; y < ysize; y++ )
  1575. {
  1576. for ( int x = 0; x < xsize; x++ )
  1577. {
  1578. dblImg.setPixel ( x, y, preMap[pos](x,y) );
  1579. }
  1580. }
  1581. filterGaussSigmaApproximate<float, float, float> ( dblImg, sigma, &gaussImg );
  1582. for ( int y = 0; y < ysize; y++ )
  1583. {
  1584. for ( int x = 0; x < xsize; x++ )
  1585. {
  1586. preMap[pos](x,y) = gaussImg.getPixel ( x, y );
  1587. }
  1588. }
  1589. }
  1590. #ifdef UNCERTAINTY
  1591. filterGaussSigmaApproximate<float, float, float> ( uncert[s], sigma, &gaussUncert );
  1592. uncert[s] = gaussUncert;
  1593. //apply the gauss-filtering to all scales of every method
  1594. filterGaussSigmaApproximate<float, float, float> ( gpUncertainty[s], sigma, &gaussGPUncertainty );
  1595. filterGaussSigmaApproximate<float, float, float> ( gpMean[s], sigma, &gaussGPMean );
  1596. filterGaussSigmaApproximate<float, float, float> ( gpMeanRatio[s], sigma, &gaussGPMeanRatio );
  1597. filterGaussSigmaApproximate<float, float, float> ( gpWeightAll[s], sigma, &gaussGPWeightAll );
  1598. filterGaussSigmaApproximate<float, float, float> ( gpWeightRatio[s], sigma, &gaussGPWeightRatio );
  1599. gpUncertainty[s] = gaussGPUncertainty;
  1600. gpMean[s] = gaussGPMean;
  1601. gpMeanRatio[s] = gaussGPMeanRatio;
  1602. gpWeightAll[s] = gaussGPWeightAll;
  1603. gpWeightRatio[s] = gaussGPWeightRatio;
  1604. #endif
  1605. }
  1606. // Zusammenfassen und auswerten
  1607. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen -> zusammenfassen und auswerten" << endl;
  1608. //#pragma omp parallel for
  1609. for ( int x = 0; x < xsize; x++ )
  1610. {
  1611. for ( int y = 0; y < ysize; y++ )
  1612. {
  1613. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1614. {
  1615. double prob = 0.0;
  1616. for ( int s = 0; s < ( int ) scalesize; s++ )
  1617. {
  1618. prob += preMap.get ( x, y, j + s * klassen );
  1619. }
  1620. double val = prob / ( double ) ( scalesize );
  1621. probabilities.set ( x, y, val, j );
  1622. }
  1623. }
  1624. }
  1625. #ifdef UNCERTAINTY
  1626. for ( int x = 0; x < xsize; x++ )
  1627. {
  1628. for ( int y = 0; y < ysize; y++ )
  1629. {
  1630. for ( int s = 0; s < ( int ) scalesize; s++ )
  1631. {
  1632. gaussUncert(x,y) += uncert[s](x,y);
  1633. //and for the other methods as well
  1634. gaussGPUncertainty(x,y) += gpUncertainty[s](x,y);
  1635. gaussGPMean(x,y) += gpMean[s](x,y);
  1636. gaussGPMeanRatio(x,y) += gpMeanRatio[s](x,y);
  1637. gaussGPWeightAll(x,y) += gpWeightAll[s](x,y);
  1638. gaussGPWeightRatio(x,y) += gpWeightRatio[s](x,y);
  1639. }
  1640. gaussUncert(x,y)/=scalesize;
  1641. //and for the other methods as well
  1642. gaussGPUncertainty(x,y)/=scalesize;
  1643. gaussGPMean(x,y)/=scalesize;
  1644. gaussGPMeanRatio(x,y)/=scalesize;
  1645. gaussGPWeightAll(x,y)/=scalesize;
  1646. gaussGPWeightRatio(x,y)/=scalesize;
  1647. }
  1648. }
  1649. maxu = -numeric_limits<double>::max();
  1650. minu = numeric_limits<double>::max();
  1651. for ( int y = 0; y < ysize; y++ )
  1652. {
  1653. for ( int x = 0; x < xsize; x++ )
  1654. {
  1655. double val = uncert[0] ( x, y );
  1656. maxu = std::max ( val, maxu );
  1657. minu = std::min ( val, minu );
  1658. }
  1659. }
  1660. cout << "maxvo = " << maxu << " minvo = " << minu << endl;
  1661. maxu = -numeric_limits<float>::max();
  1662. minu = numeric_limits<float>::max();
  1663. for ( int y = 0; y < ysize; y++ )
  1664. {
  1665. for ( int x = 0; x < xsize; x++ )
  1666. {
  1667. double val = gaussUncert ( x, y );
  1668. maxu = std::max ( val, maxu );
  1669. minu = std::min ( val, minu );
  1670. }
  1671. }
  1672. cout << "maxvf = " << maxu << " minvf = " << minu << endl;
  1673. gaussUncert(0,0) = 0.0;
  1674. gaussUncert(0,1) = 0.04;
  1675. ICETools::convertToRGB ( gaussUncert, imgrgb );
  1676. imgrgb.write ( out.str() + "filtered.png" );
  1677. ICETools::convertToRGB ( gaussGPUncertainty, imgrgb );
  1678. imgrgb.write ( out.str() + "gpUncertaintyFiltered.png" );
  1679. ICETools::convertToRGB ( gaussGPMean, imgrgb );
  1680. imgrgb.write ( out.str() + "gpMeanFiltered.png" );
  1681. ICETools::convertToRGB ( gaussGPMeanRatio, imgrgb );
  1682. imgrgb.write ( out.str() + "gpMeanRatioFiltered.png" );
  1683. ICETools::convertToRGB ( gaussGPWeightAll, imgrgb );
  1684. imgrgb.write ( out.str() + "gpWeightAllFiltered.png" );
  1685. ICETools::convertToRGB ( gaussGPWeightRatio, imgrgb );
  1686. imgrgb.write ( out.str() + "gpWeightRatioFiltered.png" );
  1687. #endif
  1688. #undef VISSEMSEG
  1689. #ifdef VISSEMSEG
  1690. std::string s;
  1691. std::stringstream out;
  1692. std::vector< std::string > list2;
  1693. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  1694. out << "probmaps/" << list2.back() << ".probs";
  1695. s = out.str();
  1696. probabilities.store ( s );
  1697. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1698. {
  1699. cout << "klasse: " << j << endl;//" " << cn.text ( j ) << endl;
  1700. NICE::Matrix tmp ( probabilities.ysize, probabilities.xsize );
  1701. double maxval = 0.0;
  1702. for ( int y = 0; y < probabilities.ysize; y++ )
  1703. for ( int x = 0; x < probabilities.xsize; x++ )
  1704. {
  1705. double val = probabilities.get ( x, y, j );
  1706. tmp ( y, x ) = val;
  1707. maxval = std::max ( val, maxval );
  1708. }
  1709. NICE::ColorImage imgrgb ( probabilities.xsize, probabilities.ysize );
  1710. ICETools::convertToRGB ( tmp, imgrgb );
  1711. cout << "maxval = " << maxval << " for class " << j << endl; //cn.text ( j ) << endl;
  1712. //Show ( ON, imgrgb, cn.text ( j ) );
  1713. //showImage(imgrgb, "Ergebnis");
  1714. std::string s;
  1715. std::stringstream out;
  1716. out << "tmp" << j << ".ppm";
  1717. s = out.str();
  1718. imgrgb.writePPM ( s );
  1719. //getchar();
  1720. }
  1721. #endif
  1722. if ( useregions )
  1723. {
  1724. if ( bestclasses > 0 )
  1725. {
  1726. PSSImageLevelPrior pss ( 0, bestclasses, 0.2 );
  1727. pss.setPrior ( fV );
  1728. pss.postprocess ( segresult, probabilities );
  1729. }
  1730. //Regionen ermitteln
  1731. int regionsize = seg->segRegions ( img, mask );
  1732. Regionen.clear();
  1733. vector<vector <double> > regionprob;
  1734. #ifdef UNCERTAINTY
  1735. std::vector<double> regionUncert;
  1736. std::vector<double> regionGPUncertainty;
  1737. std::vector<double> regionGPMean;
  1738. std::vector<double> regionGPMeanRatio;
  1739. std::vector<double> regionGPWeightAll;
  1740. std::vector<double> regionGPWeightRatio;
  1741. #endif
  1742. // Wahrscheinlichkeiten für jede Region initialisieren
  1743. for ( int i = 0; i < regionsize; i++ )
  1744. {
  1745. vector<double> tmp;
  1746. for ( int j = 0; j < ( int ) probabilities.channels(); j++ )
  1747. {
  1748. tmp.push_back ( 0.0 );
  1749. }
  1750. regionprob.push_back ( tmp );
  1751. Regionen.push_back ( pair<int, Example> ( 0, Example() ) );
  1752. #ifdef UNCERTAINTY
  1753. regionUncert.push_back ( 0.0 );
  1754. regionGPUncertainty.push_back ( 0.0 );
  1755. regionGPMean.push_back ( 0.0 );
  1756. regionGPMeanRatio.push_back ( 0.0 );
  1757. regionGPWeightAll.push_back ( 0.0 );
  1758. regionGPWeightRatio.push_back ( 0.0 );
  1759. #endif
  1760. }
  1761. // Wahrscheinlichkeiten für Regionen bestimmen
  1762. for ( int x = 0; x < xsize; x++ )
  1763. {
  1764. for ( int y = 0; y < ysize; y++ )
  1765. {
  1766. int pos = mask ( x, y );
  1767. Regionen[pos].second.weight += 1.0;
  1768. Regionen[pos].second.x += x;
  1769. Regionen[pos].second.y += y;
  1770. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1771. {
  1772. double val = probabilities.get ( x, y, j );
  1773. regionprob[pos][j] += val;
  1774. }
  1775. #ifdef UNCERTAINTY
  1776. regionUncert[pos] += gaussUncert ( x, y );
  1777. regionGPUncertainty[pos] += gaussGPUncertainty ( x, y );
  1778. regionGPMean[pos] += gaussGPMean ( x, y );
  1779. regionGPMeanRatio[pos] += gaussGPMeanRatio ( x, y );
  1780. regionGPWeightAll[pos] += gaussGPWeightAll ( x, y );
  1781. regionGPWeightRatio[pos] += gaussGPWeightRatio ( x, y );
  1782. #endif
  1783. }
  1784. }
  1785. /*
  1786. cout << "regions: " << regionsize << endl;
  1787. cout << "outfeats: " << endl;
  1788. for(int j = 0; j < regionprob.size(); j++)
  1789. {
  1790. for(int i = 0; i < regionprob[j].size(); i++)
  1791. {
  1792. cout << regionprob[j][i] << " ";
  1793. }
  1794. cout << endl;
  1795. }
  1796. cout << endl;
  1797. getchar();*/
  1798. // beste Wahrscheinlichkeit je Region wählen
  1799. for ( int i = 0; i < regionsize; i++ )
  1800. {
  1801. if ( Regionen[i].second.weight > 0 )
  1802. {
  1803. Regionen[i].second.x /= ( int ) Regionen[i].second.weight;
  1804. Regionen[i].second.y /= ( int ) Regionen[i].second.weight;
  1805. }
  1806. double maxval = -numeric_limits<double>::max();
  1807. int maxpos = 0;
  1808. for ( int j = 0 ; j < ( int ) regionprob[i].size(); j++ )
  1809. {
  1810. if ( forbidden_classes.find ( j ) != forbidden_classes.end() )
  1811. continue;
  1812. regionprob[i][j] /= Regionen[i].second.weight;
  1813. if ( maxval < regionprob[i][j] )
  1814. {
  1815. maxval = regionprob[i][j];
  1816. maxpos = j;
  1817. }
  1818. probabilities.set ( Regionen[i].second.x, Regionen[i].second.y, regionprob[i][j], j );
  1819. }
  1820. Regionen[i].first = maxpos;
  1821. #ifdef UNCERTAINTY
  1822. regionUncert[i] /= Regionen[i].second.weight;
  1823. regionGPUncertainty[i] /= Regionen[i].second.weight;
  1824. regionGPMean[i] /= Regionen[i].second.weight;
  1825. regionGPMeanRatio[i] /= Regionen[i].second.weight;
  1826. regionGPWeightAll[i] /= Regionen[i].second.weight;
  1827. regionGPWeightRatio[i] /= Regionen[i].second.weight;
  1828. #endif
  1829. }
  1830. // Pixel jeder Region labeln
  1831. for ( int y = 0; y < ( int ) mask.cols(); y++ )
  1832. {
  1833. for ( int x = 0; x < ( int ) mask.rows(); x++ )
  1834. {
  1835. int pos = mask ( x, y );
  1836. segresult.setPixel ( x, y, Regionen[pos].first );
  1837. #ifdef UNCERTAINTY
  1838. gaussUncert ( x, y ) = regionUncert[pos];
  1839. gaussGPUncertainty ( x, y ) = regionGPUncertainty[pos];
  1840. gaussGPMean ( x, y ) = regionGPMean[pos];
  1841. gaussGPMeanRatio ( x, y ) = regionGPMeanRatio[pos];
  1842. gaussGPWeightAll ( x, y ) = regionGPWeightAll[pos];
  1843. gaussGPWeightRatio ( x, y ) = regionGPWeightRatio[pos];
  1844. #endif
  1845. }
  1846. }
  1847. #ifdef UNCERTAINTY
  1848. maxu = -numeric_limits<float>::max();
  1849. minu = numeric_limits<float>::max();
  1850. for ( int y = 0; y < ysize; y++ )
  1851. {
  1852. for ( int x = 0; x < xsize; x++ )
  1853. {
  1854. //float val = uncert(x,y);
  1855. double val = gaussUncert ( x, y );
  1856. maxu = std::max ( val, maxu );
  1857. minu = std::min ( val, minu );
  1858. }
  1859. }
  1860. cout << "maxvr = " << maxu << " minvr = " << minu << endl;
  1861. // uncert(0,0) = 1;
  1862. // uncert(0,1) = 0;
  1863. ICETools::convertToRGB ( gaussUncert, imgrgb );
  1864. imgrgb.write ( out.str() + "region.png" );
  1865. ICETools::convertToRGB ( gaussGPUncertainty, imgrgb );
  1866. imgrgb.write ( out.str() + "gpUncertaintyRegion.png" );
  1867. ICETools::convertToRGB ( gaussGPMean, imgrgb );
  1868. imgrgb.write ( out.str() + "gpMeanRegion.png" );
  1869. ICETools::convertToRGB ( gaussGPMeanRatio, imgrgb );
  1870. imgrgb.write ( out.str() + "gpMeanRatioRegion.png" );
  1871. ICETools::convertToRGB ( gaussGPWeightAll, imgrgb );
  1872. imgrgb.write ( out.str() + "gpWeightAllRegion.png" );
  1873. ICETools::convertToRGB ( gaussGPWeightRatio, imgrgb );
  1874. imgrgb.write ( out.str() + "gpWeightRatioRegion.png" );
  1875. #endif
  1876. #undef WRITEREGIONS
  1877. #ifdef WRITEREGIONS
  1878. RegionGraph rg;
  1879. seg->getGraphRepresentation ( img, mask, rg );
  1880. for ( uint pos = 0; pos < regionprob.size(); pos++ )
  1881. {
  1882. rg[pos]->setProbs ( regionprob[pos] );
  1883. }
  1884. std::string s;
  1885. std::stringstream out;
  1886. std::vector< std::string > list;
  1887. StringTools::split ( Globals::getCurrentImgFN (), '/', list );
  1888. out << "rgout/" << list.back() << ".graph";
  1889. string writefile = out.str();
  1890. rg.write ( writefile );
  1891. #endif
  1892. }
  1893. else
  1894. {
  1895. PSSImageLevelPrior pss ( 1, 4, 0.2 );
  1896. pss.setPrior ( fV );
  1897. pss.postprocess ( segresult, probabilities );
  1898. }
  1899. // Saubermachen:
  1900. clog << "[log] SemSegCsurka::classifyregions: sauber machen" << endl;
  1901. for ( int i = 0; i < ( int ) pce.size(); i++ )
  1902. {
  1903. pce[i].second.clean();
  1904. }
  1905. pce.clear();
  1906. if ( cSIFT != NULL )
  1907. delete cSIFT;
  1908. if ( writeFeats != NULL )
  1909. delete writeFeats;
  1910. if ( readFeats != NULL )
  1911. delete readFeats;
  1912. getFeats = NULL;
  1913. }
  1914. void SemSegCsurka::semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities )
  1915. {
  1916. Examples regions;
  1917. NICE::Matrix regionmask;
  1918. classifyregions ( ce, segresult, probabilities, regions, regionmask );
  1919. if ( userellocprior || srg != NULL || gcopt != NULL )
  1920. {
  1921. if ( userellocprior )
  1922. relloc->postprocess ( regions, probabilities );
  1923. if ( srg != NULL )
  1924. srg->optimizeShape ( regions, regionmask, probabilities );
  1925. if ( gcopt != NULL )
  1926. gcopt->optimizeImage ( regions, regionmask, probabilities );
  1927. // Pixel jeder Region labeln
  1928. for ( int y = 0; y < ( int ) regionmask.cols(); y++ )
  1929. {
  1930. for ( int x = 0; x < ( int ) regionmask.rows(); x++ )
  1931. {
  1932. int pos = regionmask ( x, y );
  1933. segresult.setPixel ( x, y, regions[pos].first );
  1934. }
  1935. }
  1936. }
  1937. #ifndef NOVISUAL
  1938. #undef VISSEMSEG
  1939. #ifdef VISSEMSEG
  1940. // showImage(img);
  1941. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1942. {
  1943. cout << "klasse: " << j << " " << cn.text ( j ) << endl;
  1944. NICE::Matrix tmp ( probabilities.ysize, probabilities.xsize );
  1945. double maxval = -numeric_limits<double>::max();
  1946. for ( int y = 0; y < probabilities.ysize; y++ )
  1947. for ( int x = 0; x < probabilities.xsize; x++ )
  1948. {
  1949. double val = probabilities.get ( x, y, j );
  1950. tmp ( y, x ) = val;
  1951. maxval = std::max ( val, maxval );
  1952. }
  1953. NICE::ColorImage imgrgb ( probabilities.xsize, probabilities.ysize );
  1954. ICETools::convertToRGB ( tmp, imgrgb );
  1955. cout << "maxval = " << maxval << " for class " << cn.text ( j ) << endl;
  1956. Show ( ON, imgrgb, cn.text ( j ) );
  1957. imgrgb.Write ( "tmp.ppm" );
  1958. getchar();
  1959. }
  1960. #endif
  1961. #endif
  1962. }