Commit f3d96894 authored by André Anjos's avatar André Anjos 💬

Add remaining C++ classes into main module

parent 46895ca7
......@@ -196,6 +196,8 @@ setup(
Extension("xbob.learn.libsvm._library",
[
"xbob/learn/libsvm/file.cpp",
"xbob/learn/libsvm/machine.cpp",
"xbob/learn/libsvm/trainer.cpp",
],
packages = packages,
include_dirs = include_dirs,
......@@ -204,6 +206,7 @@ setup(
define_macros = define_macros,
library_dirs = library_dirs,
libraries = libraries,
boost_modules = ['filesystem'],
),
],
......
......@@ -8,14 +8,14 @@
* Copyright (C) 2011-2013 Idiap Research Institute, Martigny, Switzerland
*/
#ifndef BOB_MACHINE_SVM_H
#define BOB_MACHINE_SVM_H
#ifndef BOB_LEARN_LIBSVM_MACHINE_H
#define BOB_LEARN_LIBSVM_MACHINE_H
#include <svm.h>
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
#include <blitz/array.h>
#include <fstream>
#include <svm.h>
#include <bob/io/HDF5File.h>
// We need to declare the svm_model type for libsvm < 3.0.0. The next bit of
......@@ -42,86 +42,7 @@ struct svm_model {
};
#endif
namespace bob { namespace machine {
/**
* @ingroup MACHINE
* @{
*/
/**
* Loads a given libsvm data file. The data file format, as defined on the
* library README is like this:
*
* [label] [index1]:[value1] [index2]:[value2] ...
*
* The labels are integer values, so are the indexes, starting from "1" (and
* not from zero as a C-programmer would expect. The values are floating
* point.
*
* Zero values are suppressed - this is a sparse format.
*/
class SVMFile {
public: //api
/**
* Constructor, initializes the file readout.
*/
SVMFile (const std::string& filename);
/**
* Destructor virtualization
*/
virtual ~SVMFile();
/**
* Returns the size of each entry in the file, in number of floats
*/
inline size_t shape() const { return m_shape; }
/**
* Returns the number of samples in the file.
*/
inline size_t samples() const { return m_n_samples; }
/**
* Resets the file, going back to the beginning.
*/
void reset();
/**
* Reads the next entry. Values are organized according to the indexed
* labels at the file. Returns 'false' if the file is over or something
* goes wrong.
*/
bool read(int& label, blitz::Array<double,1>& values);
/**
* Reads the next entry on the file, but without checking. Returns
* 'false' if the file is over or something goes wrong reading the file.
*/
bool read_(int& label, blitz::Array<double,1>& values);
/**
* Returns the name of the file being read.
*/
inline const std::string& filename() const { return m_filename; }
/**
* Tests if the file is still good to go.
*/
inline bool good() const { return m_file.good(); }
inline bool eof() const { return m_file.eof(); }
inline bool fail() const { return m_file.fail(); }
private: //representation
std::string m_filename; ///< The path to the file being read
std::ifstream m_file; ///< The file I'm reading.
size_t m_shape; ///< Number of floats in samples
size_t m_n_samples; ///< total number of samples at input file
};
namespace bob { namespace learn { namespace libsvm {
/**
* Here is the problem: libsvm does not provide a simple way to extract the
......@@ -141,7 +62,7 @@ namespace bob { namespace machine {
/**
* Interface to svm_model, from libsvm. Incorporates prediction.
*/
class SupportVector {
class Machine {
public: //api
......@@ -169,7 +90,7 @@ namespace bob { namespace machine {
* 1.0). If you need scaling to be applied, set it individually using the
* appropriate methods bellow.
*/
SupportVector(const std::string& model_file);
Machine(const std::string& model_file);
/**
* Builds a new Support Vector Machine from an HDF5 file containing the
......@@ -177,23 +98,23 @@ namespace bob { namespace machine {
* from the file. Using this constructor assures a 100% state recovery
* from previous sessions.
*/
SupportVector(bob::io::HDF5File& config);
Machine(bob::io::HDF5File& config);
/**
* Builds a new SVM model from a trained model. Scaling parameters will
* be neutral (subtraction := 0.0, division := 1.0).
*
* @note: This method is typically only used by the respective
* bob::trainer::SupportVectorTrainer as it requires the creation of the
* bob::trainer::MachineTrainer as it requires the creation of the
* object "svm_model". You can still make use of it if you decide to
* implement the model instantiation yourself.
*/
SupportVector(boost::shared_ptr<svm_model> model);
Machine(boost::shared_ptr<svm_model> model);
/**
* Virtual d'tor
*/
virtual ~SupportVector();
virtual ~Machine();
/**
* Tells the input size this machine expects
......@@ -354,9 +275,9 @@ namespace bob { namespace machine {
private: //not implemented
SupportVector(const SupportVector& other);
Machine(const Machine& other);
SupportVector& operator= (const SupportVector& other);
Machine& operator= (const Machine& other);
private: //methods
......@@ -375,9 +296,6 @@ namespace bob { namespace machine {
};
/**
* @}
*/
}}
}}}
#endif /* BOB_MACHINE_SVM_H */
#endif /* BOB_LEARN_LIBSVM_MACHINE_H */
/**
* @file bob/trainer/SVMTrainer.h
* @date Sat Dec 17 14:41:56 2011 +0100
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Sat Dec 17 14:41:56 2011 +0100
*
* @brief C++ bindings to libsvm (training bits)
* @brief C++ bindings to libsvm (training bit)
*
* Copyright (C) 2011-2013 Idiap Research Institute, Martigny, Switzerland
* Copyright (C) 2011-2014 Idiap Research Institute, Martigny, Switzerland
*/
#ifndef BOB_TRAINER_SVMTRAINER_H
#define BOB_TRAINER_SVMTRAINER_H
#ifndef BOB_LEARN_LIBSVM_TRAINER_H
#define BOB_LEARN_LIBSVM_TRAINER_H
#include <vector>
#include "kernel_machine_cxx.h"
#include "machine.h"
namespace bob { namespace trainer {
/**
* @ingroup TRAINER
* @{
*/
namespace bob { namespace learn { namespace libsvm {
/**
* This class emulates the behavior of the command line utility called
......@@ -30,7 +25,7 @@ namespace bob { namespace trainer {
*
* Fell free to implement those and remove these remarks.
*/
class SVMTrainer {
class Trainer {
public: //api
......@@ -38,9 +33,9 @@ namespace bob { namespace trainer {
* Builds a new trainer setting the default parameters as defined in the
* command line application svm-train.
*/
SVMTrainer(
bob::machine::SupportVector::svm_t svm_type=bob::machine::SupportVector::C_SVC,
bob::machine::SupportVector::kernel_t kernel_type=bob::machine::SupportVector::RBF,
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
......@@ -57,7 +52,7 @@ namespace bob { namespace trainer {
/**
* Destructor virtualisation
*/
virtual ~SVMTrainer();
virtual ~Trainer();
/**
* Trains a new machine for multi-class classification. If the number of
......@@ -66,14 +61,14 @@ namespace bob { namespace trainer {
* 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::machine::SupportVector> train
boost::shared_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::machine::SupportVector> train
boost::shared_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;
......@@ -81,11 +76,11 @@ namespace bob { namespace trainer {
/**
* Getters and setters for all parameters
*/
bob::machine::SupportVector::svm_t getSvmType() const { return (bob::machine::SupportVector::svm_t)m_param.svm_type; }
void setSvmType(bob::machine::SupportVector::svm_t v) { m_param.svm_type = v; }
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; }
bob::machine::SupportVector::kernel_t getKernelType() const { return (bob::machine::SupportVector::kernel_t)m_param.kernel_type; }
void setKernelType(bob::machine::SupportVector::kernel_t v) { m_param.kernel_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; }
int getDegree() const { return m_param.degree; }
void setDegree(int v) { m_param.degree = v; }
......@@ -125,9 +120,6 @@ namespace bob { namespace trainer {
};
/**
* @}
*/
}}
}}}
#endif /* BOB_TRAINER_SVMTRAINER_H */
#endif /* BOB_LEARN_LIBSVM_TRAINER_H */
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -14,24 +14,24 @@
using namespace boost::python;
static boost::shared_ptr<bob::machine::SupportVector> train1
static boost::shared_ptr<bob::learn::libsvm::Machine> train1
(const bob::trainer::SVMTrainer& trainer, object data) {
stl_input_iterator<bob::python::const_ndarray> dbegin(data), dend;
std::vector<bob::python::const_ndarray> vdata_ref(dbegin, dend);
std::vector<blitz::Array<double,2> > vdata;
for(std::vector<bob::python::const_ndarray>::iterator it=vdata_ref.begin();
for(std::vector<bob::python::const_ndarray>::iterator it=vdata_ref.begin();
it!=vdata_ref.end(); ++it)
vdata.push_back(it->bz<double,2>());
return trainer.train(vdata);
}
static boost::shared_ptr<bob::machine::SupportVector> train2
static boost::shared_ptr<bob::learn::libsvm::Machine> train2
(const bob::trainer::SVMTrainer& trainer, object data, bob::python::const_ndarray sub,
bob::python::const_ndarray div) {
stl_input_iterator<bob::python::const_ndarray> dbegin(data), dend;
std::vector<bob::python::const_ndarray> vdata_ref(dbegin, dend);
std::vector<blitz::Array<double,2> > vdata;
for(std::vector<bob::python::const_ndarray>::iterator it=vdata_ref.begin();
for(std::vector<bob::python::const_ndarray>::iterator it=vdata_ref.begin();
it!=vdata_ref.end(); ++it)
vdata.push_back(it->bz<double,2>());
return trainer.train(vdata, sub.bz<double,1>(), div.bz<double,1>());
......@@ -39,10 +39,10 @@ static boost::shared_ptr<bob::machine::SupportVector> train2
void bind_trainer_svm() {
class_<bob::trainer::SVMTrainer, boost::shared_ptr<bob::trainer::SVMTrainer> >("SVMTrainer", "This class emulates the behavior of the command line utility called svm-train, from libsvm. These bindings do not support:\n\n * Precomputed Kernels\n * Regression Problems\n * Different weights for every label (-wi option in svm-train)\n\nFell free to implement those and remove these remarks.", no_init)
.def(init<optional<bob::machine::SupportVector::svm_t, bob::machine::SupportVector::kernel_t, int, double, double, double, double, double, double, double, bool, bool> >(
.def(init<optional<bob::learn::libsvm::Machine::svm_t, bob::learn::libsvm::Machine::kernel_t, int, double, double, double, double, double, double, double, bool, bool> >(
(arg("self"),
arg("svm_type")=bob::machine::SupportVector::C_SVC,
arg("kernel_type")=bob::machine::SupportVector::RBF,
arg("svm_type")=bob::learn::libsvm::Machine::C_SVC,
arg("kernel_type")=bob::learn::libsvm::Machine::RBF,
arg("degree")=3, //for poly
arg("gamma")=0., //for poly/rbf/sigmoid
arg("coef0")=0., //for poly/sigmoid
......
/**
* @file trainer/cxx/SVMTrainer.cc
* @date Sun 4 Mar 10:02:45 2012 CET
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Sun 4 Mar 10:02:45 2012 CET
*
* @brief Implementation of the SVM training methods
*
* Copyright (C) 2011-2013 Idiap Research Institute, Martigny, Switzerland
* Copyright (C) 2011-2014 Idiap Research Institute, Martigny, Switzerland
*/
#include <xbob.learn.libsvm/trainer.h>
#include <boost/format.hpp>
#include <boost/make_shared.hpp>
#include <boost/algorithm/string.hpp>
#include <bob/trainer/SVMTrainer.h>
#include <bob/core/logging.h>
#ifdef BOB_DEBUG
......@@ -24,12 +22,12 @@ static std::string strip(const char* s) {
#endif
static void debug_libsvm(const char* s) {
TDEBUG1("[libsvm-" << libsvm_version << "] " << strip(s));
TDEBUG1("[libsvm-" << LIBSVM_VERSION << "] " << strip(s));
}
bob::trainer::SVMTrainer::SVMTrainer(
bob::machine::SupportVector::svm_t svm_type,
bob::machine::SupportVector::kernel_t kernel_type,
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,
......@@ -61,7 +59,7 @@ bob::trainer::SVMTrainer::SVMTrainer(
m_param.weight = 0;
}
bob::trainer::SVMTrainer::~SVMTrainer() { }
bob::learn::libsvm::Trainer::~Trainer() { }
/**
* Erases an SVM problem:
......@@ -210,7 +208,7 @@ static void svm_model_free(svm_model*& m) {
#endif
}
boost::shared_ptr<bob::machine::SupportVector> bob::trainer::SVMTrainer::train
boost::shared_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,10 +257,10 @@ boost::shared_ptr<bob::machine::SupportVector> bob::trainer::SVMTrainer::train
//save newly created machine to file, reload from there to get rid of memory
//dependencies due to the poorly implemented memory model in libsvm
boost::shared_ptr<svm_model> new_model =
bob::machine::svm_unpickle(bob::machine::svm_pickle(model));
bob::learn::libsvm::svm_unpickle(bob::learn::libsvm::svm_pickle(model));
boost::shared_ptr<bob::machine::SupportVector> retval =
boost::make_shared<bob::machine::SupportVector>(new_model);
boost::shared_ptr<bob::learn::libsvm::Machine> retval =
boost::make_shared<bob::learn::libsvm::Machine>(new_model);
//sets up the scaling parameters given as input
retval->setInputSubtraction(input_subtraction);
......@@ -271,7 +269,7 @@ boost::shared_ptr<bob::machine::SupportVector> bob::trainer::SVMTrainer::train
return retval;
}
boost::shared_ptr<bob::machine::SupportVector> bob::trainer::SVMTrainer::train
boost::shared_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