toyExampleUnsupervisedGP.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @file toyExample.cpp
  3. * @brief just a toy tool
  4. * @author Erik Rodner
  5. * @date 04/07/2009
  6. */
  7. #include <iomanip>
  8. #include <core/imagedisplay/SimpleSelector.h>
  9. #include <core/image/CrossT.h>
  10. #include "core/basics/Config.h"
  11. #include "vislearning/baselib/ICETools.h"
  12. #include "vislearning/regression/gpregression/RegGaussianProcess.h"
  13. #include "vislearning/math/kernels/KernelRBF.h"
  14. #include "core/vector/VectorT.h"
  15. #include "core/vector/MatrixT.h"
  16. #include "core/image/ImageT.h"
  17. #include "core/imagedisplay/ImageDisplay.h"
  18. using namespace OBJREC;
  19. using namespace NICE;
  20. using namespace std;
  21. #ifndef NOVISUAL
  22. void selectTrainingSet ( VVector & train, Vector & labels, NICE::Image & img)
  23. {
  24. vector<int> colors;
  25. vector<CoordT<double> > points;
  26. NICE::selectColoredPoints ( img, points, colors, "Select some points!", 3 );
  27. int k = 0;
  28. for ( vector<CoordT<double> >::const_iterator i = points.begin();
  29. i != points.end(); i++,k++ )
  30. {
  31. NICE::Vector feature ( 2 );
  32. feature[0] = i->x / img.width();
  33. feature[1] = i->y / img.height();
  34. train.push_back ( feature );
  35. if ( colors[k] == 1 )
  36. colors[k] = -1;
  37. else if ( colors[k] == 2 )
  38. colors[k] = 1;
  39. else if ( colors[k] == 3 )
  40. colors[k] = 0;
  41. labels.append ( colors[k] );
  42. }
  43. }
  44. #endif
  45. void markBoundary ( const NICE::Image & imgclassno, NICE::Image & mark )
  46. {
  47. for ( int y = 0 ; y < imgclassno.height(); y++ )
  48. for ( int x = 0 ; x < imgclassno.width(); x++ )
  49. {
  50. int val = imgclassno.getPixel(x,y);
  51. bool boundary = false;
  52. for ( int i = -1 ; (i <= 1) && (!boundary) ; i++ )
  53. for ( int j = -1 ; (j <= 1) && (!boundary) ; j++ )
  54. {
  55. int xn = x + i;
  56. int yn = y + j;
  57. if ( (xn<0) || (yn<0) || (xn>=imgclassno.width()) || (yn>=imgclassno.height()) )
  58. continue;
  59. int valn = imgclassno.getPixel(xn,yn);
  60. if ( valn != val )
  61. boundary = true;
  62. }
  63. if ( boundary )
  64. mark.setPixel(x,y,1);
  65. }
  66. }
  67. /**
  68. just a toy tool
  69. */
  70. int main (int argc, char **argv)
  71. {
  72. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  73. Config conf ( argc, argv );
  74. conf.store(cout);
  75. int xsize = conf.gI("main", "xsize", 300 );
  76. int ysize = conf.gI("main", "ysize", 300 );
  77. NICE::Image img (xsize, ysize);
  78. img.set(255);
  79. NICE::Image mark (img);
  80. mark.set(0);
  81. VVector train;
  82. Vector labels;
  83. std::string trainsetcache = conf.gS("main", "trainset", "");
  84. bool readtrainset = conf.gB("main", "readtrainset", false);
  85. if ( readtrainset && (trainsetcache.size() > 0 ) )
  86. {
  87. ifstream ifs ( trainsetcache.c_str(), ios::in );
  88. if ( !ifs.good() )
  89. fthrow(IOException, "Unable to read training data from " << trainsetcache << "." );
  90. ifs >> labels;
  91. train.restore ( ifs, VVector::FILEFORMAT_LINE );
  92. ifs.close ();
  93. cerr << "Labels: " << labels.size() << " // Examples " << train.size() << endl;
  94. }
  95. int k = 0;
  96. for ( VVector::const_iterator i = train.begin();
  97. i != train.end(); i++,k++ )
  98. {
  99. double classno = labels[k];
  100. const Vector & x = *i;
  101. Cross cross ( Coord( (int)(x[0]*mark.width()), (int)(x[1]*mark.height()) ), 10 );
  102. if ( classno < 0 )
  103. mark.draw ( cross, 1 );
  104. else if ( classno > 0 )
  105. mark.draw ( cross, 2 );
  106. else
  107. mark.draw ( cross, 3 );
  108. }
  109. bool selectManually = conf.gB("main", "select", true);
  110. if ( selectManually )
  111. {
  112. #ifdef NOVISUAL
  113. fprintf (stderr, "toyExample: visual manual selection needs ICE visualization\n");
  114. #else
  115. selectTrainingSet ( train, labels, img );
  116. #endif
  117. }
  118. bool writetrainset = conf.gB("main", "writetrainset", false);
  119. if ( writetrainset && (trainsetcache.size() > 0) )
  120. {
  121. ofstream ofs ( trainsetcache.c_str(), ios::out );
  122. if ( !ofs.good() )
  123. fthrow(IOException, "Unable to write training data to " << trainsetcache << "." );
  124. ofs << labels;
  125. ofs << endl;
  126. train.store ( ofs, VVector::FILEFORMAT_LINE );
  127. ofs.close ();
  128. }
  129. if ( train.size() <= 0 )
  130. {
  131. fthrow(Exception, "Size of the training set is zero!");
  132. }
  133. // do something
  134. KernelRBF kernelFunction ( conf.gD("main", "loggamma", 0.0) );
  135. RegressionAlgorithm *regression = new RegGaussianProcess ( &conf, &kernelFunction );
  136. cerr << labels << endl;
  137. regression->teach ( train, labels );
  138. NICE::FloatImage imgd (img.width(), img.height());
  139. NICE::Image imgclassno (img);
  140. for ( int y = 0 ; y < img.height(); y++ )
  141. for ( int x = 0 ; x < img.width(); x++ )
  142. {
  143. NICE::Vector example ( 2 );
  144. example[0] = x / (double)img.width();
  145. example[1] = y / (double)img.height();
  146. double value = regression->predict ( example );
  147. imgd.setPixel(x,y, 1.0 / ( 1.0 + exp(-value) ));
  148. imgclassno.setPixel(x,y, ( value < 0 ) ? 1 : 2);
  149. }
  150. markBoundary ( imgclassno, mark );
  151. Image imgScore ( img.width(), img.height() );
  152. floatToGrayScaled ( imgd, &imgScore );
  153. #ifndef NOVISUAL
  154. showImageOverlay ( img, mark );
  155. showImageOverlay ( imgScore, mark );
  156. showImageOverlay ( imgclassno, imgclassno );
  157. #endif
  158. return 0;
  159. }