OTB  10.0.0
Orfeo Toolbox
otbPCAModel.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 otbPCAModel_h
21 #define otbPCAModel_h
22 
25 
26 // Quiet a deprecation warning
27 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
28 
29 #if defined(__GNUC__) || defined(__clang__)
30 #pragma GCC diagnostic push
31 
32 #if (defined (__GNUC__) && (__GNUC__ >= 9)) || (defined (__clang__) && (__clang_major__ >= 10))
33 #pragma GCC diagnostic ignored "-Wdeprecated-copy"
34 #endif
35 
36 #pragma GCC diagnostic ignored "-Wshadow"
37 #pragma GCC diagnostic ignored "-Wunused-parameter"
38 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
39 #pragma GCC diagnostic ignored "-Wsign-compare"
40 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
41 #if defined(__clang__)
42 #pragma clang diagnostic ignored "-Wheader-guard"
43 
44 #if defined(__apple_build_version__)
45 /* Need AppleClang >= 9.0.0 to support -Wexpansion-to-defined */
46 #if __apple_build_version__ >= 9000000
47 #pragma clang diagnostic ignored "-Wexpansion-to-defined"
48 #endif
49 #elif __clang_major__ > 3
50 #pragma clang diagnostic ignored "-Wexpansion-to-defined"
51 #endif
52 
53 #else
54 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
55 #endif
56 #endif
57 #include "otb_shark.h"
58 #include <shark/Algorithms/Trainers/PCA.h>
59 #if defined(__GNUC__) || defined(__clang__)
60 #pragma GCC diagnostic pop
61 #endif
62 
63 namespace otb
64 {
65 
72 template <class TInputValue>
73 class ITK_EXPORT PCAModel : public MachineLearningModel<itk::VariableLengthVector<TInputValue>, itk::VariableLengthVector<TInputValue>>
74 {
75 public:
76  typedef PCAModel Self;
77  typedef MachineLearningModel<itk::VariableLengthVector<TInputValue>, itk::VariableLengthVector<TInputValue>> Superclass;
78  typedef itk::SmartPointer<Self> Pointer;
79  typedef itk::SmartPointer<const Self> ConstPointer;
80 
82  typedef typename Superclass::InputSampleType InputSampleType;
83  typedef typename Superclass::InputListSampleType InputListSampleType;
84  typedef typename InputListSampleType::Pointer ListSamplePointerType;
87  typedef typename Superclass::TargetListSampleType TargetListSampleType;
88 
89  // Confidence map related typedefs
90  typedef typename Superclass::ConfidenceValueType ConfidenceValueType;
92  typedef typename Superclass::ConfidenceListSampleType ConfidenceListSampleType;
93 
94  typedef typename Superclass::ProbaSampleType ProbaSampleType;
95  typedef typename Superclass::ProbaListSampleType ProbaListSampleType;
96 
97  itkNewMacro(Self);
98  itkTypeMacro(PCAModel, DimensionalityReductionModel);
99 
100  itkSetMacro(DoResizeFlag, bool);
101 
102  itkSetMacro(WriteEigenvectors, bool);
103  itkGetMacro(WriteEigenvectors, bool);
104 
105  bool CanReadFile(const std::string& filename) override;
106  bool CanWriteFile(const std::string& filename) override;
107 
108  void Save(const std::string& filename, const std::string& name = "") override;
109  void Load(const std::string& filename, const std::string& name = "") override;
110 
111  void Train() override;
112 
113 protected:
114  PCAModel();
115  ~PCAModel() override;
116 
117  virtual TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType* quality = nullptr, ProbaSampleType* proba = nullptr) const override;
118 
119  virtual void DoPredictBatch(const InputListSampleType*, const unsigned int& startIndex, const unsigned int& size, TargetListSampleType*,
120  ConfidenceListSampleType* quality = nullptr, ProbaListSampleType* proba = nullptr) const override;
121 
122 private:
123  shark::LinearModel<> m_Encoder;
124  shark::LinearModel<> m_Decoder;
125  shark::PCA m_PCA;
128 };
129 } // end namespace otb
130 
131 
132 #ifndef OTB_MANUAL_INSTANTIATION
133 #include "otbPCAModel.hxx"
134 #endif
135 
136 
137 #endif
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests,...
MLMTargetTraits< TTargetValue >::SampleType TargetSampleType
bool m_WriteEigenvectors
Definition: otbPCAModel.h:127
InputListSampleType::Pointer ListSamplePointerType
Definition: otbPCAModel.h:84
Superclass::TargetValueType TargetValueType
Definition: otbPCAModel.h:85
shark::PCA m_PCA
Definition: otbPCAModel.h:125
Superclass::TargetListSampleType TargetListSampleType
Definition: otbPCAModel.h:87
Superclass::ProbaListSampleType ProbaListSampleType
Definition: otbPCAModel.h:95
shark::LinearModel m_Encoder
Definition: otbPCAModel.h:123
Superclass::InputSampleType InputSampleType
Definition: otbPCAModel.h:82
Superclass::ConfidenceSampleType ConfidenceSampleType
Definition: otbPCAModel.h:91
bool m_DoResizeFlag
Definition: otbPCAModel.h:126
Superclass::ProbaSampleType ProbaSampleType
Definition: otbPCAModel.h:94
Superclass::InputListSampleType InputListSampleType
Definition: otbPCAModel.h:83
Superclass::ConfidenceValueType ConfidenceValueType
Definition: otbPCAModel.h:90
itk::SmartPointer< Self > Pointer
Definition: otbPCAModel.h:78
Superclass::InputValueType InputValueType
Definition: otbPCAModel.h:81
MachineLearningModel< itk::VariableLengthVector< TInputValue >, itk::VariableLengthVector< TInputValue > > Superclass
Definition: otbPCAModel.h:77
PCAModel Self
Definition: otbPCAModel.h:76
shark::LinearModel m_Decoder
Definition: otbPCAModel.h:124
Superclass::ConfidenceListSampleType ConfidenceListSampleType
Definition: otbPCAModel.h:92
itk::SmartPointer< const Self > ConstPointer
Definition: otbPCAModel.h:79
Superclass::TargetSampleType TargetSampleType
Definition: otbPCAModel.h:86
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.