/usr/include/OTB-5.8/otbSVMMachineLearningModel.txx is in libotb-dev 5.8.0+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | /*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef otbSVMMachineLearningModel_txx
#define otbSVMMachineLearningModel_txx
#include <fstream>
#include "itkMacro.h"
#include "otbSVMMachineLearningModel.h"
#include "otbOpenCVUtils.h"
namespace otb
{
template <class TInputValue, class TOutputValue>
SVMMachineLearningModel<TInputValue,TOutputValue>
::SVMMachineLearningModel() :
m_SVMModel (new CvSVM),
m_SVMType(CvSVM::C_SVC),
m_KernelType(CvSVM::RBF),
m_Degree(0),
m_Gamma(1),
m_Coef0(0),
m_C(1),
m_Nu(0),
m_P(0),
m_TermCriteriaType(CV_TERMCRIT_ITER),
m_MaxIter(1000),
m_Epsilon(FLT_EPSILON),
m_ParameterOptimization(false),
m_OutputDegree(0),
m_OutputGamma(1),
m_OutputCoef0(0),
m_OutputC(1),
m_OutputNu(0),
m_OutputP(0)
{
this->m_ConfidenceIndex = true;
this->m_IsRegressionSupported = true;
}
template <class TInputValue, class TOutputValue>
SVMMachineLearningModel<TInputValue,TOutputValue>
::~SVMMachineLearningModel()
{
delete m_SVMModel;
}
/** Train the machine learning model */
template <class TInputValue, class TOutputValue>
void
SVMMachineLearningModel<TInputValue,TOutputValue>
::Train()
{
// Check that the SVM type is compatible with the chosen mode (classif/regression)
if ( bool(m_SVMType == CvSVM::NU_SVR || m_SVMType == CvSVM::EPS_SVR) != this->m_RegressionMode)
{
itkGenericExceptionMacro("SVM type incompatible with chosen mode (classification or regression."
"SVM types for classification are C_SVC, NU_SVC, ONE_CLASS. "
"SVM types for regression are NU_SVR, EPS_SVR");
}
//convert listsample to opencv matrix
cv::Mat samples;
otb::ListSampleToMat<InputListSampleType>(this->GetInputListSample(), samples);
cv::Mat labels;
otb::ListSampleToMat<TargetListSampleType>(this->GetTargetListSample(),labels);
// Set up SVM's parameters
CvTermCriteria term_crit = cvTermCriteria(m_TermCriteriaType, m_MaxIter, m_Epsilon);
CvSVMParams params( m_SVMType, m_KernelType, m_Degree, m_Gamma, m_Coef0, m_C, m_Nu, m_P, ITK_NULLPTR , term_crit );
// Train the SVM
if (!m_ParameterOptimization)
{
m_SVMModel->train(samples, labels, cv::Mat(), cv::Mat(), params);
}
else
{
//Trains SVM with optimal parameters.
//train_auto(const Mat& trainData, const Mat& responses, const Mat& varIdx, const Mat& sampleIdx,
//CvSVMParams params, int k_fold=10, CvParamGrid Cgrid=CvSVM::get_default_grid(CvSVM::C),
//CvParamGrid gammaGrid=CvSVM::get_default_grid(CvSVM::GAMMA),
//CvParamGrid pGrid=CvSVM::get_default_grid(CvSVM::P), CvParamGrid nuGrid=CvSVM::get_default_grid(CvSVM::NU),
//CvParamGrid coeffGrid=CvSVM::get_default_grid(CvSVM::COEF), CvParamGrid degreeGrid=CvSVM::get_default_grid(CvSVM::DEGREE),
//bool balanced=false)
//We used default parameters grid. If not enough, those grids should be expose to the user.
m_SVMModel->train_auto(samples, labels, cv::Mat(), cv::Mat(), params);
}
// Export of the SVM parameters into the class SVMMachineLearningModel
m_OutputDegree = m_SVMModel->get_params().degree;
m_OutputGamma = m_SVMModel->get_params().gamma;
m_OutputCoef0 = m_SVMModel->get_params().coef0;
m_OutputC = m_SVMModel->get_params().C;
m_OutputNu = m_SVMModel->get_params().nu;
m_OutputP = m_SVMModel->get_params().p;
}
template <class TInputValue, class TOutputValue>
typename SVMMachineLearningModel<TInputValue,TOutputValue>
::TargetSampleType
SVMMachineLearningModel<TInputValue,TOutputValue>
::DoPredict(const InputSampleType & input, ConfidenceValueType *quality) const
{
//convert listsample to Mat
cv::Mat sample;
otb::SampleToMat<InputSampleType>(input,sample);
double result = m_SVMModel->predict(sample,false);
TargetSampleType target;
target[0] = static_cast<TOutputValue>(result);
if (quality != ITK_NULLPTR)
{
(*quality) = m_SVMModel->predict(sample,true);
}
return target;
}
template <class TInputValue, class TOutputValue>
void
SVMMachineLearningModel<TInputValue,TOutputValue>
::Save(const std::string & filename, const std::string & name)
{
if (name == "")
m_SVMModel->save(filename.c_str(), ITK_NULLPTR);
else
m_SVMModel->save(filename.c_str(), name.c_str());
}
template <class TInputValue, class TOutputValue>
void
SVMMachineLearningModel<TInputValue,TOutputValue>
::Load(const std::string & filename, const std::string & name)
{
if (name == "")
m_SVMModel->load(filename.c_str(), ITK_NULLPTR);
else
m_SVMModel->load(filename.c_str(), name.c_str());
}
template <class TInputValue, class TOutputValue>
bool
SVMMachineLearningModel<TInputValue,TOutputValue>
::CanReadFile(const std::string & file)
{
std::ifstream ifs;
ifs.open(file.c_str());
if(!ifs)
{
std::cerr<<"Could not read file "<<file<<std::endl;
return false;
}
while (!ifs.eof())
{
std::string line;
std::getline(ifs, line);
//if (line.find(m_SVMModel->getName()) != std::string::npos)
if (line.find(CV_TYPE_NAME_ML_SVM) != std::string::npos)
{
//std::cout<<"Reading a "<<CV_TYPE_NAME_ML_SVM<<" model"<<std::endl;
return true;
}
}
ifs.close();
return false;
}
template <class TInputValue, class TOutputValue>
bool
SVMMachineLearningModel<TInputValue,TOutputValue>
::CanWriteFile(const std::string & itkNotUsed(file))
{
return false;
}
template <class TInputValue, class TOutputValue>
void
SVMMachineLearningModel<TInputValue,TOutputValue>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
// Call superclass implementation
Superclass::PrintSelf(os,indent);
}
} //end namespace otb
#endif
|