RelativeLocationPrior.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. #include "RelativeLocationPrior.h"
  2. #include "core/image/FilterT.h"
  3. using namespace std;
  4. using namespace NICE;
  5. using namespace OBJREC;
  6. RelativeLocationPrior::RelativeLocationPrior()
  7. {
  8. conf = new Config();
  9. mapsize = 200;
  10. }
  11. RelativeLocationPrior::RelativeLocationPrior ( const Config *_conf ) : conf ( _conf )
  12. {
  13. }
  14. void RelativeLocationPrior::setClassNo ( int _classno )
  15. {
  16. classno = _classno;
  17. Init();
  18. }
  19. void RelativeLocationPrior::Init()
  20. {
  21. std::string section = "PostProcessRLP";
  22. mapsize = conf->gI ( section, "mapsize", 200 );
  23. featdim = classno * 3;
  24. //Priorsmaps erzeugen
  25. for ( int i = 0; i < classno; i++ )
  26. {
  27. NICE::MultiChannelImageT<double> *tmp = new NICE::MultiChannelImageT<double> ( mapsize, mapsize, classno);
  28. tmp->setAll ( 0.0 );
  29. priormaps.push_back ( tmp );
  30. }
  31. }
  32. RelativeLocationPrior::~RelativeLocationPrior()
  33. {
  34. for ( int i = 0; i < classno; i++ )
  35. {
  36. delete priormaps[i];
  37. }
  38. }
  39. void RelativeLocationPrior::trainPriorsMaps ( Examples &regions, int xsize, int ysize )
  40. {
  41. for ( int j = 0; j < ( int ) regions.size(); j++ )
  42. {
  43. for ( int i = 0; i < ( int ) regions.size(); i++ )
  44. {
  45. if ( i == j )
  46. continue;
  47. int x = regions[i].second.x - regions[j].second.x;
  48. int y = regions[i].second.y - regions[j].second.y;
  49. convertCoords ( x, xsize );
  50. convertCoords ( y, ysize );
  51. priormaps[regions[i].first]->set ( x, y, priormaps[regions[i].first]->get ( x, y, regions[j].first ) + 1.0/*regions[j].second.weight*/, regions[j].first );
  52. }
  53. }
  54. }
  55. void RelativeLocationPrior::finishPriorsMaps ( ClassNames &cn )
  56. {
  57. // Priormaps normalisieren
  58. double alpha = 5;
  59. for ( int i = 0; i < classno; i++ )
  60. {
  61. for ( int j = 0; j < classno; j++ )
  62. {
  63. double val = 0.0;
  64. for ( int x = 0; x < mapsize; x++ )
  65. {
  66. for ( int y = 0; y < mapsize; y++ )
  67. {
  68. val = std::max ( val, priormaps[i]->get ( x, y, j ) );
  69. }
  70. }
  71. if ( val != 0.0 )
  72. {
  73. for ( int x = 0; x < mapsize; x++ )
  74. {
  75. for ( int y = 0; y < mapsize; y++ )
  76. {
  77. double old = priormaps[i]->get ( x, y, j );
  78. #undef DIRICHLET
  79. #ifdef DIRICHLET
  80. old = ( old + alpha ) / ( val + classno * alpha );
  81. #else
  82. old /= val;
  83. #endif
  84. priormaps[i]->set ( x, y, old, j );
  85. }
  86. }
  87. }
  88. }
  89. }
  90. double sigma = 0.1 * ( double ) mapsize; // 10 percent of the maps height/width
  91. // Smoothing the maps
  92. for ( int j = 0; j < classno; j++ )
  93. {
  94. for ( int i = 0; i < classno; i++ )
  95. {
  96. NICE::FloatImage tmp ( mapsize, mapsize );
  97. tmp.set ( 0.0 );
  98. for ( int x = 0; x < mapsize; x++ )
  99. {
  100. for ( int y = 0; y < mapsize; y++ )
  101. {
  102. tmp.setPixelQuick ( x, y, priormaps[j]->get ( x, y, i ) );
  103. }
  104. }
  105. NICE::FloatImage out;
  106. NICE::FilterT<float,float,float> filter;
  107. filter.filterGaussSigmaApproximate( tmp, sigma, &out );
  108. for ( int x = 0; x < mapsize; x++ )
  109. {
  110. for ( int y = 0; y < mapsize; y++ )
  111. {
  112. priormaps[j]->set ( x, y, out.getPixel ( x, y ), i );
  113. }
  114. }
  115. }
  116. }
  117. // Sum of all pixels over all classes at a certain position equals 1
  118. for ( int i = 0; i < classno; i++ )
  119. {
  120. for ( int x = 0; x < mapsize; x++ )
  121. {
  122. for ( int y = 0; y < mapsize; y++ )
  123. {
  124. double val = 0.0;
  125. for ( int j = 0; j < classno; j++ )
  126. {
  127. val += priormaps[i]->get ( x, y, j );
  128. }
  129. if ( val != 0.0 )
  130. {
  131. for ( int j = 0; j < classno; j++ )
  132. {
  133. double old = priormaps[i]->get ( x, y, j );
  134. old /= val;
  135. priormaps[i]->set ( x, y, old, j );
  136. }
  137. }
  138. }
  139. }
  140. }
  141. #undef VISDEBUG
  142. #ifdef VISDEBUG
  143. #ifndef NOVISUAL
  144. NICE::ColorImage rgbim ( ( classno - 1 ) * ( mapsize + 10 ), ( classno - 1 ) * ( mapsize + 10 ) );
  145. double maxval = -numeric_limits<double>::max();
  146. double minval = numeric_limits<double>::max();
  147. for ( int j = 0; j < classno; j++ )
  148. {
  149. if ( j == 6 ) continue;
  150. for ( int i = 0; i < classno; i++ )
  151. {
  152. if ( i == 6 ) continue;
  153. for ( int x = 0; x < mapsize; x++ )
  154. {
  155. for ( int y = 0; y < mapsize; y++ )
  156. {
  157. double val = priormaps[j]->get ( x, y, i );
  158. maxval = std::max ( val, maxval );
  159. minval = std::min ( val, minval );
  160. }
  161. }
  162. }
  163. }
  164. int jcounter = 0;
  165. for ( int j = 0; j < classno; j++ )
  166. {
  167. if ( j == 6 ) continue;
  168. int icounter = 0;
  169. for ( int i = 0; i < classno; i++ )
  170. {
  171. if ( i == 6 ) continue;
  172. NICE::FloatImage tmp ( mapsize, mapsize );
  173. tmp.set ( 0.0 );
  174. for ( int x = 0; x < mapsize; x++ )
  175. {
  176. for ( int y = 0; y < mapsize; y++ )
  177. {
  178. tmp.setPixel ( x, y, priormaps[j]->get ( x, y, i ) );
  179. }
  180. }
  181. tmp.setPixel ( 0, 0, maxval );
  182. tmp.setPixel ( 0, 1, minval );
  183. cout << "i: " << cn.text ( i ) << endl;
  184. NICE::ColorImage imgrgb2 ( mapsize, mapsize );
  185. ICETools::convertToRGB ( tmp, imgrgb2 );
  186. imgrgb2.setPixel ( 0, 0, 2, imgrgb2.getPixel ( 1, 0, 2 ) );
  187. imgrgb2.setPixel ( 0, 1, 2, imgrgb2.getPixel ( 1, 1, 2 ) );
  188. imgrgb2.setPixel ( 0, 0, 0, imgrgb2.getPixel ( 1, 0, 0 ) );
  189. imgrgb2.setPixel ( 0, 1, 0, imgrgb2.getPixel ( 1, 1, 0 ) );
  190. imgrgb2.setPixel ( 0, 0, 1, imgrgb2.getPixel ( 1, 0, 1 ) );
  191. imgrgb2.setPixel ( 0, 1, 1, imgrgb2.getPixel ( 1, 1, 1 ) );
  192. for ( int y = 0; y < mapsize; y++ )
  193. {
  194. for ( int x = 0; x < mapsize; x++ )
  195. {
  196. rgbim.setPixel ( x + jcounter* ( mapsize + 10 ), y + icounter* ( mapsize + 10 ), 2, imgrgb2.getPixel ( x, y, 2 ) );
  197. rgbim.setPixel ( x + jcounter* ( mapsize + 10 ), y + icounter* ( mapsize + 10 ), 0, imgrgb2.getPixel ( x, y, 0 ) );
  198. rgbim.setPixel ( x + jcounter* ( mapsize + 10 ), y + icounter* ( mapsize + 10 ), 1, imgrgb2.getPixel ( x, y, 1 ) );
  199. }
  200. }
  201. icounter++;
  202. }
  203. jcounter++;
  204. }
  205. rgbim.write ( "tmp.ppm" );
  206. #endif
  207. #endif
  208. }
  209. void RelativeLocationPrior::trainClassifier ( Examples &regions, NICE::MultiChannelImageT<double> & probabilities )
  210. {
  211. // Creating a feature vector for all regions and adding it to the training set
  212. getFeature ( regions, probabilities );
  213. for ( int i = 0; i < ( int ) regions.size(); i++ )
  214. {
  215. trainingsdata.push_back ( pair<int, Example> ( regions[i].first, regions[i].second ) );
  216. regions[i].second.svec = NULL;
  217. }
  218. }
  219. void RelativeLocationPrior::finishClassifier()
  220. {
  221. //////////////////////
  222. // Train Classifier //
  223. //////////////////////
  224. FeaturePool fp;
  225. Feature *f = new SparseVectorFeature ( featdim );
  226. f->explode ( fp );
  227. delete f;
  228. //feature size
  229. int s = 3;
  230. classifiers.resize ( classno );
  231. for ( int i = 0; i < classno; i++ )
  232. {
  233. classifiers[i] = SLR ( conf, "ClassifierSMLR" );
  234. Examples ex2;
  235. int countex = 0;
  236. for ( int j = 0; j < ( int ) trainingsdata.size(); j++ )
  237. {
  238. Example e;
  239. int z = 0;
  240. e.svec = new SparseVector ( s + 1 );
  241. for ( int k = i * s; k < i*s + s; k++, z++ )
  242. {
  243. double val = trainingsdata[j].second.svec->get ( k );
  244. if ( val != 0.0 )
  245. ( *e.svec ) [z] = val;
  246. }
  247. ( *e.svec ) [s] = 1.0;
  248. ex2.push_back ( pair<int, Example> ( trainingsdata[j].first, e ) );
  249. if ( trainingsdata[j].first == i )
  250. countex++;
  251. }
  252. if ( ex2.size() <= 2 || countex < 1 )
  253. continue;
  254. classifiers[i].train ( fp, ex2, i );
  255. for ( int j = 0; j < ( int ) ex2.size(); j++ )
  256. {
  257. delete ex2[j].second.svec;
  258. ex2[j].second.svec = NULL;
  259. }
  260. }
  261. trainingsdata.clear();
  262. }
  263. void RelativeLocationPrior::postprocess ( Examples &regions, NICE::MultiChannelImageT<double> & probabilities )
  264. {
  265. getFeature ( regions, probabilities );
  266. int s = 3;
  267. for ( int i = 0; i < ( int ) regions.size(); i++ )
  268. {
  269. FullVector overall_distribution ( classno + 1 );
  270. overall_distribution[classno] = 0.0;
  271. double maxp = -numeric_limits<double>::max();
  272. int bestclass = 0;
  273. double sum = 0.0;
  274. for ( int c = 0; c < classno; c++ )
  275. {
  276. Example e;
  277. int z = 0;
  278. e.svec = new SparseVector ( s + 1 );
  279. for ( int k = c * s; k < c*s + s; k++, z++ )
  280. {
  281. double val = regions[i].second.svec->get ( k );
  282. if ( val != 0.0 )
  283. ( *e.svec ) [z] = val;
  284. }
  285. ( *e.svec ) [s] = 1.0;
  286. overall_distribution[c] = classifiers[c].classify ( e );
  287. sum += overall_distribution[c];
  288. if ( maxp < overall_distribution[c] )
  289. {
  290. bestclass = c;
  291. maxp = overall_distribution[c];
  292. }
  293. delete e.svec;
  294. e.svec = NULL;
  295. }
  296. for ( int c = 0; c < classno; c++ )
  297. {
  298. overall_distribution[c] /= sum;
  299. }
  300. ClassificationResult r = ClassificationResult ( bestclass, overall_distribution );
  301. if ( bestclass < 0 )
  302. {
  303. regions[i].second.svec->store ( cout );
  304. cout << endl;
  305. cout << "fehler: besclass=" << bestclass << endl;
  306. for ( int j = 0; j < ( int ) probabilities.channels(); j++ )
  307. {
  308. cout << "j: " << j << " score: " << r.scores[j] << endl;
  309. }
  310. }
  311. regions[i].first = bestclass;
  312. }
  313. }
  314. void RelativeLocationPrior::convertCoords ( int &x, int xsize )
  315. {
  316. x = ( int ) round ( ( double ( x ) + ( double ) xsize ) / ( 2.0 * ( double ) xsize ) * ( ( double ) mapsize - 1.0 ) );
  317. x = std::min ( x, mapsize - 1 );
  318. x = std::max ( x, 0 );
  319. }
  320. void RelativeLocationPrior::getFeature ( Examples &regions, NICE::MultiChannelImageT<double> & probabilities )
  321. {
  322. int xsize, ysize;
  323. xsize = probabilities.width();
  324. ysize = probabilities.height();
  325. // get best classes
  326. vector<int> bestclasses ( regions.size(), -1 );
  327. for ( int r = 0; r < ( int ) regions.size(); r++ )
  328. {
  329. double maxval = -numeric_limits<double>::max();
  330. for ( int c = 0; c < ( int ) probabilities.channels(); c++ )
  331. {
  332. double val = probabilities.get ( regions[r].second.x, regions[r].second.y, c );
  333. if ( maxval < val )
  334. {
  335. bestclasses[r] = c;
  336. maxval = val;
  337. }
  338. }
  339. }
  340. vector<double> alpha;
  341. for ( int r = 0; r < ( int ) regions.size(); r++ )
  342. {
  343. double tmpalpha = probabilities.get ( regions[r].second.x, regions[r].second.y, bestclasses[r] ) * regions[r].second.weight;
  344. alpha.push_back ( tmpalpha );
  345. }
  346. // create f_relloc
  347. vector<vector<double> > vother;
  348. vector<vector<double> > vself;
  349. for ( int i = 0; i < ( int ) regions.size(); i++ )
  350. {
  351. vector<double> v, w;
  352. vother.push_back ( v );
  353. vself.push_back ( w );
  354. for ( int c = 0; c < classno; c++ )
  355. {
  356. double tmp_vother = 0.0;
  357. double tmp_self = 0.0;
  358. for ( int j = 0; j < ( int ) regions.size(); j++ )
  359. {
  360. if ( j == i )
  361. continue;
  362. int x = regions[i].second.x - regions[j].second.x;
  363. int y = regions[i].second.y - regions[j].second.y;
  364. convertCoords ( x, xsize );
  365. convertCoords ( y, ysize );
  366. double val = priormaps[c]->get ( x, y, bestclasses[j] ) * alpha[j]; ;
  367. if ( bestclasses[j] == bestclasses[i] ) //parts of the object
  368. {
  369. tmp_self += val;
  370. }
  371. else//Kontextinformationen
  372. {
  373. tmp_vother += val;
  374. }
  375. }
  376. if ( fabs ( tmp_self ) < 10e-7 )
  377. tmp_self = 10e-7;
  378. if ( fabs ( tmp_vother ) < 10e-7 )
  379. tmp_vother = 10e-7;
  380. vother[i].push_back ( tmp_vother );
  381. vself[i].push_back ( tmp_self );
  382. }
  383. }
  384. for ( int r = 0; r < ( int ) regions.size(); r++ )
  385. {
  386. if ( regions[r].second.svec != NULL )
  387. {
  388. delete regions[r].second.svec;
  389. regions[r].second.svec = NULL;
  390. }
  391. if ( regions[r].second.vec != NULL )
  392. {
  393. delete regions[r].second.vec;
  394. regions[r].second.vec = NULL;
  395. }
  396. regions[r].second.svec = new SparseVector ( classno*3 );
  397. int counter = 0;
  398. for ( int i = 0; i < classno; i++ )
  399. {
  400. //appearence feature (old probability for each class)
  401. double fapp = log ( probabilities.get ( regions[r].second.x, regions[r].second.y, i ) );
  402. if ( fabs ( fapp ) > 10e-7 )
  403. ( * ( regions[r].second.svec ) ) [counter] = fapp;
  404. counter++;
  405. double val = log ( vother[r][i] );
  406. if ( fabs ( val ) > 10e-7 )
  407. ( * ( regions[r].second.svec ) ) [counter] = val;
  408. counter++;
  409. val = log ( vself[r][i] );
  410. if ( fabs ( val ) > 10e-7 )
  411. ( * ( regions[r].second.svec ) ) [counter] = val;
  412. counter++;
  413. }
  414. }
  415. }
  416. void RelativeLocationPrior::restore ( istream & is, int format )
  417. {
  418. is >> classno;
  419. is >> mapsize;
  420. is >> featdim;
  421. // Create prior maps
  422. for ( int i = 0; i < classno; i++ )
  423. {
  424. NICE::MultiChannelImageT<double> *tmp = new NICE::MultiChannelImageT<double> ( mapsize, mapsize, classno);
  425. tmp->setAll ( 0.0 );
  426. priormaps.push_back ( tmp );
  427. }
  428. double val;
  429. for ( int i = 0; i < classno; i++ )
  430. {
  431. for ( int j = 0; j < classno; j++ )
  432. {
  433. for ( int x = 0; x < mapsize; x++ )
  434. {
  435. for ( int y = 0; y < mapsize; y++ )
  436. {
  437. is >> val;
  438. priormaps[i]->set ( x, y, val, j );
  439. }
  440. }
  441. }
  442. }
  443. classifiers.resize ( classno );
  444. for ( int i = 0; i < classno; i++ )
  445. {
  446. classifiers[i] = SLR();
  447. classifiers[i].restore ( is, format );
  448. }
  449. }
  450. void RelativeLocationPrior::store ( ostream & os, int format ) const
  451. {
  452. os << classno << " ";
  453. os << mapsize << " ";
  454. os << featdim << endl;
  455. for ( int i = 0; i < classno; i++ )
  456. {
  457. for ( int j = 0; j < classno; j++ )
  458. {
  459. for ( int x = 0; x < mapsize; x++ )
  460. {
  461. for ( int y = 0; y < mapsize; y++ )
  462. {
  463. os << priormaps[i]->get ( x, y, j ) << " ";
  464. }
  465. }
  466. }
  467. }
  468. for ( int i = 0; i < classno; i++ )
  469. {
  470. classifiers[i].store ( os, format );
  471. }
  472. }
  473. void RelativeLocationPrior::clear ()
  474. {
  475. }