Commit bca5cddf authored by Davis King's avatar Davis King

Added more refinements and also bindings for svm_rank_trainer.

parent d7b8dfbc
......@@ -8,6 +8,7 @@ add_python_module(dlib
src/matrix.cpp
src/vector.cpp
src/svm_c_trainer.cpp
src/svm_rank_trainer.cpp
src/decision_functions.cpp
src/other.cpp
src/basic.cpp
......
......@@ -9,6 +9,7 @@
#include <dlib/string.h>
#include "serialize_pickle.h"
#include "pyassert.h"
using namespace std;
using namespace dlib;
......@@ -95,6 +96,8 @@ string sparse_vector__repr__ (const std::vector<std::pair<unsigned long,double>
return sout.str();
}
template <typename T>
void resize(T& v, unsigned long n) { v.resize(n); }
void bind_basic_types()
{
......@@ -103,10 +106,14 @@ void bind_basic_types()
.def("__init__", make_constructor(&array_from_object))
.def("__str__", array__str__)
.def("__repr__", array__repr__)
.def("clear", &std::vector<double>::clear)
.def("resize", resize<std::vector<double> >)
.def_pickle(serialize_pickle<std::vector<double> >());
class_<std::vector<matrix<double,0,1> > >("vectors")
.def(vector_indexing_suite<std::vector<matrix<double,0,1> > >())
.def("clear", &std::vector<matrix<double,0,1> >::clear)
.def("resize", resize<std::vector<matrix<double,0,1> > >)
.def_pickle(serialize_pickle<std::vector<matrix<double,0,1> > >());
typedef pair<unsigned long,double> pair_type;
......@@ -122,10 +129,14 @@ void bind_basic_types()
.def(vector_indexing_suite<std::vector<pair_type> >())
.def("__str__", sparse_vector__str__)
.def("__repr__", sparse_vector__repr__)
.def("clear", &std::vector<pair_type >::clear)
.def("resize", resize<std::vector<pair_type > >)
.def_pickle(serialize_pickle<std::vector<pair_type> >());
class_<std::vector<std::vector<pair_type> > >("sparse_vectors")
.def(vector_indexing_suite<std::vector<std::vector<pair_type> > >())
.def("clear", &std::vector<std::vector<pair_type> >::clear)
.def("resize", resize<std::vector<std::vector<pair_type> > >)
.def_pickle(serialize_pickle<std::vector<std::vector<pair_type> > >());
}
......
......@@ -71,6 +71,20 @@ typename df_type::scalar_type get_bias(
return df.b;
}
template <typename df_type>
void set_bias(
df_type& df,
double b
)
{
if (df.basis_vectors.size() == 0)
{
PyErr_SetString( PyExc_ValueError, "Decision function is empty." );
boost::python::throw_error_already_set();
}
df.b = b;
}
template <typename kernel_type>
void add_linear_df (
const std::string name
......@@ -79,8 +93,8 @@ void add_linear_df (
typedef decision_function<kernel_type> df_type;
class_<df_type>(name.c_str())
.def("predict", predict<df_type>)
.def("get_weights", get_weights<df_type>)
.def("get_bias", get_bias<df_type>)
.add_property("weights", &get_weights<df_type>)
.add_property("bias", get_bias<df_type>, set_bias<df_type>)
.def_pickle(serialize_pickle<df_type>());
}
......
......@@ -7,6 +7,7 @@ void bind_svm_c_trainer();
void bind_decision_functions();
void bind_basic_types();
void bind_other();
void bind_svm_rank_trainer();
BOOST_PYTHON_MODULE(dlib)
......@@ -17,4 +18,5 @@ BOOST_PYTHON_MODULE(dlib)
bind_decision_functions();
bind_basic_types();
bind_other();
bind_svm_rank_trainer();
}
......@@ -80,14 +80,10 @@ class_<trainer_type> setup_trainer (
return class_<trainer_type>(name.c_str())
.def("train", train<trainer_type>)
.def("set_c", set_c<trainer_type>)
.def("set_c_class1", set_c_class1<trainer_type>)
.def("set_c_class2", set_c_class2<trainer_type>)
.def("get_c_class1", get_c_class1<trainer_type>)
.def("get_c_class2", get_c_class2<trainer_type>)
.def("get_epsilon", get_epsilon<trainer_type>)
.def("set_epsilon", set_epsilon<trainer_type>)
.def("get_cache_size", get_cache_size<trainer_type>)
.def("set_cache_size", set_cache_size<trainer_type>);
.add_property("c_class1", get_c_class1<trainer_type>, set_c_class1<trainer_type>)
.add_property("c_class2", get_c_class2<trainer_type>, set_c_class2<trainer_type>)
.add_property("epsilon", get_epsilon<trainer_type>, set_epsilon<trainer_type>)
.add_property("cache_size", get_cache_size<trainer_type>, set_cache_size<trainer_type>);
}
void set_gamma (
......@@ -128,12 +124,10 @@ double get_gamma_sparse (
void bind_svm_c_trainer()
{
setup_trainer<svm_c_trainer<radial_basis_kernel<sample_type> > >("svm_c_trainer_radial_basis")
.def("set_gamma", set_gamma)
.def("get_gamma", get_gamma);
.add_property("gamma", get_gamma, set_gamma);
setup_trainer<svm_c_trainer<sparse_radial_basis_kernel<sparse_vect> > >("svm_c_trainer_sparse_radial_basis")
.def("set_gamma", set_gamma_sparse)
.def("get_gamma", get_gamma_sparse);
.add_property("gamma", get_gamma, set_gamma);
setup_trainer<svm_c_trainer<histogram_intersection_kernel<sample_type> > >("svm_c_trainer_histogram_intersection");
......
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include <dlib/matrix.h>
#include "serialize_pickle.h"
#include <dlib/svm.h>
#include "pyassert.h"
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
using namespace dlib;
using namespace std;
using namespace boost::python;
typedef matrix<double,0,1> sample_type;
typedef std::vector<std::pair<unsigned long,double> > sparse_vect;
// ----------------------------------------------------------------------------------------
namespace dlib
{
template <typename T>
bool operator== (
const ranking_pair<T>& ,
const ranking_pair<T>&
)
{
pyassert(false, "It is illegal to compare ranking pair objects for equality.");
return false;
}
}
template <typename T>
void resize(T& v, unsigned long n) { v.resize(n); }
// ----------------------------------------------------------------------------------------
template <typename trainer_type>
typename trainer_type::trained_function_type train1 (
const trainer_type& trainer,
const ranking_pair<typename trainer_type::sample_type>& sample
)
{
typedef ranking_pair<typename trainer_type::sample_type> st;
pyassert(is_ranking_problem(std::vector<st>(1, sample)), "Invalid inputs");
return trainer.train(sample);
}
template <typename trainer_type>
typename trainer_type::trained_function_type train2 (
const trainer_type& trainer,
const std::vector<ranking_pair<typename trainer_type::sample_type> >& samples
)
{
pyassert(is_ranking_problem(samples), "Invalid inputs");
return trainer.train(samples);
}
template <typename trainer_type>
void set_epsilon ( trainer_type& trainer, double eps)
{
pyassert(eps > 0, "epsilon must be > 0");
trainer.set_epsilon(eps);
}
template <typename trainer_type>
double get_epsilon ( const trainer_type& trainer) { return trainer.get_epsilon(); }
template <typename trainer_type>
void set_c ( trainer_type& trainer, double C)
{
pyassert(C > 0, "C must be > 0");
trainer.set_c(C);
}
template <typename trainer_type>
double get_c (const trainer_type& trainer)
{
return trainer.get_c();
}
template <typename trainer>
void add_ranker (
const char* name
)
{
class_<trainer>(name)
.add_property("epsilon", get_epsilon<trainer>, set_epsilon<trainer>)
.add_property("c", get_c<trainer>, set_c<trainer>)
.add_property("max_iterations", &trainer::get_max_iterations, &trainer::set_max_iterations)
.add_property("force_last_weight_to_1", &trainer::forces_last_weight_to_1, &trainer::force_last_weight_to_1)
.add_property("learns_nonnegative_weights", &trainer::learns_nonnegative_weights, &trainer::set_learns_nonnegative_weights)
.def("train", train1<trainer>)
.def("train", train2<trainer>)
.def("be_verbose", &trainer::be_verbose)
.def("be_quiet", &trainer::be_quiet);
}
// ----------------------------------------------------------------------------------------
void bind_svm_rank_trainer()
{
class_<ranking_pair<sample_type> >("ranking_pair")
.add_property("relevant", &ranking_pair<sample_type>::relevant)
.add_property("nonrelevant", &ranking_pair<sample_type>::nonrelevant)
.def_pickle(serialize_pickle<ranking_pair<sample_type> >());
class_<ranking_pair<sparse_vect> >("sparse_ranking_pair")
.add_property("relevant", &ranking_pair<sparse_vect>::relevant)
.add_property("nonrelevant", &ranking_pair<sparse_vect>::nonrelevant)
.def_pickle(serialize_pickle<ranking_pair<sparse_vect> >());
typedef std::vector<ranking_pair<sample_type> > ranking_pairs;
class_<ranking_pairs>("ranking_pairs")
.def(vector_indexing_suite<ranking_pairs>())
.def("clear", &ranking_pairs::clear)
.def("resize", resize<ranking_pairs>)
.def_pickle(serialize_pickle<ranking_pairs>());
typedef std::vector<ranking_pair<sparse_vect> > sparse_ranking_pairs;
class_<sparse_ranking_pairs>("sparse_ranking_pairs")
.def(vector_indexing_suite<sparse_ranking_pairs>())
.def("clear", &sparse_ranking_pairs::clear)
.def("resize", resize<sparse_ranking_pairs>)
.def_pickle(serialize_pickle<sparse_ranking_pairs>());
add_ranker<svm_rank_trainer<linear_kernel<sample_type> > >("svm_rank_trainer");
add_ranker<svm_rank_trainer<sparse_linear_kernel<sparse_vect> > >("svm_rank_trainer_sparse");
}
......@@ -107,11 +107,13 @@ void bind_vector()
{
class_<cv>("vector", init<>())
.def("set_size", &cv_set_size)
.def("resize", &cv_set_size)
.def("__init__", make_constructor(&cv_from_object))
.def("__repr__", &cv__repr__)
.def("__str__", &cv__str__)
.def("__len__", &cv__len__)
.def("__getitem__", &cv__getitem__)
.def("__setitem__", &cv__setitem__)
.add_property("shape", &cv_get_matrix_size)
.def_pickle(serialize_pickle<cv>());
}
......
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