/***************************************************************************** Copyright (c) 2005, Uwe Schmitt (http://www.procoders.net) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of procoders.net nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #define WINVER 0x0500 #include #include #include #include #include #include #include "pcSVM.hpp" #include "Helpers.hpp" std::string buildImageString(int w, int h, float xmin, float xmax, float ymin, float ymax, const ClassifierCSVM &klass) { std::string retval; retval.resize(w*h); float xfac = (xmax-xmin)/(w-1.0); float yfac = (ymax-ymin)/(h-1.0); const char POS=char(255); const char NEG=char(0); pcsvm::SparseVector sv; // es wird in x- und y-richtung nur jeder zweite punkt // betrachtet damit das ganze schneller wird for (int ze=h-1, i=0; ze>=0; ze-=2) { sv[1]=ze*yfac+ymin; for (int sp=0; sp 0 ? POS : NEG; } if (ze>0) i += w; } return retval; } BOOST_PYTHON_MODULE(imageTool) { using namespace boost::python; def("buildImageString", &buildImageString); } using namespace pcsvm; namespace py=boost::python; #define SVFloat SparseVector /////////////////////////////////////////////////////////////////////////////////// // // mapping c++ exceptions --> python exceptions // registrierung der exception-translater weiter unten // unter BOOST_PYTHON_MODULE(SparseVector) // /////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// // // SparseVector extra classes / functions // //////////////////////////////////////////////////////////////////////////////////////////////////// /* SVFloat mul(py::object &self, py::object &other) { if (isType(self)) return imul(py::extract(other), py::extract(self)); if (isType(other)) return imul(py::extract(self), py::extract(other)); // return dot(py::extract(self) , py::extract(other)); } */ ////////////////// Hilfsklasse um SVFloat::map mit Python-Funktion aufrufen zu können: class PyCallable: public SVFloat::FunctionType { public: PyCallable(const py::object &o): fun_(o.attr("__call__")) { }; virtual ~PyCallable() {}; virtual float operator()(float v) const { return py::extract(fun_(v)); } private: py::object fun_; }; ////////////////// Python-Version von SVFloat::map: SVFloat PyMap(const SVFloat &v, const py::object &o) { return v.map(PyCallable(o)); } bool PyNonZero(const SVFloat &v) { return (! v.empty()); } ////////////////// Iterator über einzelnen SVFloat: class SVIterWrap { public: SVIterWrap(const SparseVector &v): ib_(v.data_.begin()), ie_(v.data_.end()) { }; py::object next() { py::object retval; if (ib_ != ie_) { retval= py::make_tuple(ib_->index, ib_->value); ++ib_; } else { PyErr_SetString(PyExc_StopIteration, "No more data."); py::throw_error_already_set(); } return retval; } private: SVFloat::const_iterator ib_, ie_; }; ////////////////// erzeugt Iterator, wird später an Python-Version von SVFloat angehängt SVIterWrap SVIterWrapFabric(const SVFloat &v) { return SVIterWrap(v); } ////////////////// Template um später die paarweisen SVFloat-Iteratoren nach Python zu wrappen: // template class IterWrapper: public ITER { public: IterWrapper(const SparseVector &v1, const SparseVector &v2): ITER(v1, v2) {}; py::object next() { py::object retval; if (isValid()) { retval = py::make_tuple((*this).actualValue_.idx, (*this).actualValue_.val1, (*this).actualValue_.val2); ++(*this); } else { PyErr_SetString(PyExc_StopIteration, "No more data."); py::throw_error_already_set(); } return retval; } }; ////////////////// SVFloat aus Liste von Tupeln erzeugen SVFloat Python_Constructor_From_List(py::object &ob) { SVFloat retVal = SVFloat(); // check ob type PyType pt = getType(ob); if (pt != LIST && pt != TUPLE) // && pt != ARRAY) throw TypeError("only list allowed for initialization."); int length = PyLen(ob); py::object item, idx, entry; int idx_value; float entry_value; item=ob[0]; pt = getType(item); if ((pt == TUPLE || pt == LIST) && PyLen(item)==2) { for (int i=0; i < length; ++i) { // get i-th item of list item=ob[i]; // item is tuple or compatible ? if (PyLen(item) !=2) throw TypeError("item has wrong format"); // get index and check idx = item[0]; if (getType(idx) != INTEGER) throw TypeError("list item: index is no integer"); idx_value = py::extract(idx); if (idx_value<0) throw TypeError("list item: index is negative"); // get value and check entry = item[1]; pt = getType(entry); if (pt!=FLOAT) if (pt != INTEGER) throw TypeError("list item: value ist not float"); else entry_value = (float)(py::extract(entry)); else entry_value = py::extract(entry); // build sparse vector retVal.put(idx_value, entry_value); } } else if (pt == FLOAT || pt == INTEGER) { for (int i=0; i < length; ++i) { item=ob[i]; pt = getType(item); if (pt!=FLOAT) if (pt != INTEGER) throw TypeError("list item: value ist not float"); else entry_value = (float)(py::extract(item)); else entry_value = py::extract(item); // build sparse vector retVal.put(i, entry_value); } } else throw TypeError("initializer has wrong format"); return retVal; } ////////////////// Fabric function inline SVFloat Python_Constructor() { return SVFloat(); } //////////////////////////////////////////////////////////////////////////////////////////////////// // // KernelFunctions: extra classes / functions // //////////////////////////////////////////////////////////////////////////////////////////////////// class PythonKernel: public KernelBase { public: PythonKernel(py::object obj): obj_(obj) { }; virtual std::string name() const { std::string obj_name; obj_name = py::extract(obj_.attr("__str__")()); return ""; } py::object pythonFunction() const { return obj_; } virtual float operator()(const SVFloat &v1, const SVFloat &v2) const { return py::extract(obj_.attr("__call__")(v1,v2)); } private: py::object obj_; }; //////////////////////////////////////////////////////////////////////////////////////////////////// // // Example/Problem: extra classes / functions // //////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////// Iterator über Problem template class ProblemIterWrap { public: ProblemIterWrap(const Problem &v): ib_(v.begin()), ie_(v.end()) { }; Example next() { Example retval; if (ib_ != ie_) { retval= *ib_; ++ib_; } else { PyErr_SetString(PyExc_StopIteration, "No more data."); py::throw_error_already_set(); } return retval; } private: typename Problem::const_iterator ib_, ie_; }; ////////////////// erzeugt Iterator, wird später an Python-Version von SVFloat angehängt template ProblemIterWrap IterWrapFabric(const Problem &p) { return ProblemIterWrap(p); } template Problem ProblemFromList(py::object &ob) { Problem retval; py::extract arg(ob); if( !arg.check()) throw TypeError("only list allowed for initialization."); py::object argAsList = arg(); int length = PyLen(ob); py::object item, sv, label; // iterate over list for (int i=0; i < length; ++i) { // get i-th item of list item=argAsList[i]; // item is tuple or compatible ? if (PyLen(item) !=2) throw TypeError("list item has wrong format"); // get sv and check py::extract sv(item[0]); if (!sv.check()) throw TypeError("list item: first argument of tuple is no SparseVector."); // get label and check py::extract label(item[1]); if (!label.check()) throw TypeError("list item: second argument of tuple is invalid label."); // build sparse vector retval.push_back(Example(sv(), label())); } return retval; } //////////////////////////////////////////////////////////////////////////////////////////////////// // // Classifier/MatrixCache: extra classes / functions // //////////////////////////////////////////////////////////////////////////////////////////////////// template T Fabric() { return T(); } /* void train0(py::object self) { self.attr("train")(1.0); } */ CSVMTrainInfo (ClassifierCSVM::*train0)() = &ClassifierCSVM::train; CSVMTrainInfo (ClassifierCSVM::*train1)(float) = &ClassifierCSVM::train; CSVMTrainInfo (ClassifierCSVM::*train2)(float, float) = &ClassifierCSVM::train; ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// // // // // HIER GEHT DIE DEFINITION DES PYTHON-MODULES LOS // // // ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// BOOST_PYTHON_MODULE(pySVM) { using namespace boost::python; // register_exception_translator(&translateKernelError); register_exception_translator(&translateRuntimeError); register_exception_translator(&translateTypeError); // converters to_python_converter< std::pair, int_pair_to_tuple>(); to_python_converter< std::list< int> , list_of_T_to_python_list >(); to_python_converter< std::list< float> , list_of_T_to_python_list >(); // Iterator über Sparsevector class_("Iter", init() [with_custodian_and_ward<1,2>()]) .def("next", &SVIterWrap::next) .def("__iter__", pass_through); // klasse SparseVector : // class_("SparseVector") //.def(self * other()) .def("put", &SVFloat::put) .def("get", &SVFloat::get) .def("__getitem__", &SVFloat::get) .def("__setitem__", &SVFloat::put) .def("__nonzero__", &::PyNonZero) .def("__iter__", &::SVIterWrapFabric, with_custodian_and_ward_postcall<0,1>()) // , return_internal_reference<1>()) .def("prune", &SVFloat::prune) .def("map", &::PyMap) // , with_custodian_and_ward<0,2>()) .def(self + other()) .def(self - other()) //.def("__imul__", &imul) //.def("__rmul__", &imul) .def(self * other()) .def(float() * self) .def(self * other()) .def(self / other()) .def(self += other()) .def(self -= other()) .def(self_ns::operator-(self) ) .def("__str__", &SVFloat::to_str ) .def("lastIndex", &SVFloat::lastIndex) .def("entries", &SVFloat::entries) .def("norm", &SVFloat::norm) .def("copy", &SVFloat::copy) ; // Iteratoren über Sparse-Vector-Paare wrappen: //typedef IterWrapper AnyWrap; #define AnyWrap IterWrapper class_< AnyWrap >("AnyIter", init()[with_custodian_and_ward<1,2, with_custodian_and_ward<1,3> >()]) .def("next", &AnyWrap::next) .def("__iter__", pass_through) ; //typedef IterWrapper BothWrap; #define BothWrap IterWrapper class_< BothWrap >("BothIter", init()[with_custodian_and_ward<1,2, with_custodian_and_ward<1,3> >()]) //class_< BothWrap >("BothIter", init()) .def("next", &BothWrap::next) .def("__iter__", pass_through) ; //typedef IterWrapper FullWrap; #define FullWrap IterWrapper class_< FullWrap >("FullIter", init()[with_custodian_and_ward<1,2, with_custodian_and_ward<1,3> >()]) //class_< FullWrap >("FullIter", init()) .def("next", &FullWrap::next) .def("__iter__", pass_through) ; // ans modul angängen: // def("dot", &dot); def("norm", &norm); // folgende werden im wrapper zur verfügugn gestellt: def("SparseVector", Python_Constructor); def("SparseVector", Python_Constructor_From_List); class_("KernelBase",no_init) ; /* class_, boost::noncopyable>("SimpleKernel",no_init) .def(self * other() ) .def(other() * self) .def(self+self); */ class_ >("LinearKernel") .def("__str__", &LinearKernel::name ) .def("__call__", &LinearKernel::operator() ) ; class_ >("PolynomialKernel") .def("__str__", &PolynomialKernel::name ) .def(init()) .def("__call__", &PolynomialKernel::operator() ) ; class_ >("RadialKernel") .def("__str__", &RadialKernel::name ) .def(init()) .def(init()) .def("__call__", &RadialKernel::operator() ) ; class_ >("PythonKernel", init()[with_custodian_and_ward_postcall<1,2>()]) .def("pythonFunction", &PythonKernel::pythonFunction) .def("__str__", &PythonKernel::name ) .def("__call__", &PythonKernel::operator() ) ; //.def(str(self) ); // void (ExternalKernel::*srt1)(float) = &ExternalKernel::setRuntimeParameters; // void (ExternalKernel::*srt2)(float, float) = &ExternalKernel::setRuntimeParameters; // void (ExternalKernel::*srt3)(float, float, float) = &ExternalKernel::setRuntimeParameters; // void (ExternalKernel::*srt4)(float, float, float, float) = &ExternalKernel::setRuntimeParameters; //class_ >("ExternalKernel", init()) //.def(self_ns::str(self) ) //.def("setRuntimeParameters", srt1) //.def("setRuntimeParameters", srt2) //.def("setRuntimeParameters", srt3) //.def("setRuntimeParameters", srt4); // //; /* class_ >("CompoundKernel") .def("__call__", &CompoundKernel::operator() ) .def("__str__", &CompoundKernel::name ) .def(self * other() ) .def(other() * self) .def(self + self) .def(self + other() ) .def(other() + self ) ; */ #define ClassificationExample Example #define RegressionExample Example class_("ClassificationExample") .def(init()) // ohne custodian/ward da kopie angelegt wird .def_readwrite("label", &ClassificationExample::label) .def_readwrite("vector", &ClassificationExample::vector) .def("__str__", &ClassificationExample::to_str) ; class_("RegressionExample") .def(init()) // ohne custodian/ward da kopie angelegt wird .def_readwrite("label", &RegressionExample::label) .def_readwrite("vector", &RegressionExample::vector) .def("__str__", &RegressionExample::to_str) //.def(str(self) ) ; ; #define ClassificationProblem Problem #define RegressionProblem Problem class_("ClassificationProblem") .def("__getitem__", &ClassificationProblem::get) .def("__setitem__", &ClassificationProblem::put) .def("__iter__", &::IterWrapFabric, with_custodian_and_ward_postcall<0,1>()) .def("__len__", &ClassificationProblem::size) .def("push_back", &ClassificationProblem::push_back) .def("append", &ClassificationProblem::push_back) .def("reserve", &ClassificationProblem::reserve) .def("normalize", &ClassificationProblem::normalize) .def("__str__", &ClassificationProblem::to_str) ; class_("RegressionProblem") .def("put", &RegressionProblem::put) .def("get", &RegressionProblem::get) .def("__getitem__", &RegressionProblem::get) .def("__setitem__", &RegressionProblem::put) .def("__iter__", &IterWrapFabric,with_custodian_and_ward_postcall<0,1>()) .def("__len__", &RegressionProblem::size) .def("push_back", &RegressionProblem::push_back) .def("append", &RegressionProblem::push_back) .def("reserve", &RegressionProblem::reserve) .def("normalize", &RegressionProblem::normalize) .def("__str__", &RegressionProblem::to_str) ; // Iterator über Problem //typedef ProblemIterWrap PIWfloat; #define PIWfloat ProblemIterWrap class_("RegressionProblemIter", init()[with_custodian_and_ward_postcall<1,2>()]) .def("next", &PIWfloat::next) .def("__iter__", pass_through); //typedef ProblemIterWrap PIWbool; #define PIWbool ProblemIterWrap class_("ClassProblemIter", init()[with_custodian_and_ward_postcall<1,2>()]) .def("next", &PIWbool::next) .def("__iter__", pass_through); def("ClassificationProblem", &ProblemFromList); def("ClassificationProblem", &Fabric); def("RegressionProblem", &ProblemFromList); def("RegressionProblem", &Fabric); // class_("MatrixCacheI", no_init) ; // class_ >("MatrixCache", init()) ; // //class_("TrainingInfoCSVM") // .def_readonly("problemSize", &ClassifierCSVM::TrainingInfo::problemSize) // .def_readonly("numSV", &ClassifierCSVM::TrainingInfo::numSV) // .def_readonly("cp", &ClassifierCSVM::TrainingInfo::cp) // .def_readonly("cn", &ClassifierCSVM::TrainingInfo::cn) // .def_readonly("numIter", &ClassifierCSVM::TrainingInfo::numIter) // .add_property("SVIndices", make_getter(&ClassifierCSVM::TrainingInfo::SVIndices, return_value_policy())) // .add_property("SVWeights", make_getter(&ClassifierCSVM::TrainingInfo::SVWeights, return_value_policy())) //; // class_("CSVMTrainInfo") .def_readonly("numIter", &CSVMTrainInfo::numIter) .def_readonly("numSV", &CSVMTrainInfo::numSV) .def_readonly("svWeights", &CSVMTrainInfo::svWeights) ; class_("ClassifierCSVM", init()[with_custodian_and_ward_postcall<1,2, with_custodian_and_ward_postcall<1,3> >()]) .def("setKernel", &ClassifierCSVM::setKernel, with_custodian_and_ward_postcall<0,1>()) .def("train", train0) .def("train", train1) .def("train", train2) .def("getNumSVs", &ClassifierCSVM::getNumSVs) .def("getThreshold", &ClassifierCSVM::getThreshold) //.def("setCache", &ClassifierCSVM::setCache, with_custodian_and_ward<1,2>()) //folgendes in solver verlagern: //.def("setEps", &ClassifierCSVM::setEps) //.def("setMaxIter", &ClassifierCSVM::setMaxIter) //.def("doShrinking", &ClassifierCSVM::doShrinking) //.def("calculateTrainingInfo", &ClassifierCSVM::calculateTrainingInfo) //.def_readonly("sv", &ClassifierCSVM::supportVectors_) .def("__call__", &ClassifierCSVM::operator()) ; def("buildImageString", buildImageString); }