Commit 8b3c00a4 authored by André Anjos's avatar André Anjos 💬

Semi-final version of module functionality

parent 52bdb308
......@@ -74,7 +74,7 @@ class libsvm:
libraries when searching.
"""
candidates = find_header('svm.h', subpaths=['libsvm'])
candidates = find_header('svm.h', subpaths=['', 'libsvm'])
if not candidates:
raise RuntimeError("could not find libsvm's `svm.h' - have you installed libsvm on this machine?")
......@@ -195,13 +195,14 @@ setup(
),
Extension("xbob.learn.libsvm._library",
[
"xbob/learn/libsvm/pytrainer.cpp",
"xbob/learn/libsvm/pyfile.cpp",
"xbob/learn/libsvm/pymachine.cpp",
"xbob/learn/libsvm/main.cpp",
"xbob/learn/libsvm/file.cpp",
"xbob/learn/libsvm/machine.cpp",
"xbob/learn/libsvm/trainer.cpp",
"xbob/learn/libsvm/pyutils.cpp",
"xbob/learn/libsvm/pyfile.cpp",
"xbob/learn/libsvm/pymachine.cpp",
"xbob/learn/libsvm/pytrainer.cpp",
"xbob/learn/libsvm/main.cpp",
],
packages = packages,
include_dirs = include_dirs,
......
......@@ -60,6 +60,10 @@ typedef struct {
#define PyBobLearnLibsvmMachine_Check_RET int
#define PyBobLearnLibsvmMachine_Check_PROTO (PyObject* o)
#define PyBobLearnLibsvmMachine_NewFromMachine_NUM 5
#define PyBobLearnLibsvmMachine_NewFromMachine_RET PyObject*
#define PyBobLearnLibsvmMachine_NewFromMachine_PROTO (bob::learn::libsvm::Machine* m)
/******************************************
* Bindings for xbob.learn.libsvm.Trainer *
******************************************/
......@@ -69,15 +73,43 @@ typedef struct {
bob::learn::libsvm::Trainer* cxx;
} PyBobLearnLibsvmTrainerObject;
#define PyBobLearnLibsvmTrainer_Type_NUM 5
#define PyBobLearnLibsvmTrainer_Type_NUM 6
#define PyBobLearnLibsvmTrainer_Type_TYPE PyTypeObject
#define PyBobLearnLibsvmTrainer_Check_NUM 6
#define PyBobLearnLibsvmTrainer_Check_NUM 7
#define PyBobLearnLibsvmTrainer_Check_RET int
#define PyBobLearnLibsvmTrainer_Check_PROTO (PyObject* o)
/*********************************
* Bindings to general utilities *
*********************************/
#define PyBobLearnLibsvm_MachineTypeAsString_NUM 8
#define PyBobLearnLibsvm_MachineTypeAsString_RET PyObject*
#define PyBobLearnLibsvm_MachineTypeAsString_PROTO (bob::learn::libsvm::machine_t s)
#define PyBobLearnLibsvm_StringAsMachineType_NUM 9
#define PyBobLearnLibsvm_StringAsMachineType_RET bob::learn::libsvm::machine_t
#define PyBobLearnLibsvm_StringAsMachineType_PROTO (PyObject* o)
#define PyBobLearnLibsvm_CStringAsMachineType_NUM 10
#define PyBobLearnLibsvm_CStringAsMachineType_RET bob::learn::libsvm::machine_t
#define PyBobLearnLibsvm_CStringAsMachineType_PROTO (const char* s)
#define PyBobLearnLibsvm_KernelTypeAsString_NUM 11
#define PyBobLearnLibsvm_KernelTypeAsString_RET PyObject*
#define PyBobLearnLibsvm_KernelTypeAsString_PROTO (bob::learn::libsvm::kernel_t s)
#define PyBobLearnLibsvm_StringAsKernelType_NUM 12
#define PyBobLearnLibsvm_StringAsKernelType_RET bob::learn::libsvm::kernel_t
#define PyBobLearnLibsvm_StringAsKernelType_PROTO (PyObject* o)
#define PyBobLearnLibsvm_CStringAsKernelType_NUM 13
#define PyBobLearnLibsvm_CStringAsKernelType_RET bob::learn::libsvm::kernel_t
#define PyBobLearnLibsvm_CStringAsKernelType_PROTO (const char* s)
/* Total number of C API pointers */
#define PyXbobLearnLibsvm_API_pointers 7
#define PyXbobLearnLibsvm_API_pointers 14
#ifdef XBOB_LEARN_LIBSVM_MODULE
......@@ -105,6 +137,8 @@ typedef struct {
PyBobLearnLibsvmMachine_Check_RET PyBobLearnLibsvmMachine_Check PyBobLearnLibsvmMachine_Check_PROTO;
PyBobLearnLibsvmMachine_NewFromMachine_RET PyBobLearnLibsvmMachine_NewFromMachine PyBobLearnLibsvmMachine_NewFromMachine_PROTO;
/******************************************
* Bindings for xbob.learn.libsvm.Trainer *
******************************************/
......@@ -113,6 +147,22 @@ typedef struct {
PyBobLearnLibsvmTrainer_Check_RET PyBobLearnLibsvmTrainer_Check PyBobLearnLibsvmTrainer_Check_PROTO;
/*********************************
* Bindings to general utilities *
*********************************/
PyBobLearnLibsvm_MachineTypeAsString_RET PyBobLearnLibsvm_MachineTypeAsString PyBobLearnLibsvm_MachineTypeAsString_PROTO;
PyBobLearnLibsvm_StringAsMachineType_RET PyBobLearnLibsvm_StringAsMachineType PyBobLearnLibsvm_StringAsMachineType_PROTO;
PyBobLearnLibsvm_CStringAsMachineType_RET PyBobLearnLibsvm_CStringAsMachineType PyBobLearnLibsvm_CStringAsMachineType_PROTO;
PyBobLearnLibsvm_KernelTypeAsString_RET PyBobLearnLibsvm_KernelTypeAsString PyBobLearnLibsvm_KernelTypeAsString_PROTO;
PyBobLearnLibsvm_StringAsKernelType_RET PyBobLearnLibsvm_StringAsKernelType PyBobLearnLibsvm_StringAsKernelType_PROTO;
PyBobLearnLibsvm_CStringAsKernelType_RET PyBobLearnLibsvm_CStringAsKernelType PyBobLearnLibsvm_CStringAsKernelType_PROTO;
#else
/* This section is used in modules that use `xbob.learn.libsvm's' C-API */
......@@ -161,6 +211,8 @@ typedef struct {
# define PyBobLearnLibsvmMachine_Check (*(PyBobLearnLibsvmMachine_Check_RET (*)PyBobLearnLibsvmMachine_Check_PROTO) PyXbobLearnLibsvm_API[PyBobLearnLibsvmMachine_Check_NUM])
# define PyBobLearnLibsvmMachine_NewFromMachine (*(PyBobLearnLibsvmMachine_NewFromMachine_RET (*)PyBobLearnLibsvmMachine_NewFromMachine_PROTO) PyXbobLearnLibsvm_API[PyBobLearnLibsvmMachine_NewFromMachine_NUM])
/******************************************
* Bindings for xbob.learn.libsvm.Trainer *
******************************************/
......@@ -169,6 +221,22 @@ typedef struct {
# define PyBobLearnLibsvmTrainer_Check (*(PyBobLearnLibsvmTrainer_Check_RET (*)PyBobLearnLibsvmTrainer_Check_PROTO) PyXbobLearnLibsvm_API[PyBobLearnLibsvmTrainer_Check_NUM])
/*********************************
* Bindings to general utilities *
*********************************/
# define PyBobLearnLibsvm_MachineTypeAsString (*(PyBobLearnLibsvm_MachineTypeAsString_RET (*)PyBobLearnLibsvm_MachineTypeAsString_PROTO) PyXbobLearnLibsvm_API[PyBobLearnLibsvm_MachineTypeAsString_NUM])
# define PyBobLearnLibsvm_StringAsMachineType (*(PyBobLearnLibsvm_StringAsMachineType_RET (*)PyBobLearnLibsvm_StringAsMachineType_PROTO) PyXbobLearnLibsvm_API[PyBobLearnLibsvm_StringAsMachineType_NUM])
# define PyBobLearnLibsvm_CStringAsMachineType (*(PyBobLearnLibsvm_CStringAsMachineType_RET (*)PyBobLearnLibsvm_CStringAsMachineType_PROTO) PyXbobLearnLibsvm_API[PyBobLearnLibsvm_CStringAsMachineType_NUM])
# define PyBobLearnLibsvm_KernelTypeAsString (*(PyBobLearnLibsvm_KernelTypeAsString_RET (*)PyBobLearnLibsvm_KernelTypeAsString_PROTO) PyXbobLearnLibsvm_API[PyBobLearnLibsvm_KernelTypeAsString_NUM])
# define PyBobLearnLibsvm_StringAsKernelType (*(PyBobLearnLibsvm_StringAsKernelType_RET (*)PyBobLearnLibsvm_StringAsKernelType_PROTO) PyXbobLearnLibsvm_API[PyBobLearnLibsvm_StringAsKernelType_NUM])
# define PyBobLearnLibsvm_CStringAsKernelType (*(PyBobLearnLibsvm_CStringAsKernelType_RET (*)PyBobLearnLibsvm_CStringAsKernelType_PROTO) PyXbobLearnLibsvm_API[PyBobLearnLibsvm_CStringAsKernelType_NUM])
# if !defined(NO_IMPORT_ARRAY)
/**
......
......@@ -44,6 +44,22 @@ struct svm_model {
namespace bob { namespace learn { namespace libsvm {
enum machine_t {
C_SVC,
NU_SVC,
ONE_CLASS,
EPSILON_SVR,
NU_SVR
}; /* the machine type */
enum kernel_t {
LINEAR,
POLY,
RBF,
SIGMOID,
PRECOMPUTED
}; /* kernel type used on the machine */
/**
* Here is the problem: libsvm does not provide a simple way to extract the
* information from the SVM structure. There are lots of cases and allocation
......@@ -66,22 +82,6 @@ namespace bob { namespace learn { namespace libsvm {
public: //api
enum svm_t {
C_SVC,
NU_SVC,
ONE_CLASS,
EPSILON_SVR,
NU_SVR
}; /* svm_type */
enum kernel_t {
LINEAR,
POLY,
RBF,
SIGMOID,
PRECOMPUTED
}; /* kernel_type */
/**
* Builds a new Support Vector Machine from a libsvm model file
*
......@@ -144,7 +144,7 @@ namespace bob { namespace learn { namespace libsvm {
/**
* SVM type
*/
svm_t machineType() const;
machine_t machineType() const;
/**
* Kernel type
......
......@@ -11,6 +11,7 @@
#define BOB_LEARN_LIBSVM_TRAINER_H
#include <vector>
#include <memory>
#include "machine.h"
namespace bob { namespace learn { namespace libsvm {
......@@ -34,16 +35,10 @@ namespace bob { namespace learn { namespace libsvm {
* command line application svm-train.
*/
Trainer(
bob::learn::libsvm::Machine::svm_t svm_type=bob::learn::libsvm::Machine::C_SVC,
bob::learn::libsvm::Machine::kernel_t kernel_type=bob::learn::libsvm::Machine::RBF,
int degree=3, //for poly
double gamma=0., //for poly/rbf/sigmoid
double coef0=0., //for poly/sigmoid
machine_t machine_type=C_SVC,
kernel_t kernel_type=RBF,
double cache_size=100, //in MB
double eps=1.e-3, //stopping criteria epsilon
double C=1., //for C_SVC, EPSILON_SVR and NU_SVR
double nu=0.5, //for NU_SVC, ONE_CLASS and NU_SVR
double p=0.1, //for EPSILON_SVR, this is the "epsilon" value there
bool shrinking=true, //use the shrinking heuristics
bool probability=false //do probability estimates
);
......@@ -61,14 +56,14 @@ namespace bob { namespace learn { namespace libsvm {
* from 1 (i.e., 1, 2, 3, 4, etc.). If what you want is regression, the
* size of the input data array should be 1.
*/
boost::shared_ptr<bob::learn::libsvm::Machine> train
std::unique_ptr<bob::learn::libsvm::Machine> train
(const std::vector<blitz::Array<double,2> >& data) const;
/**
* This version accepts scaling parameters that will be applied
* column-wise to the input data.
*/
boost::shared_ptr<bob::learn::libsvm::Machine> train
std::unique_ptr<bob::learn::libsvm::Machine> train
(const std::vector<blitz::Array<double,2> >& data,
const blitz::Array<double,1>& input_subtract,
const blitz::Array<double,1>& input_division) const;
......@@ -76,11 +71,11 @@ namespace bob { namespace learn { namespace libsvm {
/**
* Getters and setters for all parameters
*/
bob::learn::libsvm::Machine::svm_t getSvmType() const { return (bob::learn::libsvm::Machine::svm_t)m_param.svm_type; }
void setSvmType(bob::learn::libsvm::Machine::svm_t v) { m_param.svm_type = v; }
machine_t getMachineType() const { return (machine_t)m_param.svm_type; }
void setMachineType(machine_t v) { m_param.svm_type = v; }
bob::learn::libsvm::Machine::kernel_t getKernelType() const { return (bob::learn::libsvm::Machine::kernel_t)m_param.kernel_type; }
void setKernelType(bob::learn::libsvm::Machine::kernel_t v) { m_param.kernel_type = v; }
kernel_t getKernelType() const { return (kernel_t)m_param.kernel_type; }
void setKernelType(kernel_t v) { m_param.kernel_type = v; }
int getDegree() const { return m_param.degree; }
void setDegree(int v) { m_param.degree = v; }
......
......@@ -178,11 +178,11 @@ int bob::learn::libsvm::Machine::classLabel(size_t i) const {
}
bob::learn::libsvm::Machine::svm_t bob::learn::libsvm::Machine::machineType() const {
return (svm_t)svm_get_svm_type(m_model.get());
bob::learn::libsvm::machine_t bob::learn::libsvm::Machine::machineType() const {
return (machine_t)svm_get_svm_type(m_model.get());
}
bob::learn::libsvm::Machine::kernel_t bob::learn::libsvm::Machine::kernelType() const {
bob::learn::libsvm::kernel_t bob::learn::libsvm::Machine::kernelType() const {
return (kernel_t)m_model->param.kernel_type;
}
......
......@@ -93,6 +93,8 @@ static PyObject* create_module (void) {
PyXbobLearnLibsvm_API[PyBobLearnLibsvmMachine_Check_NUM] = (void *)&PyBobLearnLibsvmMachine_Check;
PyXbobLearnLibsvm_API[PyBobLearnLibsvmMachine_NewFromMachine_NUM] = (void *)&PyBobLearnLibsvmMachine_NewFromMachine;
/******************************************
* Bindings for xbob.learn.libsvm.Trainer *
******************************************/
......@@ -101,6 +103,22 @@ static PyObject* create_module (void) {
PyXbobLearnLibsvm_API[PyBobLearnLibsvmTrainer_Check_NUM] = (void *)&PyBobLearnLibsvmTrainer_Check;
/*********************************
* Bindings to general utilities *
*********************************/
PyXbobLearnLibsvm_API[PyBobLearnLibsvm_MachineTypeAsString_NUM] = (void *)&PyBobLearnLibsvm_MachineTypeAsString;
PyXbobLearnLibsvm_API[PyBobLearnLibsvm_StringAsMachineType_NUM] = (void *)&PyBobLearnLibsvm_StringAsMachineType;
PyXbobLearnLibsvm_API[PyBobLearnLibsvm_CStringAsMachineType_NUM] = (void *)&PyBobLearnLibsvm_CStringAsMachineType;
PyXbobLearnLibsvm_API[PyBobLearnLibsvm_KernelTypeAsString_NUM] = (void *)&PyBobLearnLibsvm_KernelTypeAsString;
PyXbobLearnLibsvm_API[PyBobLearnLibsvm_StringAsKernelType_NUM] = (void *)&PyBobLearnLibsvm_StringAsKernelType;
PyXbobLearnLibsvm_API[PyBobLearnLibsvm_CStringAsKernelType_NUM] = (void *)&PyBobLearnLibsvm_CStringAsKernelType;
#if PY_VERSION_HEX >= 0x02070000
/* defines the PyCapsule */
......
......@@ -274,51 +274,21 @@ static PyObject* PyBobLearnLibsvmMachine_getLabels
return retval;
}
PyDoc_STRVAR(s_svm_type_str, "svm_type");
PyDoc_STRVAR(s_svm_type_doc, "The type of SVM machine contained");
PyDoc_STRVAR(s_machine_type_str, "machine_type");
PyDoc_STRVAR(s_machine_type_doc, "The type of SVM machine contained");
static PyObject* PyBobLearnLibsvmMachine_getSvmType
static PyObject* PyBobLearnLibsvmMachine_getMachineType
(PyBobLearnLibsvmMachineObject* self, void* /*closure*/) {
switch(self->cxx->machineType()) {
case bob::learn::libsvm::Machine::C_SVC:
return Py_BuildValue("s", "C_SVC");
case bob::learn::libsvm::Machine::NU_SVC:
return Py_BuildValue("s", "NU_SVC");
case bob::learn::libsvm::Machine::ONE_CLASS:
return Py_BuildValue("s", "ONE_CLASS");
case bob::learn::libsvm::Machine::EPSILON_SVR:
return Py_BuildValue("s", "EPSILON_SVR");
case bob::learn::libsvm::Machine::NU_SVR:
return Py_BuildValue("s", "NU_SVR");
}
// if you get to this point, an error occurred somewhere - corruption?
PyErr_Format(PyExc_AssertionError, "`%s' has a machine type which is not legal (%d) - DEBUG ME", Py_TYPE(self)->tp_name, (int)self->cxx->machineType());
return 0;
return PyBobLearnLibsvm_MachineTypeAsString(self->cxx->machineType());
}
PyDoc_STRVAR(s_svm_kernel_type_str, "kernel_type");
PyDoc_STRVAR(s_svm_kernel_type_doc,
"The type of kernel used by the support vectors in this machine");
static PyObject* PyBobLearnLibsvmMachine_getSvmKernelType
static PyObject* PyBobLearnLibsvmMachine_getKernelType
(PyBobLearnLibsvmMachineObject* self, void* /*closure*/) {
switch(self->cxx->kernelType()) {
case bob::learn::libsvm::Machine::LINEAR:
return Py_BuildValue("s", "LINEAR");
case bob::learn::libsvm::Machine::POLY:
return Py_BuildValue("s", "POLY");
case bob::learn::libsvm::Machine::RBF:
return Py_BuildValue("s", "RBF");
case bob::learn::libsvm::Machine::SIGMOID:
return Py_BuildValue("s", "SIGMOID");
case bob::learn::libsvm::Machine::PRECOMPUTED:
return Py_BuildValue("s", "PRECOMPUTED");
}
// if you get to this point, an error occurred somewhere - corruption?
PyErr_Format(PyExc_AssertionError, "`%s' has a kernel type which is not legal (%d) - DEBUG ME", Py_TYPE(self)->tp_name, (int)self->cxx->machineType());
return 0;
return PyBobLearnLibsvm_KernelTypeAsString(self->cxx->kernelType());
}
PyDoc_STRVAR(s_degree_str, "degree");
......@@ -391,15 +361,15 @@ static PyGetSetDef PyBobLearnLibsvmMachine_getseters[] = {
0
},
{
s_svm_type_str,
(getter)PyBobLearnLibsvmMachine_getSvmType,
s_machine_type_str,
(getter)PyBobLearnLibsvmMachine_getMachineType,
0,
s_svm_type_doc,
s_machine_type_doc,
0
},
{
s_svm_kernel_type_str,
(getter)PyBobLearnLibsvmMachine_getSvmKernelType,
(getter)PyBobLearnLibsvmMachine_getKernelType,
0,
s_svm_kernel_type_doc,
0
......@@ -1071,6 +1041,17 @@ static PyObject* PyBobLearnLibsvmMachine_new
}
PyObject* PyBobLearnLibsvmMachine_NewFromMachine
(bob::learn::libsvm::Machine* m) {
PyBobLearnLibsvmMachineObject* retval = (PyBobLearnLibsvmMachineObject*)PyBobLearnLibsvmMachine_new(&PyBobLearnLibsvmMachine_Type, 0, 0);
retval->cxx = m; ///< takes ownership
return reinterpret_cast<PyObject*>(retval);
}
PyTypeObject PyBobLearnLibsvmMachine_Type = {
PyVarObject_HEAD_INIT(0, 0)
s_svm_str, /* tp_name */
......
This diff is collapsed.
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Fri 28 Mar 13:08:40 2014 CET
*
* @brief Bindings for a Bob compatible LIBSVM-based Trainer for SVMs
*
* Copyright (C) 2011-2014 Idiap Research Institute, Martigny, Switzerland
*/
#define XBOB_LEARN_LIBSVM_MODULE
#include <xbob.blitz/cleanup.h>
#include <xbob.learn.libsvm/api.h>
PyObject* PyBobLearnLibsvm_MachineTypeAsString(bob::learn::libsvm::machine_t s) {
switch(s) {
case bob::learn::libsvm::C_SVC:
return Py_BuildValue("s", "C_SVC");
case bob::learn::libsvm::NU_SVC:
return Py_BuildValue("s", "NU_SVC");
case bob::learn::libsvm::ONE_CLASS:
return Py_BuildValue("s", "ONE_CLASS");
case bob::learn::libsvm::EPSILON_SVR:
return Py_BuildValue("s", "EPSILON_SVR");
case bob::learn::libsvm::NU_SVR:
return Py_BuildValue("s", "NU_SVR");
default:
PyErr_Format(PyExc_AssertionError, "illegal machine type (%d) - DEBUG ME", s);
return 0;
}
}
bob::learn::libsvm::machine_t PyBobLearnLibsvm_StringAsMachineType(PyObject* o) {
//sane way to extract a string from an object w/o macros
PyObject* args = Py_BuildValue("(O)", o);
auto args_ = make_safe(args);
const char* s = 0;
if (!PyArg_ParseTuple(args, "s", &s)) return (bob::learn::libsvm::machine_t)-1;
return PyBobLearnLibsvm_CStringAsMachineType(s);
}
bob::learn::libsvm::machine_t PyBobLearnLibsvm_CStringAsMachineType(const char* s) {
static const char* available = "`C_SVC' or `NU_SVC'";
std::string s_(s);
if (s_ == "C_SVC") {
return bob::learn::libsvm::C_SVC;
}
else if (s_ == "NU_SVC") {
return bob::learn::libsvm::NU_SVC;
}
else if (s_ == "ONE_CLASS") {
PyErr_Format(PyExc_NotImplementedError, "support for `%s' is not currently implemented by these bindings - choose from %s", s, available);
return bob::learn::libsvm::ONE_CLASS;
}
else if (s_ == "EPSILON_SVR") {
PyErr_Format(PyExc_NotImplementedError, "support for `%s' is not currently implemented by these bindings - choose from %s", s, available);
return bob::learn::libsvm::EPSILON_SVR;
}
else if (s_ == "NU_SVR") {
PyErr_Format(PyExc_NotImplementedError, "support for `%s' is not currently implemented by these bindings - choose from %s", s, available);
return bob::learn::libsvm::NU_SVR;
}
PyErr_Format(PyExc_ValueError, "SVM type `%s' is not supported by these bindings - choose from %s", s, available);
return (bob::learn::libsvm::machine_t)(-1);
}
PyObject* PyBobLearnLibsvm_KernelTypeAsString(bob::learn::libsvm::kernel_t s) {
switch(s) {
case bob::learn::libsvm::LINEAR:
return Py_BuildValue("s", "LINEAR");
case bob::learn::libsvm::POLY:
return Py_BuildValue("s", "POLY");
case bob::learn::libsvm::RBF:
return Py_BuildValue("s", "RBF");
case bob::learn::libsvm::SIGMOID:
return Py_BuildValue("s", "SIGMOID");
case bob::learn::libsvm::PRECOMPUTED:
return Py_BuildValue("s", "PRECOMPUTED");
default:
// if you get to this point, an error occurred somewhere - corruption?
PyErr_Format(PyExc_AssertionError, "illegal kernel type (%d) - DEBUG ME", s);
return 0;
}
}
bob::learn::libsvm::kernel_t PyBobLearnLibsvm_StringAsKernelType(PyObject* o) {
//portable way to extract a string from an object w/o macros
PyObject* args = Py_BuildValue("(O)", o);
auto args_ = make_safe(args);
const char* s = 0;
if (!PyArg_ParseTuple(args, "s", &s)) return (bob::learn::libsvm::kernel_t)-1;
return PyBobLearnLibsvm_CStringAsKernelType(s);
}
bob::learn::libsvm::kernel_t PyBobLearnLibsvm_CStringAsKernelType(const char* s) {
static const char* available = "`LINEAR', `POLY', `RBF' or `SIGMOID'";
std::string s_(s);
if (s_ == "LINEAR") {
return bob::learn::libsvm::LINEAR;
}
else if (s_ == "POLY") {
return bob::learn::libsvm::POLY;
}
else if (s_ == "RBF") {
return bob::learn::libsvm::RBF;
}
else if (s_ == "SIGMOID") {
return bob::learn::libsvm::SIGMOID;
}
else if (s_ == "PRECOMPUTED") {
PyErr_Format(PyExc_NotImplementedError, "support for `%s' kernels is not currently implemented by these bindings - choose from %s", s, available);
}
PyErr_Format(PyExc_ValueError, "SVM kernel type `%s' is not supported by these bindings - choose from %s", s, available);
return (bob::learn::libsvm::kernel_t)(-1);
}
......@@ -26,30 +26,24 @@ static void debug_libsvm(const char* s) {
}
bob::learn::libsvm::Trainer::Trainer(
bob::learn::libsvm::Machine::svm_t svm_type,
bob::learn::libsvm::Machine::kernel_t kernel_type,
int degree,
double gamma,
double coef0,
bob::learn::libsvm::machine_t machine_type,
bob::learn::libsvm::kernel_t kernel_type,
double cache_size,
double eps,
double C,
double nu,
double p,
bool shrinking,
bool probability
)
{
m_param.svm_type = svm_type;
m_param.svm_type = machine_type;
m_param.kernel_type = kernel_type;
m_param.degree = degree;
m_param.gamma = gamma;
m_param.coef0 = coef0;
m_param.degree = 3;
m_param.gamma = 0.;
m_param.coef0 = 0.;
m_param.cache_size = cache_size;
m_param.eps = eps;
m_param.C = C;
m_param.nu = nu;
m_param.p = p;
m_param.C = 1;
m_param.nu = 0.5;
m_param.p = 0.1;
m_param.shrinking = shrinking;
m_param.probability = probability;
......@@ -208,7 +202,7 @@ static void svm_model_free(svm_model*& m) {
#endif
}
boost::shared_ptr<bob::learn::libsvm::Machine> bob::learn::libsvm::Trainer::train
std::unique_ptr<bob::learn::libsvm::Machine> bob::learn::libsvm::Trainer::train
(const std::vector<blitz::Array<double, 2> >& data,
const blitz::Array<double,1>& input_subtraction,
const blitz::Array<double,1>& input_division) const {
......@@ -259,8 +253,8 @@ boost::shared_ptr<bob::learn::libsvm::Machine> bob::learn::libsvm::Trainer::trai
boost::shared_ptr<svm_model> new_model =
bob::learn::libsvm::svm_unpickle(bob::learn::libsvm::svm_pickle(model));
boost::shared_ptr<bob::learn::libsvm::Machine> retval =
boost::make_shared<bob::learn::libsvm::Machine>(new_model);
std::unique_ptr<bob::learn::libsvm::Machine> retval
(new bob::learn::libsvm::Machine(new_model));
//sets up the scaling parameters given as input
retval->setInputSubtraction(input_subtraction);
......@@ -269,7 +263,7 @@ boost::shared_ptr<bob::learn::libsvm::Machine> bob::learn::libsvm::Trainer::trai
return retval;
}
boost::shared_ptr<bob::learn::libsvm::Machine> bob::learn::libsvm::Trainer::train
std::unique_ptr<bob::learn::libsvm::Machine> bob::learn::libsvm::Trainer::train
(const std::vector<blitz::Array<double,2> >& data) const {
int n_features = data[0].extent(blitz::secondDim);
blitz::Array<double,1> sub(n_features);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment