OTB  9.0.0
Orfeo Toolbox
otbAutoencoderModel.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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 // Quiet a deprecation warning
28 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
29 
30 #if defined(__GNUC__) || defined(__clang__)
31 #pragma GCC diagnostic push
32 
33 #if (defined (__GNUC__) && (__GNUC__ >= 9)) || (defined (__clang__) && (__clang_major__ >= 10))
34 #pragma GCC diagnostic ignored "-Wdeprecated-copy"
35 #endif
36 
37 #pragma GCC diagnostic ignored "-Wshadow"
38 #pragma GCC diagnostic ignored "-Wunused-parameter"
39 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
40 #pragma GCC diagnostic ignored "-Wsign-compare"
41 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
42 #if defined(__clang__)
43 #pragma clang diagnostic ignored "-Wheader-guard"
44 #pragma clang diagnostic ignored "-Wdivision-by-zero"
45 
46 #if defined(__apple_build_version__)
47 /* Need AppleClang >= 9.0.0 to support -Wexpansion-to-defined */
48 #if __apple_build_version__ >= 9000000
49 #pragma clang diagnostic ignored "-Wexpansion-to-defined"
50 #endif
51 #elif __clang_major__ > 3
52 #pragma clang diagnostic ignored "-Wexpansion-to-defined"
53 #endif
54 
55 #else
56 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
57 #endif
58 #endif
59 #include "otb_shark.h"
60 #include <shark/Algorithms/StoppingCriteria/AbstractStoppingCriterion.h>
61 #include <shark/Models/LinearModel.h>
62 #include <shark/Models/ConcatenatedModel.h>
63 #include <shark/Models/NeuronLayers.h>
64 #if defined(__GNUC__) || defined(__clang__)
65 #pragma GCC diagnostic pop
66 #endif
67 
68 namespace otb
69 {
77 template <class TInputValue, class NeuronType>
78 class ITK_EXPORT AutoencoderModel : public MachineLearningModel<itk::VariableLengthVector<TInputValue>, itk::VariableLengthVector<TInputValue>>
79 {
80 public:
82  typedef MachineLearningModel<itk::VariableLengthVector<TInputValue>, itk::VariableLengthVector<TInputValue>> Superclass;
83  typedef itk::SmartPointer<Self> Pointer;
84  typedef itk::SmartPointer<const Self> ConstPointer;
85 
87  typedef typename Superclass::InputSampleType InputSampleType;
88  typedef typename Superclass::InputListSampleType InputListSampleType;
89  typedef typename InputListSampleType::Pointer ListSamplePointerType;
92  typedef typename Superclass::TargetListSampleType TargetListSampleType;
93 
95  typedef typename Superclass::ConfidenceValueType ConfidenceValueType;
97  typedef typename Superclass::ConfidenceListSampleType ConfidenceListSampleType;
98 
99  typedef typename Superclass::ProbaSampleType ProbaSampleType;
100  typedef typename Superclass::ProbaListSampleType ProbaListSampleType;
102  typedef shark::ConcatenatedModel<shark::RealVector> ModelType;
103  typedef shark::LinearModel<shark::RealVector, NeuronType> LayerType;
104  typedef shark::LinearModel<shark::RealVector, shark::LinearNeuron> OutLayerType;
105 
106  itkNewMacro(Self);
107  itkTypeMacro(AutoencoderModel, DimensionalityReductionModel);
108 
109  itkGetMacro(NumberOfHiddenNeurons, itk::Array<unsigned int>);
110  itkSetMacro(NumberOfHiddenNeurons, itk::Array<unsigned int>);
111 
112  itkGetMacro(NumberOfIterations, unsigned int);
113  itkSetMacro(NumberOfIterations, unsigned int);
114 
115  itkGetMacro(NumberOfIterationsFineTuning, unsigned int);
116  itkSetMacro(NumberOfIterationsFineTuning, unsigned int);
117 
118  itkGetMacro(Epsilon, double);
119  itkSetMacro(Epsilon, double);
120 
121  itkGetMacro(InitFactor, double);
122  itkSetMacro(InitFactor, double);
123 
124  itkGetMacro(Regularization, itk::Array<double>);
125  itkSetMacro(Regularization, itk::Array<double>);
126 
127  itkGetMacro(Noise, itk::Array<double>);
128  itkSetMacro(Noise, itk::Array<double>);
129 
130  itkGetMacro(Rho, itk::Array<double>);
131  itkSetMacro(Rho, itk::Array<double>);
132 
133  itkGetMacro(Beta, itk::Array<double>);
134  itkSetMacro(Beta, itk::Array<double>);
135 
136  itkGetMacro(WriteLearningCurve, bool);
137  itkSetMacro(WriteLearningCurve, bool);
138 
139  itkSetMacro(WriteWeights, bool);
140  itkGetMacro(WriteWeights, bool);
141 
142  itkGetMacro(LearningCurveFileName, std::string);
143  itkSetMacro(LearningCurveFileName, std::string);
144 
145  bool CanReadFile(const std::string& filename) override;
146  bool CanWriteFile(const std::string& filename) override;
147 
148  void Save(const std::string& filename, const std::string& name = "") override;
149  void Load(const std::string& filename, const std::string& name = "") override;
150 
151  void Train() override;
152 
153  template <class T>
154  void TrainOneLayer(shark::AbstractStoppingCriterion<T>& criterion, unsigned int, shark::Data<shark::RealVector>&, std::ostream&);
155 
156  template <class T>
157  void TrainOneSparseLayer(shark::AbstractStoppingCriterion<T>& criterion, unsigned int, shark::Data<shark::RealVector>&, std::ostream&);
158 
159  template <class T>
160  void TrainNetwork(shark::AbstractStoppingCriterion<T>& criterion, shark::Data<shark::RealVector>&, std::ostream&);
161 
162 protected:
164  ~AutoencoderModel() override;
165 
166  virtual TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType* quality = nullptr, ProbaSampleType* proba = nullptr) const override;
167 
168  virtual void DoPredictBatch(const InputListSampleType*, const unsigned int& startIndex, const unsigned int& size, TargetListSampleType*,
169  ConfidenceListSampleType* quality = nullptr, ProbaListSampleType* proba = nullptr) const override;
170 
171 private:
174  std::vector<LayerType> m_InLayers;
176  itk::Array<unsigned int> m_NumberOfHiddenNeurons;
177 
179  unsigned int m_NumberOfIterations; // stop the training after a fixed number of iterations
180  unsigned int m_NumberOfIterationsFineTuning; // stop the fine tuning after a fixed number of iterations
181  double m_Epsilon; // Stops the training when the training error seems to converge
182  itk::Array<double> m_Regularization; // L2 Regularization parameter
183  itk::Array<double> m_Noise; // probability for an input to be set to 0 (denosing autoencoder)
184  itk::Array<double> m_Rho; // Sparsity parameter
185  itk::Array<double> m_Beta; // Sparsity regularization parameter
186  double m_InitFactor; // Weight initialization factor (the weights are initialized at m_initfactor/sqrt(inputDimension) )
188 
189  bool m_WriteLearningCurve; // Flag for writing the learning curve into a txt file
190  std::string m_LearningCurveFileName; // Name of the output learning curve printed after training
192 };
193 } // end namespace otb
194 
195 #ifndef OTB_MANUAL_INSTANTIATION
196 #include "otbAutoencoderModel.hxx"
197 #endif
198 
199 #endif
otb::AutoencoderModel::TargetValueType
Superclass::TargetValueType TargetValueType
Definition: otbAutoencoderModel.h:90
otb::AutoencoderModel::Self
AutoencoderModel Self
Definition: otbAutoencoderModel.h:81
otb::AutoencoderModel::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbAutoencoderModel.h:83
otbAutoencoderModel.hxx
otb::AutoencoderModel::m_NumberOfIterations
unsigned int m_NumberOfIterations
Definition: otbAutoencoderModel.h:179
otb::MachineLearningModel< itk::VariableLengthVector< TInputValue >, itk::VariableLengthVector< TInputValue > >::InputValueType
MLMSampleTraits< itk::VariableLengthVector< TInputValue > >::ValueType InputValueType
Definition: otbMachineLearningModel.h:83
otb::AutoencoderModel::ModelType
shark::ConcatenatedModel< shark::RealVector > ModelType
Neural network related typedefs.
Definition: otbAutoencoderModel.h:102
otb::AutoencoderModel::ListSamplePointerType
InputListSampleType::Pointer ListSamplePointerType
Definition: otbAutoencoderModel.h:89
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbMachineLearningModelTraits.h
otb::AutoencoderModel
Definition: otbAutoencoderModel.h:78
otb::AutoencoderModel::InputListSampleType
Superclass::InputListSampleType InputListSampleType
Definition: otbAutoencoderModel.h:88
otb::AutoencoderModel::m_Noise
itk::Array< double > m_Noise
Definition: otbAutoencoderModel.h:183
otb::AutoencoderModel::TargetSampleType
Superclass::TargetSampleType TargetSampleType
Definition: otbAutoencoderModel.h:91
otb::AutoencoderModel::InputValueType
Superclass::InputValueType InputValueType
Definition: otbAutoencoderModel.h:86
otb::AutoencoderModel::m_Rho
itk::Array< double > m_Rho
Definition: otbAutoencoderModel.h:184
otb::AutoencoderModel::m_Encoder
ModelType m_Encoder
Definition: otbAutoencoderModel.h:173
otb::AutoencoderModel::LayerType
shark::LinearModel< shark::RealVector, NeuronType > LayerType
Definition: otbAutoencoderModel.h:103
otb::AutoencoderModel::m_OutLayer
OutLayerType m_OutLayer
Definition: otbAutoencoderModel.h:175
otbMachineLearningModel.h
otb::AutoencoderModel::m_InLayers
std::vector< LayerType > m_InLayers
Definition: otbAutoencoderModel.h:174
otb::AutoencoderModel::m_InitFactor
double m_InitFactor
Definition: otbAutoencoderModel.h:186
otb::AutoencoderModel::ProbaSampleType
Superclass::ProbaSampleType ProbaSampleType
Definition: otbAutoencoderModel.h:99
otb::AutoencoderModel::ConfidenceListSampleType
Superclass::ConfidenceListSampleType ConfidenceListSampleType
Definition: otbAutoencoderModel.h:97
otb::AutoencoderModel::ProbaListSampleType
Superclass::ProbaListSampleType ProbaListSampleType
Definition: otbAutoencoderModel.h:100
otb::AutoencoderModel::m_Epsilon
double m_Epsilon
Definition: otbAutoencoderModel.h:181
otb::MachineLearningModel< itk::VariableLengthVector< TInputValue >, itk::VariableLengthVector< TInputValue > >::ConfidenceSampleType
MLMTargetTraits< double >::SampleType ConfidenceSampleType
Definition: otbMachineLearningModel.h:97
otb::AutoencoderModel::ConfidenceValueType
Superclass::ConfidenceValueType ConfidenceValueType
Confidence map related typedefs.
Definition: otbAutoencoderModel.h:95
otb::AutoencoderModel::m_Regularization
itk::Array< double > m_Regularization
Definition: otbAutoencoderModel.h:182
otb::MachineLearningModel
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests,...
Definition: otbMachineLearningModel.h:70
otb::AutoencoderModel::m_WriteWeights
bool m_WriteWeights
Definition: otbAutoencoderModel.h:191
otb::AutoencoderModel::m_NumberOfIterationsFineTuning
unsigned int m_NumberOfIterationsFineTuning
Definition: otbAutoencoderModel.h:180
otb::AutoencoderModel::OutLayerType
shark::LinearModel< shark::RealVector, shark::LinearNeuron > OutLayerType
Definition: otbAutoencoderModel.h:104
otb::MachineLearningModel< itk::VariableLengthVector< TInputValue >, itk::VariableLengthVector< TInputValue > >::TargetValueType
MLMTargetTraits< itk::VariableLengthVector< TInputValue > >::ValueType TargetValueType
Definition: otbMachineLearningModel.h:90
otb::AutoencoderModel::m_Beta
itk::Array< double > m_Beta
Definition: otbAutoencoderModel.h:185
otb::AutoencoderModel::InputSampleType
Superclass::InputSampleType InputSampleType
Definition: otbAutoencoderModel.h:87
otb::AutoencoderModel::m_LearningCurveFileName
std::string m_LearningCurveFileName
Definition: otbAutoencoderModel.h:190
otb::AutoencoderModel::Superclass
MachineLearningModel< itk::VariableLengthVector< TInputValue >, itk::VariableLengthVector< TInputValue > > Superclass
Definition: otbAutoencoderModel.h:82
otb::AutoencoderModel::ConfidenceSampleType
Superclass::ConfidenceSampleType ConfidenceSampleType
Definition: otbAutoencoderModel.h:96
otb::AutoencoderModel::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbAutoencoderModel.h:84
otb::AutoencoderModel::m_NumberOfHiddenNeurons
itk::Array< unsigned int > m_NumberOfHiddenNeurons
Definition: otbAutoencoderModel.h:176
otb::MachineLearningModel::TargetSampleType
MLMTargetTraits< TTargetValue >::SampleType TargetSampleType
Definition: otbMachineLearningModel.h:91
otb::AutoencoderModel::m_WriteLearningCurve
bool m_WriteLearningCurve
Definition: otbAutoencoderModel.h:189
otb::AutoencoderModel::TargetListSampleType
Superclass::TargetListSampleType TargetListSampleType
Definition: otbAutoencoderModel.h:92