[opengm] 40/386: learnable functions in python seems to work

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:35:02 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository opengm.

commit 62e22c793b3a40d9d35ea45ed0bf8b3b9c990341
Author: DerThorsten <thorsten.beier at iwr.uni-heidelberg.de>
Date:   Mon Dec 15 14:58:58 2014 +0100

    learnable functions in python seems to work
---
 include/opengm/python/opengmpython.hxx             | 61 +++++++++++++---------
 .../python/opengm/opengmcore/opengmcore.cpp        | 19 +++++++
 .../python/opengm/opengmcore/pyFunctionTypes.cxx   | 36 ++++++++++++-
 src/interfaces/python/opengm/opengmcore/pyGm.cxx   | 10 ++--
 4 files changed, 96 insertions(+), 30 deletions(-)

diff --git a/include/opengm/python/opengmpython.hxx b/include/opengm/python/opengmpython.hxx
index dc7588c..7418d2d 100644
--- a/include/opengm/python/opengmpython.hxx
+++ b/include/opengm/python/opengmpython.hxx
@@ -18,6 +18,9 @@
 #include "opengm/functions/truncated_squared_difference.hxx"
 #include "opengm/functions/sparsemarray.hxx"
 
+#include "opengm/functions/learnable/lpotts.hxx"
+
+
 #include <opengm/python/opengmpython.hxx>
 #include <opengm/python/converter.hxx>
 #include <opengm/python/numpyview.hxx>
@@ -45,31 +48,36 @@ namespace python{
       typedef opengm::ExplicitFunction<V ,I,I> type;
    };
 
-   template<class V,class I>
-   struct FTLGen{
-
-      typedef V ValueType;
-      typedef I IndexType;
-      typedef I LabelType;
-      typedef opengm::ExplicitFunction                      <ValueType,IndexType,LabelType> PyExplicitFunction;
-      typedef opengm::PottsFunction                         <ValueType,IndexType,LabelType> PyPottsFunction;
-      typedef opengm::PottsNFunction                        <ValueType,IndexType,LabelType> PyPottsNFunction;
-      typedef opengm::PottsGFunction                        <ValueType,IndexType,LabelType> PyPottsGFunction;
-      typedef opengm::TruncatedAbsoluteDifferenceFunction   <ValueType,IndexType,LabelType> PyTruncatedAbsoluteDifferenceFunction;
-      typedef opengm::TruncatedSquaredDifferenceFunction    <ValueType,IndexType,LabelType> PyTruncatedSquaredDifferenceFunction;
-      typedef opengm::SparseFunction                        <ValueType,IndexType,LabelType> PySparseFunction; 
-      typedef PythonFunction                                <ValueType,IndexType,LabelType> PyPythonFunction; 
-
-      typedef typename opengm::meta::TypeListGenerator<
-         PyExplicitFunction,
-         PyPottsFunction,
-         PyPottsNFunction,
-         PyPottsGFunction,
-         PyTruncatedAbsoluteDifferenceFunction,
-         PyTruncatedSquaredDifferenceFunction,
-         PySparseFunction,
-         PyPythonFunction
-      >::type type;
+    template<class V,class I>
+    struct FTLGen{
+
+        typedef V ValueType;
+        typedef I IndexType;
+        typedef I LabelType;
+        typedef opengm::ExplicitFunction                      <ValueType,IndexType,LabelType> PyExplicitFunction;
+        typedef opengm::PottsFunction                         <ValueType,IndexType,LabelType> PyPottsFunction;
+        typedef opengm::PottsNFunction                        <ValueType,IndexType,LabelType> PyPottsNFunction;
+        typedef opengm::PottsGFunction                        <ValueType,IndexType,LabelType> PyPottsGFunction;
+        typedef opengm::TruncatedAbsoluteDifferenceFunction   <ValueType,IndexType,LabelType> PyTruncatedAbsoluteDifferenceFunction;
+        typedef opengm::TruncatedSquaredDifferenceFunction    <ValueType,IndexType,LabelType> PyTruncatedSquaredDifferenceFunction;
+        typedef opengm::SparseFunction                        <ValueType,IndexType,LabelType> PySparseFunction; 
+        typedef PythonFunction                                <ValueType,IndexType,LabelType> PyPythonFunction; 
+        // learning functions
+        typedef opengm::functions::learnable::LPotts          <ValueType,IndexType,LabelType> PyLPottsFunction;
+
+
+
+        typedef typename opengm::meta::TypeListGenerator<
+            PyExplicitFunction,
+            PyPottsFunction,
+            PyPottsNFunction,
+            PyPottsGFunction,
+            PyTruncatedAbsoluteDifferenceFunction,
+            PyTruncatedSquaredDifferenceFunction,
+            PySparseFunction,
+            PyPythonFunction,
+            PyLPottsFunction
+        >::type type;
    };
 
 
@@ -79,6 +87,8 @@ namespace python{
    typedef GmIndexType GmLabelType;
 
 
+   typedef opengm::learning::Weights<GmValueType> PyWeights;
+
 
    // different function types
    typedef opengm::ExplicitFunction                      <GmValueType,GmIndexType,GmLabelType> GmExplicitFunction;
@@ -91,6 +101,7 @@ namespace python{
    typedef opengm::TruncatedSquaredDifferenceFunction    <GmValueType,GmIndexType,GmLabelType> GmTruncatedSquaredDifferenceFunction;
    typedef opengm::SparseFunction                        <GmValueType,GmIndexType,GmLabelType> GmSparseFunction; 
    typedef opengm::python::PythonFunction                <GmValueType,GmIndexType,GmLabelType> GmPythonFunction; 
+   typedef opengm::functions::learnable::LPotts          <GmValueType,GmIndexType,GmLabelType> PyLPottsFunction;
 
    typedef std::vector<GmIndexType> IndexVectorType;
    typedef std::vector<IndexVectorType> IndexVectorVectorType;
diff --git a/src/interfaces/python/opengm/opengmcore/opengmcore.cpp b/src/interfaces/python/opengm/opengmcore/opengmcore.cpp
index 7040b99..af7af84 100644
--- a/src/interfaces/python/opengm/opengmcore/opengmcore.cpp
+++ b/src/interfaces/python/opengm/opengmcore/opengmcore.cpp
@@ -166,6 +166,21 @@ typename D::value_type  dequePushBack(
 
 
 
+
+template<class V>
+void pyExportWeights(const std::string & clsName){
+
+    typedef opengm::learning::Weights<V> Weights;
+
+    boost::python::class_<Weights>(clsName.c_str(),boost::python::init<const size_t >())
+        .def("__getitem__", &Weights::getWeight)
+        .def("__setitem__", &Weights::setWeight)
+    ;
+
+}
+
+
+
 BOOST_PYTHON_MODULE_INIT(_opengmcore) {
    Py_Initialize();
    PyEval_InitThreads();
@@ -264,6 +279,10 @@ BOOST_PYTHON_MODULE_INIT(_opengmcore) {
       
    }
 
+   // for learning
+   {
+        pyExportWeights<opengm::python::GmValueType>("Weights");
+   }
 
 
 
diff --git a/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx b/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx
index 6571b3d..f618621 100644
--- a/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx
+++ b/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx
@@ -169,6 +169,27 @@ namespace pyfunction{
       return f;
    }
 
+
+
+
+   template<class FUNCTION>
+   FUNCTION * lPottsConstructor(
+        opengm::python::PyWeights & pyWeights,
+        const opengm::python::GmLabelType numberOfLabels,
+        opengm::python::NumpyView<opengm::python::GmIndexType,1> weightIds,
+        opengm::python::NumpyView<opengm::python::GmValueType,1> features
+
+    ){
+      FUNCTION * f = NULL;
+      
+      std::vector<size_t>      weightIdVec(weightIds.begin(), weightIds.end());
+      std::vector<opengm::python::GmValueType> featureVec(features.begin(), features.end());
+
+      f = new FUNCTION(pyWeights, numberOfLabels, weightIdVec, featureVec);
+      return f;
+   }
+
+
    ////////////////////////////////////////
    // EXPLICIT FUNCTION
    ////////////////////////////////////////
@@ -335,7 +356,7 @@ void export_functiontypes(){
    typedef opengm::TruncatedSquaredDifferenceFunction    <ValueType,IndexType,LabelType> PyTruncatedSquaredDifferenceFunction;
    typedef opengm::SparseFunction                        <ValueType,IndexType,LabelType> PySparseFunction; 
    typedef opengm::python::PythonFunction                <ValueType,IndexType,LabelType> PyPythonFunction; 
-    
+   typedef opengm::functions::learnable::LPotts          <ValueType,IndexType,LabelType> PyLPottsFunction;
    // vector exporters
    export_function_type_vector<PyExplicitFunction>("ExplicitFunctionVector");
    
@@ -599,6 +620,19 @@ void export_functiontypes(){
    )
    ;
 
+
+   FUNCTION_TYPE_EXPORTER_HELPER(PyLPottsFunction,"LPottsFunction")
+    .def("__init__", make_constructor(&pyfunction::lPottsConstructor<PyLPottsFunction> ,default_call_policies(),
+         (
+            boost::python::arg("weights"),
+            boost::python::arg("numberOfLabels"),
+            boost::python::arg("weightIds"),
+            boost::python::arg("features")
+         )
+      ),
+   "todo"
+   );
+
 }
 
 template void export_functiontypes<opengm::python::GmValueType,opengm::python::GmIndexType>();
diff --git a/src/interfaces/python/opengm/opengmcore/pyGm.cxx b/src/interfaces/python/opengm/opengmcore/pyGm.cxx
index 0fe3cc9..41d6cb3 100644
--- a/src/interfaces/python/opengm/opengmcore/pyGm.cxx
+++ b/src/interfaces/python/opengm/opengmcore/pyGm.cxx
@@ -683,6 +683,7 @@ namespace pygm {
          typedef opengm::TruncatedSquaredDifferenceFunction    <ValueType,IndexType,LabelType> PyTruncatedSquaredDifferenceFunction;
          typedef opengm::SparseFunction                        <ValueType,IndexType,LabelType> PySparseFunction; 
          typedef opengm::python::PythonFunction                <ValueType,IndexType,LabelType> PyPythonFunction; 
+         typedef opengm::functions::learnable::LPotts          <ValueType,IndexType,LabelType> PyLPottsFunction;
 
          if(fname==std::string("explicit")){
             return gm. template  reserveFunctions<PyExplicitFunction>(size);
@@ -1458,7 +1459,7 @@ void export_gm() {
    typedef opengm::TruncatedSquaredDifferenceFunction    <ValueType,IndexType,LabelType> PyTruncatedSquaredDifferenceFunction;
    typedef opengm::SparseFunction                        <ValueType,IndexType,LabelType> PySparseFunction; 
    typedef opengm::python::PythonFunction                <ValueType,IndexType,LabelType> PyPythonFunction; 
-
+   typedef opengm::functions::learnable::LPotts          <ValueType,IndexType,LabelType> PyLPottsFunction;
 
 
    typedef typename PyGm::FunctionIdentifier PyFid;
@@ -1840,7 +1841,7 @@ void export_gm() {
    .def("_addFunctions_vector",&pygm::addFunctionsGenericVectorPy<PyGm,PyPythonFunction>,return_value_policy<manage_new_object>(),args("functions"),"todo")
 
 
-   .def("_addFunction",&pygm::addFunctionGenericPy<PyGm,PyPottsFunction>,args("function"))
+   .def("_addFunction",&pygm::addFunctionGenericPy<PyGm,PyLPottsFunction>,args("function"))
    .def("_addFunction",&pygm::addFunctionGenericPy<PyGm,PyPottsFunction>,args("function"))
    .def("_addFunction",&pygm::addFunctionGenericPy<PyGm,PyPottsNFunction>,args("function"))
    .def("_addFunction",&pygm::addFunctionGenericPy<PyGm,PyPottsGFunction>,args("function"))
@@ -1850,9 +1851,10 @@ void export_gm() {
    .def("_addFunction",&pygm::addFunctionGenericPy<PyGm,PyTruncatedSquaredDifferenceFunction>,args("function"))
    .def("_addFunction",&pygm::addFunctionGenericPy<PyGm,PySparseFunction>,args("function"))
    .def("_addFunction",&pygm::addFunctionGenericPy<PyGm,PyPythonFunction>,args("function"))
-	.def("_addFunction", &pygm::addFunctionNpPy<PyGm>,args("function"))
+   .def("_addFunction",&pygm::addFunctionGenericPy<PyGm,PyPythonFunction>,args("function"))
+   .def("_addFunction", &pygm::addFunctionNpPy<PyGm>,args("function"))
    .def("_addFactor", &pygm::addFactor_Any<PyGm,int>, (arg("fid"),arg("variableIndices"),arg("finalize")))
-	.def("_addFactor", &pygm::addFactor_Numpy<PyGm>, (arg("fid"),arg("variableIndices"),arg("finalize")))
+   .def("_addFactor", &pygm::addFactor_Numpy<PyGm>, (arg("fid"),arg("variableIndices"),arg("finalize")))
    .def("_addFactor", &pygm::addFactor_Vector<PyGm>, (arg("fid"),arg("variableIndices"),arg("finalize")))
    .def("_addUnaryFactors_vector_numpy", &pygm::addUnaryFactors_Vector_Numpy<PyGm>, (arg("fid"),arg("variableIndices"),arg("finalize")))
    .def("_addFactors_vector_numpy", &pygm::addFactors_Vector_Numpy<PyGm>, (arg("fid"),arg("variableIndices"),arg("finalize")))

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/opengm.git



More information about the debian-science-commits mailing list