SemSegNovelty.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. NICE::MultiChannelImageT<double> m_CurrentImageFeatures;
  99. ///////////////////////////////
  100. // SEGMENTATION STUFF //
  101. ///////////////////////////////
  102. //! just store the name of our segmentation method.
  103. // Theoretically redundant, but currently makes things easier for store and restore...
  104. std::string s_rsMethode;
  105. //! low level Segmentation method
  106. RegionSegmentationMethod *regionSeg;
  107. //! boolean whether to reuse segmentation results for single images in different runs
  108. bool reuseSegmentation;
  109. //! contains filenames of images and indices of contained regions, that where already queried, to prevent them from being queried again
  110. std::map<std::string,std::set<int> > queriedRegions;
  111. std::pair<std::string, int> currentRegionToQuery;
  112. ///////////////////////////////
  113. // protected methods
  114. ///////////////////////////////
  115. inline void computeClassificationResults( const NICE::MultiChannelImageT<double> & feats,
  116. NICE::ImageT<int> & segresult,
  117. NICE::MultiChannelImageT<double> & probabilities,
  118. const int & xsize,
  119. const int & ysize,
  120. const int & featdim );
  121. void computeNoveltyByRandom( NICE::FloatImage & noveltyImage,
  122. const NICE::MultiChannelImageT<double> & feats,
  123. NICE::ImageT<int> & segresult,
  124. NICE::MultiChannelImageT<double> & probabilities,
  125. const int & xsize, const int & ysize, const int & featdim );
  126. void computeNoveltyByVariance( NICE::FloatImage & noveltyImage,
  127. const NICE::MultiChannelImageT<double> & feats,
  128. NICE::ImageT<int> & segresult,
  129. NICE::MultiChannelImageT<double> & probabilities,
  130. const int & xsize, const int & ysize, const int & featdim );
  131. void computeNoveltyByGPUncertainty ( NICE::FloatImage & noveltyImage,
  132. const NICE::MultiChannelImageT<double> & feats,
  133. NICE::ImageT<int> & segresult,
  134. NICE::MultiChannelImageT<double> & probabilities,
  135. const int & xsize, const int & ysize, const int & featdim );
  136. void computeNoveltyByGPMean ( NICE::FloatImage & noveltyImage,
  137. const NICE::MultiChannelImageT<double> & feats,
  138. NICE::ImageT<int> & segresult,
  139. NICE::MultiChannelImageT<double> & probabilities,
  140. const int & xsize, const int & ysize, const int & featdim );
  141. void computeNoveltyByGPMeanRatio ( NICE::FloatImage & noveltyImage,
  142. const NICE::MultiChannelImageT<double> & feats,
  143. NICE::ImageT<int> & segresult,
  144. NICE::MultiChannelImageT<double> & probabilities,
  145. const int & xsize, const int & ysize, const int & featdim );
  146. void computeNoveltyByGPWeightAll ( NICE::FloatImage & noveltyImage,
  147. const NICE::MultiChannelImageT<double> & feats,
  148. NICE::ImageT<int> & segresult,
  149. NICE::MultiChannelImageT<double> & probabilities,
  150. const int & xsize, const int & ysize, const int & featdim );
  151. void computeNoveltyByGPWeightRatio ( NICE::FloatImage & noveltyImage,
  152. const NICE::MultiChannelImageT<double> & feats,
  153. NICE::ImageT<int> & segresult,
  154. NICE::MultiChannelImageT<double> & probabilities,
  155. const int & xsize, const int & ysize, const int & featdim );
  156. public:
  157. /**
  158. * @brief default constructor
  159. * @author Alexander Freytag
  160. * @date 06-02-2014 ( dd-mm-yyyy )
  161. */
  162. SemSegNovelty ( );
  163. /**
  164. * @brief recommended constructor
  165. * @author Alexander Freytag
  166. * @param conf needs a configfile
  167. * @param md and a MultiDataset (contains images and other things)
  168. */
  169. SemSegNovelty ( const NICE::Config *conf, const MultiDataset *md );
  170. /** simple destructor */
  171. virtual ~SemSegNovelty();
  172. void initFromConfig ( const NICE::Config * conf, const std::string _confSection = "SemSegNovelty" );
  173. /** The trainingstep
  174. * @param md and a MultiDataset (contains images and other things)
  175. */
  176. void train ( const MultiDataset *md );
  177. /** The main procedure. Input: Image, Output: Segmented Image with pixelwise labeles and the probabilities
  178. * @param ce image data
  179. * @param segresult result of the semantic segmentation with a label for each pixel
  180. * @param probabilities multi-channel image with one channel for each class and corresponding probabilities for each pixel
  181. */
  182. void semanticseg ( CachedExample *ce,
  183. NICE::ImageT<int> & segresult,
  184. NICE::MultiChannelImageT<double> & probabilities );
  185. void semanticseg ( CachedExample *ce,
  186. NICE::MultiChannelImageT<int> & segresult,
  187. NICE::MultiChannelImage3DT<double> & probabilities )
  188. {}
  189. /**
  190. * @brief visualize a specific region in the original image
  191. *
  192. * @param img input image
  193. * @param regions map of the regions
  194. * @param region visualize this region
  195. * @param outimage result
  196. * @return void
  197. **/
  198. void visualizeRegion(const NICE::ColorImage &img, const NICE::Matrix &regions, int region, NICE::ColorImage &outimage);
  199. /**
  200. * @brief Add a new example to the known training data
  201. *
  202. * @param newExample (NICE::Vector) the feature vector of the new examples
  203. * @param newClassNo (int) the corresponding GT class number
  204. * @return void
  205. **/
  206. void addNewExample(const NICE::Vector & newExample, const int & newClassNo);
  207. /**
  208. * @brief Add those examples, which belong to the most novel region seen so far
  209. *
  210. * @return void
  211. **/
  212. virtual void addNovelExamples();
  213. /**
  214. * @brief Get a pointer to the examples extracted from the most novel region seen so far
  215. *
  216. * @return Examples *
  217. **/
  218. virtual const Examples * getNovelExamples() const;
  219. ///////////////////// INTERFACE PERSISTENT /////////////////////
  220. // interface specific methods for store and restore
  221. ///////////////////// INTERFACE PERSISTENT /////////////////////
  222. /**
  223. * @brief Load active-segmentation-object from external file (stream)
  224. * @author Alexander Freytag
  225. */
  226. virtual void restore ( std::istream & is, int format = 0 );
  227. /**
  228. * @brief Save active-segmentation-object to external file (stream)
  229. * @author Alexander Freytag
  230. */
  231. virtual void store( std::ostream & os, int format = 0 ) const;
  232. /**
  233. * @brief Clear active-segmentation-object object
  234. * @author Alexander Freytag
  235. */
  236. virtual void clear ();
  237. };
  238. } //namespace
  239. #endif //_NICE_SEMSEGNOVELTYINCLUDE