RFCsurka.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "vislearning/features/regionfeatures/RFCsurka.h"
  2. #include "vislearning/classifier/fpclassifier/randomforest/FPCRandomForests.h"
  3. #include "vislearning/classifier/fpclassifier/logisticregression/FPCSMLR.h"
  4. #include "core/image/Filter.h"
  5. #include <iostream>
  6. using namespace OBJREC;
  7. using namespace std;
  8. using namespace NICE;
  9. RFCsurka::RFCsurka( const Config *_conf, LocalFeature *_lf ):RFBoV(_conf, _lf)
  10. {
  11. lf = _lf;
  12. string sscales = conf->gS("SIFTTest", "scales", "1+2.0+3.0");
  13. grid = conf->gI("SIFTTest", "grid", 20);
  14. sigmaweight = conf->gD ( "RFCsurka", "sigmaweight", 0.2 );
  15. string::size_type pos = 0;
  16. string::size_type oldpos = 0;
  17. while(pos != string::npos)
  18. {
  19. pos = sscales.find("+", oldpos);
  20. string val;
  21. if(pos == string::npos)
  22. val = sscales.substr(oldpos);
  23. else
  24. val = sscales.substr(oldpos, pos-oldpos);
  25. double d = atof(val.c_str());
  26. scales.push_back(d);
  27. oldpos = pos+1;
  28. }
  29. gmm = NULL;
  30. pca = NULL;
  31. fpc = NULL;
  32. vec = NULL;
  33. }
  34. RFCsurka::~RFCsurka()
  35. {
  36. }
  37. void RFCsurka::setPCA(PCA *_pca)
  38. {
  39. pca = _pca;
  40. }
  41. void RFCsurka::setGMM(GMM *_gmm)
  42. {
  43. gmm = _gmm;
  44. }
  45. void RFCsurka::setClassifier(FeaturePoolClassifier *_fpc)
  46. {
  47. fpc = _fpc;
  48. vec = NULL;
  49. nbclasses = fpc->getMaxClassNo ()+1;
  50. }
  51. void RFCsurka::setClassifier(VecClassifier *_vec)
  52. {
  53. vec = _vec;
  54. fpc = NULL;
  55. nbclasses = vec->getMaxClassNo ()+1;
  56. }
  57. void RFCsurka::extract ( const NICE::Image & img, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
  58. {
  59. VVector positions, features;
  60. getPositions(rg, mask, positions);
  61. lf->getDescriptors(img, positions, features);
  62. assert(features.size() == positions.size());
  63. convert(mask, rg, features, feats, positions);
  64. }
  65. void RFCsurka::extractRGB ( const NICE::ColorImage & cimg, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
  66. {
  67. assert(fpc != NULL || vec != NULL);
  68. VVector positions, features;
  69. getPositions(rg, mask, positions);
  70. lf->getDescriptors(cimg, positions, features);
  71. assert(features.size() == positions.size());
  72. convert(mask, rg, features, feats, positions);
  73. }
  74. void RFCsurka::convert(const Matrix &mask, const RegionGraph &rg, VVector &infeats, VVector &outfeats, VVector &positions)
  75. {
  76. outfeats.reSize(rg.size(),nbclasses);
  77. bool usepca = false;
  78. bool usegmm = false;
  79. if(pca != NULL)
  80. usepca = true;
  81. if(gmm != NULL)
  82. usegmm = true;
  83. set<double> scales;
  84. map<double,int> scalesmap;
  85. for(int i = 0; i < (int)infeats.size(); i++)
  86. {
  87. scales.insert(positions[i][2]);
  88. }
  89. int s = 0;
  90. for(set<double>::const_iterator iter = scales.begin(); iter != scales.end(); ++iter, s++)
  91. {
  92. scalesmap[*iter] = s;
  93. }
  94. int xsize = mask.rows();
  95. int ysize = mask.cols();
  96. MultiChannelImageT<double> preMap ( xsize,ysize,nbclasses*scales.size(),true );
  97. MultiChannelImageT<double> probabilities ( xsize, ysize, nbclasses, true);
  98. preMap.setAll(0.0);
  99. probabilities.setAll(0.0);
  100. // Die Wahrscheinlichkeitsmaps mit den einzelnen Wahrscheinlichkeiten je Skalierung füllen
  101. for(int i = 0; i < (int)infeats.size(); i++)
  102. {
  103. Vector *feat = new Vector(infeats[i]);
  104. Example ex(feat);
  105. if(usepca)
  106. *ex.vec = pca->getFeatureVector ( *ex.vec, true );
  107. if(usegmm)
  108. {
  109. Vector probs;
  110. gmm->getProbs(*ex.vec, probs);
  111. *ex.vec = probs;
  112. }
  113. ClassificationResult r;
  114. if(fpc != NULL)
  115. r = fpc->classify ( ex );
  116. else
  117. r = vec->classify(*ex.vec);
  118. for ( int j = 0 ; j < ( int ) probabilities.numChannels; j++ )
  119. preMap.set ( positions[i][0],positions[i][1],r.scores[j],j+scalesmap[positions[i][2]]*nbclasses);
  120. delete ex.vec;
  121. ex.vec = NULL;
  122. }
  123. s = 0;
  124. vector<double> sigmavec;
  125. for(set<double>::const_iterator iter = scales.begin(); iter != scales.end(); ++iter, s++)
  126. {
  127. double sigma = sigmaweight*16.0* (*iter);
  128. sigmavec.push_back(sigma);
  129. }
  130. for(int s = 0; s < (int)sigmavec.size(); s++)
  131. {
  132. double sigma = sigmavec[s];
  133. cerr << "sigma: " << sigma << endl;
  134. #pragma omp parallel for
  135. for ( int i = 0; i < nbclasses; i++ )
  136. {
  137. int pos = i+s*nbclasses;
  138. double maxval = preMap.data[pos][0];
  139. double minval = preMap.data[pos][0];
  140. for ( int z = 1; z < xsize*ysize; z++ )
  141. {
  142. maxval = std::max ( maxval, preMap.data[pos][z] );
  143. minval = std::min ( minval, preMap.data[pos][z] );
  144. }
  145. NICE::FloatImage dblImg( xsize, ysize);
  146. NICE::FloatImage gaussImg( xsize, ysize);
  147. long int offset = 0;
  148. for ( int y = 0; y < ysize; y++ )
  149. {
  150. for ( int x = 0; x < xsize; x++, offset++ )
  151. {
  152. dblImg.setPixel(x,y,preMap.data[pos][offset]);
  153. }
  154. }
  155. filterGaussSigmaApproximate<float,float,float>( dblImg, sigma, &gaussImg );
  156. offset = 0;
  157. for ( int y = 0; y < ysize; y++ )
  158. {
  159. for ( int x = 0; x < xsize; x++, offset++ )
  160. {
  161. preMap.data[pos][offset]=gaussImg.getPixel(x,y);
  162. }
  163. }
  164. }
  165. }
  166. int scalesize = scales.size();
  167. // Zusammenfassen und auswerten
  168. #pragma omp parallel for
  169. for ( int x = 0; x < xsize; x++ )
  170. {
  171. for ( int y = 0; y < ysize; y++ )
  172. {
  173. for ( int j = 0 ; j < ( int ) probabilities.numChannels; j++ )
  174. {
  175. double prob = 0.0;
  176. for ( int s = 0; s < ( int ) scalesize; s++ )
  177. {
  178. prob+=preMap.get ( x,y,j+s*nbclasses );
  179. }
  180. double val = prob / ( double ) ( scalesize );
  181. probabilities.set ( x,y,val, j );
  182. }
  183. }
  184. }
  185. // Wahrscheinlichkeiten für Regionen bestimmen
  186. for ( int x = 0; x < xsize; x++ )
  187. {
  188. for ( int y = 0; y < ysize; y++ )
  189. {
  190. for ( int j = 0 ; j < ( int ) probabilities.numChannels; j++ )
  191. {
  192. int r = mask(x,y);
  193. double val = probabilities.get ( x,y,j );
  194. outfeats[r][j] += val;
  195. }
  196. }
  197. }
  198. #pragma omp parallel for
  199. for(int i = 0; i < (int)outfeats.size(); i++)
  200. {
  201. double sum = 0.0;
  202. for(int j = 0; j < (int)outfeats[i].size(); j++)
  203. {
  204. sum += outfeats[i][j];
  205. }
  206. for(int j = 0; j < (int)outfeats[i].size(); j++)
  207. {
  208. outfeats[i][j]/=sum;
  209. }
  210. }
  211. #undef VISSEMSEG
  212. #ifdef VISSEMSEG
  213. // showImage(img);
  214. for ( int j = 0 ; j < ( int ) probabilities.numChannels; j++ )
  215. {
  216. cout << "klasse: " << j << endl;//" " << cn.text ( j ) << endl;
  217. NICE::Matrix tmp ( probabilities.ysize, probabilities.xsize );
  218. double maxval = 0.0;
  219. for ( int y = 0; y < probabilities.ysize; y++ )
  220. for ( int x = 0; x < probabilities.xsize; x++ )
  221. {
  222. double val = probabilities.get ( x,y,j );
  223. tmp(y, x) = val;
  224. maxval = std::max ( val, maxval );
  225. }
  226. NICE::ColorImage imgrgb (probabilities.xsize, probabilities.ysize);
  227. ICETools::convertToRGB ( tmp, imgrgb );
  228. cout << "maxval = " << maxval << " for class " << j << endl; //cn.text ( j ) << endl;
  229. imgrgb.write("out.ppm");
  230. showImage(imgrgb, "Ergebnis");
  231. }
  232. #endif
  233. }
  234. void RFCsurka::getPositions(const RegionGraph &rg, const NICE::Matrix &mask, VVector &positions)
  235. {
  236. vector<int> featinreg(rg.size(), 0);
  237. int x0 = grid/2;
  238. for(int y = 0; y < (int)mask.cols(); y+=grid)
  239. {
  240. for(int x = x0; x < (int)mask.rows(); x+=grid)
  241. {
  242. int r = (int)mask(x,y);
  243. for(int s = 0; s < (int)scales.size(); s++)
  244. {
  245. featinreg[r]++;
  246. Vector vec(3);
  247. vec[0] = x;
  248. vec[1] = y;
  249. vec[2] = scales[s];
  250. positions.push_back(vec);
  251. }
  252. }
  253. if(x0 == 0)
  254. {
  255. x0 = grid/2;
  256. }
  257. else
  258. {
  259. x0 = 0;
  260. }
  261. }
  262. for(int i = 0; i < (int)featinreg.size(); i++)
  263. {
  264. if(featinreg[i] == 0)
  265. {
  266. int x, y;
  267. rg[i]->getCentroid(x,y);
  268. for(int s = 0; s < (int)scales.size(); s++)
  269. {
  270. Vector vec(3);
  271. vec[0] = x;
  272. vec[1] = y;
  273. vec[2] = scales[s];
  274. positions.push_back(vec);
  275. //FIXME: Achtung es kann in seltenen Fällen vorkommen, dass der Mittelpunkt einer kleinen Region nicht zur Region gehört -> abfangen
  276. }
  277. }
  278. }
  279. }