• Manuel Grizonnet's avatar
    ENH: rename files with txx extension to hxx · 3a1fd1fc
    Manuel Grizonnet authored
    OTB followed since the beginning the ITK convention and use .txx extension for all
    template classes. Nevertheless, some development tools do not recognize .txx
    file extension. Other tool like GitHub can't do in-browser syntax highlighting for
    txx files I think.
    
    The root problem is the use of the txx which should be changed to hxx (or hpp).
    
    In 2011, after an in-depth discussion near April 20, 2011 on the
    Insight-Developers mailing list, ITK rename all txx files to hxx (and event
    prevent the push of .txx files with a pre-commit hook). It happens is major release v4.
    
    You can find some arguments in the discussion about the change and also in other
    projects related to ITK which applied the same modification, see for instance VXL:
    
    https://github.com/vxl/vxl/issues/209
    
    This commit apply now the same modification for OTB.
    
    I understand that it will change some habit for developers and don't bring new
    features but I think that in general it is better to stay align with ITK guidelines.
    
    In my opinion, it always facilitate the use of OTB and ITK together if we share
    when we can the same code architecture, directory organization, naming
    conventions...
    3a1fd1fc
otbTrainLibSVM.hxx 8.06 KiB
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 * This file is part of Orfeo Toolbox
 *     https://www.orfeo-toolbox.org/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
#ifndef otbTrainLibSVM_txx
#define otbTrainLibSVM_txx
#include "otbLearningApplicationBase.h"
#include "otbLibSVMMachineLearningModel.h"
namespace otb
namespace Wrapper
  template <class TInputValue, class TOutputValue>
  void
  LearningApplicationBase<TInputValue,TOutputValue>
  ::InitLibSVMParams()
    AddChoice("classifier.libsvm", "LibSVM classifier");
    SetParameterDescription("classifier.libsvm", "This group of parameters allows setting SVM classifier parameters.");
    AddParameter(ParameterType_Choice, "classifier.libsvm.k", "SVM Kernel Type");
    AddChoice("classifier.libsvm.k.linear", "Linear");
    SetParameterDescription("classifier.libsvm.k.linear", 
      "Linear Kernel, no mapping is done, this is the fastest option.");
    AddChoice("classifier.libsvm.k.rbf", "Gaussian radial basis function");
    SetParameterDescription("classifier.libsvm.k.rbf", 
      "This kernel is a good choice in most of the case. It is "
      "an exponential function of the euclidian distance between "
      "the vectors.");
    AddChoice("classifier.libsvm.k.poly", "Polynomial");
    SetParameterDescription("classifier.libsvm.k.poly", 
      "Polynomial Kernel, the mapping is a polynomial function.");
    AddChoice("classifier.libsvm.k.sigmoid", "Sigmoid");
    SetParameterDescription("classifier.libsvm.k.sigmoid", 
      "The kernel is a hyperbolic tangente function of the vectors.");
    SetParameterString("classifier.libsvm.k", "linear");
    SetParameterDescription("classifier.libsvm.k", "SVM Kernel Type.");
    AddParameter(ParameterType_Choice, "classifier.libsvm.m", "SVM Model Type");
    SetParameterDescription("classifier.libsvm.m", "Type of SVM formulation.");
    if (this->m_RegressionFlag)
      AddChoice("classifier.libsvm.m.epssvr", "Epsilon Support Vector Regression");
      SetParameterDescription("classifier.libsvm.m.epssvr",
       "The distance between feature vectors from the training set and the "
       "fitting hyper-plane must be less than Epsilon. For outliers the penalty "
       "multiplier C is used ");
      AddChoice("classifier.libsvm.m.nusvr", "Nu Support Vector Regression");
      SetParameterString("classifier.libsvm.m", "epssvr");
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
SetParameterDescription("classifier.libsvm.m.nusvr", "Same as the epsilon regression except that this time the bounded " "parameter nu is used instead of epsilon"); } else { AddChoice("classifier.libsvm.m.csvc", "C support vector classification"); SetParameterDescription("classifier.libsvm.m.csvc", "This formulation allows imperfect separation of classes. The penalty " "is set through the cost parameter C."); AddChoice("classifier.libsvm.m.nusvc", "Nu support vector classification"); SetParameterDescription("classifier.libsvm.m.nusvc", "This formulation allows imperfect separation of classes. The penalty " "is set through the cost parameter Nu. As compared to C, Nu is harder " "to optimize, and may not be as fast."); AddChoice("classifier.libsvm.m.oneclass", "Distribution estimation (One Class SVM)"); SetParameterDescription("classifier.libsvm.m.oneclass", "All the training data are from the same class, SVM builds a boundary " "that separates the class from the rest of the feature space."); SetParameterString("classifier.libsvm.m", "csvc"); } AddParameter(ParameterType_Float, "classifier.libsvm.c", "Cost parameter C"); SetParameterFloat("classifier.libsvm.c",1.0); SetParameterDescription("classifier.libsvm.c", "SVM models have a cost parameter C (1 by default) to control the " "trade-off between training errors and forcing rigid margins."); AddParameter(ParameterType_Float, "classifier.libsvm.nu", "Cost parameter Nu"); SetParameterFloat("classifier.libsvm.nu",0.5); SetParameterDescription("classifier.libsvm.nu", "Cost parameter Nu, in the range 0..1, the larger the value, " "the smoother the decision."); // It seems that it miss a nu parameter for the nu-SVM use. AddParameter(ParameterType_Bool, "classifier.libsvm.opt", "Parameters optimization"); SetParameterDescription("classifier.libsvm.opt", "SVM parameters optimization flag."); AddParameter(ParameterType_Bool, "classifier.libsvm.prob", "Probability estimation"); SetParameterDescription("classifier.libsvm.prob", "Probability estimation flag."); if (this->m_RegressionFlag) { AddParameter(ParameterType_Float, "classifier.libsvm.eps", "Epsilon"); SetParameterFloat("classifier.libsvm.eps",1e-3); SetParameterDescription("classifier.libsvm.eps", "The distance between feature vectors from the training set and " "the fitting hyper-plane must be less than Epsilon. For outliers" "the penalty mutliplier is set by C."); } } template <class TInputValue, class TOutputValue> void LearningApplicationBase<TInputValue,TOutputValue> ::TrainLibSVM(typename ListSampleType::Pointer trainingListSample, typename TargetListSampleType::Pointer trainingLabeledListSample, std::string modelPath) { typedef otb::LibSVMMachineLearningModel<InputValueType, OutputValueType> LibSVMType; typename LibSVMType::Pointer libSVMClassifier = LibSVMType::New(); libSVMClassifier->SetRegressionMode(this->m_RegressionFlag); libSVMClassifier->SetInputListSample(trainingListSample); libSVMClassifier->SetTargetListSample(trainingLabeledListSample); //SVM Option //TODO : Add other options ? libSVMClassifier->SetParameterOptimization(GetParameterInt("classifier.libsvm.opt")); libSVMClassifier->SetDoProbabilityEstimates(GetParameterInt("classifier.libsvm.prob"));
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
libSVMClassifier->SetNu(GetParameterFloat("classifier.libsvm.nu")); libSVMClassifier->SetC(GetParameterFloat("classifier.libsvm.c")); switch (GetParameterInt("classifier.libsvm.k")) { case 0: // LINEAR libSVMClassifier->SetKernelType(LINEAR); break; case 1: // RBF libSVMClassifier->SetKernelType(RBF); break; case 2: // POLY libSVMClassifier->SetKernelType(POLY); break; case 3: // SIGMOID libSVMClassifier->SetKernelType(SIGMOID); break; default: // DEFAULT = LINEAR libSVMClassifier->SetKernelType(LINEAR); break; } if (this->m_RegressionFlag) { switch (GetParameterInt("classifier.libsvm.m")) { case 0: // EPSILON_SVR libSVMClassifier->SetSVMType(EPSILON_SVR); break; case 1: // NU_SVR libSVMClassifier->SetSVMType(NU_SVR); break; default: libSVMClassifier->SetSVMType(EPSILON_SVR); break; } libSVMClassifier->SetEpsilon(GetParameterFloat("classifier.libsvm.eps")); } else { switch (GetParameterInt("classifier.libsvm.m")) { case 0: // C_SVC libSVMClassifier->SetSVMType(C_SVC); break; case 1: // NU_SVC libSVMClassifier->SetSVMType(NU_SVC); break; case 2: // ONE_CLASS libSVMClassifier->SetSVMType(ONE_CLASS); break; default: libSVMClassifier->SetSVMType(C_SVC); break; } } libSVMClassifier->Train(); libSVMClassifier->Save(modelPath); } } //end namespace wrapper } //end namespace otb #endif