[opengm] 77/386: add editable dataset and its python wrappers

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 2b878be21ac20d58563ca8092b5be108affae7a0
Author: mschiegg <martin.schiegg at iwr.uni-heidelberg.de>
Date:   Tue Dec 16 18:19:24 2014 +0100

    add editable dataset and its python wrappers
---
 .../opengm/learning/dataset/editabledataset.hxx    | 75 ++++++++++++++++++++++
 .../python/opengm/opengmcore/CMakeLists.txt        |  4 ++
 .../python/opengm/opengmcore/opengmcore.cpp        | 11 +++-
 .../python/opengm/opengmcore/pyDataset.cxx         | 57 ++++++++++++++++
 .../python/opengm/opengmcore/pyDataset.hxx         |  4 ++
 src/interfaces/python/opengm/opengmcore/pyLoss.cxx | 38 +++++++++++
 src/interfaces/python/opengm/opengmcore/pyLoss.hxx |  4 ++
 7 files changed, 192 insertions(+), 1 deletion(-)

diff --git a/include/opengm/learning/dataset/editabledataset.hxx b/include/opengm/learning/dataset/editabledataset.hxx
new file mode 100644
index 0000000..2d1130a
--- /dev/null
+++ b/include/opengm/learning/dataset/editabledataset.hxx
@@ -0,0 +1,75 @@
+#pragma once
+#ifndef OPENGM_EDITABLEDATASET_HXX
+#define OPENGM_EDITABLEDATASET_HXX
+
+#include <vector>
+#include <cstdlib>
+
+#include <opengm/learning/dataset/dataset.hxx>
+#include <opengm/graphicalmodel/graphicalmodel_hdf5.hxx>
+#include "../../graphicalmodel/weights.hxx"
+#include "../loss/noloss.hxx"
+
+namespace opengm {
+   namespace datasets{
+
+     template<class GM, class LOSS>
+      class EditableDataset : public Dataset<GM, LOSS>{
+      public:
+         typedef GM                     GMType;
+         typedef GM                     GMWITHLOSS;
+         typedef LOSS                   LossType;
+         typedef typename GM::ValueType ValueType;
+         typedef typename GM::IndexType IndexType;
+         typedef typename GM::LabelType LabelType;
+         typedef opengm::learning::Weights<ValueType> Weights;
+         typedef std::vector<LabelType> GTVector;
+
+         EditableDataset(size_t numInstances=0) : Dataset<GM, LOSS>(numInstances) {}
+         EditableDataset(std::vector<GM>& gms, std::vector<GTVector >& gts);
+
+         void setInstance(const size_t i, GM& gm, GTVector& gt);
+         void pushBackInstance(GM& gm, GTVector& gt);
+         void setWeights(Weights& w);
+      };
+
+    template<class GM, class LOSS>
+    EditableDataset<GM, LOSS>::EditableDataset(std::vector<GM>& gms,
+                                                    std::vector<GTVector >& gts)
+        : Dataset<GM, LOSS>(gms.size())
+    {
+        for(size_t i=0; i<gms.size(); ++i){
+            setInstance(i, gms[i], gts[i]);
+            this->buildModelWithLoss(i);
+        }
+    }
+
+    template<class GM, class LOSS>
+    void EditableDataset<GM, LOSS>::setInstance(const size_t i, GM& gm, GTVector& gt) {
+        OPENGM_CHECK_OP(i, <, this->gms_.size(),"");
+        OPENGM_CHECK_OP(i, <, this->gts_.size(),"");
+        OPENGM_CHECK_OP(i, <, this->gmsWithLoss_.size(),"");
+        this->gms_[i] = gm;
+        this->gts_[i] = gt;
+        this->buildModelWithLoss(i);
+    }
+
+    template<class GM, class LOSS>
+    void EditableDataset<GM, LOSS>::pushBackInstance(GM& gm, GTVector& gt) {
+        this->gms_.push_back(gm);
+        this->gts_.push_back(gt);
+        this->gmsWithLoss_.resize(this->gts_.size());
+        this->buildModelWithLoss(this->gts_.size()-1);
+        OPENGM_CHECK_OP(this->gms_.size(), ==, this->gts_.size(),"");
+        OPENGM_CHECK_OP(this->gms_.size(), ==, this->gmsWithLoss_.size(),"");
+    }
+
+    template<class GM, class LOSS>
+    void EditableDataset<GM, LOSS>::setWeights(Weights& w) {
+        this->weights_ = w;
+    }
+
+   } // namespace datasets
+} // namespace opengm
+
+#endif 
diff --git a/src/interfaces/python/opengm/opengmcore/CMakeLists.txt b/src/interfaces/python/opengm/opengmcore/CMakeLists.txt
index e70955e..0e791a0 100644
--- a/src/interfaces/python/opengm/opengmcore/CMakeLists.txt
+++ b/src/interfaces/python/opengm/opengmcore/CMakeLists.txt
@@ -46,6 +46,8 @@ if(APPLE)
     pySpace.cxx
     pyVector.cxx
     pyEnum.cxx
+    pyDataset.cxx
+    pyLoss.cxx
     )
 else()
     add_library(_opengmcore SHARED  
@@ -62,6 +64,8 @@ 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 6847aae..499e6a3 100644
--- a/src/interfaces/python/opengm/opengmcore/opengmcore.cpp
+++ b/src/interfaces/python/opengm/opengmcore/opengmcore.cpp
@@ -14,6 +14,10 @@
 #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>
 #include <opengm/python/numpyview.hxx>
@@ -30,6 +34,8 @@
 #include "pyFunctionGen.hxx"
 #include "pySpace.hxx"
 #include "pyVector.hxx"
+#include "pyLoss.hxx"
+#include "pyDataset.hxx"
 
 
 #include "opengm/functions/explicit_function.hxx"
@@ -685,7 +691,6 @@ BOOST_PYTHON_MODULE_INIT(_opengmcore) {
    }
 
 
-
    //export_rag();
    export_config();
    export_vectors<opengm::python::GmIndexType>();
@@ -714,6 +719,10 @@ 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.cxx b/src/interfaces/python/opengm/opengmcore/pyDataset.cxx
new file mode 100644
index 0000000..e65097a
--- /dev/null
+++ b/src/interfaces/python/opengm/opengmcore/pyDataset.cxx
@@ -0,0 +1,57 @@
+#include <boost/python.hpp>
+#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
+#include <stdexcept>
+#include <stddef.h>
+
+#include <opengm/learning/dataset/editabledataset.hxx>
+#include <opengm/learning/loss/hammingloss.hxx>
+#include <opengm/learning/loss/generalized-hammingloss.hxx>
+#include <opengm/learning/loss/noloss.hxx>
+#include <opengm/python/numpyview.hxx>
+#include <opengm/python/opengmpython.hxx>
+#include <opengm/python/converter.hxx>
+
+using namespace boost::python;
+
+template<class GM, class LOSS>
+void pySetInstance(opengm::datasets::EditableDataset<GM, LOSS>& ds,
+                   const size_t i,
+                   GM& gm,
+                   const opengm::python::NumpyView<typename GM::LabelType,1>& gt) {
+    std::vector<typename GM::LabelType> gt_vector(gt.begin(), gt.end());
+    ds.setInstance(i, gm, gt_vector);
+}
+
+template<class GM, class LOSS>
+void pyPushBackInstance(opengm::datasets::EditableDataset<GM,LOSS>& ds,
+                        GM& gm,
+                        const opengm::python::NumpyView<typename GM::LabelType,1>& gt) {
+    std::vector<typename GM::LabelType> gt_vector(gt.begin(), gt.end());
+    ds.pushBackInstance(gm, gt_vector);
+}
+
+template<class GM, class LOSS>
+void export_dataset(const std::string& className){
+    typedef opengm::datasets::EditableDataset<GM,LOSS > PyDataset;
+
+   class_<PyDataset > (className.c_str(), init<size_t> ())
+           .def("lockModel", &PyDataset::lockModel)
+           .def("unlockModel", &PyDataset::unlockModel)
+           .def("getModel", &PyDataset::getModel, return_internal_reference<>())
+           .def("getModelWithLoss", &PyDataset::getModelWithLoss, return_internal_reference<>())
+           .def("getGT", &PyDataset::getGT, return_internal_reference<>())
+           .def("getWeights", &PyDataset::getWeights, return_internal_reference<>())
+           .def("getNumberOfWeights", &PyDataset::getNumberOfWeights)
+           .def("getNumberOfModels", &PyDataset::getNumberOfModels)
+           .def("setInstance", &pySetInstance<GM,LOSS>)
+           .def("pushBackInstance", &pyPushBackInstance<GM,LOSS>)
+           .def("setWeights", &PyDataset::setWeights)
+   ;
+
+}
+
+
+template void export_dataset<opengm::python::GmAdder, opengm::learning::HammingLoss> (const std::string& className);
+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/opengmcore/pyDataset.hxx b/src/interfaces/python/opengm/opengmcore/pyDataset.hxx
new file mode 100644
index 0000000..0680758
--- /dev/null
+++ b/src/interfaces/python/opengm/opengmcore/pyDataset.hxx
@@ -0,0 +1,4 @@
+
+
+template<class GM, class LOSS>
+void export_dataset(const std::string& className);
diff --git a/src/interfaces/python/opengm/opengmcore/pyLoss.cxx b/src/interfaces/python/opengm/opengmcore/pyLoss.cxx
new file mode 100644
index 0000000..0ac6ad5
--- /dev/null
+++ b/src/interfaces/python/opengm/opengmcore/pyLoss.cxx
@@ -0,0 +1,38 @@
+#include <boost/python.hpp>
+#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
+#include <stdexcept>
+#include <stddef.h>
+
+#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>
+
+using namespace boost::python;
+
+template <class GM>
+void export_loss(){
+   typedef typename std::vector<typename GM::LabelType>::const_iterator Literator;
+   typedef typename std::vector<typename GM::LabelType>::const_iterator Niterator;
+   typedef opengm::learning::HammingLoss PyHammingLoss;
+   typedef opengm::learning::NoLoss PyNoLoss;
+   typedef opengm::learning::GeneralizedHammingLoss PyGeneralizedHammingLoss;
+
+   class_<PyHammingLoss >("HammingLoss")
+           .def("loss", &PyHammingLoss::loss<Literator,Literator>)
+           .def("addLoss", &PyHammingLoss::addLoss<GM, Literator>)
+   ;
+
+   class_<PyNoLoss >("NoLoss")
+           .def("loss", &PyNoLoss::loss<Literator,Literator>)
+           .def("addLoss", &PyNoLoss::addLoss<GM, Literator>)
+   ;
+
+   class_<PyGeneralizedHammingLoss >("GeneralizedHammingLoss", init<Niterator,Niterator,Literator,Literator>())
+           .def("loss", &PyGeneralizedHammingLoss::loss<Literator,Literator>)
+           .def("addLoss", &PyGeneralizedHammingLoss::addLoss<GM, Literator>)
+   ;
+}
+
+template void export_loss<opengm::python::GmAdder>();
diff --git a/src/interfaces/python/opengm/opengmcore/pyLoss.hxx b/src/interfaces/python/opengm/opengmcore/pyLoss.hxx
new file mode 100644
index 0000000..271adeb
--- /dev/null
+++ b/src/interfaces/python/opengm/opengmcore/pyLoss.hxx
@@ -0,0 +1,4 @@
+
+
+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