toyExampleUnsupervisedGP.cpp 4.9 KB

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