Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
otb
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
273
Issues
273
List
Boards
Labels
Service Desk
Milestones
Merge Requests
8
Merge Requests
8
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Main Repositories
otb
Commits
66b6545b
Commit
66b6545b
authored
Feb 22, 2008
by
Cyrille Valladeau
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modification de svm : ajout de la fonctionnalite de kernel composite.
parent
8a9af8ef
Changes
7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
3120 additions
and
2643 deletions
+3120
-2643
Code/Learning/otbSVMModel.h
Code/Learning/otbSVMModel.h
+6
-0
Code/Learning/otbSVMModel.txx
Code/Learning/otbSVMModel.txx
+4
-0
Testing/Utilities/CMakeLists.txt
Testing/Utilities/CMakeLists.txt
+7
-0
Testing/Utilities/otbSVMComposedKernelFunctorTest.cxx
Testing/Utilities/otbSVMComposedKernelFunctorTest.cxx
+151
-0
Testing/Utilities/otbUtilitiesTests.cxx
Testing/Utilities/otbUtilitiesTests.cxx
+1
-1
Utilities/otbsvm/svm.cxx
Utilities/otbsvm/svm.cxx
+2795
-2595
Utilities/otbsvm/svm.h
Utilities/otbsvm/svm.h
+156
-47
No files found.
Code/Learning/otbSVMModel.h
View file @
66b6545b
...
...
@@ -423,6 +423,7 @@ public:
return
m_Model
->
sv_coef
;
}
/** Evaluate model */
double
Evaluate
(
void
);
...
...
@@ -454,6 +455,11 @@ private:
struct
svm_problem
m_Problem
;
struct
svm_node
*
m_XSpace
;
///** SVM Model Vector for composed kernel */
//std::vector<struct svm_model*> m_ModelList;
/** Ponderation list to apply to each svm_model of the composed kernel*/
//std::vector<double> m_PonderationModelList;
/** Pointer to generic kernel functor */
// GenericKernelFunctorBase * m_GenericKernelFunctor;
...
...
Code/Learning/otbSVMModel.txx
View file @
66b6545b
...
...
@@ -40,6 +40,7 @@ SVMModel< TInputPixel, TLabel >::SVMModel()
m_Problem.y = new double[1];
m_Problem.x = new struct svm_node*[1];
m_XSpace = new struct svm_node[1];
//m_GenericKernelFunctor = NULL;
// m_Model->param.kernel_generic = NULL;
// m_Model->param.nr_weight = 0;
...
...
@@ -194,6 +195,9 @@ SVMModel<TInputPixel, TLabel>
return (values);
}
// FIXME
}// end namespace otb
...
...
Testing/Utilities/CMakeLists.txt
View file @
66b6545b
...
...
@@ -92,6 +92,10 @@ ADD_TEST(utTvSvmGenericKernelBasicOperationsTest ${UTILITIES_TESTS}
${
TEMP
}
/svmGenericKernelBasicOperationsTest.txt
)
ADD_TEST
(
utTvSvmComposedKernelFunctorTest
${
UTILITIES_TESTS
}
otbSVMComposedKernelFunctorTest
)
# ------- lib otb6S ------------------------------
# The file is not read. It a screen user scan. main.exe < 132.149.107.66.wloip20967.INP
...
...
@@ -128,6 +132,8 @@ ADD_TEST(utTvTinyXMLTest ${UTILITIES_TESTS}
)
# ------- Fichiers sources CXX -----------------------------------
SET
(
UtilitiesTests_SRCS
ossimIntegrationTest.cxx
...
...
@@ -140,6 +146,7 @@ svmGenericKernelTest.cxx
6SFunctionMainTest.cxx
tinyXMLlibTest.cxx
svmGenericKernelBasicOperationsTest.cxx
otbSVMComposedKernelFunctorTest.cxx
)
INCLUDE_DIRECTORIES
(
"
${
OTBTesting_BINARY_DIR
}
"
)
...
...
Testing/Utilities/otbSVMComposedKernelFunctorTest.cxx
0 → 100644
View file @
66b6545b
/*=========================================================================
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.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "itkExceptionObject.h"
#include "otbImage.h"
#include <iostream>
#include "otbSVMModel.h"
#include "otbSVMKernels.h"
#include "svm.h"
int
otbSVMComposedKernelFunctorTest
(
int
argc
,
char
*
argv
[]
)
{
typedef
unsigned
char
InputPixelType
;
typedef
unsigned
char
LabelPixelType
;
typedef
otb
::
SVMModel
<
InputPixelType
,
LabelPixelType
>
ModelType
;
ModelType
::
Pointer
svmModel
=
ModelType
::
New
();
otb
::
CustomKernelFunctor
customFunctor
;
otb
::
SAMKernelFunctor
SAMFunctor
;
ComposedKernelFunctor
composedKernelFunctor
;
composedKernelFunctor
.
AddKernelFunctorModelToKernelList
(
&
customFunctor
);
composedKernelFunctor
.
AddKernelFunctorModelToKernelList
(
&
SAMFunctor
);
composedKernelFunctor
.
AddPonderationToPonderationList
(
1.5
);
composedKernelFunctor
.
AddPonderationToPonderationList
(
2.0
);
composedKernelFunctor
.
SetName
(
"compositionFilter"
);
struct
svm_model
*
model
;
model
=
(
struct
svm_model
*
)
malloc
(
sizeof
(
struct
svm_model
));
model
->
param
.
kernel_type
=
6
;
model
->
nr_class
=
2
;
model
->
l
=
5
;
model
->
sv_coef
=
Malloc
(
double
*
,
model
->
nr_class
-
1
);
for
(
int
i
=
0
;
i
<
model
->
nr_class
-
1
;
i
++
)
model
->
sv_coef
[
i
]
=
Malloc
(
double
,
model
->
l
);
model
->
SV
=
Malloc
(
svm_node
*
,
model
->
l
);
for
(
int
n
=
0
;
n
<
model
->
l
;
++
n
)
{
model
->
SV
[
n
]
=
Malloc
(
svm_node
,
1
);
model
->
SV
[
n
]
->
index
=
-
1
;
model
->
SV
[
n
]
->
value
=
0.
;
}
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
=
Malloc
(
double
,
1
);
model
->
probA
=
Malloc
(
double
,
1
);
model
->
probB
=
Malloc
(
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
;
model
->
label
=
Malloc
(
int
,
2
);
model
->
label
[
0
]
=
1
;
model
->
label
[
1
]
=
-
1
;
model
->
nSV
=
Malloc
(
int
,
2
);
model
->
nSV
[
0
]
=
3
;
model
->
nSV
[
1
]
=
2
;
model
->
param
.
kernel_composed
=
&
composedKernelFunctor
;
svmModel
->
SetModel
(
model
);
struct
svm_node
*
x
=
Malloc
(
struct
svm_node
,
3
);
struct
svm_node
*
y
=
Malloc
(
struct
svm_node
,
3
);
struct
svm_node
**
SVx
=
Malloc
(
svm_node
*
,
1
);
struct
svm_node
**
SVy
=
Malloc
(
svm_node
*
,
1
);
SVx
[
0
]
=
Malloc
(
svm_node
,
1
);
SVy
[
0
]
=
Malloc
(
svm_node
,
1
);
SVx
[
0
]
=
&
x
[
0
];
SVy
[
0
]
=
&
y
[
0
];
x
[
0
].
index
=
1
;
x
[
0
].
value
=
10
;
x
[
1
].
index
=
-
1
;
x
[
1
].
value
=
10000
;
y
[
0
].
index
=
1
;
y
[
0
].
value
=
5
;
y
[
1
].
index
=
-
1
;
y
[
1
].
value
=
10000
;
double
resAdd
=
0.
;
double
res1
=
0.
;
double
res2
=
0.
;
res1
=
customFunctor
(
SVx
[
0
],
SVy
[
0
],
model
->
param
);
std
::
cout
<<
"customFunctor : "
<<
res1
<<
std
::
endl
;
res2
=
SAMFunctor
(
SVx
[
0
],
SVy
[
0
],
model
->
param
);
std
::
cout
<<
"SAMFunctor : "
<<
res2
<<
std
::
endl
;
resAdd
=
(
*
(
svmModel
->
GetModel
()
->
param
.
kernel_composed
))(
SVx
[
0
],
SVy
[
0
],
svmModel
->
GetModel
()
->
param
);
std
::
cout
<<
"composed : "
<<
resAdd
<<
std
::
endl
;
svmModel
->
GetModel
()
->
param
.
kernel_composed
->
SetMultiplyKernelFunctor
(
true
);
resAdd
=
(
*
(
svmModel
->
GetModel
()
->
param
.
kernel_composed
))(
SVx
[
0
],
SVy
[
0
],
svmModel
->
GetModel
()
->
param
);
std
::
cout
<<
"composed : "
<<
resAdd
<<
std
::
endl
;
svmModel
->
GetModel
()
->
param
.
kernel_composed
->
print_parameters
();
svmModel
->
SaveModel
(
"test.txt"
);
ModelType
::
Pointer
svmModelBis
=
ModelType
::
New
();
svmModelBis
->
LoadModel
(
"test.txt"
);
svmModelBis
->
GetModel
()
->
param
.
kernel_composed
->
print_parameters
();
svmModelBis
->
SaveModel
(
"testBis.txt"
);
return
EXIT_SUCCESS
;
}
Testing/Utilities/otbUtilitiesTests.cxx
View file @
66b6545b
...
...
@@ -36,5 +36,5 @@ REGISTER_TEST(svmGenericKernelTest);
REGISTER_TEST
(
SIXSFunctionMainOtbTest
);
REGISTER_TEST
(
tinyXMLlibTest
);
REGISTER_TEST
(
svmGenericKernelBasicOperationsTest
);
REGISTER_TEST
(
otbSVMComposedKernelFunctorTest
);
}
Utilities/otbsvm/svm.cxx
View file @
66b6545b
This diff is collapsed.
Click to expand it.
Utilities/otbsvm/svm.h
View file @
66b6545b
...
...
@@ -5,9 +5,11 @@
//namespace otb
//{
class
GenericKernelFunctorBase
;
class
ComposedKernelFunctor
;
//}
#include <map>
#include <vector>
#include "otbMacro.h"
#ifdef __cplusplus
...
...
@@ -29,32 +31,34 @@ struct svm_problem
enum
{
C_SVC
,
NU_SVC
,
ONE_CLASS
,
EPSILON_SVR
,
NU_SVR
};
/* svm_type */
//OTB's modifications
enum
{
LINEAR
,
POLY
,
RBF
,
SIGMOID
,
PRECOMPUTED
,
GENERIC
};
/* kernel_type */
enum
{
LINEAR
,
POLY
,
RBF
,
SIGMOID
,
PRECOMPUTED
,
GENERIC
,
COMPOSED
};
/* kernel_type */
struct
svm_parameter
{
int
svm_type
;
int
kernel_type
;
int
degree
;
/* for poly */
double
gamma
;
/* for poly/rbf/sigmoid */
double
coef0
;
/* for poly/sigmoid */
double
const_coef
;
/* for otbSVMKernels.h. */
double
lin_coef
;
/* for otbSVMKernels.h. */
char
custom
[
500
];
/* for user supplied kernel */
//OTB's modifications : Use by the generic kernel
/*otb::*/
GenericKernelFunctorBase
*
kernel_generic
;
/* these are for training only */
double
cache_size
;
/* in MB */
double
eps
;
/* stopping criteria */
double
C
;
/* for C_SVC, EPSILON_SVR and NU_SVR */
int
nr_weight
;
/* for C_SVC */
int
*
weight_label
;
/* for C_SVC */
double
*
weight
;
/* for C_SVC */
double
nu
;
/* for NU_SVC, ONE_CLASS, and NU_SVR */
double
p
;
/* for EPSILON_SVR */
int
shrinking
;
/* use the shrinking heuristics */
int
probability
;
/* do probability estimates */
int
svm_type
;
int
kernel_type
;
int
degree
;
/* for poly */
double
gamma
;
/* for poly/rbf/sigmoid */
double
coef0
;
/* for poly/sigmoid */
double
const_coef
;
/* for otbSVMKernels.h. */
double
lin_coef
;
/* for otbSVMKernels.h. */
char
custom
[
500
];
/* for user supplied kernel */
//OTB's modifications : Use by the generic kernel
/*otb::*/
GenericKernelFunctorBase
*
kernel_generic
;
// Composed kernel
ComposedKernelFunctor
*
kernel_composed
;
/* these are for training only */
double
cache_size
;
/* in MB */
double
eps
;
/* stopping criteria */
double
C
;
/* for C_SVC, EPSILON_SVR and NU_SVR */
int
nr_weight
;
/* for C_SVC */
int
*
weight_label
;
/* for C_SVC */
double
*
weight
;
/* for C_SVC */
double
nu
;
/* for NU_SVC, ONE_CLASS, and NU_SVR */
double
p
;
/* for EPSILON_SVR */
int
shrinking
;
/* use the shrinking heuristics */
int
probability
;
/* do probability estimates */
};
//
...
...
@@ -62,23 +66,24 @@ struct svm_parameter
//
struct
svm_model
{
svm_parameter
param
;
// parameter
int
nr_class
;
// number of classes, = 2 in regression/one class svm
int
l
;
// total #SV
svm_node
**
SV
;
// SVs (SV[l])
double
**
sv_coef
;
// coefficients for SVs in decision functions (sv_coef[k-1][l])
double
*
rho
;
// constants in decision functions (rho[k*(k-1)/2])
double
*
probA
;
// pariwise probability information
double
*
probB
;
// for classification only
int
*
label
;
// label of each class (label[k])
int
*
nSV
;
// number of SVs for each class (nSV[k])
// nSV[0] + nSV[1] + ... + nSV[k-1] = l
// XXX
int
free_sv
;
// 1 if svm_model is created by svm_load_model
// 0 if svm_model is created by svm_train
svm_parameter
param
;
// parameter
int
nr_class
;
// number of classes, = 2 in regression/one class svm
int
l
;
// total #SV
svm_node
**
SV
;
// SVs (SV[l])
double
**
sv_coef
;
// coefficients for SVs in decision functions (sv_coef[k-1][l])
double
*
rho
;
// constants in decision functions (rho[k*(k-1)/2])
double
*
probA
;
// pariwise probability information
double
*
probB
;
// for classification only
int
*
label
;
// label of each class (label[k])
int
*
nSV
;
// number of SVs for each class (nSV[k])
// nSV[0] + nSV[1] + ... + nSV[k-1] = l
// XXX
int
free_sv
;
// 1 if svm_model is created by svm_load_model
bool
delete_composed
;
// to know if the composed functor was set using load method
// 0 if svm_model is created by svm_train
};
struct
svm_model
*
svm_train
(
const
struct
svm_problem
*
prob
,
const
struct
svm_parameter
*
param
);
...
...
@@ -120,6 +125,9 @@ public:
GenericKernelFunctorBase
()
:
m_Name
(
"FunctorName"
)
{};
virtual
~
GenericKernelFunctorBase
()
{};
typedef
std
::
map
<
std
::
string
,
std
::
string
>
MapType
;
typedef
MapType
::
iterator
MapIterator
;
typedef
MapType
::
const_iterator
MapConstIterator
;
template
<
class
T
>
T
GetValue
(
const
char
*
option
)
const
...
...
@@ -141,7 +149,7 @@ public:
m_MapParameters
[
std
::
string
(
option
)]
=
lValeur
;
}
/
*
/* FOR VISUAL 6 COMPILATION (visual 6 doesn't consider template)*****************
*
#define otbGetValueMacro(name,type) \
virtual type GetValue##name (const char *option) const \
{ \
...
...
@@ -204,23 +212,124 @@ public:
virtual
svm_node
*
add
(
const
svm_node
*
px
,
const
svm_node
*
py
)
const
;
virtual
void
SetName
(
const
std
::
string
&
name
)
{
m_Name
=
name
;}
virtual
void
SetName
(
std
::
string
name
)
{
m_Name
=
name
;}
virtual
std
::
string
GetName
(
void
)
{
return
m_Name
;}
virtual
const
std
::
string
GetName
(
void
)
const
{
return
m_Name
;}
private:
virtual
void
SetMapParameters
(
const
MapType
&
map
){
m_MapParameters
=
map
;
};
virtual
const
MapType
&
GetMapParameters
()
const
{
return
m_MapParameters
;
};
virtual
MapType
GetMapParameters
(){
return
m_MapParameters
;
};
typedef
std
::
map
<
std
::
string
,
std
::
string
>
MapType
;
typedef
MapType
::
iterator
MapIterator
;
typedef
MapType
::
const_iterator
MapConstIterator
;
private:
/** Kernel functor parameters */
MapType
m_MapParameters
;
/** Functor label name */
/** Functor label name
(without space)
*/
std
::
string
m_Name
;
};
class
ComposedKernelFunctor
:
public
GenericKernelFunctorBase
{
public:
ComposedKernelFunctor
()
{
this
->
SetName
(
"ComposedFunctorName"
);
this
->
SetValue
<
bool
>
(
"MultiplyKernelFunctor"
,
false
);
};
virtual
~
ComposedKernelFunctor
()
{
for
(
unsigned
int
i
=
0
;
i
<
m_HaveToBeDeletedList
.
size
();
i
++
)
{
for
(
unsigned
int
j
=
0
;
j
<
m_KernelFunctorList
.
size
();
j
++
)
{
if
(
m_KernelFunctorList
[
j
]
==
m_HaveToBeDeletedList
[
i
])
{
delete
m_KernelFunctorList
[
j
];
m_HaveToBeDeletedList
[
i
]
=
NULL
;
}
}
}
};
typedef
std
::
vector
<
GenericKernelFunctorBase
*>
KernelListType
;
virtual
double
operator
()(
const
svm_node
*
x
,
const
svm_node
*
y
,
const
svm_parameter
&
param
)
const
// = 0
{
double
out
=
0.
;
if
(
m_KernelFunctorList
.
size
()
!=
0
&&
m_PonderationList
.
size
()
!=
0
&&
m_KernelFunctorList
.
size
()
==
m_PonderationList
.
size
())
{
for
(
unsigned
int
i
=
0
;
i
<
m_KernelFunctorList
.
size
();
i
++
)
{
if
((
this
->
GetValue
<
bool
>
(
"MultiplyKernelFunctor"
))
==
false
)
{
out
+=
m_PonderationList
[
i
]
*
(
*
m_KernelFunctorList
[
i
])(
x
,
y
,
param
);
}
else
{
out
*=
(
*
m_KernelFunctorList
[
i
])(
x
,
y
,
param
);
}
}
}
else
{
itkGenericExceptionMacro
(
<<
"ComposedKernelFunctor::operator() : lists dimensions mismatch"
);
}
return
out
;
}
virtual
int
load_parameters
(
FILE
**
pfile
);
virtual
int
save_parameters
(
FILE
**
pfile
,
const
char
*
composed_kernel_parameters_keyword
)
const
;
virtual
void
print_parameters
(
void
)
const
;
//virtual void Update(void){};
/** Set/Get the SVM Model vector for the composed kernel */
KernelListType
GetKernelFunctorList
(){
return
m_KernelFunctorList
;
};
void
SetKernelFunctorList
(
KernelListType
kernelFunctorList
){
m_KernelFunctorList
=
kernelFunctorList
;
};
// Add 1 element to the end of the list
void
AddKernelFunctorModelToKernelList
(
GenericKernelFunctorBase
*
kernelfunctor
){
m_KernelFunctorList
.
push_back
(
kernelfunctor
);
};
/** Generic kernel functors that have to be deleted. */
KernelListType
GetHaveToBeDeletedList
(){
return
m_HaveToBeDeletedList
;
};
void
SetHaveToBeDeletedList
(
KernelListType
kernelFunctorList
){
m_HaveToBeDeletedList
=
kernelFunctorList
;
};
// Add 1 element to the end of the list
void
AddKernelFunctorModelToDeleteKernelList
(
GenericKernelFunctorBase
*
kernelfunctor
){
m_HaveToBeDeletedList
.
push_back
(
kernelfunctor
);
};
/** Set/Get the ponderation list to apply to each svm_model of the composed kernel */
std
::
vector
<
double
>
GetPonderationList
(){
return
m_PonderationList
;
};
void
SetPonderationModelList
(
const
std
::
vector
<
double
>
&
list
){
m_PonderationList
=
list
;
};
// Add 1 element to the end of the list
void
AddPonderationToPonderationList
(
const
double
&
pond
){
m_PonderationList
.
push_back
(
pond
);
};
/** Set/Get the boolean to know which operation has to be done with the kernel functors. */
void
SetMultiplyKernelFunctor
(
bool
val
){
this
->
SetValue
<
bool
>
(
"MultiplyKernelFunctor"
,
val
);
};
bool
GetMultiplyKernelFunctor
(){
return
(
this
->
GetValue
<
bool
>
(
"MultiplyKernelFunctor"
));
};
private:
typedef
GenericKernelFunctorBase
::
MapType
MapType
;
typedef
GenericKernelFunctorBase
::
MapIterator
MapIterator
;
typedef
GenericKernelFunctorBase
::
MapConstIterator
MapConstIterator
;
/** Generic kernel functors that composed kernel */
KernelListType
m_KernelFunctorList
;
/** Generic kernel functors that have to be deleted.
* This list was made for the load_parameters methods where you set new functors using new.
* But, in other cases, functor can be added with reference. Thus, we need to know which ones have to be deleted. */
KernelListType
m_HaveToBeDeletedList
;
/** Ponderation list to apply to each svm_model of the composed kernel*/
std
::
vector
<
double
>
m_PonderationList
;
};
//} // namespace otb
#endif
/* _LIBSVM_H */
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment