OTB  6.7.0
Orfeo Toolbox
otbAutoencoderModel.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #ifndef otbAutoencoderModel_h
21 #define otbAutoencoderModel_h
22 
25 #include <string>
26 
27 #if defined(__GNUC__) || defined(__clang__)
28 #pragma GCC diagnostic push
29 #pragma GCC diagnostic ignored "-Wshadow"
30 #pragma GCC diagnostic ignored "-Wunused-parameter"
31 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
32 #pragma GCC diagnostic ignored "-Wsign-compare"
33 #if defined(__clang__)
34 #pragma clang diagnostic ignored "-Wheader-guard"
35 #pragma clang diagnostic ignored "-Wdivision-by-zero"
36 #else
37 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
38 #endif
39 #endif
40 #include "otb_shark.h"
41 #include <shark/Algorithms/StoppingCriteria/AbstractStoppingCriterion.h>
42 #include <shark/Models/LinearModel.h>
43 #include <shark/Models/ConcatenatedModel.h>
44 #include <shark/Models/NeuronLayers.h>
45 #if defined(__GNUC__) || defined(__clang__)
46 #pragma GCC diagnostic pop
47 #endif
48 
49 namespace otb
50 {
58 template <class TInputValue, class NeuronType>
59 class ITK_EXPORT AutoencoderModel
60  : public MachineLearningModel<
61  itk::VariableLengthVector< TInputValue>,
62  itk::VariableLengthVector< TInputValue> >
63 {
64 public:
66  typedef MachineLearningModel<
71 
72  typedef typename Superclass::InputValueType InputValueType;
73  typedef typename Superclass::InputSampleType InputSampleType;
74  typedef typename Superclass::InputListSampleType InputListSampleType;
75  typedef typename InputListSampleType::Pointer ListSamplePointerType;
76  typedef typename Superclass::TargetValueType TargetValueType;
78  typedef typename Superclass::TargetListSampleType TargetListSampleType;
79 
81  typedef typename Superclass::ConfidenceValueType ConfidenceValueType;
82  typedef typename Superclass::ConfidenceSampleType ConfidenceSampleType;
83  typedef typename Superclass::ConfidenceListSampleType ConfidenceListSampleType;
84 
86  typedef shark::ConcatenatedModel<shark::RealVector> ModelType;
87  typedef shark::LinearModel<shark::RealVector,NeuronType> LayerType;
88  typedef shark::LinearModel<shark::RealVector, shark::LinearNeuron> OutLayerType;
89 
90  itkNewMacro(Self);
91  itkTypeMacro(AutoencoderModel, DimensionalityReductionModel);
92 
93  itkGetMacro(NumberOfHiddenNeurons,itk::Array<unsigned int>);
94  itkSetMacro(NumberOfHiddenNeurons,itk::Array<unsigned int>);
95 
96  itkGetMacro(NumberOfIterations,unsigned int);
97  itkSetMacro(NumberOfIterations,unsigned int);
98 
99  itkGetMacro(NumberOfIterationsFineTuning,unsigned int);
100  itkSetMacro(NumberOfIterationsFineTuning,unsigned int);
101 
102  itkGetMacro(Epsilon,double);
103  itkSetMacro(Epsilon,double);
104 
105  itkGetMacro(InitFactor,double);
106  itkSetMacro(InitFactor,double);
107 
108  itkGetMacro(Regularization,itk::Array<double>);
109  itkSetMacro(Regularization,itk::Array<double>);
110 
111  itkGetMacro(Noise,itk::Array<double>);
112  itkSetMacro(Noise,itk::Array<double>);
113 
114  itkGetMacro(Rho,itk::Array<double>);
115  itkSetMacro(Rho,itk::Array<double>);
116 
117  itkGetMacro(Beta,itk::Array<double>);
118  itkSetMacro(Beta,itk::Array<double>);
119 
120  itkGetMacro(WriteLearningCurve,bool);
121  itkSetMacro(WriteLearningCurve,bool);
122 
123  itkSetMacro(WriteWeights, bool);
124  itkGetMacro(WriteWeights, bool);
125 
126  itkGetMacro(LearningCurveFileName,std::string);
127  itkSetMacro(LearningCurveFileName,std::string);
128 
129  bool CanReadFile(const std::string & filename) override;
130  bool CanWriteFile(const std::string & filename) override;
131 
132  void Save(const std::string & filename, const std::string & name="") override;
133  void Load(const std::string & filename, const std::string & name="") override;
134 
135  void Train() override;
136 
137  template <class T>
138  void TrainOneLayer(
139  shark::AbstractStoppingCriterion<T> & criterion,
140  unsigned int,
141  shark::Data<shark::RealVector> &,
142  std::ostream&);
143 
144  template <class T>
145  void TrainOneSparseLayer(
146  shark::AbstractStoppingCriterion<T> & criterion,
147  unsigned int,
148  shark::Data<shark::RealVector> &,
149  std::ostream&);
150 
151  template <class T>
152  void TrainNetwork(
153  shark::AbstractStoppingCriterion<T> & criterion,
154  shark::Data<shark::RealVector> &,
155  std::ostream&);
156 
157 protected:
159  ~AutoencoderModel() override;
160 
161  virtual TargetSampleType DoPredict(
162  const InputSampleType& input,
163  ConfidenceValueType * quality = nullptr) const override;
164 
165  virtual void DoPredictBatch(
166  const InputListSampleType *,
167  const unsigned int & startIndex,
168  const unsigned int & size,
170  ConfidenceListSampleType * quality = nullptr) const override;
171 
172 private:
175  std::vector<LayerType> m_InLayers;
178 
180  unsigned int m_NumberOfIterations; // stop the training after a fixed number of iterations
181  unsigned int m_NumberOfIterationsFineTuning; // stop the fine tuning after a fixed number of iterations
182  double m_Epsilon; // Stops the training when the training error seems to converge
183  itk::Array<double> m_Regularization; // L2 Regularization parameter
184  itk::Array<double> m_Noise; // probability for an input to be set to 0 (denosing autoencoder)
185  itk::Array<double> m_Rho; // Sparsity parameter
186  itk::Array<double> m_Beta; // Sparsity regularization parameter
187  double m_InitFactor; // Weight initialization factor (the weights are intialized at m_initfactor/sqrt(inputDimension) )
189 
190  bool m_WriteLearningCurve; // Flag for writing the learning curve into a txt file
191  std::string m_LearningCurveFileName; // Name of the output learning curve printed after training
193 };
194 } // end namespace otb
195 
196 #ifndef OTB_MANUAL_INSTANTIATION
197 #include "otbAutoencoderModel.hxx"
198 #endif
199 
200 #endif
201 
Superclass::ConfidenceValueType ConfidenceValueType
Confidence map related typedefs.
shark::LinearModel< shark::RealVector, shark::LinearNeuron > OutLayerType
itk::Array< double > m_Noise
Superclass::TargetListSampleType TargetListSampleType
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests...
Superclass::TargetSampleType TargetSampleType
itk::Array< double > m_Regularization
Superclass::ConfidenceListSampleType ConfidenceListSampleType
std::vector< LayerType > m_InLayers
unsigned int m_NumberOfIterationsFineTuning
itk::Array< double > m_Beta
Superclass::ConfidenceSampleType ConfidenceSampleType
MachineLearningModel< itk::VariableLengthVector< TInputValue >, itk::VariableLengthVector< TInputValue > > Superclass
shark::ConcatenatedModel< shark::RealVector > ModelType
Neural network related typedefs.
Superclass::InputListSampleType InputListSampleType
Superclass::TargetValueType TargetValueType
itk::Array< unsigned int > m_NumberOfHiddenNeurons
itk::SmartPointer< const Self > ConstPointer
itk::SmartPointer< Self > Pointer
InputListSampleType::Pointer ListSamplePointerType
Superclass::InputSampleType InputSampleType
Superclass::InputValueType InputValueType
shark::LinearModel< shark::RealVector, NeuronType > LayerType
itk::Array< double > m_Rho
MLMTargetTraits< TTargetValue >::SampleType TargetSampleType