Commit affd197e authored by Davis King's avatar Davis King

Refined the python bindings more. Still lots to do though.

parent 1a64bcc3
......@@ -7,4 +7,6 @@ add_python_module(dlib
src/dlib.cpp
src/matrix.cpp
src/vector.cpp
src/svm_c_trainer.cpp)
src/svm_c_trainer.cpp
src/decision_funcions.cpp
)
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include "serialize_pickle.h"
#include <dlib/svm.h>
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;
template <typename decision_function>
double predict (
const decision_function& df,
const typename decision_function::kernel_type::sample_type& samp
)
{
if (df.basis_vectors.size() == 0)
{
return 0;
}
else if (df.basis_vectors(0).size() != samp.size())
{
std::ostringstream sout;
sout << "Input vector should have " << df.basis_vectors(0).size() << " dimensions, not " << samp.size() << ".";
PyErr_SetString( PyExc_IndexError, sout.str().c_str() );
boost::python::throw_error_already_set();
}
return df(samp);
}
template <typename kernel_type>
void add_df (
const std::string name
)
{
typedef decision_function<kernel_type> df_type;
class_<df_type>(name.c_str())
.def("predict", &predict<df_type>)
.def_pickle(serialize_pickle<df_type>());
}
void bind_decision_functions()
{
add_df<linear_kernel<sample_type> >("_decision_function_linear");
add_df<sparse_linear_kernel<sparse_vect> >("_decision_function_sparse_linear");
add_df<radial_basis_kernel<sample_type> >("_decision_function_radial_basis");
add_df<sparse_radial_basis_kernel<sparse_vect> >("_decision_function_sparse_radial_basis");
}
......@@ -18,15 +18,101 @@ using namespace boost::python;
void bind_matrix();
void bind_vector();
void bind_svm_c_trainer();
void bind_decision_functions();
boost::shared_ptr<std::vector<double> > array_from_object(object obj)
{
extract<long> thesize(obj);
if (thesize.check())
{
long nr = thesize;
boost::shared_ptr<std::vector<double> > temp(new std::vector<double>(nr));
return temp;
}
else
{
const long nr = len(obj);
boost::shared_ptr<std::vector<double> > temp(new std::vector<double>(nr));
for ( long r = 0; r < nr; ++r)
{
(*temp)[r] = extract<double>(obj[r]);
}
return temp;
}
}
string array__str__ (const std::vector<double>& v)
{
std::ostringstream sout;
for (unsigned long i = 0; i < v.size(); ++i)
{
sout << v[i];
if (i+1 < v.size())
sout << "\n";
}
return sout.str();
}
string array__repr__ (const std::vector<double>& v)
{
std::ostringstream sout;
sout << "dlib.array([";
for (unsigned long i = 0; i < v.size(); ++i)
{
sout << v[i];
if (i+1 < v.size())
sout << ", ";
}
sout << "])";
return sout.str();
}
string pair__str__ (const std::pair<unsigned long,double>& p)
{
std::ostringstream sout;
sout << p.first << ": " << p.second;
return sout.str();
}
string pair__repr__ (const std::pair<unsigned long,double>& p)
{
std::ostringstream sout;
sout << "dlib.pair(" << p.first << ", " << p.second << ")";
return sout.str();
}
string sparse_vector__str__ (const std::vector<std::pair<unsigned long,double> >& v)
{
std::ostringstream sout;
for (unsigned long i = 0; i < v.size(); ++i)
{
sout << v[i].first << ": " << v[i].second;
if (i+1 < v.size())
sout << "\n";
}
return sout.str();
}
string sparse_vector__repr__ (const std::vector<std::pair<unsigned long,double> >& v)
{
std::ostringstream sout;
sout << "< dlib.sparse_vector containing: \n" << sparse_vector__str__(v) << " >";
return sout.str();
}
BOOST_PYTHON_MODULE(dlib)
{
bind_matrix();
bind_vector();
bind_svm_c_trainer();
bind_decision_functions();
class_<std::vector<double> >("array")
class_<std::vector<double> >("array", init<>())
.def(vector_indexing_suite<std::vector<double> >())
.def("__init__", make_constructor(&array_from_object))
.def("__str__", array__str__)
.def("__repr__", array__repr__)
.def_pickle(serialize_pickle<std::vector<double> >());
class_<std::vector<matrix<double,0,1> > >("vectors")
......@@ -34,14 +120,18 @@ BOOST_PYTHON_MODULE(dlib)
.def_pickle(serialize_pickle<std::vector<matrix<double,0,1> > >());
typedef pair<unsigned long,double> pair_type;
class_<pair_type>("pair", "help message", init<>() )
class_<pair_type>("pair", "This object is used to represent the elements of a sparse_vector.", init<>() )
.def(init<unsigned long,double>())
.def_readwrite("first",&pair_type::first, "THE FIRST, LOVE IT!")
.def_readwrite("second",&pair_type::second)
.def_readwrite("first",&pair_type::first, "This field represents the index/dimension number.")
.def_readwrite("second",&pair_type::second, "This field contains the value in a vector at dimension specified by the first field.")
.def("__str__", pair__str__)
.def("__repr__", pair__repr__)
.def_pickle(serialize_pickle<pair_type>());
class_<std::vector<pair_type> >("sparse_vector")
.def(vector_indexing_suite<std::vector<pair_type> >())
.def("__str__", sparse_vector__str__)
.def("__repr__", sparse_vector__repr__)
.def_pickle(serialize_pickle<std::vector<pair_type> >());
class_<std::vector<std::vector<pair_type> > >("sparse_vectors")
......
......@@ -2,6 +2,7 @@
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include <dlib/matrix.h>
#include <dlib/string.h>
#include "serialize_pickle.h"
......@@ -16,15 +17,29 @@ void matrix_set_size(matrix<double>& m, long nr, long nc)
m = 0;
}
string matrix_double__repr__(matrix<double>& c)
{
ostringstream sout;
sout << "< dlib.matrix containing: \n";
sout << c;
return trim(sout.str()) + " >";
}
string matrix_double__str__(matrix<double>& c)
{
ostringstream sout;
sout << c;
return sout.str();
return trim(sout.str());
}
boost::shared_ptr<matrix<double> > make_matrix_from_size(long nr, long nc)
{
if (nr < 0 || nc < 0)
{
PyErr_SetString( PyExc_IndexError, "Input dimensions can't be negative."
);
boost::python::throw_error_already_set();
}
boost::shared_ptr<matrix<double> > temp(new matrix<double>(nr,nc));
*temp = 0;
return temp;
......@@ -89,6 +104,13 @@ string mat_row__str__(mat_row& c)
return sout.str();
}
string mat_row__repr__(mat_row& c)
{
ostringstream sout;
sout << "< matrix row: " << mat(c.data,1, c.size);
return trim(sout.str()) + " >";
}
long mat_row__len__(mat_row& m)
{
return m.size;
......@@ -130,7 +152,7 @@ void bind_matrix()
{
class_<mat_row>("_row")
.def("__len__", &mat_row__len__)
.def("__repr__", &mat_row__str__)
.def("__repr__", &mat_row__repr__)
.def("__str__", &mat_row__str__)
.def("__setitem__", &mat_row__setitem__)
.def("__getitem__", &mat_row__getitem__);
......@@ -139,7 +161,7 @@ void bind_matrix()
.def("__init__", make_constructor(&make_matrix_from_size))
.def("set_size", &matrix_set_size)
.def("__init__", make_constructor(&from_object))
.def("__repr__", &matrix_double__str__)
.def("__repr__", &matrix_double__repr__)
.def("__str__", &matrix_double__str__)
.def("__len__", &matrix_double__len__)
.def("__getitem__", &matrix_double__getitem__, with_custodian_and_ward_postcall<0,1>())
......
......@@ -34,8 +34,9 @@ struct serialize_pickle : boost::python::pickle_suite
throw_error_already_set();
}
std::string& data = extract<std::string&>(state[0]);
std::istringstream sin(data);
str data = extract<str>(state[0]);
std::string temp(extract<const char*>(data), len(data));
std::istringstream sin(temp);
deserialize(item, sin);
}
};
......
......@@ -21,9 +21,6 @@ void bind_kernel(
class_<trainer>("svm_c_trainer")
.def("train", &trainer::template train<std::vector<sample_type>,std::vector<double> >);
typedef decision_function<kernel_type> df;
class_<df>("df")
.def("predict", &df::operator());
}
......
......@@ -17,10 +17,29 @@ void cv_set_size(cv& m, long s)
m = 0;
}
string cv__str__(cv& c)
string cv__str__(const cv& v)
{
ostringstream sout;
sout << c;
for (long i = 0; i < v.size(); ++i)
{
sout << v(i);
if (i+1 < v.size())
sout << "\n";
}
return sout.str();
}
string cv__repr__ (const cv& v)
{
std::ostringstream sout;
sout << "dlib.vector([";
for (unsigned long i = 0; i < v.size(); ++i)
{
sout << v(i);
if (i+1 < v.size())
sout << ", ";
}
sout << "])";
return sout.str();
}
......@@ -89,7 +108,7 @@ void bind_vector()
class_<cv>("vector", init<>())
.def("set_size", &cv_set_size)
.def("__init__", make_constructor(&cv_from_object))
.def("__repr__", &cv__str__)
.def("__repr__", &cv__repr__)
.def("__str__", &cv__str__)
.def("__len__", &cv__len__)
.def("__getitem__", &cv__getitem__)
......
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