Commit 82e47317d4465939c1c4f39a7aa283d4c174d703

Authored by Cédric Traizet
1 parent 0c0351e2

some cmments and documentation added

include/AutoencoderModel.h
@@ -27,7 +27,7 @@ public: @@ -27,7 +27,7 @@ public:
27 itkNewMacro(Self); 27 itkNewMacro(Self);
28 itkTypeMacro(AutoencoderModel, DimensionalityReductionModel); 28 itkTypeMacro(AutoencoderModel, DimensionalityReductionModel);
29 29
30 - unsigned int GetDimension() {return m_NumberOfHiddenNeurons;}; 30 + unsigned int GetDimension() {return m_NumberOfHiddenNeurons;}; // Override the Dimensionality Reduction model method, it is used in the dimensionality reduction filter to set the output image size
31 itkGetMacro(NumberOfHiddenNeurons,unsigned int); 31 itkGetMacro(NumberOfHiddenNeurons,unsigned int);
32 itkSetMacro(NumberOfHiddenNeurons,unsigned int); 32 itkSetMacro(NumberOfHiddenNeurons,unsigned int);
33 33
@@ -47,9 +47,7 @@ public: @@ -47,9 +47,7 @@ public:
47 void Load(const std::string & filename, const std::string & name="") ITK_OVERRIDE; 47 void Load(const std::string & filename, const std::string & name="") ITK_OVERRIDE;
48 48
49 void Train() ITK_OVERRIDE; 49 void Train() ITK_OVERRIDE;
50 - //void Dimensionality_reduction() {}; // Dimensionality reduction is done by DoPredict  
51 -  
52 - 50 +
53 protected: 51 protected:
54 AutoencoderModel(); 52 AutoencoderModel();
55 ~AutoencoderModel() ITK_OVERRIDE; 53 ~AutoencoderModel() ITK_OVERRIDE;
@@ -59,11 +57,14 @@ protected: @@ -59,11 +57,14 @@ protected:
59 57
60 private: 58 private:
61 59
  60 + /** Network attributes */
62 AutoencoderType m_net; 61 AutoencoderType m_net;
63 unsigned int m_NumberOfHiddenNeurons; 62 unsigned int m_NumberOfHiddenNeurons;
  63 +
  64 + /** Training parameters */
64 unsigned int m_NumberOfIterations; 65 unsigned int m_NumberOfIterations;
65 - double m_Regularization;  
66 - double m_Noise; 66 + double m_Regularization; // L2 Regularization parameter
  67 + double m_Noise; // probability for an input to be set to 0 (denosing autoencoder)
67 }; 68 };
68 } // end namespace otb 69 } // end namespace otb
69 70
include/AutoencoderModel.txx
@@ -6,12 +6,13 @@ @@ -6,12 +6,13 @@
6 #include <shark/Data/Dataset.h> 6 #include <shark/Data/Dataset.h>
7 #include "itkMacro.h" 7 #include "itkMacro.h"
8 #include "otbSharkUtils.h" 8 #include "otbSharkUtils.h"
  9 +
9 //include train function 10 //include train function
10 #include <shark/ObjectiveFunctions/ErrorFunction.h> 11 #include <shark/ObjectiveFunctions/ErrorFunction.h>
11 #include <shark/Algorithms/GradientDescent/Rprop.h>// the RProp optimization algorithm 12 #include <shark/Algorithms/GradientDescent/Rprop.h>// the RProp optimization algorithm
12 #include <shark/ObjectiveFunctions/Loss/SquaredLoss.h> // squared loss used for regression 13 #include <shark/ObjectiveFunctions/Loss/SquaredLoss.h> // squared loss used for regression
13 #include <shark/ObjectiveFunctions/Regularizer.h> //L2 regulariziation 14 #include <shark/ObjectiveFunctions/Regularizer.h> //L2 regulariziation
14 -#include <shark/Models/ImpulseNoiseModel.h>//noise source to corrupt the inputs 15 +#include <shark/Models/ImpulseNoiseModel.h> //noise source to corrupt the inputs
15 #include <shark/Models/ConcatenatedModel.h>//to concatenate the noise with the model 16 #include <shark/Models/ConcatenatedModel.h>//to concatenate the noise with the model
16 17
17 namespace otb 18 namespace otb
@@ -43,7 +44,7 @@ void AutoencoderModel&lt;TInputValue,AutoencoderType&gt;::Train() @@ -43,7 +44,7 @@ void AutoencoderModel&lt;TInputValue,AutoencoderType&gt;::Train()
43 std::size_t inputs = dataDimension(inputSamples); 44 std::size_t inputs = dataDimension(inputSamples);
44 m_net.setStructure(inputs, m_NumberOfHiddenNeurons); 45 m_net.setStructure(inputs, m_NumberOfHiddenNeurons);
45 initRandomUniform(m_net,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs)); 46 initRandomUniform(m_net,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs));
46 - shark::ImpulseNoiseModel noise(m_Noise,0.0);//set an input pixel with probability p to 0 47 + shark::ImpulseNoiseModel noise(m_Noise,0.0); //set an input pixel with probability m_Noise to 0
47 shark::ConcatenatedModel<shark::RealVector,shark::RealVector> model = noise>> m_net; 48 shark::ConcatenatedModel<shark::RealVector,shark::RealVector> model = noise>> m_net;
48 49
49 shark::LabeledData<shark::RealVector,shark::RealVector> trainSet(inputSamples,inputSamples);//labels identical to inputs 50 shark::LabeledData<shark::RealVector,shark::RealVector> trainSet(inputSamples,inputSamples);//labels identical to inputs
@@ -92,7 +93,7 @@ template &lt;class TInputValue, class AutoencoderType&gt; @@ -92,7 +93,7 @@ template &lt;class TInputValue, class AutoencoderType&gt;
92 void AutoencoderModel<TInputValue,AutoencoderType>::Save(const std::string & filename, const std::string & name) 93 void AutoencoderModel<TInputValue,AutoencoderType>::Save(const std::string & filename, const std::string & name)
93 { 94 {
94 std::ofstream ofs(filename); 95 std::ofstream ofs(filename);
95 - ofs << m_net.name() << std::endl; //first line 96 + ofs << m_net.name() << std::endl; // the first line of the model file contains a key
96 boost::archive::polymorphic_text_oarchive oa(ofs); 97 boost::archive::polymorphic_text_oarchive oa(ofs);
97 m_net.write(oa); 98 m_net.write(oa);
98 ofs.close(); 99 ofs.close();
@@ -163,6 +164,5 @@ void AutoencoderModel&lt;TInputValue,AutoencoderType&gt; @@ -163,6 +164,5 @@ void AutoencoderModel&lt;TInputValue,AutoencoderType&gt;
163 } 164 }
164 } 165 }
165 166
166 -  
167 } // namespace otb 167 } // namespace otb
168 #endif 168 #endif
include/DimensionalityReductionModel.h
@@ -25,42 +25,32 @@ @@ -25,42 +25,32 @@
25 namespace otb 25 namespace otb
26 { 26 {
27 27
28 -/** \class MachineLearningModel  
29 - * \brief MachineLearningModel is the base class for all classifier objects (SVM, KNN,  
30 - * Random Forests, Artificial Neural Network, ...) implemented in the supervised classification framework of the OTB. 28 +/** \class DimensionalityReductionModel
  29 + * \brief DimensionalityReductionModel is the base class for all dimensionality Reduction objects (PCA, autoencoders and SOM) implemented in the dimensionality Reduction framework of the OTB.
31 * 30 *
32 - * MachineLearningModel is an abstract object that specifies behavior and  
33 - * interface of supervised classifiers (SVM, KNN, Random Forests, Artificial  
34 - * Neural Network, ...) in the generic supervised classification framework of the OTB.  
35 - * The main generic virtual methods specifically implemented in each classifier  
36 - * derived from the MachineLearningModel class are two learning-related methods:  
37 - * Train() and Save(), and three classification-related methods: Load(), 31 + * DimensionalityReductionModel is an abstract object that specifies behavior and
  32 + * interface of dimensionality reduction algorithms (PCA, autoencoders and SOM) in the generic dimensionality Reduction framework of the OTB.
  33 + * The main generic virtual methods specifically implemented in each model
  34 + * derived from the DimensionalityReductionModel class are two learning-related methods:
  35 + * Train() and Save(), and three dimensionality reduction related methods: Load(),
38 * DoPredict() and optionnaly DoPredictBatch(). 36 * DoPredict() and optionnaly DoPredictBatch().
39 * 37 *
40 * Thus, each classifier derived from the MachineLearningModel class 38 * Thus, each classifier derived from the MachineLearningModel class
41 * computes its corresponding model with Train() and exports it with 39 * computes its corresponding model with Train() and exports it with
42 * the help of the Save() method. 40 * the help of the Save() method.
43 * 41 *
44 - * It is also possible to classify any input sample composed of several 42 + * It is also possible to reduce the dimensionality of any input sample composed of several
45 * features (or any number of bands in the case of a pixel extracted 43 * features (or any number of bands in the case of a pixel extracted
46 * from a multi-band image) with the help of the Predict() method which 44 * from a multi-band image) with the help of the Predict() method which
47 * needs a previous loading of the classification model with the Load() method. 45 * needs a previous loading of the classification model with the Load() method.
48 * 46 *
49 - * \sa MachineLearningModelFactory  
50 - * \sa LibSVMMachineLearningModel  
51 - * \sa SVMMachineLearningModel  
52 - * \sa BoostMachineLearningModel  
53 - * \sa KNearestNeighborsMachineLearningModel  
54 - * \sa DecisionTreeMachineLearningModel  
55 - * \sa RandomForestsMachineLearningModel  
56 - * \sa GradientBoostedTreeMachineLearningModel  
57 - * \sa NormalBayesMachineLearningModel  
58 - * \sa NeuralNetworkMachineLearningModel  
59 - * \sa SharkRandomForestsMachineLearningModel  
60 - * \sa ImageClassificationFilter 47 + * \sa DimensionalityReductionModelFactory
  48 + * \sa SOMModel
  49 + * \sa PCAModel
  50 + * \sa AutoencderModel
  51 + * \sa ImageDimensionalityReductionFilter
61 * 52 *
62 - *  
63 - * \ingroup OTBSupervised 53 + * \ingroup cbDimensionalityReduction
64 */ 54 */
65 template <class TInputValue, class TTargetValue> 55 template <class TInputValue, class TTargetValue>
66 class ITK_EXPORT DimensionalityReductionModel 56 class ITK_EXPORT DimensionalityReductionModel
@@ -121,7 +111,7 @@ public: @@ -121,7 +111,7 @@ public:
121 /** Get the size of the output after dimensionality reduction */ 111 /** Get the size of the output after dimensionality reduction */
122 virtual unsigned int GetDimension() = 0; 112 virtual unsigned int GetDimension() = 0;
123 113
124 - /**\name Classification model file manipulation */ 114 + /**\name Dimensionality Reduction model file manipulation */
125 //@{ 115 //@{
126 /** Save the model to file */ 116 /** Save the model to file */
127 virtual void Save(const std::string & filename, const std::string & name="") = 0; 117 virtual void Save(const std::string & filename, const std::string & name="") = 0;
@@ -132,10 +122,10 @@ public: @@ -132,10 +122,10 @@ public:
132 122
133 /**\name Classification model file compatibility tests */ 123 /**\name Classification model file compatibility tests */
134 //@{ 124 //@{
135 - /** Is the input model file readable and compatible with the corresponding classifier ? */ 125 + /** Is the input model file readable and compatible with the corresponding model ? */
136 virtual bool CanReadFile(const std::string &) = 0; 126 virtual bool CanReadFile(const std::string &) = 0;
137 127
138 - /** Is the input model file writable and compatible with the corresponding classifier ? */ 128 + /** Is the input model file writable and compatible with the corresponding model ? */
139 virtual bool CanWriteFile(const std::string &) = 0; 129 virtual bool CanWriteFile(const std::string &) = 0;
140 //@} 130 //@}
141 131
@@ -165,19 +155,13 @@ protected: @@ -165,19 +155,13 @@ protected:
165 /** Is DoPredictBatch multi-threaded ? */ 155 /** Is DoPredictBatch multi-threaded ? */
166 bool m_IsDoPredictBatchMultiThreaded; 156 bool m_IsDoPredictBatchMultiThreaded;
167 157
168 -  
169 -  
170 -  
171 -  
172 private: 158 private:
173 /** Actual implementation of BatchPredicition 159 /** Actual implementation of BatchPredicition
174 * Default implementation will call DoPredict iteratively 160 * Default implementation will call DoPredict iteratively
175 * \param input The input batch 161 * \param input The input batch
176 - * \param startIndex Index of the first sample to predict  
177 - * \param size Number of samples to predict  
178 - * \param target Pointer to the list of produced labels  
179 - * \param quality Pointer to the list of produced confidence  
180 - * values, or NULL 162 + * \param startIndex Index of the first sample to reduce
  163 + * \param size Number of samples to reduce
  164 + * \param target Pointer to the list of reduced samples
181 * 165 *
182 * Override me if internal implementation allows for batch 166 * Override me if internal implementation allows for batch
183 * prediction. 167 * prediction.
@@ -187,11 +171,9 @@ private: @@ -187,11 +171,9 @@ private:
187 */ 171 */
188 virtual void DoPredictBatch(const InputListSampleType * input, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType * target) const; 172 virtual void DoPredictBatch(const InputListSampleType * input, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType * target) const;
189 173
190 - /** Actual implementation of single sample prediction  
191 - * \param input sample to predict  
192 - * \param quality Pointer to a variable to store confidence value,  
193 - * or NULL  
194 - * \return The predicted label 174 + /** Actual implementation of single sample reduction
  175 + * \param input sample to reduce
  176 + * \return The reduced sample
195 */ 177 */
196 virtual TargetSampleType DoPredict(const InputSampleType& input) const = 0; 178 virtual TargetSampleType DoPredict(const InputSampleType& input) const = 0;
197 179
include/ImageDimensionalityReductionFilter.txx
@@ -163,14 +163,10 @@ ImageDimensionalityReductionFilter&lt;TInputImage, TOutputImage, TMaskImage&gt; @@ -163,14 +163,10 @@ ImageDimensionalityReductionFilter&lt;TInputImage, TOutputImage, TMaskImage&gt;
163 InputIteratorType inIt(inputPtr, outputRegionForThread); 163 InputIteratorType inIt(inputPtr, outputRegionForThread);
164 OutputIteratorType outIt(outputPtr, outputRegionForThread); 164 OutputIteratorType outIt(outputPtr, outputRegionForThread);
165 165
166 - // typedef typename ModelType::InputValueType InputValueType;  
167 typedef typename ModelType::InputSampleType InputSampleType; 166 typedef typename ModelType::InputSampleType InputSampleType;
168 typedef typename ModelType::InputListSampleType InputListSampleType; 167 typedef typename ModelType::InputListSampleType InputListSampleType;
169 typedef typename ModelType::TargetValueType TargetValueType; 168 typedef typename ModelType::TargetValueType TargetValueType;
170 - // typedef typename ModelType::TargetSampleType TargetSampleType;  
171 typedef typename ModelType::TargetListSampleType TargetListSampleType; 169 typedef typename ModelType::TargetListSampleType TargetListSampleType;
172 - // typedef typename ModelType::ConfidenceValueType ConfidenceValueType;  
173 - // typedef typename ModelType::ConfidenceSampleType ConfidenceSampleType;  
174 170
175 typename InputListSampleType::Pointer samples = InputListSampleType::New(); 171 typename InputListSampleType::Pointer samples = InputListSampleType::New();
176 unsigned int num_features = inputPtr->GetNumberOfComponentsPerPixel(); 172 unsigned int num_features = inputPtr->GetNumberOfComponentsPerPixel();
@@ -193,11 +189,10 @@ ImageDimensionalityReductionFilter&lt;TInputImage, TOutputImage, TMaskImage&gt; @@ -193,11 +189,10 @@ ImageDimensionalityReductionFilter&lt;TInputImage, TOutputImage, TMaskImage&gt;
193 typename TargetListSampleType::Pointer labels; 189 typename TargetListSampleType::Pointer labels;
194 190
195 // This call is threadsafe 191 // This call is threadsafe
196 - //labels = m_Model->PredictBatch(samples,confidences);  
197 labels = m_Model->PredictBatch(samples); 192 labels = m_Model->PredictBatch(samples);
  193 +
198 // Set the output values 194 // Set the output values
199 195
200 -  
201 typename TargetListSampleType::ConstIterator labIt = labels->Begin(); 196 typename TargetListSampleType::ConstIterator labIt = labels->Begin();
202 197
203 for (outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt) 198 for (outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt)