From 614af727e1e8cf9f0e7dd902ca1060fce2a77429 Mon Sep 17 00:00:00 2001 From: Julien Michel <julien.michel@orfeo-toolbox.org> Date: Mon, 23 Mar 2015 15:13:56 +0100 Subject: [PATCH] ENH: Removing all code relying on the patched LibSVM interface --- .../include/otbChangeProfileKernelFunctor.h | 91 -- .../include/otbMixturePolyRBFKernelFunctor.h | 93 -- .../include/otbNonGaussianRBFKernelFunctor.h | 89 -- .../SVMLearning/include/otbSVMKernels.h | 1119 ----------------- .../include/otbSpectralAngleKernelFunctor.h | 94 -- .../Learning/SVMLearning/src/CMakeLists.txt | 4 - .../src/otbChangeProfileKernelFunctor.cxx | 77 -- .../src/otbMixturePolyRBFKernelFunctor.cxx | 125 -- .../src/otbNonGaussianRBFKernelFunctor.cxx | 72 -- .../src/otbSpectralAngleKernelFunctor.cxx | 67 - .../Learning/SVMLearning/test/CMakeLists.txt | 113 -- ...leKernelFunctorImageClassificationTest.cxx | 89 -- ...leKernelFunctorImageModelEstimatorTest.cxx | 79 -- .../test/otbSVMLearningTestDriver.cxx | 7 - .../otbSVMModelCopyComposedKernelTest.cxx | 48 - .../test/otbSVMModelCopyGenericKernelTest.cxx | 51 - .../test/otbSVMModelGenericKernelLoadSave.cxx | 83 -- .../test/otbSVMModelGenericKernelsTest.cxx | 387 ------ 18 files changed, 2688 deletions(-) delete mode 100644 Modules/Learning/SVMLearning/include/otbChangeProfileKernelFunctor.h delete mode 100644 Modules/Learning/SVMLearning/include/otbMixturePolyRBFKernelFunctor.h delete mode 100644 Modules/Learning/SVMLearning/include/otbNonGaussianRBFKernelFunctor.h delete mode 100644 Modules/Learning/SVMLearning/include/otbSVMKernels.h delete mode 100644 Modules/Learning/SVMLearning/include/otbSpectralAngleKernelFunctor.h delete mode 100644 Modules/Learning/SVMLearning/src/otbChangeProfileKernelFunctor.cxx delete mode 100644 Modules/Learning/SVMLearning/src/otbMixturePolyRBFKernelFunctor.cxx delete mode 100644 Modules/Learning/SVMLearning/src/otbNonGaussianRBFKernelFunctor.cxx delete mode 100644 Modules/Learning/SVMLearning/src/otbSpectralAngleKernelFunctor.cxx delete mode 100644 Modules/Learning/SVMLearning/test/otbSVMInverseCosSpectralAngleKernelFunctorImageClassificationTest.cxx delete mode 100644 Modules/Learning/SVMLearning/test/otbSVMInverseCosSpectralAngleKernelFunctorImageModelEstimatorTest.cxx delete mode 100644 Modules/Learning/SVMLearning/test/otbSVMModelCopyComposedKernelTest.cxx delete mode 100644 Modules/Learning/SVMLearning/test/otbSVMModelCopyGenericKernelTest.cxx delete mode 100644 Modules/Learning/SVMLearning/test/otbSVMModelGenericKernelLoadSave.cxx delete mode 100644 Modules/Learning/SVMLearning/test/otbSVMModelGenericKernelsTest.cxx diff --git a/Modules/Learning/SVMLearning/include/otbChangeProfileKernelFunctor.h b/Modules/Learning/SVMLearning/include/otbChangeProfileKernelFunctor.h deleted file mode 100644 index 3a9d13b2dd..0000000000 --- a/Modules/Learning/SVMLearning/include/otbChangeProfileKernelFunctor.h +++ /dev/null @@ -1,91 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - Copyright (c) GET / ENST Bretagne. All rights reserved. - See GETCopyright.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 __otbChangeProfileKernelFunctor_h -#define __otbChangeProfileKernelFunctor_h - -#include "otb_libsvm.h" - -namespace otb -{ - -/** - * \class ChangeProfileKernelFunctor - * \brief Change Profile Kernel. - * - * Performs a decision point of view dedicated to the multiscale - * change profile delivered by otb::KullbackLeiblerProfileImageFilter. - * - * It is implemented as a polynomial kernel: - * \f$ \exp - \left( \gamma \left( \max_i | x_i - y_i | \right)^\textrm{degree} + \textrm{coef} \right) \f$. - * - * The parameters \f$ \gamma \f$, \f$ \textrm{coef} \f$ and \f$ \textrm{degree} \f$ are - * to be fixed through \code SetValue \endcode with keywords: Gamma (def 1.0), - * Coef (def 1.0) and Degree (def 1.0). - * - * \ingroup OTBSVMLearning - */ - -class ChangeProfileKernelFunctor - : public GenericKernelFunctorBase -{ -public: - typedef ChangeProfileKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - double operator ()(const svm_node * x, const svm_node * y, - const svm_parameter& param) const; - - ChangeProfileKernelFunctor (); - virtual ~ChangeProfileKernelFunctor () {} - - /** Specific implementation of \code Update \endcode to split m_MapParameters - * into specific variables to speed up kernel evaluations */ - void Update(); - -protected: - ChangeProfileKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - ChangeProfileKernelFunctor& - operator=(const Self& copy) - { - Superclass::operator =(copy); - Update(); - return *this; - } - -private: - double m_Coef; - double m_Degree; - double m_Gamma; -}; - -} // end of namespace otb - -#endif diff --git a/Modules/Learning/SVMLearning/include/otbMixturePolyRBFKernelFunctor.h b/Modules/Learning/SVMLearning/include/otbMixturePolyRBFKernelFunctor.h deleted file mode 100644 index 9af23edab8..0000000000 --- a/Modules/Learning/SVMLearning/include/otbMixturePolyRBFKernelFunctor.h +++ /dev/null @@ -1,93 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - Copyright (c) GET / ENST Bretagne. All rights reserved. - See GETCopyright.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 __otbMixturePolyRBFKernelFunctor_h -#define __otbMixturePolyRBFKernelFunctor_h - -#include "otb_libsvm.h" - -namespace otb -{ - -/** \class MixturePolyRBFKernelFunctor - * \brief Mixture of kernels. - * - * Performs the mixture of kind: \f$ \mu k_1(x, y) + (1-\mu) k_2(x, y) \f$ - * with \f$ k_1(x, y)=\left( \gamma_1 x\cdot y + c_0 \right) ^d \f$ a - * polynomial kernel and - * \f$ k_2(x, y) = \exp\left( - \gamma_2 \| x-y\-^2 \right) \f$ an RBF one. - * - * Variable to be instanciated (through \code SetValue \endcode) are: - * Mixture (def=0.5), GammaPoly (def=1.0), CoefPoly (def=1.0), - * DegreePoly (def=2), GammaRBF (def=1.0) - * - * \ingroup OTBSVMLearning - */ - -class MixturePolyRBFKernelFunctor - : public GenericKernelFunctorBase -{ -public: - typedef MixturePolyRBFKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - double operator ()(const svm_node * x, const svm_node * y, - const svm_parameter& param) const; - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - MixturePolyRBFKernelFunctor (); - virtual ~MixturePolyRBFKernelFunctor () {} - - /** Specific implementation of \code Update \endcode to split m_MapParameters - * into specific variables to speed up kernel evaluations */ - void Update(); - -protected: - MixturePolyRBFKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - MixturePolyRBFKernelFunctor& - operator=(const Self& copy) - { - Superclass::operator =(copy); - Update(); - return *this; - } - - double m_Mixture; - double m_GammaPoly; - double m_CoefPoly; - int m_DegreePoly; - double m_GammaRBF; - -private: - inline double powi(double base, int times) const; -}; - -} // end of namespace otb - -#endif diff --git a/Modules/Learning/SVMLearning/include/otbNonGaussianRBFKernelFunctor.h b/Modules/Learning/SVMLearning/include/otbNonGaussianRBFKernelFunctor.h deleted file mode 100644 index c7f6bb6db0..0000000000 --- a/Modules/Learning/SVMLearning/include/otbNonGaussianRBFKernelFunctor.h +++ /dev/null @@ -1,89 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - Copyright (c) GET / ENST Bretagne. All rights reserved. - See GETCopyright.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 __otbNonGaussianRBFKernelFunctor_h -#define __otbNonGaussianRBFKernelFunctor_h - -#include "otb_libsvm.h" - -//FIXME: shouldn't it be in the Functor namespace? -namespace otb -{ -/** \class NonGaussianRBFKernelFunctor - * \brief Performs an RBF kernel evaluation that better suit sample distribution with high Kurtosis. - * - * It is of kind - * \f$ \exp\left( - \gamma \sum_i | x_i^\alpha - y_i^\alpha |^\beta \right) \f$ - * where \f$ 0 \leqslant \alpha \leqslant 1 \f$ and - * \f$ 0 \leqslant \beta \leqslant 2 \f$. - * - * Variables to be instanciated (through \code SetValue \endcode) are: - * Alpha (def=1), Beta (def=2) and Gamma (def 1.0). - * - * - * \ingroup OTBSVMLearning - */ -class NonGaussianRBFKernelFunctor - : public GenericKernelFunctorBase -{ -public: - typedef NonGaussianRBFKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - double operator ()(const svm_node * x, const svm_node * y, - const svm_parameter& param) const; - - NonGaussianRBFKernelFunctor (); - virtual ~NonGaussianRBFKernelFunctor () {} - - /** Specific implementation of \code Update \endcode to split m_MapParameters - * into specific variables to speed up kernel evaluations */ - void Update(); - -protected: - NonGaussianRBFKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - NonGaussianRBFKernelFunctor& - operator=(const Self& copy) - { - Superclass::operator =(copy); - Update(); - return *this; - } - -protected: - double m_Alpha; - double m_Beta; - double m_Gamma; - -}; - -} // end of namespace otb - -#endif diff --git a/Modules/Learning/SVMLearning/include/otbSVMKernels.h b/Modules/Learning/SVMLearning/include/otbSVMKernels.h deleted file mode 100644 index 21b88af547..0000000000 --- a/Modules/Learning/SVMLearning/include/otbSVMKernels.h +++ /dev/null @@ -1,1119 +0,0 @@ -/*========================================================================= - - 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 __otbSVMKernels_h -#define __otbSVMKernels_h - -#include <vector> -#include <algorithm> // for std::find -#include <cstring> // for strpbrk - -#include "itkNumericTraits.h" -#include "otbMath.h" - -// Existing kernels -#include "otbSpectralAngleKernelFunctor.h" -#include "otbChangeProfileKernelFunctor.h" -#include "otbNonGaussianRBFKernelFunctor.h" -#include "otbMixturePolyRBFKernelFunctor.h" - -#include "otb_libsvm.h" - -namespace otb -{ -/** \class CustomKernelFunctor - * \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class CustomKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef CustomKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - CustomKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("custom"); - } - virtual ~CustomKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& /*param*/) const - { - return (dot(x, x) - 2.0 * dot(x, y) + dot(y, y)); - } - -protected: - CustomKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - CustomKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } -}; - -/** \class InvMultiQuadricKernelFunctor - * \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class InvMultiQuadricKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef InvMultiQuadricKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - InvMultiQuadricKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("invMultiQuadric"); - this->SetValue<double>("const_coef", 1); - } - virtual ~InvMultiQuadricKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - double mq = this->GetValue<double>("const_coef") + m_Custom(x, y, param); - if (mq == 0.0) - { - return itk::NumericTraits<double>::max(); - } - return 1.0 / sqrt(mq); - } - -protected: - InvMultiQuadricKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - InvMultiQuadricKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - CustomKernelFunctor m_Custom; -}; - -/** \class KModKernelFunctor - * \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class KModKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef KModKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - KModKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("KMod"); - this->SetValue<double>("const_coef", 1); - } - virtual ~KModKernelFunctor() {} - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - double mq = this->GetValue<double>("const_coef") + m_Custom(x, y, param); - - if (mq == 0.0) - { - return itk::NumericTraits<double>::max(); - } - return exp(param.gamma / mq) - 1.0; - } - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - -protected: - KModKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - KModKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - CustomKernelFunctor m_Custom; -}; - -/** \class SAMKernelFunctor - * \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class SAMKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef SAMKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - SAMKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("SAM"); - } - virtual ~SAMKernelFunctor() {} - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& /*param*/) const - { - double den = dot(x, x) * dot(y, y); - if (den <= 0.) - { - return 0.0; - } - double ss = dot(x, y); - return vcl_acos(ss / vcl_sqrt(den)); - } - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - -protected: - SAMKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - SAMKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } -}; - -/** \class RadialSAMKernelFunctor - * \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class RadialSAMKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef RadialSAMKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - RadialSAMKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("RadialSAM"); - } - virtual ~RadialSAMKernelFunctor() {} - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - return vcl_exp(-param.gamma * m_Sam(x, y, param)); - } - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - -protected: - RadialSAMKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - RadialSAMKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - SAMKernelFunctor m_Sam; -}; - -/** \class InverseCosSAMKernelFunctor - * \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class InverseCosSAMKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef InverseCosSAMKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - InverseCosSAMKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("InverseCosSAM"); - } - virtual ~InverseCosSAMKernelFunctor() {} - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - return 1.0 - vcl_cos(m_Sam(x, y, param)); - } - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - -protected: - InverseCosSAMKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - InverseCosSAMKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - SAMKernelFunctor m_Sam; -}; - -/** \class InvMultiQuadraticSAMKernelFunctor - * \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class InvMultiQuadraticSAMKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef InvMultiQuadraticSAMKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - InvMultiQuadraticSAMKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("invMultiQuadraticSAM"); - this->SetValue<double>("const_coef", 1); - } - virtual ~InvMultiQuadraticSAMKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - double mq = this->GetValue<double>("const_coef") + m_Sam(x, y, param); - - if (mq == 0.) - { - return itk::NumericTraits<double>::max(); - } - return 1. / sqrt(mq); - } - -protected: - InvMultiQuadraticSAMKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - InvMultiQuadraticSAMKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - SAMKernelFunctor m_Sam; -}; - -/** \class KModSAMKernelFunctor - * \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class KModSAMKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef KModSAMKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - KModSAMKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("KModSAM"); - this->SetValue<double>("const_coef", 1); - } - virtual ~KModSAMKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - double mq = this->GetValue<double>("const_coef") + m_Sam(x, y, param); - - if (mq == 0.) - { - return itk::NumericTraits<double>::max(); - } - return vcl_exp(param.gamma / mq) - 1.0; - } - -protected: - KModSAMKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - KModSAMKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - SAMKernelFunctor m_Sam; -}; - -/** \class RBFKernelFunctor - * \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class RBFKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef RBFKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - RBFKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("RBF"); - this->SetValue<double>("gamma_coef", 0.5); - } - virtual ~RBFKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - double res = this->GetValue<double>("gamma_coef") * m_Custom(x, y, param); - return vcl_exp(-res); - } - - virtual double derivative(const svm_node *x, - const svm_node *y, - const svm_parameter& param, - int degree, - int index, - bool isAtEnd, - double constValue) const - { - double gamma = this->GetValue<double>("gamma_coef"); - double kernelValue = 0.; - double xval = 0.; - double yval = 0.; - int compt = 0; - - const svm_node *xtemp = x; - const svm_node *ytemp = y; - - bool stop = false; - while (xtemp->index != -1 && ytemp->index != -1 && stop == false) - { - if (xtemp->index == ytemp->index) - { - if (compt == index) - { - xval = xtemp->value; - yval = ytemp->value; - stop = true; - } - else - { - compt++; - ++ytemp; - ++xtemp; - } - } - else - { - if (xtemp->index > ytemp->index) ++ytemp; - else ++xtemp; - } - } - - if (isAtEnd == true) - { - kernelValue = this->operator ()(x, y, param); - } - else - { - kernelValue = constValue; - } - - if (degree < 0) - { - return 0; - } - switch (degree) - { - case 0: - return kernelValue; - break; - case 1: - return (-2 * gamma * (yval - xval) * kernelValue); - break; - default: - return (-2 * gamma * - ((degree - 1) * - this->derivative(x, y, param, degree - 2, index, isAtEnd, constValue) + - (yval - xval) * derivative(x, y, param, degree - 1, index, isAtEnd, constValue))); - break; - } - - } - -protected: - RBFKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - RBFKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - CustomKernelFunctor m_Custom; -}; - -/** \class RBFRBFSAMKernelFunctor -* \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class RBFRBFSAMKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef RBFRBFSAMKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - RBFRBFSAMKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("RBFRBFSAM"); - this->SetValue<double>("lin_coef", 1.); - } - virtual ~RBFRBFSAMKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - return (this->GetValue<double>("lin_coef") * vcl_exp(-param.gamma * m_Custom(x, y, param)) - + (1.0 - this->GetValue<double>("lin_coef")) * m_RadialSam(x, y, param)); - } - -protected: - RBFRBFSAMKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - RBFRBFSAMKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - CustomKernelFunctor m_Custom; - RadialSAMKernelFunctor m_RadialSam; -}; - -/** \class PolyRBFSAMKernelFunctor -* \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class PolyRBFSAMKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef PolyRBFSAMKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - PolyRBFSAMKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("polyRBFSAM"); - this->SetValue<double>("const_coef", 1); - this->SetValue<double>("lin_coef", 1); - } - virtual ~PolyRBFSAMKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - return this->GetValue<double>("const_lin") * vcl_pow(dot(x, y) + this->GetValue<double>("const_coef"), param.degree) - + (1.0 - this->GetValue<double>("const_coef")) - * m_RadialSam(x, y, param); - } - -protected: - PolyRBFSAMKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - PolyRBFSAMKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - RadialSAMKernelFunctor m_RadialSam; -}; - -/** \class RBFDiffKernelFunctor -* \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class RBFDiffKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef RBFDiffKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - RBFDiffKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("RBFDiff"); - } - virtual ~RBFDiffKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - - double total = 0.; - - while (x->index != -1 && y->index != -1) - { - if (x->index == y->index) - { - total = total + vcl_exp(-param.gamma * vcl_abs(x->value - y->value)); - ++x; - ++y; - } - else - { - if (x->index < y->index) ++x; - else ++y; - } - } - return total; - } - -protected: - RBFDiffKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - RBFDiffKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -}; - -/** \class CustomLinearKernelFunctor -* \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class CustomLinearKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef CustomLinearKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - CustomLinearKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("customLinear"); - } - virtual ~CustomLinearKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - double value=-param.gamma*m_Custom(x, y, param); - if(value<-30.0) - { - return 0.0; - } - - return (vcl_exp(value)); - } - -protected: - CustomLinearKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - CustomLinearKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - CustomKernelFunctor m_Custom; -}; - -/** \class GroupedRBFKernelFunctor -* \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class GroupedRBFKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef GroupedRBFKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - GroupedRBFKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("groupedRBF"); - } - virtual ~GroupedRBFKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - const char * parameters = param.custom; - const char * twoPoints = ":"; - const char * position = strpbrk (parameters, twoPoints); - int twoPointsPosition = 0; - double total = 0.; - double value = 0.; - int numberOfGroups = atoi(parameters); - - twoPointsPosition = position - parameters; - - parameters = parameters + twoPointsPosition + 1; - - int i, j; - std::vector<int> begin; - begin.resize(numberOfGroups); - std::vector<int> end; - end.resize(numberOfGroups); - - for (i = 0; i < numberOfGroups; ++i) - { - begin[i] = atoi(parameters); - position = strpbrk (parameters, twoPoints); - twoPointsPosition = position - parameters; - parameters = parameters + twoPointsPosition + 1; - - end[i] = atoi(parameters); - parameters = parameters + twoPointsPosition + 1; - } - - const svm_node * xBuff = x; - const svm_node * yBuff = y; - - int sizeX = 0; - int sizeY = 0; - - while (xBuff->index != 1) - { - ++sizeX; - ++xBuff; - } - - while (yBuff->index != 1) - { - ++sizeY; - ++yBuff; - } - - const svm_node * xTemp = NULL; - const svm_node * yTemp = NULL; - - int index = 0; - - if (sizeX && sizeY) - { - svm_node* xGroup = new svm_node[sizeX]; - svm_node* yGroup = new svm_node[sizeY]; - for (j = 0; j < numberOfGroups; ++j) - { - xTemp = x; - yTemp = y; - index = 0; - - while (xTemp->index != 1 && yTemp->index != 1) - { - xGroup[index].index = xTemp->index; - yGroup[index].index = yTemp->index; - if ((xTemp->index < begin[j]) || (xTemp->index > end[j])) - { - xGroup[index].value = 0; - yGroup[index].value = 0; - } - else - { - xGroup[index].value = xTemp->value; - yGroup[index].value = yTemp->value; - } - ++index; - ++xTemp; - ++yTemp; - } - - // value can have different value according to j - value = m_CustomLinear(xGroup, yGroup, param); - - total += value; - } - delete[] xGroup; - delete[] yGroup; - } - - else if ((sizeX > 0) && (sizeY == 0)) - { - svm_node* xGroup = new svm_node[sizeX]; - svm_node* yGroup = new svm_node[sizeY]; - - for (j = 0; j < numberOfGroups; ++j) - { - xTemp = x; - index = 0; - - while (xTemp->index != -1) - { - xGroup[index].index = xTemp->index; - yGroup[index].index = xTemp->index; - - if ((xTemp->index < begin[j]) || (xTemp->index > end[j])) - { - xGroup[index].value = 0; - yGroup[index].value = 0; - } - else - { - xGroup[index].value = xTemp->value; - yGroup[index].value = 0; - } - ++index; - ++xTemp; - } - - // value can have different value according to j - value = m_CustomLinear(xGroup, yGroup, param); - - total += value; - } - delete[] xGroup; - delete[] yGroup; - } - - else - { - total = static_cast<double>(numberOfGroups) * m_CustomLinear(x, y, param); - } - - if (xTemp != NULL) delete xTemp; - if (yTemp != NULL) delete yTemp; - - return total; - } - -protected: - GroupedRBFKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - GroupedRBFKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -private: - CustomLinearKernelFunctor m_CustomLinear; -}; - -/** \class GroupingAdaptiveKernelFunctor - * \brief Undocumented - * - * \ingroup OTBSVMLearning - */ -class GroupingAdaptiveKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef GroupingAdaptiveKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - GroupingAdaptiveKernelFunctor() : GenericKernelFunctorBase() - { - this->SetName("groupingAdaptive"); - this->SetValue<double>("lin_coef", 1.); - this->SetValue<double>("const_coef", 1.); - } - virtual ~GroupingAdaptiveKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const - { - const char * parameters = param.custom; - const char * twoPoints = ":"; - const char * position = strpbrk (parameters, twoPoints); - int twoPointsPosition = 0; - double total = 0.; - double value = 0.; - int numberOfGroups = atoi(parameters); - - twoPointsPosition = position - parameters; - - parameters = parameters + twoPointsPosition + 1; - - int i, j; - - std::vector<int> begin; - begin.resize(numberOfGroups); - std::vector<int> end; - end.resize(numberOfGroups); - - for (i = 0; i < numberOfGroups; ++i) - { - begin[i] = atoi(parameters); - position = strpbrk (parameters, twoPoints); - twoPointsPosition = position - parameters; - parameters = parameters + twoPointsPosition + 1; - - end[i] = atoi(parameters); - parameters = parameters + twoPointsPosition + 1; - } - - const svm_node * xBuff = x; - const svm_node * yBuff = y; - - int sizeX = 0; - int sizeY = 0; - - while (xBuff->index != 1) - { - ++sizeX; - ++xBuff; - } - - while (yBuff->index != 1) - { - ++sizeY; - ++yBuff; - } - - const svm_node * xTemp = NULL; - const svm_node * yTemp = NULL; - - int index = 0; - - if (sizeX && sizeY) - { - svm_node* xGroup = new svm_node[sizeX]; - svm_node* yGroup = new svm_node[sizeY]; - - for (j = 0; j < numberOfGroups; ++j) - { - xTemp = x; - yTemp = y; - index = 0; - - while (xTemp->index != 1 && yTemp->index != 1) - { - xGroup[index].index = xTemp->index; - yGroup[index].index = yTemp->index; - if ((xTemp->index < begin[j]) || (xTemp->index > end[j])) - { - xGroup[index].value = 0; - yGroup[index].value = 0; - } - else - { - xGroup[index].value = xTemp->value; - yGroup[index].value = yTemp->value; - } - ++index; - ++xTemp; - ++yTemp; - } - - // value can have different value according to j - value = vcl_pow(this->GetValue<double>("lin_coef") * dot(xGroup, yGroup) + this->GetValue<double>( - "const_coef"), static_cast<double>(param.degree)); - - total += value; - } - delete[] xGroup; - delete[] yGroup; - } - - else if ((sizeX > 0) && (sizeY == 0)) - { - svm_node* xGroup = new svm_node[sizeX]; - svm_node* yGroup = new svm_node[sizeY]; - - for (j = 0; j < numberOfGroups; ++j) - { - xTemp = x; - index = 0; - - while (xTemp->index != 1) - { - xGroup[index].index = xTemp->index; - yGroup[index].index = xTemp->index; - - if ((xTemp->index < begin[j]) || (xTemp->index > end[j])) - { - xGroup[index].value = 0; - yGroup[index].value = 0; - } - else - { - xGroup[index].value = xTemp->value; - yGroup[index].value = 0; - } - ++index; - ++xTemp; - } - - // value can have different value according to j - value = vcl_pow(this->GetValue<double>("lin_coef") * dot(xGroup, yGroup) + this->GetValue<double>( - "const_coef"), static_cast<double>(param.degree)); - - total += value; - } - delete[] xGroup; - delete[] yGroup; - } - - else - { - CustomKernelFunctor custom; - total = static_cast<double>(numberOfGroups) * custom(x, y, param); - } - - return total; - } - - -protected: - GroupingAdaptiveKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - GroupingAdaptiveKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } - -}; - -} -#endif diff --git a/Modules/Learning/SVMLearning/include/otbSpectralAngleKernelFunctor.h b/Modules/Learning/SVMLearning/include/otbSpectralAngleKernelFunctor.h deleted file mode 100644 index e1578db9ca..0000000000 --- a/Modules/Learning/SVMLearning/include/otbSpectralAngleKernelFunctor.h +++ /dev/null @@ -1,94 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - Copyright (c) GET / ENST Bretagne. All rights reserved. - See GETCopyright.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 __otbSpectralAngleKernelFunctor_h -#define __otbSpectralAngleKernelFunctor_h - -#include "otb_libsvm.h" - -namespace otb -{ - -/** \class SpectralAngleKernelFunctor - * \brief Spectral Angle Kernel - * - * Performs an inverse multiquadric kernel evaluation that suits - * spectral data. - * - * It is of kind: - * - * \f$ frac{1}{\sqrt{ c_0 + \cos\textrm{SAM(x, y)} }} \f$ where \f$ \textrm{SAM(x, y)} \f$ is - * Spectral Angle Measure: - * \f$ \textrm{SAM}(x, y) = \cos^{-1}\left( \frac{x \cdot y}{\|x\| \|y\|} \right) \f$. - * - * The only parameter \f$ c_0 \f$ is to be fixed by the \code SetValue \endcode - * interface with keyword Coef (def 2.0). - * - * - * \ingroup OTBSVMLearning - */ - -class SpectralAngleKernelFunctor - : public GenericKernelFunctorBase -{ -public: - typedef SpectralAngleKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - double operator ()(const svm_node * x, const svm_node * y, - const svm_parameter& param) const; - - SpectralAngleKernelFunctor (); - virtual ~SpectralAngleKernelFunctor () {} - - /** Specific implementation of \code Update \endcode to split m_MapParameters - * into specific variables to speed up kernel evaluations */ - void Update(); - -protected: - SpectralAngleKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - SpectralAngleKernelFunctor& - operator=(const Self& copy) - { - Superclass::operator =(copy); - Update(); - return *this; - } - -private: - - double SAM(const svm_node * x, const svm_node * y) const; - - double m_Coef; -}; - -} // end of namespace otb - -#endif diff --git a/Modules/Learning/SVMLearning/src/CMakeLists.txt b/Modules/Learning/SVMLearning/src/CMakeLists.txt index 9ae454f571..88b7068d75 100644 --- a/Modules/Learning/SVMLearning/src/CMakeLists.txt +++ b/Modules/Learning/SVMLearning/src/CMakeLists.txt @@ -1,9 +1,5 @@ set(OTBSVMLearning_SRC - otbChangeProfileKernelFunctor.cxx otbExhaustiveExponentialOptimizer.cxx - otbNonGaussianRBFKernelFunctor.cxx - otbMixturePolyRBFKernelFunctor.cxx - otbSpectralAngleKernelFunctor.cxx ) add_library(OTBSVMLearning ${OTBSVMLearning_SRC}) diff --git a/Modules/Learning/SVMLearning/src/otbChangeProfileKernelFunctor.cxx b/Modules/Learning/SVMLearning/src/otbChangeProfileKernelFunctor.cxx deleted file mode 100644 index 91d2db41ea..0000000000 --- a/Modules/Learning/SVMLearning/src/otbChangeProfileKernelFunctor.cxx +++ /dev/null @@ -1,77 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - Copyright (c) GET / ENST Bretagne. All rights reserved. - See GETCopyright.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. - -=========================================================================*/ -#include <cmath> -#include "otbChangeProfileKernelFunctor.h" - -namespace otb -{ - -ChangeProfileKernelFunctor -::ChangeProfileKernelFunctor () - : GenericKernelFunctorBase () -{ - this->SetName("ChangeProfile"); - m_Coef = 0.0; - m_Degree = 1.0; - m_Gamma = 1.0; - - SetValue("Coef", m_Coef); - SetValue("Degree", m_Degree); - SetValue("Gamma", m_Gamma); -} - -void -ChangeProfileKernelFunctor -::Update() -{ - m_Coef = GetValue<double>("Coef"); - m_Degree = GetValue<double>("Degree"); - m_Gamma = GetValue<double>("Gamma"); -} - -double -ChangeProfileKernelFunctor -::operator ()(const svm_node * x, const svm_node * y, - const svm_parameter& /*param*/) const -{ - double theMax(0.); - double theCur(0.); - - while (x->index != -1 && y->index != -1) - { - if (x->index == y->index) - { - theCur = fabs(x->value - y->value); - ++x; - ++y; - } - else - { - if (x->index > y->index) ++y; - else ++x; - } - - if (theCur > theMax) theMax = theCur; - } - - return exp(-m_Gamma * pow(theMax, m_Degree) + m_Coef); -} - -} // end of namespace otb diff --git a/Modules/Learning/SVMLearning/src/otbMixturePolyRBFKernelFunctor.cxx b/Modules/Learning/SVMLearning/src/otbMixturePolyRBFKernelFunctor.cxx deleted file mode 100644 index 2ad3d72087..0000000000 --- a/Modules/Learning/SVMLearning/src/otbMixturePolyRBFKernelFunctor.cxx +++ /dev/null @@ -1,125 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - Copyright (c) GET / ENST Bretagne. All rights reserved. - See GETCopyright.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. - -=========================================================================*/ -#include <cmath> -#include "otbMixturePolyRBFKernelFunctor.h" - -namespace otb -{ - -MixturePolyRBFKernelFunctor -::MixturePolyRBFKernelFunctor () - : GenericKernelFunctorBase () -{ - this->SetName("MixturePolyRBF"); - m_Mixture = 0.5; - m_GammaPoly = 1.0; - m_CoefPoly = 1.0; - m_DegreePoly = 2; - m_GammaRBF = 1.0; - - SetValue("Mixture", m_Mixture); - SetValue("GammaPoly", m_GammaPoly); - SetValue("CoefPoly", m_CoefPoly); - SetValue("DegreePoly", m_DegreePoly); - SetValue("GammaRBF", m_GammaRBF); -} - -double -MixturePolyRBFKernelFunctor -::operator ()(const svm_node * x, const svm_node * y, - const svm_parameter& /*param*/) const -{ - double kernel_poly = 0.0; - if (m_Mixture > 0.0) - kernel_poly = powi(m_GammaPoly * this->dot(x, y) + m_CoefPoly, - m_DegreePoly); - - double kernel_rbf = 0.0; - if (m_Mixture < 1.0) - { - double sum = 0.0; - while (x->index != -1 && y->index != -1) - { - if (x->index == y->index) - { - double d = x->value - y->value; - sum += d * d; - ++x; - ++y; - } - else - { - if (x->index > y->index) - { - sum += y->value * y->value; - ++y; - } - else - { - sum += x->value * x->value; - ++x; - } - } - } - - while (x->index != -1) - { - sum += x->value * x->value; - ++x; - } - - while (y->index != -1) - { - sum += y->value * y->value; - ++y; - } - - kernel_rbf = exp(-m_GammaRBF * sum); - } - - return m_Mixture * kernel_poly + (1.0 - m_Mixture) * kernel_rbf; -} - -void -MixturePolyRBFKernelFunctor -::Update() -{ - m_Mixture = GetValue<double>("Mixture"); - m_GammaPoly = GetValue<double>("GammaPoly"); - m_CoefPoly = GetValue<double>("CoefPoly"); - m_DegreePoly = GetValue<int>("DegreePoly"); - m_GammaRBF = GetValue<double>("GammaRBF"); -} - -double -MixturePolyRBFKernelFunctor -::powi(double base, int times) const -{ - double tmp = base, ret = 1.0; - - for (int t = times; t > 0; t /= 2) - { - if (t % 2 == 1) ret *= tmp; - tmp = tmp * tmp; - } - return ret; -} - -} // end of namespace otb diff --git a/Modules/Learning/SVMLearning/src/otbNonGaussianRBFKernelFunctor.cxx b/Modules/Learning/SVMLearning/src/otbNonGaussianRBFKernelFunctor.cxx deleted file mode 100644 index 0073eede37..0000000000 --- a/Modules/Learning/SVMLearning/src/otbNonGaussianRBFKernelFunctor.cxx +++ /dev/null @@ -1,72 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - Copyright (c) GET / ENST Bretagne. All rights reserved. - See GETCopyright.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. - -=========================================================================*/ -#include <cmath> -#include "otbNonGaussianRBFKernelFunctor.h" - -namespace otb -{ - -NonGaussianRBFKernelFunctor -::NonGaussianRBFKernelFunctor () - : GenericKernelFunctorBase () -{ - this->SetName("NonGaussianRBF"); - m_Alpha = 1.0; - m_Beta = 2.0; - m_Gamma = 1.0; - - SetValue("Alpha", m_Alpha); - SetValue("Beta", m_Beta); - SetValue("Gamma", m_Gamma); -} - -double -NonGaussianRBFKernelFunctor -::operator ()(const svm_node * x, const svm_node * y, - const svm_parameter& /*param*/) const -{ - double pr_x, pr_y, diff, sum = 0.0; - - while (x->index != -1 && y->index != -1) - { - pr_x = pow(x->value, m_Alpha); - pr_y = pow(y->value, m_Alpha); - - diff = pow(fabs(pr_x - pr_y), m_Beta); - - sum += diff; - - ++x; - ++y; - } - - return exp(-m_Gamma * sum); -} - -void -NonGaussianRBFKernelFunctor -::Update() -{ - m_Alpha = GetValue<double>("Alpha"); - m_Beta = GetValue<double>("Beta"); - m_Gamma = GetValue<double>("Gamma"); -} - -} // end of namespace otb diff --git a/Modules/Learning/SVMLearning/src/otbSpectralAngleKernelFunctor.cxx b/Modules/Learning/SVMLearning/src/otbSpectralAngleKernelFunctor.cxx deleted file mode 100644 index 260db28030..0000000000 --- a/Modules/Learning/SVMLearning/src/otbSpectralAngleKernelFunctor.cxx +++ /dev/null @@ -1,67 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - Copyright (c) GET / ENST Bretagne. All rights reserved. - See GETCopyright.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. - -=========================================================================*/ -#include <cmath> -#include <cfloat> - -#include "otbSpectralAngleKernelFunctor.h" - -namespace otb -{ - -SpectralAngleKernelFunctor -::SpectralAngleKernelFunctor () - : GenericKernelFunctorBase () -{ - this->SetName("SAMcoeff"); - m_Coef = 2.0; - SetValue("Coef", m_Coef); -} - -void -SpectralAngleKernelFunctor -::Update() -{ - m_Coef = GetValue<double>("Coef"); -} - -double -SpectralAngleKernelFunctor -::operator ()(const svm_node * x, const svm_node * y, - const svm_parameter& /*param*/) const -{ - double mq = m_Coef + SAM(x, y); - - if (mq == 0.0) return DBL_MAX; - - return 1.0 / sqrt(mq); -} - -double -SpectralAngleKernelFunctor -::SAM(const svm_node * x, const svm_node * y) const -{ - double den = dot(x, x) * dot(y, y); - if (den <= 0.0) return 0.0; - - double ss = dot(x, y); - return /*acos*/ (ss / sqrt(den)); -} - -} diff --git a/Modules/Learning/SVMLearning/test/CMakeLists.txt b/Modules/Learning/SVMLearning/test/CMakeLists.txt index cf59b2da82..0349453c82 100644 --- a/Modules/Learning/SVMLearning/test/CMakeLists.txt +++ b/Modules/Learning/SVMLearning/test/CMakeLists.txt @@ -3,17 +3,14 @@ otb_module_test() set(OTBSVMLearningTests otbSVMLearningTestDriver.cxx otbSVMImageClassificationWithRuleFilter.cxx -otbSVMModelCopyGenericKernelTest.cxx otbLabelMapSVMClassifier.cxx otbSVMModelCopyTest.cxx otbSVMPointSetModelEstimatorTrain.cxx otbExhaustiveExponentialOptimizerNew.cxx otbSVMImageModelEstimatorModelAccessor.cxx otbSVMImageClassificationFilter.cxx -otbSVMModelGenericKernelLoadSave.cxx otbSVMModelNew.cxx otbSVMImageModelEstimatorNew.cxx -otbSVMModelGenericKernelsTest.cxx otbSVMImageModelEstimatorTrainOneClass.cxx otbSVMCrossValidationCostFunctionNew.cxx otbSVMModelLoadSave.cxx @@ -27,11 +24,8 @@ otbSVMSampleListModelEstimatorTest.cxx otbSVMModelAccessor.cxx otbSVMModelLoad.cxx otbSVMImageClassificationFilterNew.cxx -otbSVMInverseCosSpectralAngleKernelFunctorImageModelEstimatorTest.cxx -otbSVMInverseCosSpectralAngleKernelFunctorImageClassificationTest.cxx otbSVMPointSetModelEstimatorNew.cxx otbSVMMarginSampler.cxx -otbSVMModelCopyComposedKernelTest.cxx otbExhaustiveExponentialOptimizerTest.cxx ) @@ -50,13 +44,6 @@ otb_add_test(NAME leTvSVMImageClassificationWithRuleFilter COMMAND otbSVMLearnin ${INPUTDATA}/svm_model_image ${TEMP}/leSVMImageClassificationWithRuleFilterOutput.tif) -otb_add_test(NAME leTvSVMModelCopyGenericKernel COMMAND otbSVMLearningTestDriver - --compare-ascii ${NOTOL} ${INPUTDATA}/svm_model_generic - ${TEMP}/svmcopygeneric_test - otbSVMModelCopyGenericKernelTest - ${INPUTDATA}/svm_model_generic - ${TEMP}/svmcopygeneric_test) - otb_add_test(NAME obTvLabelMapSVMClassifier COMMAND otbSVMLearningTestDriver otbLabelMapSVMClassifier ${INPUTDATA}/maur.tif @@ -73,19 +60,6 @@ otb_add_test(NAME leTvSVMModelCopy COMMAND otbSVMLearningTestDriver ${INPUTDATA}/svm_model ${TEMP}/svmcopymodel_test) -foreach(kernelType RANGE 0 18) - otb_add_test(NAME leTvSVMKernelTest${kernelType} COMMAND otbSVMLearningTestDriver - --compare-ascii ${EPSILON_9} - ${BASELINE_FILES}/leTvSVMKernelTest${kernelType}.txt - ${TEMP}/leTvSVMKernelTest${kernelType}.txt - otbSVMKernelsTest - ${EXAMPLEDATA}/qb_RoadExtract.tif - ${EXAMPLEDATA}/qb_RoadExtract_easyClassification.shp - ${TEMP}/leTvSVMKernelTest${kernelType}.txt - ${kernelType} - ) -endforeach() - otb_add_test(NAME leTvSVMPointSetModelEstimatorTrain COMMAND otbSVMLearningTestDriver --compare-ascii ${EPSILON_3} ${BASELINE_FILES}/leTvsvm_model_pointset ${TEMP}/leTvsvm_model_pointset @@ -112,19 +86,7 @@ otb_add_test(NAME leTvSVMImageClassificationFilter COMMAND otbSVMLearningTestDri ${INPUTDATA}/svm_model_image ${TEMP}/leSVMImageClassificationFilterOutput.tif) -otb_add_test(NAME leTvSVMModelWithGenericKeywordKernelLoadSave COMMAND otbSVMLearningTestDriver - --compare-ascii ${NOTOL} ${BASELINE_FILES}/leTvsvmmodel_image_generic_version_test - ${TEMP}/leTvsvmmodel_image_generic_version_test - otbSVMModelGenericKernelLoadSave - ${INPUTDATA}/svm_model_image_generic_version - ${TEMP}/leTvsvmmodel_image_generic_version_test) -otb_add_test(NAME leTvSVMModelKernelTypeGenericLoadSave COMMAND otbSVMLearningTestDriver - --compare-ascii ${NOTOL} ${INPUTDATA}/svm_model_image_generic_kernel - ${TEMP}/leTvsvmmodel_image_generic_kernel_test - otbSVMModelGenericKernelLoadSave - ${INPUTDATA}/svm_model_image_generic_kernel - ${TEMP}/leTvsvmmodel_image_generic_kernel_test) otb_add_test(NAME leTuSVMModelNew COMMAND otbSVMLearningTestDriver otbSVMModelNew) @@ -132,62 +94,6 @@ otb_add_test(NAME leTuSVMModelNew COMMAND otbSVMLearningTestDriver otb_add_test(NAME leTuSVMImageModelEstimatorNew COMMAND otbSVMLearningTestDriver otbSVMImageModelEstimatorNew) -otb_add_test(NAME leTvSVMModelGenericKernelsTest COMMAND otbSVMLearningTestDriver - --compare-n-ascii ${NOTOL} 17 - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest1.txt - ${TEMP}/leTvSVMModelGenericKernelsTest1.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest2.txt - ${TEMP}/leTvSVMModelGenericKernelsTest2.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest3.txt - ${TEMP}/leTvSVMModelGenericKernelsTest3.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest4.txt - ${TEMP}/leTvSVMModelGenericKernelsTest4.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest5.txt - ${TEMP}/leTvSVMModelGenericKernelsTest5.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest6.txt - ${TEMP}/leTvSVMModelGenericKernelsTest6.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest7.txt - ${TEMP}/leTvSVMModelGenericKernelsTest7.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest8.txt - ${TEMP}/leTvSVMModelGenericKernelsTest8.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest9.txt - ${TEMP}/leTvSVMModelGenericKernelsTest9.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest10.txt - ${TEMP}/leTvSVMModelGenericKernelsTest10.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest11.txt - ${TEMP}/leTvSVMModelGenericKernelsTest11.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest12.txt - ${TEMP}/leTvSVMModelGenericKernelsTest12.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest13.txt - ${TEMP}/leTvSVMModelGenericKernelsTest13.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest14.txt - ${TEMP}/leTvSVMModelGenericKernelsTest14.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest15.txt - ${TEMP}/leTvSVMModelGenericKernelsTest15.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest16.txt - ${TEMP}/leTvSVMModelGenericKernelsTest16.txt - ${BASELINE_FILES}/leTvSVMModelGenericKernelsTest17.txt - ${TEMP}/leTvSVMModelGenericKernelsTest17.txt - otbSVMModelGenericKernelsTest - ${TEMP}/leTvSVMModelGenericKernelsTest1.txt - ${TEMP}/leTvSVMModelGenericKernelsTest2.txt - ${TEMP}/leTvSVMModelGenericKernelsTest3.txt - ${TEMP}/leTvSVMModelGenericKernelsTest4.txt - ${TEMP}/leTvSVMModelGenericKernelsTest5.txt - ${TEMP}/leTvSVMModelGenericKernelsTest6.txt - ${TEMP}/leTvSVMModelGenericKernelsTest7.txt - ${TEMP}/leTvSVMModelGenericKernelsTest8.txt - ${TEMP}/leTvSVMModelGenericKernelsTest9.txt - ${TEMP}/leTvSVMModelGenericKernelsTest10.txt - ${TEMP}/leTvSVMModelGenericKernelsTest11.txt - ${TEMP}/leTvSVMModelGenericKernelsTest12.txt - ${TEMP}/leTvSVMModelGenericKernelsTest13.txt - ${TEMP}/leTvSVMModelGenericKernelsTest14.txt - ${TEMP}/leTvSVMModelGenericKernelsTest15.txt - ${TEMP}/leTvSVMModelGenericKernelsTest16.txt - ${TEMP}/leTvSVMModelGenericKernelsTest17.txt - ) - otb_add_test(NAME leTvSVMImageModelEstimatorTrainOneClass COMMAND otbSVMLearningTestDriver --compare-ascii ${EPSILON_3} ${BASELINE_FILES}/leTvsvm_model_image_one_class ${TEMP}/leTvsvm_model_image_one_class @@ -317,31 +223,12 @@ otb_add_test(NAME leTuSVMModelLoad COMMAND otbSVMLearningTestDriver otb_add_test(NAME leTuSVMImageClassificationFilterNew COMMAND otbSVMLearningTestDriver otbSVMImageClassificationFilterNew) -otb_add_test(NAME leTvSVMInverseCosSpectralAngleKernelFunctorImageModelEstimatorTest COMMAND otbSVMLearningTestDriver - otbSVMInverseCosSpectralAngleKernelFunctorImageModelEstimatorTest - ${INPUTDATA}/ROI_QB_MUL_4.tif - ${INPUTDATA}/ROI_QB_MUL_4_train_one_class.tif - ${TEMP}/leTvsvm_model_image_one_class) - -otb_add_test(NAME leTvSVMInverseCosSpectralAngleKernelFunctorImageClassificationTest COMMAND otbSVMLearningTestDriver - otbSVMInverseCosSpectralAngleKernelFunctorImageClassificationTest - ${INPUTDATA}/ROI_QB_MUL_4.tif - ${INPUTDATA}/ROI_QB_MUL_4_train_one_class.tif - ${TEMP}/leTvsvm_model_image_one_class) - otb_add_test(NAME leTuSVMPointSetModelEstimatorNew COMMAND otbSVMLearningTestDriver otbSVMPointSetModelEstimatorNew) otb_add_test(NAME leTuSVMMarginSamplerNew COMMAND otbSVMLearningTestDriver otbSVMMarginSamplerNew) -otb_add_test(NAME leTvSVMModelCopyComposedKernel COMMAND otbSVMLearningTestDriver - --compare-ascii ${NOTOL} ${INPUTDATA}/svm_model_composed - ${TEMP}/svmcopycomposed_test - otbSVMModelCopyComposedKernelTest - ${INPUTDATA}/svm_model_composed - ${TEMP}/svmcopycomposed_test) - otb_add_test(NAME leTvExhaustiveExponentialOptimizerTest COMMAND otbSVMLearningTestDriver --compare-ascii ${NOTOL} ${BASELINE_FILES}/leTvExhaustiveExponentialOptimizerOutput.txt diff --git a/Modules/Learning/SVMLearning/test/otbSVMInverseCosSpectralAngleKernelFunctorImageClassificationTest.cxx b/Modules/Learning/SVMLearning/test/otbSVMInverseCosSpectralAngleKernelFunctorImageClassificationTest.cxx deleted file mode 100644 index 195cb560ee..0000000000 --- a/Modules/Learning/SVMLearning/test/otbSVMInverseCosSpectralAngleKernelFunctorImageClassificationTest.cxx +++ /dev/null @@ -1,89 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - Copyright (c) GET / ENST Bretagne. All rights reserved. - See GETCopyright.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. - -=========================================================================*/ - - - -#include "itkMacro.h" -#include "otbImage.h" -#include "otbVectorImage.h" -#include <iostream> - -#include "otbSVMImageClassificationFilter.h" -#include "otbSVMImageModelEstimator.h" -#include "otbSVMKernels.h" - -#include "otbImageFileReader.h" - -int otbSVMInverseCosSpectralAngleKernelFunctorImageClassificationTest(int argc, char* argv[]) -{ - if (argc != 4) - { - std::cerr << "Wrong number of arguments" << std::endl; - return EXIT_FAILURE; - } - - const char* inputImageFileName = argv[1]; - const char* trainingImageFileName = argv[2]; - const char* outputModelFileName = argv[3]; - - typedef double InputPixelType; - const unsigned int Dimension = 2; - typedef otb::VectorImage<InputPixelType, Dimension> InputImageType; - typedef otb::Image<int, Dimension> TrainingImageType; - typedef otb::SVMImageModelEstimator<InputImageType, - TrainingImageType> EstimatorType; - typedef otb::ImageFileReader<InputImageType> InputReaderType; - typedef otb::ImageFileReader<TrainingImageType> TrainingReaderType; - - InputReaderType::Pointer inputReader = InputReaderType::New(); - TrainingReaderType::Pointer trainingReader = TrainingReaderType::New(); - EstimatorType::Pointer svmEstimator = EstimatorType::New(); - - inputReader->SetFileName(inputImageFileName); - trainingReader->SetFileName(trainingImageFileName); - inputReader->Update(); - trainingReader->Update(); - - svmEstimator->SetInputImage(inputReader->GetOutput()); - svmEstimator->SetTrainingImage(trainingReader->GetOutput()); - svmEstimator->SetSVMType(ONE_CLASS); - - otb::InverseCosSAMKernelFunctor myKernel; - myKernel.SetValue("Coef", 1.0); - - svmEstimator->SetKernelFunctor(&myKernel); - svmEstimator->SetKernelType(GENERIC); - - svmEstimator->Update(); - - otbGenericMsgDebugMacro(<< "Saving model"); - svmEstimator->GetModel()->SaveModel(outputModelFileName); - - typedef otb::SVMImageClassificationFilter<InputImageType, - TrainingImageType> ClassifierType; - - ClassifierType::Pointer classifier = ClassifierType::New(); - - classifier->SetModel(svmEstimator->GetModel()); - classifier->SetInput(inputReader->GetOutput()); - classifier->Update(); - - return EXIT_SUCCESS; -} diff --git a/Modules/Learning/SVMLearning/test/otbSVMInverseCosSpectralAngleKernelFunctorImageModelEstimatorTest.cxx b/Modules/Learning/SVMLearning/test/otbSVMInverseCosSpectralAngleKernelFunctorImageModelEstimatorTest.cxx deleted file mode 100644 index a83d7edc7a..0000000000 --- a/Modules/Learning/SVMLearning/test/otbSVMInverseCosSpectralAngleKernelFunctorImageModelEstimatorTest.cxx +++ /dev/null @@ -1,79 +0,0 @@ -/*========================================================================= - - Program: ORFEO Toolbox - Language: C++ - Date: $Date$ - Version: $Revision$ - - - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. - See OTBCopyright.txt for details. - - Copyright (c) GET / ENST Bretagne. All rights reserved. - See GETCopyright.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. - -=========================================================================*/ - - - -#include "itkMacro.h" -#include "otbImage.h" -#include "otbVectorImage.h" -#include <iostream> - -#include "otbSVMImageModelEstimator.h" -#include "otbSVMKernels.h" - -#include "otbImageFileReader.h" - -int otbSVMInverseCosSpectralAngleKernelFunctorImageModelEstimatorTest(int argc, char* argv[]) -{ - if (argc != 4) - { - std::cerr << "Wrong number of arguments" << std::endl; - return EXIT_FAILURE; - } - - const char* inputImageFileName = argv[1]; - const char* trainingImageFileName = argv[2]; - const char* outputModelFileName = argv[3]; - - typedef double InputPixelType; - const unsigned int Dimension = 2; - typedef otb::VectorImage<InputPixelType, Dimension> InputImageType; - typedef otb::Image<int, Dimension> TrainingImageType; - typedef otb::SVMImageModelEstimator<InputImageType, - TrainingImageType> EstimatorType; - typedef otb::ImageFileReader<InputImageType> InputReaderType; - typedef otb::ImageFileReader<TrainingImageType> TrainingReaderType; - - InputReaderType::Pointer inputReader = InputReaderType::New(); - TrainingReaderType::Pointer trainingReader = TrainingReaderType::New(); - EstimatorType::Pointer svmEstimator = EstimatorType::New(); - - inputReader->SetFileName(inputImageFileName); - trainingReader->SetFileName(trainingImageFileName); - inputReader->Update(); - trainingReader->Update(); - - svmEstimator->SetInputImage(inputReader->GetOutput()); - svmEstimator->SetTrainingImage(trainingReader->GetOutput()); - svmEstimator->SetSVMType(ONE_CLASS); - - otb::InverseCosSAMKernelFunctor myKernel; - myKernel.SetValue("Coef", 1.0); - - svmEstimator->SetKernelFunctor(&myKernel); - svmEstimator->SetKernelType(GENERIC); - - svmEstimator->Update(); - - otbGenericMsgDebugMacro(<< "Saving model"); - svmEstimator->GetModel()->SaveModel(outputModelFileName); - - return EXIT_SUCCESS; -} diff --git a/Modules/Learning/SVMLearning/test/otbSVMLearningTestDriver.cxx b/Modules/Learning/SVMLearning/test/otbSVMLearningTestDriver.cxx index c29f732c71..ca5c2dbd47 100644 --- a/Modules/Learning/SVMLearning/test/otbSVMLearningTestDriver.cxx +++ b/Modules/Learning/SVMLearning/test/otbSVMLearningTestDriver.cxx @@ -2,7 +2,6 @@ void RegisterTests() { REGISTER_TEST(otbSVMImageClassificationWithRuleFilter); - REGISTER_TEST(otbSVMModelCopyGenericKernelTest); REGISTER_TEST(otbLabelMapSVMClassifierNew); REGISTER_TEST(otbLabelMapSVMClassifier); REGISTER_TEST(otbSVMModelCopyTest); @@ -10,11 +9,8 @@ void RegisterTests() REGISTER_TEST(otbExhaustiveExponentialOptimizerNew); REGISTER_TEST(otbSVMImageModelEstimatorModelAccessor); REGISTER_TEST(otbSVMImageClassificationFilter); - REGISTER_TEST(otbSVMModelGenericKernelLoadSave); REGISTER_TEST(otbSVMModelNew); REGISTER_TEST(otbSVMImageModelEstimatorNew); - REGISTER_TEST(otbSVMModelGenericKernelsTest); - REGISTER_TEST(otbSVMKernelsTest); REGISTER_TEST(otbSVMImageModelEstimatorTrainOneClass); REGISTER_TEST(otbSVMCrossValidationCostFunctionNew); REGISTER_TEST(otbSVMModelLoadSave); @@ -30,10 +26,7 @@ void RegisterTests() REGISTER_TEST(otbSVMModelAccessor); REGISTER_TEST(otbSVMModelLoad); REGISTER_TEST(otbSVMImageClassificationFilterNew); - REGISTER_TEST(otbSVMInverseCosSpectralAngleKernelFunctorImageModelEstimatorTest); - REGISTER_TEST(otbSVMInverseCosSpectralAngleKernelFunctorImageClassificationTest); REGISTER_TEST(otbSVMPointSetModelEstimatorNew); REGISTER_TEST(otbSVMMarginSamplerNew); - REGISTER_TEST(otbSVMModelCopyComposedKernelTest); REGISTER_TEST(otbExhaustiveExponentialOptimizerTest); } diff --git a/Modules/Learning/SVMLearning/test/otbSVMModelCopyComposedKernelTest.cxx b/Modules/Learning/SVMLearning/test/otbSVMModelCopyComposedKernelTest.cxx deleted file mode 100644 index 0bed81c8da..0000000000 --- a/Modules/Learning/SVMLearning/test/otbSVMModelCopyComposedKernelTest.cxx +++ /dev/null @@ -1,48 +0,0 @@ -/*========================================================================= - - 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. - -=========================================================================*/ - - - -#include "itkMacro.h" -#include <iostream> -#include "otbSVMKernels.h" -#include "otbSVMModel.h" - -int otbSVMModelCopyComposedKernelTest(int argc, char* argv[]) -{ - if (argc != 3) - { - std::cerr << "Wrong number of arguments" << std::endl; - return EXIT_FAILURE; - } - - - typedef unsigned char InputPixelType; - typedef unsigned char LabelPixelType; - - typedef otb::SVMModel<InputPixelType, LabelPixelType> ModelType; - - ModelType::Pointer svmModel = ModelType::New(); - svmModel->LoadModel(argv[1]); - - ModelType::Pointer svmModelCopy; - svmModelCopy = svmModel->GetCopy(); - svmModelCopy->SaveModel(argv[2]); - - return EXIT_SUCCESS; -} diff --git a/Modules/Learning/SVMLearning/test/otbSVMModelCopyGenericKernelTest.cxx b/Modules/Learning/SVMLearning/test/otbSVMModelCopyGenericKernelTest.cxx deleted file mode 100644 index e572ed5271..0000000000 --- a/Modules/Learning/SVMLearning/test/otbSVMModelCopyGenericKernelTest.cxx +++ /dev/null @@ -1,51 +0,0 @@ -/*========================================================================= - - 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. - -=========================================================================*/ - - - -#include "itkMacro.h" -#include <iostream> -#include "otbSVMKernels.h" -#include "otbSVMModel.h" - -int otbSVMModelCopyGenericKernelTest(int argc, char* argv[]) -{ - if (argc != 3) - { - std::cerr << "Wrong number of arguments" << std::endl; - return EXIT_FAILURE; - } - - typedef unsigned char InputPixelType; - typedef unsigned char LabelPixelType; - - typedef otb::SVMModel<InputPixelType, LabelPixelType> ModelType; - - // Create the model to be copied - ModelType::Pointer svmModel = ModelType::New(); - otb::RBFKernelFunctor lFunctor; - svmModel->SetKernelFunctor(&lFunctor); - svmModel->LoadModel(argv[1]); - - // Copy the model and print it - ModelType::Pointer svmModelCopy; - svmModelCopy = svmModel->GetCopy(); - svmModelCopy->SaveModel(argv[2]); - - return EXIT_SUCCESS; -} diff --git a/Modules/Learning/SVMLearning/test/otbSVMModelGenericKernelLoadSave.cxx b/Modules/Learning/SVMLearning/test/otbSVMModelGenericKernelLoadSave.cxx deleted file mode 100644 index 005b7d2f62..0000000000 --- a/Modules/Learning/SVMLearning/test/otbSVMModelGenericKernelLoadSave.cxx +++ /dev/null @@ -1,83 +0,0 @@ -/*========================================================================= - - 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. - -=========================================================================*/ - - - -#include "itkMacro.h" -#include "otbImage.h" -#include <iostream> - -#include "otbSVMModel.h" - -namespace otb -{ - -class LinearKernelFunctor : public GenericKernelFunctorBase -{ -public: - typedef LinearKernelFunctor Self; - typedef GenericKernelFunctorBase Superclass; - - LinearKernelFunctor() : GenericKernelFunctorBase() {} - virtual ~LinearKernelFunctor() {} - - // Deep copy operator - virtual GenericKernelFunctorBase* Clone() const - { - return new Self(*this); - } - - virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter&) const - { - return this->dot(x, y); - } - -protected: - LinearKernelFunctor(const Self& copy) - : Superclass(copy) - { - *this = copy; - } - - LinearKernelFunctor& operator=(const Self& copy) - { - Superclass::operator =(copy); - return *this; - } -}; - -} - -int otbSVMModelGenericKernelLoadSave(int itkNotUsed(argc), char* argv[]) -{ - typedef unsigned char InputPixelType; - typedef unsigned char LabelPixelType; - - typedef otb::SVMModel<InputPixelType, LabelPixelType> ModelType; - - ModelType::Pointer svmModel = ModelType::New(); - - otb::LinearKernelFunctor lFunctor; - - svmModel->SetKernelFunctor(&lFunctor); - - svmModel->LoadModel(argv[1]); - svmModel->SaveModel(argv[2]); - - return EXIT_SUCCESS; -} diff --git a/Modules/Learning/SVMLearning/test/otbSVMModelGenericKernelsTest.cxx b/Modules/Learning/SVMLearning/test/otbSVMModelGenericKernelsTest.cxx deleted file mode 100644 index bae9f7fe9e..0000000000 --- a/Modules/Learning/SVMLearning/test/otbSVMModelGenericKernelsTest.cxx +++ /dev/null @@ -1,387 +0,0 @@ -/*========================================================================= - - 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. - -=========================================================================*/ - - - -#include "otbImage.h" -#include <iostream> - -#include "otbSVMModel.h" -#include "otbSVMKernels.h" - -//For second test -#include "otbSVMSampleListModelEstimator.h" -#include "otbSVMKernels.h" -#include "otbVectorImage.h" -#include "otbImageFileReader.h" -#include "otbVectorData.h" -#include "otbVectorDataFileReader.h" -#include "otbListSampleGenerator.h" - -int otbSVMModelGenericKernelsTest(int argc, char* argv[]) -{ - if (argc != 18) - { - return EXIT_FAILURE; - } - - typedef unsigned char InputPixelType; - typedef unsigned char LabelPixelType; - - typedef otb::SVMModel<InputPixelType, LabelPixelType> ModelType; - - ModelType::Pointer svmModel = ModelType::New(); - - otb::CustomKernelFunctor customFunctor; - otb::InvMultiQuadricKernelFunctor invMultiQuadricFunctor; - otb::SAMKernelFunctor SAMFunctor; - otb::KModKernelFunctor kModFunctor; - otb::RadialSAMKernelFunctor radialSAMFunctor; - otb::InvMultiQuadraticSAMKernelFunctor invMultiQuadraticSAMFunctor; - otb::KModSAMKernelFunctor KModSAMFunctor; - otb::RBFRBFSAMKernelFunctor RBFRBFSAMFunctor; - otb::PolyRBFSAMKernelFunctor polyRBFSAMFunctor; - otb::RBFDiffKernelFunctor RBFDiffFunctor; - otb::CustomLinearKernelFunctor customLinearFunctor; - otb::GroupedRBFKernelFunctor groupedRBFFunctor; - otb::GroupingAdaptiveKernelFunctor groupingAdaptiveFunctor; - otb::SpectralAngleKernelFunctor spectAngleFunctor; - otb::NonGaussianRBFKernelFunctor nonGaussRBFFunctor; - otb::ChangeProfileKernelFunctor changeProFunctor; - otb::MixturePolyRBFKernelFunctor mixPolRBFFunctor; - - invMultiQuadricFunctor.SetValue<double>("const_coef", 2.); - invMultiQuadraticSAMFunctor.SetValue<double>("const_coef", 3.); - kModFunctor.SetValue<double>("const_coef", 1.5); - KModSAMFunctor.SetValue<double>("const_coef", 1.8); - RBFRBFSAMFunctor.SetValue<double>("const_coef", 1.7); - polyRBFSAMFunctor.SetValue<double>("const_coef", 1.); - polyRBFSAMFunctor.SetValue<double>("lin_coef", 1.2); - groupingAdaptiveFunctor.SetValue<double>("const_coef", 8.2); - groupingAdaptiveFunctor.SetValue<double>("lin_coef", 5.2); - spectAngleFunctor.SetValue<double>("Coef", 2.5); - nonGaussRBFFunctor.SetValue<double>("Alpha", 1.1); - nonGaussRBFFunctor.SetValue<double>("Beta", 2.1); - nonGaussRBFFunctor.SetValue<double>("Gamma", 1.1); - changeProFunctor.SetValue<double>("Coef", 0.1); - changeProFunctor.SetValue<double>("Degree", 1.1); - changeProFunctor.SetValue<double>("Gamma", 1.1); - mixPolRBFFunctor.SetValue<double>("Mixture", 0.5); - mixPolRBFFunctor.SetValue<double>("GammaPoly", 1.0); - mixPolRBFFunctor.SetValue<double>("CoefPoly", 1.0); - mixPolRBFFunctor.SetValue<double>("DegreePoly", 2.0); - mixPolRBFFunctor.SetValue<double>("GammaRBF", 1.0); - - - struct svm_model *model; - model = new svm_model; - model->param.svm_type = 0; - model->param.kernel_type = 5; - model->nr_class = 2; - model->l = 5; - model->sv_coef = new double*[model->nr_class - 1]; - - for (int i = 0; i < model->nr_class - 1; ++i) - model->sv_coef[i] = new double[model->l]; - model->SV = new svm_node *[model->l]; - - model->sv_coef[0][0] = 0.125641; - model->sv_coef[0][1] = 1; - model->sv_coef[0][2] = 0; - model->sv_coef[0][3] = -1; - model->sv_coef[0][4] = -0.54994; - - model->rho = new double[1]; - model->probA = new double[1]; - model->probB = new double[1]; - model->rho[0] = 22.3117; - model->probA[0] = -0.541009; - model->probB[0] = -0.687381; - model->param.const_coef = 2.; - model->param.lin_coef = 5.; - model->param.gamma = 1.5; - model->param.degree = 2; - - struct svm_node *p = new struct svm_node[20]; - for (unsigned int n = 0; n < 20; ++n) - { - p[n].index = -1; - p[n].value = 0.; - } - model->SV[0] = &p[0]; - model->SV[1] = &p[4]; - model->SV[2] = &p[8]; - model->SV[3] = &p[12]; - model->SV[4] = &p[16]; - p[0].index = 1; - p[0].value = 89; - p[1].index = 2; - p[1].value = 123; - p[2].index = 3; - p[2].value = 134; - p[3].index = -1; - p[4].index = 1; - p[4].value = 80; - p[5].index = 2; - p[5].value = 113; - p[6].index = 3; - p[6].value = 120; - p[7].index = -1; - p[8].index = 1; - p[8].value = 75; - p[9].index = 2; - p[9].value = 108; - p[10].index = 3; - p[10].value = 105; - p[11].index = -1; - p[12].index = 1; - p[12].value = 34; - p[13].index = 2; - p[13].value = 60; - p[14].index = 3; - p[14].value = 67; - p[15].index = -1; - p[16].index = 1; - p[16].value = 208; - p[17].index = 2; - p[17].value = 255; - p[18].index = 1; - p[18].value = 255; - p[19].index = -1; - - model->label = new int[2]; - model->label[0] = 1; - model->label[1] = -1; - model->nSV = new int[2]; - model->nSV[0] = 3; - model->nSV[1] = 2; - - model->param.kernel_generic = customFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[1]); - model->param.kernel_generic = invMultiQuadricFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[2]); - model->param.kernel_generic = SAMFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[3]); - model->param.kernel_generic = kModFunctor.Clone(); ; - svmModel->SetModel(model); - svmModel->SaveModel(argv[4]); - model->param.kernel_generic = radialSAMFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[5]); - model->param.kernel_generic = invMultiQuadraticSAMFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[6]); - model->param.kernel_generic = KModSAMFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[7]); - model->param.kernel_generic = RBFRBFSAMFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[8]); - model->param.kernel_generic = polyRBFSAMFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[9]); - model->param.kernel_generic = RBFDiffFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[10]); - model->param.kernel_generic = customLinearFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[11]); - model->param.kernel_generic = groupedRBFFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[12]); - model->param.kernel_generic = groupingAdaptiveFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[13]); - model->param.kernel_generic = spectAngleFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[14]); - model->param.kernel_generic = nonGaussRBFFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[15]); - model->param.kernel_generic = changeProFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[16]); - model->param.kernel_generic = mixPolRBFFunctor.Clone(); - svmModel->SetModel(model); - svmModel->SaveModel(argv[17]); - - // Free all memory - - - for (int i = 0; i < model->nr_class - 1; ++i) - { - delete [] model->sv_coef[i]; - } - delete [] model->sv_coef; - - delete [] model->SV; - - delete model->rho; - delete model->probA; - delete model->probB; - delete [] model->label; - delete [] model->nSV; - delete model; - delete [] p; - - return EXIT_SUCCESS; -} - -template<class KernelType> -int otbSVMKernelsTest_generic(int argc, char* argv[]) -{ - if (argc != 5) - { - return EXIT_FAILURE; - } - - std::string imageFilename = argv[1]; - std::string vectorDataFilename = argv[2]; - std::string outputModelFileName= argv[3]; - int maxTrainingSize = 500; - int maxValidationSize = 500; - double validationTrainingProportion = 0.5; - - std::string classKey = "Class"; - - typedef double PixelType; - typedef otb::VectorImage<PixelType, 2> ImageType; - typedef otb::ImageFileReader<ImageType> ReaderType; - - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(imageFilename); - reader->UpdateOutputInformation(); - - typedef otb::VectorData<float, 2> VectorDataType; - typedef otb::VectorDataFileReader<VectorDataType> VectorDataReaderType; - - VectorDataReaderType::Pointer vectorReader = VectorDataReaderType::New(); - vectorReader->SetFileName(vectorDataFilename); - vectorReader->Update(); - - typedef otb::ListSampleGenerator<ImageType, VectorDataType> ListSampleGeneratorType; - ListSampleGeneratorType::Pointer generator = ListSampleGeneratorType::New(); - generator->SetMaxTrainingSize(maxTrainingSize); - generator->SetMaxValidationSize(maxValidationSize); - generator->SetValidationTrainingProportion(validationTrainingProportion); - - generator->SetInput(reader->GetOutput()); - generator->SetInputVectorData(vectorReader->GetOutput()); - - generator->Update(); - std::cout << generator << std::endl; - - typedef ListSampleGeneratorType::ListSampleType ListSampleType; - typedef ListSampleGeneratorType::ListLabelType TrainingListSampleType; - typedef otb::SVMSampleListModelEstimator<ListSampleType, TrainingListSampleType> EstimatorType; - - EstimatorType::Pointer estimator = EstimatorType::New(); - estimator->SetInputSampleList(generator->GetTrainingListSample()); - estimator->SetTrainingSampleList(generator->GetTrainingListLabel()); - - -// KernelType::Pointer kernel = KernelType::New(); - KernelType kernel; - std::cout << "Kernel: " << kernel.GetName() << std::endl; - estimator->SetKernelFunctor(&kernel); - estimator->SetKernelType(GENERIC); - - estimator->Update(); - - estimator->GetModel()->SaveModel(outputModelFileName); - - return EXIT_SUCCESS; -} - -int otbSVMKernelsTest(int argc, char* argv[]) -{ - if (argc != 5) - { - std::cerr << "Usage: " << argv[0] << " inputImage inputVectorData outputModelFileName kernelType" - << std::endl; - return EXIT_FAILURE; - } - - int kernelType = atoi(argv[4]); - switch (kernelType) - { - case 0: - return otbSVMKernelsTest_generic<otb::CustomKernelFunctor>(argc, argv); - break; - case 1: - return otbSVMKernelsTest_generic<otb::InvMultiQuadricKernelFunctor>(argc, argv); - break; - case 2: - return otbSVMKernelsTest_generic<otb::KModKernelFunctor>(argc, argv); - break; - case 3: - return otbSVMKernelsTest_generic<otb::SAMKernelFunctor>(argc, argv); - break; - case 4: - return otbSVMKernelsTest_generic<otb::RadialSAMKernelFunctor>(argc, argv); - break; - case 5: - return otbSVMKernelsTest_generic<otb::InverseCosSAMKernelFunctor>(argc, argv); - break; - case 6: - return otbSVMKernelsTest_generic<otb::InvMultiQuadraticSAMKernelFunctor>(argc, argv); - break; - case 7: - return otbSVMKernelsTest_generic<otb::KModSAMKernelFunctor>(argc, argv); - break; - case 8: - return otbSVMKernelsTest_generic<otb::RBFKernelFunctor>(argc, argv); - break; - case 9: - return otbSVMKernelsTest_generic<otb::RBFRBFSAMKernelFunctor>(argc, argv); - break; - case 10: - return otbSVMKernelsTest_generic<otb::PolyRBFSAMKernelFunctor>(argc, argv); - break; - case 11: - return otbSVMKernelsTest_generic<otb::RBFDiffKernelFunctor>(argc, argv); - break; - case 12: - return otbSVMKernelsTest_generic<otb::CustomLinearKernelFunctor>(argc, argv); - break; - case 13: - return otbSVMKernelsTest_generic<otb::GroupedRBFKernelFunctor>(argc, argv); - break; - case 14: - return otbSVMKernelsTest_generic<otb::GroupingAdaptiveKernelFunctor>(argc, argv); - break; - case 15: - return otbSVMKernelsTest_generic<otb::SpectralAngleKernelFunctor>(argc, argv); - break; - case 16: - return otbSVMKernelsTest_generic<otb::NonGaussianRBFKernelFunctor>(argc, argv); - break; - case 17: - return otbSVMKernelsTest_generic<otb::ChangeProfileKernelFunctor>(argc, argv); - break; - case 18: - return otbSVMKernelsTest_generic<otb::MixturePolyRBFKernelFunctor>(argc, argv); - break; - default: - std::cerr << "No more kernel available\n"; - return EXIT_FAILURE; - } -} -- GitLab