toyExampleUnsupervisedGP.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. int numClasses = 3;
  78. NICE::Image img (xsize, ysize);
  79. img.set(255);
  80. NICE::Image mark (img);
  81. mark.set(0);
  82. VVector train;
  83. Vector labels;
  84. std::string trainsetcache = conf.gS("main", "trainset", "");
  85. bool readtrainset = conf.gB("main", "readtrainset", false);
  86. if ( readtrainset && (trainsetcache.size() > 0 ) )
  87. {
  88. ifstream ifs ( trainsetcache.c_str(), ios::in );
  89. if ( !ifs.good() )
  90. fthrow(IOException, "Unable to read training data from " << trainsetcache << "." );
  91. ifs >> labels;
  92. train.restore ( ifs, VVector::FILEFORMAT_LINE );
  93. ifs.close ();
  94. cerr << "Labels: " << labels.size() << " // Examples " << train.size() << endl;
  95. }
  96. int k = 0;
  97. for ( VVector::const_iterator i = train.begin();
  98. i != train.end(); i++,k++ )
  99. {
  100. double classno = labels[k];
  101. const Vector & x = *i;
  102. Cross cross ( Coord( (int)(x[0]*mark.width()), (int)(x[1]*mark.height()) ), 10 );
  103. if ( classno < 0 )
  104. mark.draw ( cross, 1 );
  105. else if ( classno > 0 )
  106. mark.draw ( cross, 2 );
  107. else
  108. mark.draw ( cross, 3 );
  109. }
  110. bool selectManually = conf.gB("main", "select", true);
  111. if ( selectManually )
  112. {
  113. #ifdef NOVISUAL
  114. fprintf (stderr, "toyExample: visual manual selection needs ICE visualization\n");
  115. #else
  116. selectTrainingSet ( train, labels, img );
  117. #endif
  118. }
  119. bool writetrainset = conf.gB("main", "writetrainset", false);
  120. if ( writetrainset && (trainsetcache.size() > 0) )
  121. {
  122. ofstream ofs ( trainsetcache.c_str(), ios::out );
  123. if ( !ofs.good() )
  124. fthrow(IOException, "Unable to write training data to " << trainsetcache << "." );
  125. ofs << labels;
  126. ofs << endl;
  127. train.store ( ofs, VVector::FILEFORMAT_LINE );
  128. ofs.close ();
  129. }
  130. if ( train.size() <= 0 )
  131. {
  132. fthrow(Exception, "Size of the training set is zero!");
  133. }
  134. // do something
  135. KernelRBF kernelFunction ( conf.gD("main", "loggamma", 0.0) );
  136. RegressionAlgorithm *regression = new RegGaussianProcess ( &conf, &kernelFunction );
  137. cerr << labels << endl;
  138. regression->teach ( train, labels );
  139. NICE::FloatImage imgd (img.width(), img.height());
  140. NICE::Image imgclassno (img);
  141. for ( int y = 0 ; y < img.height(); y++ )
  142. for ( int x = 0 ; x < img.width(); x++ )
  143. {
  144. NICE::Vector example ( 2 );
  145. example[0] = x / (double)img.width();
  146. example[1] = y / (double)img.height();
  147. double value = regression->predict ( example );
  148. imgd.setPixel(x,y, 1.0 / ( 1.0 + exp(-value) ));
  149. imgclassno.setPixel(x,y, ( value < 0 ) ? 1 : 2);
  150. }
  151. markBoundary ( imgclassno, mark );
  152. Image imgScore ( img.width(), img.height() );
  153. floatToGrayScaled ( imgd, &imgScore );
  154. #ifndef NOVISUAL
  155. showImageOverlay ( img, mark );
  156. showImageOverlay ( imgScore, mark );
  157. showImageOverlay ( imgclassno, imgclassno );
  158. #endif
  159. return 0;
  160. }