[opengm] 31/386: save and load datasets for learning

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:35:01 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 ae960f96002db66fac1ef11da6bca4f94a321fa2
Author: joergkappes <kappes at math.uni-heidelberg.de>
Date:   Thu Nov 20 17:25:06 2014 +0100

    save and load datasets for learning
---
 .../opengm/datastructures/marray/marray_hdf5.hxx   | 25 +++++++
 .../opengm/functions/learnable/sum_of_experts.hxx  |  2 +-
 include/opengm/learning/dataset/dataset.hxx        | 77 ++++++++++++++++++++++
 src/unittest/CMakeLists.txt                        |  1 +
 src/unittest/learning/CMakeLists.txt               | 11 ++++
 src/unittest/learning/test_dataset_io.cxx          | 52 +++++++++++++++
 6 files changed, 167 insertions(+), 1 deletion(-)

diff --git a/include/opengm/datastructures/marray/marray_hdf5.hxx b/include/opengm/datastructures/marray/marray_hdf5.hxx
index 6db10fb..cccf292 100644
--- a/include/opengm/datastructures/marray/marray_hdf5.hxx
+++ b/include/opengm/datastructures/marray/marray_hdf5.hxx
@@ -74,6 +74,8 @@ template<class T>
     void load(const hid_t&, const std::string&, Marray<T>&);
 template<class T>
     void loadShape(const hid_t&, const std::string&, Vector<T>&);
+template<class T>
+    void loadVec(const hid_t&, const std::string&, std::vector<T>&);
 template<class T, class BaseIterator, class ShapeIterator>
     void loadHyperslab(const hid_t&, const std::string&,
         BaseIterator, BaseIterator, ShapeIterator, Marray<T>&);
@@ -511,6 +513,29 @@ void loadShape(
     handleCheck.check();
 }
 
+/// Load a vector of an HDF5 dataset.
+///
+/// \param groupHandle Handle of the parent HDF5 file or group.
+/// \param datasetName Name of the HDF5 dataset.
+/// \param out Shape.
+///
+/// \sa load()
+///
+template<class T>
+void loadVec(
+    const hid_t& groupHandle,
+    const std::string& datasetName,
+    std::vector<T>& out
+)
+{
+    marray::Vector<T> v;
+    loadShape( groupHandle,datasetName,v);
+    out.resize(v.size());
+    for(size_t j=0; j<v.size(); ++j) {
+        out[j] = v[j];
+    }
+}
+
 /// Load a hyperslab from an HDF5 dataset into an Marray.
 /// 
 /// \param groupHandle Handle of the parent HDF5 file or group.
diff --git a/include/opengm/functions/learnable/sum_of_experts.hxx b/include/opengm/functions/learnable/sum_of_experts.hxx
index 9b0805c..5f4e54f 100644
--- a/include/opengm/functions/learnable/sum_of_experts.hxx
+++ b/include/opengm/functions/learnable/sum_of_experts.hxx
@@ -244,7 +244,7 @@ FunctionSerialization<opengm::functions::learnable::SumOfExperts<T, I, L> >::des
       ++indexInIterator;
    }
    //read features
-   std::vector<marray::Marray<T,L> > feat(numW,marray::Marray<T,L>(shape.begin(),shape.end()));
+   std::vector<marray::Marray<T> > feat(numW,marray::Marray<T>(shape.begin(),shape.end()));
    for(size_t i=0; i<numW;++i){   
       for(size_t j=0; j<size;++j){
          feat[i](j)=*valueInIterator;
diff --git a/include/opengm/learning/dataset/dataset.hxx b/include/opengm/learning/dataset/dataset.hxx
new file mode 100644
index 0000000..6d4752f
--- /dev/null
+++ b/include/opengm/learning/dataset/dataset.hxx
@@ -0,0 +1,77 @@
+#pragma once
+#ifndef OPENGM_DATASET_HXX
+#define OPENGM_DATASET_HXX
+
+#include <vector>
+#include <cstdlib>
+
+
+namespace opengm {
+   namespace datasets{
+
+      template<class GM>
+      class Dataset{
+      public:
+         typedef GM                     GMType;
+         typedef typename GM::ValueType ValueType;
+         typedef typename GM::IndexType IndexType;
+         typedef typename GM::LabelType LabelType; 
+         typedef opengm::Parameters<ValueType,IndexType> ModelParameters;
+
+         GM&                           getModel(const size_t i)  { return gms_[i]; }
+         const std::vector<LabelType>& getGT(const size_t i)     { return gt_; }
+         ModelParameters&              getModelParameters()      { return modelParameters_; }
+         size_t                        getNumberOfParameters()   { return 1; }
+         size_t                        getNumberOfModels()       { return gms_.size(); } 
+         
+         Dataset();
+         void load(std::string path,std::string prefix);
+
+      private:
+         std::vector<GM> gms_; 
+         std::vector<std::vector<LabelType> > gt_; 
+         ModelParameters modelParameters_;
+      };
+      
+
+
+      template<class GM>
+      Dataset<GM>::Dataset()
+         :  gms_(std::vector<GM>(0)),
+            gt_(std::vector<std::vector<LabelType> >(0)),
+            modelParameters_(ModelParameters(0))
+      {
+      }; 
+
+      template<class GM>
+      void Dataset<GM>::load(std::string datasetpath,std::string prefix){
+
+         //Load Header 
+         std::stringstream hss;
+         hss << datasetpath << "/"<<prefix<<"info.h5";
+         hid_t file =  marray::hdf5::openFile(hss.str());
+         std::vector<size_t> temp(1);
+         marray::hdf5::loadVec(file, "numberOfParameters", temp);
+         size_t numPara = temp[0];
+         marray::hdf5::loadVec(file, "numberOfModels", temp);
+         size_t numModel = temp[0];
+         marray::hdf5::closeFile(file);
+         
+         gms_.resize(numModel);
+         gt_.resize(numModel);
+         modelParameters_ = ModelParameters(numPara);
+         //Load Models and ground truth
+         for(size_t m=0; m<numModel; ++m){
+            std::stringstream ss;
+            ss  << datasetpath <<"/"<<prefix<<"gm_" << m <<".h5"; 
+            hid_t file =  marray::hdf5::openFile(ss.str()); 
+            marray::hdf5::loadVec(file, "gt", gt_[m]);
+            marray::hdf5::closeFile(file);
+            opengm::hdf5::load(gms_[m],ss.str(),"gm");
+         }
+
+      };
+   }
+} // namespace opengm
+
+#endif 
diff --git a/src/unittest/CMakeLists.txt b/src/unittest/CMakeLists.txt
index 5e4b0dc..070e983 100644
--- a/src/unittest/CMakeLists.txt
+++ b/src/unittest/CMakeLists.txt
@@ -89,4 +89,5 @@ if(BUILD_TESTING)
    endif()
 
    add_subdirectory(inference)
+   add_subdirectory(learning)
 endif()
diff --git a/src/unittest/learning/CMakeLists.txt b/src/unittest/learning/CMakeLists.txt
new file mode 100644
index 0000000..5cb5024
--- /dev/null
+++ b/src/unittest/learning/CMakeLists.txt
@@ -0,0 +1,11 @@
+
+add_definitions(-DOPENGM_DEBUG)
+
+if(BUILD_TESTING)
+   if(WITH_HDF5)
+      add_executable(test-dataset-io test_dataset_io.cxx ${headers})
+      target_link_libraries(test-dataset-io ${HDF5_LIBRARIES})
+      add_test(test-dataset-io ${CMAKE_CURRENT_BINARY_DIR}/test-dataset-io)
+   endif()
+  
+endif()
diff --git a/src/unittest/learning/test_dataset_io.cxx b/src/unittest/learning/test_dataset_io.cxx
new file mode 100644
index 0000000..0a53b44
--- /dev/null
+++ b/src/unittest/learning/test_dataset_io.cxx
@@ -0,0 +1,52 @@
+#include <vector>
+
+#include <opengm/functions/explicit_function.hxx>
+#include <opengm/unittests/test.hxx>
+#include <opengm/graphicalmodel/graphicalmodel.hxx>
+#include <opengm/operations/adder.hxx>
+#include <opengm/operations/minimizer.hxx>
+#include <opengm/inference/icm.hxx>
+#include <opengm/utilities/metaprogramming.hxx>
+
+#include <opengm/functions/learnable/lpotts.hxx>
+#include <opengm/functions/learnable/sum_of_experts.hxx>
+#include <opengm/learning/dataset/testdataset.hxx>
+#include <opengm/learning/dataset/testdataset2.hxx>
+#include <opengm/learning/dataset/dataset_io.hxx>
+#include <opengm/learning/dataset/dataset.hxx>
+
+
+//*************************************
+typedef double ValueType;
+typedef size_t IndexType;
+typedef size_t LabelType; 
+typedef opengm::meta::TypeListGenerator<opengm::ExplicitFunction<ValueType,IndexType,LabelType>, opengm::functions::learnable::LPotts<ValueType,IndexType,LabelType>, opengm::functions::learnable::SumOfExperts<ValueType,IndexType,LabelType> >::type FunctionListType;
+typedef opengm::GraphicalModel<ValueType,opengm::Adder, FunctionListType, opengm::DiscreteSpace<IndexType,LabelType> > GM; 
+typedef opengm::datasets::TestDataset<GM>  DS1;
+typedef opengm::datasets::TestDataset2<GM> DS2;
+typedef opengm::datasets::Dataset<GM>      DS;
+
+//*************************************
+
+
+int main() {
+   std::cout << " Includes are fine :-) " << std::endl; 
+  
+   {
+      DS1 dataset;
+      std::cout << "Dataset includes " << dataset.getNumberOfModels() << " instances and has " << dataset.getNumberOfParameters() << " parameters."<<std::endl; 
+      opengm::save(dataset,"./","dataset1_");
+   }
+  
+   {
+      DS2 dataset;
+      std::cout << "Dataset includes " << dataset.getNumberOfModels() << " instances and has " << dataset.getNumberOfParameters() << " parameters."<<std::endl; 
+      opengm::save(dataset,"./","dataset2_");
+   }
+
+   {
+      DS ds;
+      ds.load("./","dataset2_");
+   }
+
+}

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