SemSegNovelty.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 _NICE_SEMSEGNOVELTYINCLUDE
  8. #define _NICE_SEMSEGNOVELTYINCLUDE
  9. // nice-core includes
  10. #include <core/basics/Persistent.h>
  11. // nice-vislearning includes
  12. #include <vislearning/classifier/classifierbase/FeaturePoolClassifier.h>
  13. #include <vislearning/classifier/genericClassifierSelection.h>
  14. #include <vislearning/features/localfeatures/LocalFeatureColorWeijer.h>
  15. // nice-segmentation includes
  16. #include <segmentation/RegionSegmentationMethod.h>
  17. // nice-semseg includes
  18. #include "SemanticSegmentation.h"
  19. #include "SemSegTools.h"
  20. /** @brief pixelwise labeling systems */
  21. namespace OBJREC {
  22. class SemSegNovelty : public SemanticSegmentation
  23. {
  24. protected:
  25. /////////////////////////
  26. /////////////////////////
  27. // PROTECTED VARIABLES //
  28. /////////////////////////
  29. /////////////////////////
  30. ////////////////////////////////////////
  31. // variables only setable via configfile
  32. ////////////////////////////////////////
  33. ///////////////////////////////
  34. // FEATURE EXTRACTION //
  35. ///////////////////////////////
  36. //! feature extraction
  37. LocalFeatureColorWeijer *featExtract;
  38. //! distance between features for training
  39. int trainWsize;
  40. //! half of the window size for local features
  41. int whs;
  42. //! rectangle size for classification, 1 means pixelwise
  43. int testWSize;
  44. ///////////////////////////////
  45. // NOVELTY COMPUTATION //
  46. ///////////////////////////////
  47. enum NoveltyMethod{
  48. GPVARIANCE, // novel = large variance
  49. GPUNCERTAINTY, //novel = small uncertainty (mean / var)
  50. GPMINMEAN, //novel = small mean
  51. GPMEANRATIO, //novel = small difference between mean of most plausible class and mean of snd
  52. // most plausible class (not useful in binary settings)
  53. GPWEIGHTALL, // novel = large weight in alpha vector after updating the model (can be predicted exactly)
  54. GPWEIGHTRATIO, // novel = small difference between weights for alpha vectors with assumptions of GT label to be the most
  55. // plausible against the second most plausible class
  56. RANDOM // query regions randomly
  57. };
  58. //! specify how "novelty" shall be computed, e.g., using GP-variance, GP-uncertainty, or predicted weight entries
  59. NoveltyMethod noveltyMethod;
  60. std::string noveltyMethodString;
  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. //! find the maximum uncertainty or not within the whole test set
  66. bool findMaximumUncert;
  67. //! image with most uncertain region
  68. NICE::ColorImage maskedImg;
  69. //! for debugging and visualization: show novelty images with and without region segmentation and the most novel region
  70. bool b_visualizeALimages;
  71. ///////////////////////////////
  72. // CLASSIFICATION STUFF //
  73. ///////////////////////////////
  74. //! just store the name of our classifier
  75. // Theoretically redundant, but currently makes things easier for store and restore...
  76. std::string classifierString;
  77. //! Classifier
  78. FeaturePoolClassifier *classifier;
  79. VecClassifier *vclassifier;
  80. //! set of forbidden/background classes for the initial training
  81. std::set<int> forbidden_classesTrain;
  82. //! set of forbidden/background classes for the whole process of learning over time
  83. std::set<int> forbidden_classesActiveLearning;
  84. //! store the class numbers currently used
  85. std::set<int> classesInUse;
  86. //! obviously, the number of classes used for training (i.e., classesInUse.size() )
  87. int numberOfClasses;
  88. //! boolean whether to read the initial classifier from a file. If not, training will be performed
  89. bool read_classifier;
  90. //! boolean whether to save the final classifier or not
  91. bool save_classifier;
  92. //! The cached Data
  93. std::string cache;
  94. //! where to save the resulting images (uncertainty and classification results)
  95. std::string resultdir;
  96. //! current examples for most uncertain region
  97. Examples newTrainExamples;
  98. ///////////////////////////////
  99. // SEGMENTATION STUFF //
  100. ///////////////////////////////
  101. //! just store the name of our segmentation method.
  102. // Theoretically redundant, but currently makes things easier for store and restore...
  103. std::string s_rsMethode;
  104. //! low level Segmentation method
  105. RegionSegmentationMethod *regionSeg;
  106. //! boolean whether to reuse segmentation results for single images in different runs
  107. bool reuseSegmentation;
  108. //! contains filenames of images and indices of contained regions, that where already queried, to prevent them from being queried again
  109. std::map<std::string,std::set<int> > queriedRegions;
  110. std::pair<std::string, int> currentRegionToQuery;
  111. ///////////////////////////////
  112. // protected methods
  113. ///////////////////////////////
  114. inline void computeClassificationResults( const NICE::MultiChannelImageT<double> & feats,
  115. NICE::Image & segresult,
  116. NICE::MultiChannelImageT<double> & probabilities,
  117. const int & xsize,
  118. const int & ysize,
  119. const int & featdim );
  120. void computeNoveltyByRandom( NICE::FloatImage & noveltyImage,
  121. const NICE::MultiChannelImageT<double> & feats,
  122. NICE::Image & segresult,
  123. NICE::MultiChannelImageT<double> & probabilities,
  124. const int & xsize, const int & ysize, const int & featdim );
  125. void computeNoveltyByVariance( NICE::FloatImage & noveltyImage,
  126. const NICE::MultiChannelImageT<double> & feats,
  127. NICE::Image & segresult,
  128. NICE::MultiChannelImageT<double> & probabilities,
  129. const int & xsize, const int & ysize, const int & featdim );
  130. void computeNoveltyByGPUncertainty ( NICE::FloatImage & noveltyImage,
  131. const NICE::MultiChannelImageT<double> & feats,
  132. NICE::Image & segresult,
  133. NICE::MultiChannelImageT<double> & probabilities,
  134. const int & xsize, const int & ysize, const int & featdim );
  135. void computeNoveltyByGPMean ( NICE::FloatImage & noveltyImage,
  136. const NICE::MultiChannelImageT<double> & feats,
  137. NICE::Image & segresult,
  138. NICE::MultiChannelImageT<double> & probabilities,
  139. const int & xsize, const int & ysize, const int & featdim );
  140. void computeNoveltyByGPMeanRatio ( NICE::FloatImage & noveltyImage,
  141. const NICE::MultiChannelImageT<double> & feats,
  142. NICE::Image & segresult,
  143. NICE::MultiChannelImageT<double> & probabilities,
  144. const int & xsize, const int & ysize, const int & featdim );
  145. void computeNoveltyByGPWeightAll ( NICE::FloatImage & noveltyImage,
  146. const NICE::MultiChannelImageT<double> & feats,
  147. NICE::Image & segresult,
  148. NICE::MultiChannelImageT<double> & probabilities,
  149. const int & xsize, const int & ysize, const int & featdim );
  150. void computeNoveltyByGPWeightRatio ( NICE::FloatImage & noveltyImage,
  151. const NICE::MultiChannelImageT<double> & feats,
  152. NICE::Image & segresult,
  153. NICE::MultiChannelImageT<double> & probabilities,
  154. const int & xsize, const int & ysize, const int & featdim );
  155. public:
  156. /**
  157. * @brief default constructor
  158. * @author Alexander Freytag
  159. * @date 06-02-2014 ( dd-mm-yyyy )
  160. */
  161. SemSegNovelty ( );
  162. /**
  163. * @brief recommended constructor
  164. * @author Alexander Freytag
  165. * @param conf needs a configfile
  166. * @param md and a MultiDataset (contains images and other things)
  167. */
  168. SemSegNovelty ( const NICE::Config *conf, const MultiDataset *md );
  169. /** simple destructor */
  170. virtual ~SemSegNovelty();
  171. void initFromConfig ( const NICE::Config * conf, const std::string _confSection = "SemSegNovelty" );
  172. /** The trainingstep
  173. * @param md and a MultiDataset (contains images and other things)
  174. */
  175. void train ( const MultiDataset *md );
  176. /** The main procedure. Input: Image, Output: Segmented Image with pixelwise labeles and the probabilities
  177. * @param ce image data
  178. * @param segresult result of the semantic segmentation with a label for each pixel
  179. * @param probabilities multi-channel image with one channel for each class and corresponding probabilities for each pixel
  180. */
  181. void semanticseg ( CachedExample *ce,
  182. NICE::Image & segresult,
  183. NICE::MultiChannelImageT<double> & probabilities );
  184. /**
  185. * @brief visualize a specific region in the original image
  186. *
  187. * @param img input image
  188. * @param regions map of the regions
  189. * @param region visualize this region
  190. * @param outimage result
  191. * @return void
  192. **/
  193. void visualizeRegion(const NICE::ColorImage &img, const NICE::Matrix &regions, int region, NICE::ColorImage &outimage);
  194. /**
  195. * @brief Add a new example to the known training data
  196. *
  197. * @param newExample (NICE::Vector) the feature vector of the new examples
  198. * @param newClassNo (int) the corresponding GT class number
  199. * @return void
  200. **/
  201. void addNewExample(const NICE::Vector & newExample, const int & newClassNo);
  202. /**
  203. * @brief Add those examples, which belong to the most novel region seen so far
  204. *
  205. * @return void
  206. **/
  207. virtual void addNovelExamples();
  208. /**
  209. * @brief Get a pointer to the examples extracted from the most novel region seen so far
  210. *
  211. * @return Examples *
  212. **/
  213. virtual const Examples * getNovelExamples() const;
  214. ///////////////////// INTERFACE PERSISTENT /////////////////////
  215. // interface specific methods for store and restore
  216. ///////////////////// INTERFACE PERSISTENT /////////////////////
  217. /**
  218. * @brief Load active-segmentation-object from external file (stream)
  219. * @author Alexander Freytag
  220. */
  221. virtual void restore ( std::istream & is, int format = 0 );
  222. /**
  223. * @brief Save active-segmentation-object to external file (stream)
  224. * @author Alexander Freytag
  225. */
  226. virtual void store( std::ostream & os, int format = 0 ) const;
  227. /**
  228. * @brief Clear active-segmentation-object object
  229. * @author Alexander Freytag
  230. */
  231. virtual void clear ();
  232. };
  233. } //namespace
  234. #endif //_NICE_SEMSEGNOVELTYINCLUDE