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