[opengm] 43/386: start refactoring dataset

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:35:03 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 aec27e069be88fbd5e7d67afaacbe20e4fff9ba1
Author: Joerg Kappes <jkappes at goedel.(none)>
Date:   Mon Dec 15 15:11:02 2014 +0100

    start refactoring dataset
---
 include/opengm/learning/dataset/dataset.hxx    | 53 ++++++++++++++++++--------
 include/opengm/learning/dataset/dataset_io.hxx |  6 +--
 include/opengm/learning/loss/noloss.hxx        | 33 ++++++++++++++++
 src/unittest/learning/test_dataset_io.cxx      |  8 ++--
 4 files changed, 79 insertions(+), 21 deletions(-)

diff --git a/include/opengm/learning/dataset/dataset.hxx b/include/opengm/learning/dataset/dataset.hxx
index e1b45a2..3a155fc 100644
--- a/include/opengm/learning/dataset/dataset.hxx
+++ b/include/opengm/learning/dataset/dataset.hxx
@@ -6,53 +6,75 @@
 #include <cstdlib>
 
 #include "../../graphicalmodel/weights.hxx"
+#include "../loss/noloss.hxx"
 
 namespace opengm {
    namespace datasets{
 
-      template<class GM>
+     template<class GM, class LOSS=opengm::learning::NoLoss>
       class Dataset{
       public:
          typedef GM                     GMType;
+         typedef GM                     GMWITHLOSS;
          typedef typename GM::ValueType ValueType;
          typedef typename GM::IndexType IndexType;
          typedef typename GM::LabelType LabelType; 
          typedef opengm::learning::Weights<ValueType> Weights;
 
-         GM&                           getModel(const size_t i)  { return gms_[i]; }
-         const std::vector<LabelType>& getGT(const size_t i)     { return gt_; }
-         Weights&                      getWeights()              { return weights_; }
-         size_t                        getNumberOfWeights()      { return 1; }
-         size_t                        getNumberOfModels()       { return gms_.size(); } 
+ 	 bool                          lockModel(const size_t i)         { ++count_[i]; }
+ 	 bool                          unlockModel(const size_t i)       { OPENGM_ASSERT(count_[i]>0); --count_[i]; }
+         const GM&                     getModel(const size_t i)          { return gms_[i]; } 
+         const GMWITHLOSS&             getModelWithLoss(const size_t i)  { return gmsWithLoss_[i]; }
+         const std::vector<LabelType>& getGT(const size_t i)             { return gt_; }
+         Weights&                      getWeights()                      { return weights_; } 
+         size_t                        getNumberOfWeights()              { return weights_.numberOfWeights(); }
+         size_t                        getNumberOfModels()               { return gms_.size(); } 
          
          Dataset();
-         void load(std::string path,std::string prefix);
+         void loadAll(std::string path,std::string prefix);
 
-      private:
+      private:	
+	 std::vector<size_t> count_;
+	 std::vector<bool> isCached_;
          std::vector<GM> gms_; 
+	 std::vector<GMWITHLOSS> gmsWithLoss_; 
          std::vector<std::vector<LabelType> > gt_; 
          Weights weights_;
+
+   	 void buildModelWithLoss(size_t i);
       };
       
 
 
-      template<class GM>
-      Dataset<GM>::Dataset()
-         :  gms_(std::vector<GM>(0)),
+      template<class GM, class LOSS>
+      Dataset<GM, LOSS>::Dataset()
+         :  count_(std::vector<size_t>(0)),
+	    isCached_(std::vector<bool>(0)),
+            gms_(std::vector<GM>(0)),
+	    gmsWithLoss_(std::vector<GMWITHLOSS>(0)),
             gt_(std::vector<std::vector<LabelType> >(0)),
             weights_(Weights(0))
       {
       }; 
 
-      template<class GM>
-      void Dataset<GM>::load(std::string datasetpath,std::string prefix){
+
+     template<class GM, class LOSS>
+     void Dataset<GM, LOSS>::buildModelWithLoss(size_t i){
+	gmsWithLoss_[i] = gms_[i];
+	LOSS loss;
+	loss.addLoss(gmsWithLoss_[i], gt_[i].begin());
+      }
+
+
+     template<class GM, class LOSS>
+     void Dataset<GM, LOSS>::loadAll(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);
+         marray::hdf5::loadVec(file, "numberOfWeights", temp);
          size_t numWeights = temp[0];
          marray::hdf5::loadVec(file, "numberOfModels", temp);
          size_t numModel = temp[0];
@@ -68,7 +90,8 @@ namespace opengm {
             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");
+            opengm::hdf5::load(gms_[m],ss.str(),"gm"); 
+	    // buildModelWithLoss(m);
          }
 
       };
diff --git a/include/opengm/learning/dataset/dataset_io.hxx b/include/opengm/learning/dataset/dataset_io.hxx
index e35cc60..8445d62 100644
--- a/include/opengm/learning/dataset/dataset_io.hxx
+++ b/include/opengm/learning/dataset/dataset_io.hxx
@@ -5,7 +5,7 @@
 #include <vector>
 #include <cstdlib>
 #include <opengm/graphicalmodel/graphicalmodel_hdf5.hxx>
-#include "H5Cpp.h"
+//#include <H5Cpp.h>
 
 namespace opengm{
    template<class DATASET>
@@ -14,13 +14,13 @@ namespace opengm{
       typedef typename DATASET::GMType   GMType;
       typedef typename GMType::LabelType LabelType;
      
-      std::vector<size_t> numPara(1,dataset.getNumberOfParameters());
+      std::vector<size_t> numWeights(1,dataset.getNumberOfWeights());
       std::vector<size_t> numModels(1,dataset.getNumberOfModels());
   
       std::stringstream hss;
       hss << datasetpath << "/"<<prefix<<"info.h5";
       hid_t file = marray::hdf5::createFile(hss.str(), marray::hdf5::DEFAULT_HDF5_VERSION);
-      marray::hdf5::save(file,"numberOfParameters",numPara);
+      marray::hdf5::save(file,"numberOfWeights",numWeights);
       marray::hdf5::save(file,"numberOfModels",numModels);
       marray::hdf5::closeFile(file); 
 
diff --git a/include/opengm/learning/loss/noloss.hxx b/include/opengm/learning/loss/noloss.hxx
new file mode 100644
index 0000000..19fdd40
--- /dev/null
+++ b/include/opengm/learning/loss/noloss.hxx
@@ -0,0 +1,33 @@
+#pragma once
+#ifndef OPENGM_NO_LOSS_HXX
+#define OPENGM_NO_LOSS_HXX
+
+#include "opengm/functions/explicit_function.hxx"
+namespace opengm {
+   namespace learning {
+      class NoLoss{
+      public:
+
+         template<class IT1, class IT2>
+         double loss(IT1 labelBegin, IT1 labelEnd, IT2 GTBegin,IT2 GTEnd) const;
+  
+         template<class GM, class IT>
+         void addLoss(GM& gm, IT GTBegin) const;
+      private:
+      };
+
+      template<class IT1, class IT2>
+      double NoLoss::loss(IT1 labelBegin, const IT1 labelEnd, IT2 GTBegin, const IT2 GTEnd) const
+      {
+         double loss = 0.0;
+         return loss;
+      }
+
+      template<class GM, class IT>
+      void NoLoss::addLoss(GM& gm, IT gt) const
+      {
+      }
+   }  
+} // namespace opengm
+
+#endif 
diff --git a/src/unittest/learning/test_dataset_io.cxx b/src/unittest/learning/test_dataset_io.cxx
index 0a53b44..6a1c8f4 100644
--- a/src/unittest/learning/test_dataset_io.cxx
+++ b/src/unittest/learning/test_dataset_io.cxx
@@ -34,19 +34,21 @@ int main() {
   
    {
       DS1 dataset;
-      std::cout << "Dataset includes " << dataset.getNumberOfModels() << " instances and has " << dataset.getNumberOfParameters() << " parameters."<<std::endl; 
+      std::cout << "Dataset includes " << dataset.getNumberOfModels() << " instances and has " << dataset.getNumberOfWeights() << " parameters."<<std::endl; 
       opengm::save(dataset,"./","dataset1_");
+      std::cout <<"done!" <<std::endl;
    }
   
    {
       DS2 dataset;
-      std::cout << "Dataset includes " << dataset.getNumberOfModels() << " instances and has " << dataset.getNumberOfParameters() << " parameters."<<std::endl; 
+      std::cout << "Dataset includes " << dataset.getNumberOfModels() << " instances and has " << dataset.getNumberOfWeights() << " parameters."<<std::endl; 
       opengm::save(dataset,"./","dataset2_");
+      std::cout <<"done!" <<std::endl;
    }
 
    {
       DS ds;
-      ds.load("./","dataset2_");
+      ds.loadAll("./","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