[opengm] 80/386: new learning module

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:35:08 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 3edf711a11edb0d5325aceeb5f193fbbe316ac8e
Author: DerThorsten <thorsten.beier at iwr.uni-heidelberg.de>
Date:   Wed Dec 17 11:29:33 2014 +0100

    new learning module
---
 fubar/python_stuff.py                              | 14 +++-
 src/interfaces/python/opengm/CMakeLists.txt        |  1 +
 .../python/opengm/learning/CMakeLists.txt          | 79 ++++++++++++++++++++++
 src/interfaces/python/opengm/learning/__init__.py  |  1 +
 src/interfaces/python/opengm/learning/learning.cxx | 47 +++++++++++++
 .../opengm/{opengmcore => learning}/pyDataset.cxx  |  3 +
 .../python/opengm/learning/pyGridSearchLearner.cxx | 33 +++++++++
 .../opengm/{opengmcore => learning}/pyLoss.cxx     |  5 ++
 .../python/opengm/learning/pyWeights.cxx           | 36 ++++++++++
 .../python/opengm/opengmcore/CMakeLists.txt        |  4 --
 .../python/opengm/opengmcore/opengmcore.cpp        | 17 +----
 .../python/opengm/opengmcore/pyDataset.hxx         |  4 --
 src/interfaces/python/opengm/opengmcore/pyLoss.hxx |  4 --
 13 files changed, 218 insertions(+), 30 deletions(-)

diff --git a/fubar/python_stuff.py b/fubar/python_stuff.py
index d05ef64..852ea8a 100644
--- a/fubar/python_stuff.py
+++ b/fubar/python_stuff.py
@@ -1,5 +1,6 @@
 import opengm
 import numpy
+from opengm import learning
 np = numpy
 
 
@@ -7,6 +8,15 @@ numLabels = 3
 numVar = 6
 
 
+
+
+#################################################################
+# add a unary function 
+##################################################################
+
+print opengm.learning.DatasetWithHammingLoss
+print opengm.learning.HammingLoss
+
 # make the gm
 space = numpy.ones(numVar)*numLabels
 gm = opengm.gm(space)
@@ -14,7 +24,7 @@ gm = opengm.gm(space)
 
 
 weightVals = numpy.ones(100)*1.0
-weights = opengm.Weights(weightVals)
+weights = opengm.learning.Weights(weightVals)
 
 
 
@@ -45,7 +55,7 @@ print "unary",np.array(gm[0])
 
 
 ##################################################################
-# add a unary function 
+# add a unary function                                           
 ##################################################################
 features  = [
     numpy.array([1.0, 1.0],             dtype=opengm.value_type),
diff --git a/src/interfaces/python/opengm/CMakeLists.txt b/src/interfaces/python/opengm/CMakeLists.txt
index 4c18034..4511e23 100644
--- a/src/interfaces/python/opengm/CMakeLists.txt
+++ b/src/interfaces/python/opengm/CMakeLists.txt
@@ -37,6 +37,7 @@ include_directories(
 
 add_subdirectory(opengmcore)
 add_subdirectory(inference)
+add_subdirectory(learning)
 add_subdirectory(hdf5)
 add_subdirectory(benchmark)
 
diff --git a/src/interfaces/python/opengm/learning/CMakeLists.txt b/src/interfaces/python/opengm/learning/CMakeLists.txt
new file mode 100644
index 0000000..e865415
--- /dev/null
+++ b/src/interfaces/python/opengm/learning/CMakeLists.txt
@@ -0,0 +1,79 @@
+#--------------------------------------------------------------
+# Include Directories
+#--------------------------------------------------------------
+find_package(NUMPY)
+include_directories(
+    ${CMAKE_CURRENT_SOURCE_DIR}
+	 ${PYTHON_INCLUDE_DIRS}
+	 ${PYTHON_NUMPY_INCLUDE_DIR}
+    ${Boost_INCLUDE_DIR}
+    ${Boost_PYTHON_INCLUDE_DIR}
+)
+
+
+
+
+
+#--------------------------------------------------------------
+# Add opengmcore library
+#--------------------------------------------------------------
+if(APPLE)
+    add_library(_learning MODULE learning.cxx
+            pyWeights.cxx
+            pyDataset.cxx
+            pyLoss.cxx
+    )
+else()
+    add_library(_learning SHARED  learning.cxx
+            pyWeights.cxx
+            pyDataset.cxx
+            pyLoss.cxx
+    )
+endif(APPLE)
+
+
+#--------------------------------------------------------------
+# Link libraries
+#--------------------------------------------------------------
+if(MSVC AND NOT(MSVC_VERSION LESS 1400))
+    SET_TARGET_PROPERTIES(_learning PROPERTIES COMPILE_FLAGS "/bigobj")
+endif()
+if(APPLE)
+    SET_TARGET_PROPERTIES(_learning PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
+endif(APPLE)
+
+
+if(LINK_RT)
+    find_library(RT rt)
+    target_link_libraries(_learning ${Boost_PYTHON_LIBRARIES} rt)
+else()
+    target_link_libraries(_learning ${Boost_PYTHON_LIBRARIES})
+endif(LINK_RT)
+
+set_target_properties(_learning PROPERTIES PREFIX "")
+
+
+
+IF(WIN32)
+    SET_TARGET_PROPERTIES(_learning PROPERTIES OUTPUT_NAME "learning"  PREFIX "_" SUFFIX  ".pyd")
+ELSEIF(APPLE)
+    SET_TARGET_PROPERTIES(_learning PROPERTIES OUTPUT_NAME "learning" PREFIX "_" SUFFIX ".so")
+ELSE()
+    SET_TARGET_PROPERTIES(_learning PROPERTIES OUTPUT_NAME "learning"   PREFIX "_")
+ENDIF()
+
+
+
+
+
+
+#--------------------------------------------------------------
+# Copy from src to build
+#--------------------------------------------------------------
+
+if( ${CMAKE_CURRENT_SOURCE_DIR} STREQUAL  ${CMAKE_CURRENT_BINARY_DIR} )
+   message(STATUS "same src and build dir.")
+else()
+   message(STATUS "copy python-learning files  from src to build" )
+   file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR} )
+endif()
diff --git a/src/interfaces/python/opengm/learning/__init__.py b/src/interfaces/python/opengm/learning/__init__.py
new file mode 100644
index 0000000..84afc48
--- /dev/null
+++ b/src/interfaces/python/opengm/learning/__init__.py
@@ -0,0 +1 @@
+from _learning import *
diff --git a/src/interfaces/python/opengm/learning/learning.cxx b/src/interfaces/python/opengm/learning/learning.cxx
new file mode 100644
index 0000000..00daf80
--- /dev/null
+++ b/src/interfaces/python/opengm/learning/learning.cxx
@@ -0,0 +1,47 @@
+#include <boost/python.hpp>
+#include <stddef.h>
+#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
+#include <opengm/python/opengmpython.hxx>
+#include <opengm/python/converter.hxx>
+#include <opengm/python/numpyview.hxx>
+#include <opengm/python/pythonfunction.hxx>
+
+#include <opengm/learning/loss/hammingloss.hxx>
+#include <opengm/learning/loss/generalized-hammingloss.hxx>
+#include <opengm/learning/loss/noloss.hxx>
+
+namespace bp = boost::python;
+namespace op = opengm::python;
+namespace ol = opengm::learning;
+
+namespace opengm{
+
+    void export_weights();
+
+    template<class GM, class LOSS>
+    void export_dataset(const std::string& className);
+
+    template<class GM>
+    void export_loss();
+}
+
+
+
+BOOST_PYTHON_MODULE_INIT(_learning) {
+
+
+    Py_Initialize();
+    PyEval_InitThreads();
+    bp::numeric::array::set_module_and_type("numpy", "ndarray");
+    bp::docstring_options doc_options(true,true,false);
+
+
+    opengm::export_weights();
+
+    // export loss
+    opengm::export_loss<op::GmAdder>();
+
+    // templated datasets
+    opengm::export_dataset<op::GmAdder, ol::HammingLoss >("DatasetWithHammingLoss");
+    opengm::export_dataset<op::GmAdder, ol::NoLoss >("DatasetWithNoLoss");
+}
diff --git a/src/interfaces/python/opengm/opengmcore/pyDataset.cxx b/src/interfaces/python/opengm/learning/pyDataset.cxx
similarity index 99%
rename from src/interfaces/python/opengm/opengmcore/pyDataset.cxx
rename to src/interfaces/python/opengm/learning/pyDataset.cxx
index e65097a..5981d4c 100644
--- a/src/interfaces/python/opengm/opengmcore/pyDataset.cxx
+++ b/src/interfaces/python/opengm/learning/pyDataset.cxx
@@ -13,6 +13,8 @@
 
 using namespace boost::python;
 
+namespace opengm{
+
 template<class GM, class LOSS>
 void pySetInstance(opengm::datasets::EditableDataset<GM, LOSS>& ds,
                    const size_t i,
@@ -55,3 +57,4 @@ template void export_dataset<opengm::python::GmAdder, opengm::learning::HammingL
 template void export_dataset<opengm::python::GmAdder, opengm::learning::NoLoss> (const std::string& className);
 //template void export_dataset<opengm::python::GmAdder, opengm::learning::GeneralizedHammingLoss> (const std::string& className);
 
+}
diff --git a/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx b/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx
new file mode 100644
index 0000000..4211e80
--- /dev/null
+++ b/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx
@@ -0,0 +1,33 @@
+#include <boost/python.hpp>
+#include <boost/python/module.hpp>
+#include <opengm/python/opengmpython.hxx>
+#include <opengm/python/converter.hxx>
+#include <opengm/python/numpyview.hxx>
+
+
+
+namespace bp = boost::python;
+namespace op = opengm::python;
+namespace ol = opengm::learning;
+
+namespace opengm{
+
+    template<class V>
+    learning::Weights<V>  * pyWeightsConstructor(
+        python::NumpyView<V, 1> values                                           
+    ){
+        learning::Weights<V>   * f = new learning::Weights<V> (values.shape(0));
+        for(size_t i=0; i<values.shape(0); ++i){
+            f->setWeight(i, values(i));
+        }
+        return f;
+    }
+
+
+    template<class DATASET>
+    void export_grid_search_learner(const std::string & clsName){
+        
+    }
+
+
+}
diff --git a/src/interfaces/python/opengm/opengmcore/pyLoss.cxx b/src/interfaces/python/opengm/learning/pyLoss.cxx
similarity index 98%
rename from src/interfaces/python/opengm/opengmcore/pyLoss.cxx
rename to src/interfaces/python/opengm/learning/pyLoss.cxx
index 0ac6ad5..f06721c 100644
--- a/src/interfaces/python/opengm/opengmcore/pyLoss.cxx
+++ b/src/interfaces/python/opengm/learning/pyLoss.cxx
@@ -11,6 +11,8 @@
 
 using namespace boost::python;
 
+namespace opengm{
+    
 template <class GM>
 void export_loss(){
    typedef typename std::vector<typename GM::LabelType>::const_iterator Literator;
@@ -35,4 +37,7 @@ void export_loss(){
    ;
 }
 
+
 template void export_loss<opengm::python::GmAdder>();
+
+}
diff --git a/src/interfaces/python/opengm/learning/pyWeights.cxx b/src/interfaces/python/opengm/learning/pyWeights.cxx
new file mode 100644
index 0000000..9aadaed
--- /dev/null
+++ b/src/interfaces/python/opengm/learning/pyWeights.cxx
@@ -0,0 +1,36 @@
+#include <boost/python.hpp>
+#include <boost/python/module.hpp>
+#include <opengm/python/opengmpython.hxx>
+#include <opengm/python/converter.hxx>
+#include <opengm/python/numpyview.hxx>
+
+
+
+namespace opengm{
+
+    template<class V>
+    learning::Weights<V>  * pyWeightsConstructor(
+        python::NumpyView<V, 1> values                                           
+    ){
+        learning::Weights<V>   * f = new learning::Weights<V> (values.shape(0));
+        for(size_t i=0; i<values.shape(0); ++i){
+            f->setWeight(i, values(i));
+        }
+        return f;
+    }
+
+
+
+    void export_weights(){
+        typedef  python::GmValueType V;
+        typedef learning::Weights<V> Weights;
+        boost::python::class_<Weights>("Weights",boost::python::init<const size_t >())
+            .def("__init__", make_constructor(&pyWeightsConstructor<V> ,boost::python::default_call_policies()))
+            .def("__getitem__", &Weights::getWeight)
+            .def("__setitem__", &Weights::setWeight)
+        ;
+    }
+
+
+
+}
diff --git a/src/interfaces/python/opengm/opengmcore/CMakeLists.txt b/src/interfaces/python/opengm/opengmcore/CMakeLists.txt
index 0e791a0..e70955e 100644
--- a/src/interfaces/python/opengm/opengmcore/CMakeLists.txt
+++ b/src/interfaces/python/opengm/opengmcore/CMakeLists.txt
@@ -46,8 +46,6 @@ if(APPLE)
     pySpace.cxx
     pyVector.cxx
     pyEnum.cxx
-    pyDataset.cxx
-    pyLoss.cxx
     )
 else()
     add_library(_opengmcore SHARED  
@@ -64,8 +62,6 @@ else()
     pySpace.cxx
     pyVector.cxx
     pyEnum.cxx
-    pyDataset.cxx
-    pyLoss.cxx
     )
 endif(APPLE)
 
diff --git a/src/interfaces/python/opengm/opengmcore/opengmcore.cpp b/src/interfaces/python/opengm/opengmcore/opengmcore.cpp
index 499e6a3..e6c8689 100644
--- a/src/interfaces/python/opengm/opengmcore/opengmcore.cpp
+++ b/src/interfaces/python/opengm/opengmcore/opengmcore.cpp
@@ -14,9 +14,7 @@
 #include <opengm/utilities/tribool.hxx>
 #include <opengm/inference/inference.hxx>
 
-#include <opengm/learning/loss/hammingloss.hxx>
-#include <opengm/learning/loss/generalized-hammingloss.hxx>
-#include <opengm/learning/loss/noloss.hxx>
+
 
 #include <opengm/python/opengmpython.hxx>
 #include <opengm/python/converter.hxx>
@@ -34,8 +32,6 @@
 #include "pyFunctionGen.hxx"
 #include "pySpace.hxx"
 #include "pyVector.hxx"
-#include "pyLoss.hxx"
-#include "pyDataset.hxx"
 
 
 #include "opengm/functions/explicit_function.hxx"
@@ -685,12 +681,6 @@ BOOST_PYTHON_MODULE_INIT(_opengmcore) {
       
    }
 
-   // for learning
-   {
-        pyExportWeights<opengm::python::GmValueType>("Weights");
-   }
-
-
    //export_rag();
    export_config();
    export_vectors<opengm::python::GmIndexType>();
@@ -718,11 +708,6 @@ BOOST_PYTHON_MODULE_INIT(_opengmcore) {
 
       export_potts_model_3d<opengm::python::GmAdder>();
       export_potts_model_3d_masked<opengm::python::GmAdder>();
-
-      export_loss<opengm::python::GmAdder>();
-      export_dataset<opengm::python::GmAdder, opengm::learning::HammingLoss >("DatasetWithHammingLoss");
-//      export_dataset<opengm::python::GmAdder, opengm::learning::GeneralizedHammingLoss >("DatasetWithGeneralizedHammingLoss");
-      export_dataset<opengm::python::GmAdder, opengm::learning::NoLoss >("DatasetWithNoLoss");
    }
    //multiplier
    {
diff --git a/src/interfaces/python/opengm/opengmcore/pyDataset.hxx b/src/interfaces/python/opengm/opengmcore/pyDataset.hxx
deleted file mode 100644
index 0680758..0000000
--- a/src/interfaces/python/opengm/opengmcore/pyDataset.hxx
+++ /dev/null
@@ -1,4 +0,0 @@
-
-
-template<class GM, class LOSS>
-void export_dataset(const std::string& className);
diff --git a/src/interfaces/python/opengm/opengmcore/pyLoss.hxx b/src/interfaces/python/opengm/opengmcore/pyLoss.hxx
deleted file mode 100644
index 271adeb..0000000
--- a/src/interfaces/python/opengm/opengmcore/pyLoss.hxx
+++ /dev/null
@@ -1,4 +0,0 @@
-
-
-template<class GM>
-void export_loss();

-- 
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