Commit 82e47317d4465939c1c4f39a7aa283d4c174d703

Authored by Cédric Traizet
1 parent 0c0351e2

some cmments and documentation added

include/AutoencoderModel.h
... ... @@ -27,7 +27,7 @@ public:
27 27 itkNewMacro(Self);
28 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 31 itkGetMacro(NumberOfHiddenNeurons,unsigned int);
32 32 itkSetMacro(NumberOfHiddenNeurons,unsigned int);
33 33  
... ... @@ -47,9 +47,7 @@ public:
47 47 void Load(const std::string & filename, const std::string & name="") ITK_OVERRIDE;
48 48  
49 49 void Train() ITK_OVERRIDE;
50   - //void Dimensionality_reduction() {}; // Dimensionality reduction is done by DoPredict
51   -
52   -
  50 +
53 51 protected:
54 52 AutoencoderModel();
55 53 ~AutoencoderModel() ITK_OVERRIDE;
... ... @@ -59,11 +57,14 @@ protected:
59 57  
60 58 private:
61 59  
  60 + /** Network attributes */
62 61 AutoencoderType m_net;
63 62 unsigned int m_NumberOfHiddenNeurons;
  63 +
  64 + /** Training parameters */
64 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 69 } // end namespace otb
69 70  
... ...
include/AutoencoderModel.txx
... ... @@ -6,12 +6,13 @@
6 6 #include <shark/Data/Dataset.h>
7 7 #include "itkMacro.h"
8 8 #include "otbSharkUtils.h"
  9 +
9 10 //include train function
10 11 #include <shark/ObjectiveFunctions/ErrorFunction.h>
11 12 #include <shark/Algorithms/GradientDescent/Rprop.h>// the RProp optimization algorithm
12 13 #include <shark/ObjectiveFunctions/Loss/SquaredLoss.h> // squared loss used for regression
13 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 16 #include <shark/Models/ConcatenatedModel.h>//to concatenate the noise with the model
16 17  
17 18 namespace otb
... ... @@ -43,7 +44,7 @@ void AutoencoderModel&lt;TInputValue,AutoencoderType&gt;::Train()
43 44 std::size_t inputs = dataDimension(inputSamples);
44 45 m_net.setStructure(inputs, m_NumberOfHiddenNeurons);
45 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 48 shark::ConcatenatedModel<shark::RealVector,shark::RealVector> model = noise>> m_net;
48 49  
49 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 93 void AutoencoderModel<TInputValue,AutoencoderType>::Save(const std::string & filename, const std::string & name)
93 94 {
94 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 97 boost::archive::polymorphic_text_oarchive oa(ofs);
97 98 m_net.write(oa);
98 99 ofs.close();
... ... @@ -163,6 +164,5 @@ void AutoencoderModel&lt;TInputValue,AutoencoderType&gt;
163 164 }
164 165 }
165 166  
166   -
167 167 } // namespace otb
168 168 #endif
... ...
include/DimensionalityReductionModel.h
... ... @@ -25,42 +25,32 @@
25 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 36 * DoPredict() and optionnaly DoPredictBatch().
39 37 *
40 38 * Thus, each classifier derived from the MachineLearningModel class
41 39 * computes its corresponding model with Train() and exports it with
42 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 43 * features (or any number of bands in the case of a pixel extracted
46 44 * from a multi-band image) with the help of the Predict() method which
47 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 55 template <class TInputValue, class TTargetValue>
66 56 class ITK_EXPORT DimensionalityReductionModel
... ... @@ -121,7 +111,7 @@ public:
121 111 /** Get the size of the output after dimensionality reduction */
122 112 virtual unsigned int GetDimension() = 0;
123 113  
124   - /**\name Classification model file manipulation */
  114 + /**\name Dimensionality Reduction model file manipulation */
125 115 //@{
126 116 /** Save the model to file */
127 117 virtual void Save(const std::string & filename, const std::string & name="") = 0;
... ... @@ -132,10 +122,10 @@ public:
132 122  
133 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 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 129 virtual bool CanWriteFile(const std::string &) = 0;
140 130 //@}
141 131  
... ... @@ -165,19 +155,13 @@ protected:
165 155 /** Is DoPredictBatch multi-threaded ? */
166 156 bool m_IsDoPredictBatchMultiThreaded;
167 157  
168   -
169   -
170   -
171   -
172 158 private:
173 159 /** Actual implementation of BatchPredicition
174 160 * Default implementation will call DoPredict iteratively
175 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 166 * Override me if internal implementation allows for batch
183 167 * prediction.
... ... @@ -187,11 +171,9 @@ private:
187 171 */
188 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 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 163 InputIteratorType inIt(inputPtr, outputRegionForThread);
164 164 OutputIteratorType outIt(outputPtr, outputRegionForThread);
165 165  
166   - // typedef typename ModelType::InputValueType InputValueType;
167 166 typedef typename ModelType::InputSampleType InputSampleType;
168 167 typedef typename ModelType::InputListSampleType InputListSampleType;
169 168 typedef typename ModelType::TargetValueType TargetValueType;
170   - // typedef typename ModelType::TargetSampleType TargetSampleType;
171 169 typedef typename ModelType::TargetListSampleType TargetListSampleType;
172   - // typedef typename ModelType::ConfidenceValueType ConfidenceValueType;
173   - // typedef typename ModelType::ConfidenceSampleType ConfidenceSampleType;
174 170  
175 171 typename InputListSampleType::Pointer samples = InputListSampleType::New();
176 172 unsigned int num_features = inputPtr->GetNumberOfComponentsPerPixel();
... ... @@ -193,11 +189,10 @@ ImageDimensionalityReductionFilter&lt;TInputImage, TOutputImage, TMaskImage&gt;
193 189 typename TargetListSampleType::Pointer labels;
194 190  
195 191 // This call is threadsafe
196   - //labels = m_Model->PredictBatch(samples,confidences);
197 192 labels = m_Model->PredictBatch(samples);
  193 +
198 194 // Set the output values
199 195  
200   -
201 196 typename TargetListSampleType::ConstIterator labIt = labels->Begin();
202 197  
203 198 for (outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt)
... ...