OTB  9.0.0
Orfeo Toolbox
otbSVMMachineLearningModel.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 
21 #ifndef otbSVMMachineLearningModel_h
22 #define otbSVMMachineLearningModel_h
23 
24 #include "otbRequiresOpenCVCheck.h"
25 #include "itkLightObject.h"
26 #include "itkFixedArray.h"
28 
29 #include "otbOpenCVUtils.h"
30 
31 namespace otb
32 {
41 template <class TInputValue, class TTargetValue>
42 class ITK_EXPORT SVMMachineLearningModel : public MachineLearningModel<TInputValue, TTargetValue>
43 {
44 public:
45 
49  typedef itk::SmartPointer<Self> Pointer;
50  typedef itk::SmartPointer<const Self> ConstPointer;
51 
53  typedef typename Superclass::InputSampleType InputSampleType;
58  typedef typename Superclass::ConfidenceValueType ConfidenceValueType;
59  typedef typename Superclass::ProbaSampleType ProbaSampleType;
61  itkNewMacro(Self);
64 
66  void Train() override;
67 
69  void Save(const std::string& filename, const std::string& name = "") override;
70 
72  void Load(const std::string& filename, const std::string& name = "") override;
73 
76 
78  bool CanReadFile(const std::string&) override;
79 
81  bool CanWriteFile(const std::string&) override;
83 
84  // Setters/Getters to SVM model
85  itkGetMacro(SVMType, int);
86  itkSetMacro(SVMType, int);
87 
88  itkGetMacro(KernelType, int);
89  itkSetMacro(KernelType, int);
90 
91  // CV_TERMCRIT_ITER or CV_TERMCRIT_EPS
92  itkGetMacro(TermCriteriaType, int);
93  itkSetMacro(TermCriteriaType, int);
94 
95  itkGetMacro(MaxIter, int);
96  itkSetMacro(MaxIter, int);
97 
98  itkGetMacro(Epsilon, double);
99  itkSetMacro(Epsilon, double);
100 
101  // for poly
102  itkGetMacro(Degree, double);
103  itkSetMacro(Degree, double);
104  itkGetMacro(OutputDegree, double);
105 
106  // for poly/rbf/sigmoid
107  itkGetMacro(Gamma, double);
108  itkSetMacro(Gamma, double);
109  itkGetMacro(OutputGamma, double);
110 
111  // for poly/sigmoid
112  itkGetMacro(Coef0, double);
113  itkSetMacro(Coef0, double);
114  itkGetMacro(OutputCoef0, double);
115 
116  // for CV_SVM_C_SVC, CV_SVM_EPS_SVR and CV_SVM_NU_SVR
117  itkGetMacro(C, double);
118  itkSetMacro(C, double);
119  itkGetMacro(OutputC, double);
120 
121  // for CV_SVM_NU_SVC, CV_SVM_ONE_CLASS, and CV_SVM_NU_SVR
122  itkGetMacro(Nu, double);
123  itkSetMacro(Nu, double);
124  itkGetMacro(OutputNu, double);
125 
126  // for CV_SVM_EPS_SVR
127  itkGetMacro(P, double);
128  itkSetMacro(P, double);
129  itkGetMacro(OutputP, double);
130 
131  itkGetMacro(ParameterOptimization, bool);
132  itkSetMacro(ParameterOptimization, bool);
133 
134 protected:
137 
139  ~SVMMachineLearningModel() override = default;
140 
142  TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType* quality = nullptr, ProbaSampleType* proba = nullptr) const override;
143 
145  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
146 
147 private:
148  SVMMachineLearningModel(const Self&) = delete;
149  void operator=(const Self&) = delete;
150  cv::Ptr<cv::ml::SVM> m_SVMModel;
153  double m_Degree;
154  double m_Gamma;
155  double m_Coef0;
156  double m_C;
157  double m_Nu;
158  double m_P;
161  double m_Epsilon;
163  // Output parameters
167  double m_OutputC;
168  double m_OutputNu;
169  double m_OutputP;
170 };
171 } // end namespace otb
172 
173 #ifndef OTB_MANUAL_INSTANTIATION
175 #endif
176 
177 #endif
otb::SVMMachineLearningModel::InputValueType
Superclass::InputValueType InputValueType
Definition: otbSVMMachineLearningModel.h:52
otb::SVMMachineLearningModel::m_OutputGamma
double m_OutputGamma
Definition: otbSVMMachineLearningModel.h:165
otb::SVMMachineLearningModel::m_C
double m_C
Definition: otbSVMMachineLearningModel.h:156
otb::MachineLearningModel< TInputValue, TTargetValue >::TargetListSampleType
itk::Statistics::ListSample< TargetSampleType > TargetListSampleType
Definition: otbMachineLearningModel.h:92
otb::SVMMachineLearningModel::m_P
double m_P
Definition: otbSVMMachineLearningModel.h:158
otb::SVMMachineLearningModel::ProbaSampleType
Superclass::ProbaSampleType ProbaSampleType
Definition: otbSVMMachineLearningModel.h:59
otb::SVMMachineLearningModel::InputListSampleType
Superclass::InputListSampleType InputListSampleType
Definition: otbSVMMachineLearningModel.h:54
otb::MachineLearningModel< TInputValue, TTargetValue >::InputListSampleType
itk::Statistics::ListSample< InputSampleType > InputListSampleType
Definition: otbMachineLearningModel.h:85
otb::MachineLearningModel< TInputValue, TTargetValue >::InputValueType
MLMSampleTraits< TInputValue >::ValueType InputValueType
Definition: otbMachineLearningModel.h:83
otb::SVMMachineLearningModel::m_SVMModel
cv::Ptr< cv::ml::SVM > m_SVMModel
Definition: otbSVMMachineLearningModel.h:150
otb::SVMMachineLearningModel::m_ParameterOptimization
bool m_ParameterOptimization
Definition: otbSVMMachineLearningModel.h:162
otb::SVMMachineLearningModel::TargetSampleType
Superclass::TargetSampleType TargetSampleType
Definition: otbSVMMachineLearningModel.h:56
otb::SVMMachineLearningModel::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbSVMMachineLearningModel.h:50
otb::SVMMachineLearningModel::m_KernelType
int m_KernelType
Definition: otbSVMMachineLearningModel.h:152
otb::SVMMachineLearningModel::m_SVMType
int m_SVMType
Definition: otbSVMMachineLearningModel.h:151
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::SVMMachineLearningModel::m_Epsilon
double m_Epsilon
Definition: otbSVMMachineLearningModel.h:161
otb::SVMMachineLearningModel::Superclass
MachineLearningModel< TInputValue, TTargetValue > Superclass
Definition: otbSVMMachineLearningModel.h:48
otb::SVMMachineLearningModel::m_TermCriteriaType
int m_TermCriteriaType
Definition: otbSVMMachineLearningModel.h:159
otb::SVMMachineLearningModel::m_MaxIter
int m_MaxIter
Definition: otbSVMMachineLearningModel.h:160
otb::SVMMachineLearningModel::m_OutputC
double m_OutputC
Definition: otbSVMMachineLearningModel.h:167
otbMachineLearningModel.h
otb::SVMMachineLearningModel::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbSVMMachineLearningModel.h:49
otb::SVMMachineLearningModel::m_Gamma
double m_Gamma
Definition: otbSVMMachineLearningModel.h:154
otb::SVMMachineLearningModel::InputSampleType
Superclass::InputSampleType InputSampleType
Definition: otbSVMMachineLearningModel.h:53
otb::SVMMachineLearningModel::TargetListSampleType
Superclass::TargetListSampleType TargetListSampleType
Definition: otbSVMMachineLearningModel.h:57
otb::SVMMachineLearningModel::m_Nu
double m_Nu
Definition: otbSVMMachineLearningModel.h:157
otb::SVMMachineLearningModel::ConfidenceValueType
Superclass::ConfidenceValueType ConfidenceValueType
Definition: otbSVMMachineLearningModel.h:58
otb::SVMMachineLearningModel::m_OutputNu
double m_OutputNu
Definition: otbSVMMachineLearningModel.h:168
otb::SVMMachineLearningModel::m_Coef0
double m_Coef0
Definition: otbSVMMachineLearningModel.h:155
otb::MachineLearningModel
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests,...
Definition: otbMachineLearningModel.h:70
otb::SVMMachineLearningModel::Self
SVMMachineLearningModel Self
Definition: otbSVMMachineLearningModel.h:47
otbOpenCVUtils.h
otb::SVMMachineLearningModel::m_OutputP
double m_OutputP
Definition: otbSVMMachineLearningModel.h:169
otb::MachineLearningModel< TInputValue, TTargetValue >::TargetValueType
MLMTargetTraits< TTargetValue >::ValueType TargetValueType
Definition: otbMachineLearningModel.h:90
otb::SVMMachineLearningModel::m_Degree
double m_Degree
Definition: otbSVMMachineLearningModel.h:153
otb::SVMMachineLearningModel::m_OutputDegree
double m_OutputDegree
Definition: otbSVMMachineLearningModel.h:164
otbRequiresOpenCVCheck.h
otb::SVMMachineLearningModel
OpenCV implementation of SVM algorithm.
Definition: otbSVMMachineLearningModel.h:42
otb::MachineLearningModel::TargetSampleType
MLMTargetTraits< TTargetValue >::SampleType TargetSampleType
Definition: otbMachineLearningModel.h:91
otbSVMMachineLearningModel.hxx
otb::SVMMachineLearningModel::TargetValueType
Superclass::TargetValueType TargetValueType
Definition: otbSVMMachineLearningModel.h:55
otb::SVMMachineLearningModel::m_OutputCoef0
double m_OutputCoef0
Definition: otbSVMMachineLearningModel.h:166