SemSegCsurka2.cpp 41 KB

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