SemSegNovelty.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * @file SemSegNovelty.h
  3. * @brief semantic segmentation using the method from Csurka08
  4. * @author Björn Fröhlich, Alexander Freytag
  5. * @date 04/24/2009
  6. */
  7. #ifndef SemSegNoveltyINCLUDE
  8. #define SemSegNoveltyINCLUDE
  9. #include "SemanticSegmentation.h"
  10. #include "SemSegTools.h"
  11. #include "vislearning/classifier/classifierbase/FeaturePoolClassifier.h"
  12. #include "vislearning/classifier/genericClassifierSelection.h"
  13. #include "vislearning/features/localfeatures/LFColorWeijer.h"
  14. #include "segmentation/RegionSegmentationMethod.h"
  15. /** @brief pixelwise labeling systems */
  16. namespace OBJREC {
  17. class SemSegNovelty : public SemanticSegmentation
  18. {
  19. protected:
  20. //! boolean whether to reuse segmentation results for single images in different runs
  21. bool reuseSegmentation;
  22. //! boolean whether to read the initial classifier from a file. If not, training will be performed
  23. bool read_classifier;
  24. //! boolean whether to save the final classifier or not
  25. bool save_classifier;
  26. //! The cached Data
  27. std::string cache;
  28. //! Classifier
  29. FeaturePoolClassifier *classifier;
  30. VecClassifier *vclassifier;
  31. //! feature extraction
  32. LFColorWeijer *featExtract;
  33. //! Configuration File
  34. const NICE::Config *conf;
  35. //! distance between features for training
  36. int trainWsize;
  37. //! half of the window size for local features
  38. int whs;
  39. //! rectangle size for classification, 1 means pixelwise
  40. int testWSize;
  41. //! name of all classes
  42. ClassNames cn;
  43. //! low level Segmentation method
  44. RegionSegmentationMethod *regionSeg;
  45. //! set of forbidden/background classes for the initial training
  46. std::set<int> forbidden_classesTrain;
  47. //! set of forbidden/background classes for the whole process of learning over time
  48. std::set<int> forbidden_classesActiveLearning;
  49. //! store the class numbers currently used
  50. std::set<int> classesInUse;
  51. //! obviously, the number of classes used for training (i.e., classesInUse.size() )
  52. int numberOfClasses;
  53. //! where to save the resulting images (uncertainty and classification results)
  54. std::string resultdir;
  55. //! find the maximum uncertainty or not within the whole test set
  56. bool findMaximumUncert;
  57. //! image with most uncertain region
  58. NICE::ColorImage maskedImg;
  59. //! for debugging and visualization: show novelty images with and without region segmentation and the most novel region
  60. bool visualizeALimages;
  61. //! maximum uncertainty over all images, i.e., the novelty score of the most "novel" region of all test images
  62. double globalMaxUncert;
  63. //! determine whether a "novelty" method computes large scores for novel objects (e.g., variance), or small scores (e.g., min abs mean)
  64. bool mostNoveltyWithMaxScores;
  65. //! current examples for most uncertain region
  66. Examples newTrainExamples;
  67. //! contains filenames of images and indices of contained regions, that where already queried, to prevent them from being queried again
  68. std::map<std::string,std::set<int> > queriedRegions;
  69. std::pair<std::string, int> currentRegionToQuery;
  70. enum NoveltyMethod{
  71. GPVARIANCE, // novel = large variance
  72. GPUNCERTAINTY, //novel = small uncertainty (mean / var)
  73. GPMINMEAN, //novel = small mean
  74. GPMEANRATIO, //novel = small difference between mean of most plausible class and mean of snd
  75. // most plausible class (not useful in binary settings)
  76. GPWEIGHTALL, // novel = large weight in alpha vector after updating the model (can be predicted exactly)
  77. GPWEIGHTRATIO, // novel = small difference between weights for alpha vectors with assumptions of GT label to be the most
  78. // plausible against the second most plausible class
  79. RANDOM // query regions randomly
  80. };
  81. //! specify how "novelty" shall be computed, e.g., using GP-variance, GP-uncertainty, or predicted weight entries
  82. NoveltyMethod noveltyMethod;
  83. std::string noveltyMethodString;
  84. //! just store the name of our classifier
  85. std::string classifierString;
  86. inline void computeClassificationResults( const NICE::MultiChannelImageT<double> & feats,
  87. NICE::Image & segresult,
  88. NICE::MultiChannelImageT<double> & probabilities,
  89. const int & xsize,
  90. const int & ysize,
  91. const int & featdim );
  92. void computeNoveltyByRandom( NICE::FloatImage & noveltyImage,
  93. const NICE::MultiChannelImageT<double> & feats,
  94. NICE::Image & segresult,
  95. NICE::MultiChannelImageT<double> & probabilities,
  96. const int & xsize, const int & ysize, const int & featdim );
  97. void computeNoveltyByVariance( NICE::FloatImage & noveltyImage,
  98. const NICE::MultiChannelImageT<double> & feats,
  99. NICE::Image & segresult,
  100. NICE::MultiChannelImageT<double> & probabilities,
  101. const int & xsize, const int & ysize, const int & featdim );
  102. void computeNoveltyByGPUncertainty ( NICE::FloatImage & noveltyImage,
  103. const NICE::MultiChannelImageT<double> & feats,
  104. NICE::Image & segresult,
  105. NICE::MultiChannelImageT<double> & probabilities,
  106. const int & xsize, const int & ysize, const int & featdim );
  107. void computeNoveltyByGPMean ( NICE::FloatImage & noveltyImage,
  108. const NICE::MultiChannelImageT<double> & feats,
  109. NICE::Image & segresult,
  110. NICE::MultiChannelImageT<double> & probabilities,
  111. const int & xsize, const int & ysize, const int & featdim );
  112. void computeNoveltyByGPMeanRatio ( NICE::FloatImage & noveltyImage,
  113. const NICE::MultiChannelImageT<double> & feats,
  114. NICE::Image & segresult,
  115. NICE::MultiChannelImageT<double> & probabilities,
  116. const int & xsize, const int & ysize, const int & featdim );
  117. void computeNoveltyByGPWeightAll ( NICE::FloatImage & noveltyImage,
  118. const NICE::MultiChannelImageT<double> & feats,
  119. NICE::Image & segresult,
  120. NICE::MultiChannelImageT<double> & probabilities,
  121. const int & xsize, const int & ysize, const int & featdim );
  122. void computeNoveltyByGPWeightRatio ( NICE::FloatImage & noveltyImage,
  123. const NICE::MultiChannelImageT<double> & feats,
  124. NICE::Image & segresult,
  125. NICE::MultiChannelImageT<double> & probabilities,
  126. const int & xsize, const int & ysize, const int & featdim );
  127. public:
  128. /** constructor
  129. * @param conf needs a configfile
  130. * @param md and a MultiDataset (contains images and other things)
  131. */
  132. SemSegNovelty ( const NICE::Config *conf, const MultiDataset *md );
  133. /** simple destructor */
  134. virtual ~SemSegNovelty();
  135. /** The trainingstep
  136. * @param md and a MultiDataset (contains images and other things)
  137. */
  138. void train ( const MultiDataset *md );
  139. /** The main procedure. Input: Image, Output: Segmented Image with pixelwise labeles and the probabilities
  140. * @param ce image data
  141. * @param segresult result of the semantic segmentation with a label for each pixel
  142. * @param probabilities multi-channel image with one channel for each class and corresponding probabilities for each pixel
  143. */
  144. void semanticseg ( CachedExample *ce,
  145. NICE::Image & segresult,
  146. NICE::MultiChannelImageT<double> & probabilities );
  147. /**
  148. * @brief visualize a specific region in the original image
  149. *
  150. * @param img input image
  151. * @param regions map of the regions
  152. * @param region visualize this region
  153. * @param outimage result
  154. * @return void
  155. **/
  156. void visualizeRegion(const NICE::ColorImage &img, const NICE::Matrix &regions, int region, NICE::ColorImage &outimage);
  157. /**
  158. * @brief Add a new example to the known training data
  159. *
  160. * @param newExample (NICE::Vector) the feature vector of the new examples
  161. * @param newClassNo (int) the corresponding GT class number
  162. * @return void
  163. **/
  164. void addNewExample(const NICE::Vector & newExample, const int & newClassNo);
  165. /**
  166. * @brief Add those examples, which belong to the most novel region seen so far
  167. *
  168. * @return void
  169. **/
  170. virtual void addNovelExamples();
  171. /**
  172. * @brief Get a pointer to the examples extracted from the most novel region seen so far
  173. *
  174. * @return Examples *
  175. **/
  176. virtual const Examples * getNovelExamples() const;
  177. };
  178. } //namespace
  179. #endif