[opengm] 140/386: improved lunary to remove number of parameters

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:36:30 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 f0ac6aa8a38ceae67bbd6032480a87df29e6e0bb
Author: DerThorsten <thorsten.beier at iwr.uni-heidelberg.de>
Date:   Thu Dec 18 18:58:56 2014 +0100

    improved lunary to remove number of parameters
---
 fubar/real_example.py                         | 61 +++++++++++++++------------
 include/opengm/functions/learnable/lunary.hxx | 37 ++++++----------
 2 files changed, 48 insertions(+), 50 deletions(-)

diff --git a/fubar/real_example.py b/fubar/real_example.py
index fdb7687..429473a 100644
--- a/fubar/real_example.py
+++ b/fubar/real_example.py
@@ -5,9 +5,9 @@ import vigra
 
 nModels = 1
 nLables = 2 
-shape = [10, 10]
+shape = [6, 6]
 numVar = shape[0]*shape[1]
-nWeights = 9
+nWeights = 4
 
 def makeGt(shape):
     gt=numpy.ones(shape,dtype='uint8')
@@ -19,12 +19,12 @@ def makeGt(shape):
 weightVals = numpy.ones(nWeights)
 weights = opengm.learning.Weights(weightVals)
 
-uWeightIds = numpy.arange(6,dtype='uint64').reshape(2,3)
+uWeightIds = numpy.arange(4,dtype='uint64').reshape(2,2)
 
 
 print uWeightIds
 
-bWeightIds = numpy.array([6,7,8],dtype='uint64')
+bWeightIds = numpy.array([4,5,6],dtype='uint64')
 
 dataset = learning.createDataset(loss='h')
 dataset.setWeights(weights)
@@ -34,11 +34,11 @@ def makeFeatures(gt):
     random  = numpy.random.rand(*gt.shape)-0.5
     randGt = random + gt
     feat = []
-    for sigma in [1.0, 2.0]:
+    for sigma in [1.0, 1.5]:
         feat.append(vigra.filters.gaussianSmoothing(randGt.astype('float32'),sigma) )
 
     featB = []
-    for sigma in [1.0, 2.0]:
+    for sigma in [1.0, 1.5]:
         featB.append(vigra.filters.gaussianGradientMagnitude(randGt.astype('float32'),sigma) )
 
 
@@ -61,12 +61,15 @@ for mi in range(nModels):
 
     for x in range(shape[0]):
         for y in range(shape[1]):
-            uFeat = numpy.append(unaries[x,y,:], [1]).astype(opengm.value_type)
+            #uFeat = numpy.append(unaries[x,y,:], [1]).astype(opengm.value_type)
+            uFeat = unaries[x,y,:].astype("float64")
             uFeat = numpy.repeat(uFeat[:,numpy.newaxis],2,axis=1).T
-            print(unaries[x,y,:])
-            print(unaries.shape)
-            print(uFeat)
-            print(uFeat.shape)
+            uFeat[1,:]=1
+            #sys.exit()
+            #print(unaries[x,y,:])
+            #print(unaries.shape)
+            #print(uFeat)
+            #print(uFeat.shape)
 
             lu = opengm.LUnaryFunction(weights=weights,numberOfLabels=nLables, features=uFeat, weightIds=uWeightIds)
 
@@ -74,30 +77,34 @@ for mi in range(nModels):
             fid= gm.addFunction(lu)
             gm.addFactor(fid, y+x*shape[1])
 
-    for x in range(shape[0]):
-        for y in range(shape[1]):
-
-            if x+1 < shape[0]:
-                bFeat = numpy.append(binaries[x,y,:], [1]).astype(opengm.value_type) +  numpy.append(binaries[x+1,y,:], [1]).astype(opengm.value_type)
-                pf = opengm.LPottsFunction(weights=weights,numberOfLabels=nLables, features=bFeat, weightIds=bWeightIds)
-                fid= gm.addFunction(pf)
-                gm.addFactor(fid, [y+x*shape[1], y+(x+1)*shape[1]])
-            if y+1 < shape[1]:
-                bFeat = numpy.append(binaries[x,y,:], [1]).astype(opengm.value_type) + numpy.append(binaries[x,y+1,:], [1]).astype(opengm.value_type)
-                pf = opengm.LPottsFunction(weights=weights,numberOfLabels=nLables, features=bFeat, weightIds=bWeightIds)
-                fid= gm.addFunction(pf)
-                gm.addFactor(fid, [y+x*shape[1], y+1+x*shape[1]])
+    if False:
+        for x in range(shape[0]):
+            for y in range(shape[1]):
+
+                if x+1 < shape[0]:
+                    bFeat = numpy.append(binaries[x,y,:], [1]).astype(opengm.value_type) +  numpy.append(binaries[x+1,y,:], [1]).astype(opengm.value_type)
+                    pf = opengm.LPottsFunction(weights=weights,numberOfLabels=nLables, features=bFeat, weightIds=bWeightIds)
+                    fid= gm.addFunction(pf)
+                    gm.addFactor(fid, [y+x*shape[1], y+(x+1)*shape[1]])
+                if y+1 < shape[1]:
+                    bFeat = numpy.append(binaries[x,y,:], [1]).astype(opengm.value_type) + numpy.append(binaries[x,y+1,:], [1]).astype(opengm.value_type)
+                    pf = opengm.LPottsFunction(weights=weights,numberOfLabels=nLables, features=bFeat, weightIds=bWeightIds)
+                    fid= gm.addFunction(pf)
+                    gm.addFactor(fid, [y+x*shape[1], y+1+x*shape[1]])
 
     dataset.pushBackInstance(gm,gtFlat.astype(opengm.label_type))
+    backGt = dataset.getGT(0)
 
+    #print "back",backGt
+    #sys.exit()
 
 # for grid search learner
 lowerBounds = numpy.ones(nWeights)*-1.0
 upperBounds = numpy.ones(nWeights)*1.0
-nTestPoints  =numpy.ones(nWeights).astype('uint64')*5
+nTestPoints  =numpy.ones(nWeights).astype('uint64')*10
 
-# learner = learning.gridSearchLearner(dataset=dataset,lowerBounds=lowerBounds, upperBounds=upperBounds,nTestPoints=nTestPoints)
-learner = learning.structMaxMarginLearner(dataset, 1.0, 0.001, 0)
+learner = learning.gridSearchLearner(dataset=dataset,lowerBounds=lowerBounds, upperBounds=upperBounds,nTestPoints=nTestPoints)
+#learner = learning.structMaxMarginLearner(dataset, 1.0, 0.001, 0)
 
 learner.learn(infCls=opengm.inference.Icm, 
               parameter=opengm.InfParam())
diff --git a/include/opengm/functions/learnable/lunary.hxx b/include/opengm/functions/learnable/lunary.hxx
index d541236..22c120e 100644
--- a/include/opengm/functions/learnable/lunary.hxx
+++ b/include/opengm/functions/learnable/lunary.hxx
@@ -18,17 +18,6 @@ namespace learnable {
 
 
 
-/// Learnable feature function for two variables
-///
-/// f(u,v) = (\sum_i w_i * feat_i) I(u!=v)
-///  - w    = parameter vector
-///  - feat = feature vector
-///
-/// derive from this class and implement the function
-///   paramaterGradient(i,x)= A(x)_{i,*}*feat
-///  
-/// \ingroup functions
-
 
 template<class V, class I>
 struct FeaturesAndIndices{
@@ -149,9 +138,14 @@ LUnary<T, I, L>::weightGradient
     const L l = *begin;
 
     if(l == size()-1){
-        size_t start = labelOffset_[l];
-        if(weightNumber>=start){
-            return features_[weightNumber];
+        if(labelOffset_[l]>labelOffset_[l-1]){
+            size_t start = labelOffset_[l];
+            if(weightNumber>=start){
+                return features_[weightNumber];
+            }
+        }
+        else{
+            return V(0);
         }
     }
     else{
@@ -172,16 +166,13 @@ LUnary<T, I, L>::operator()
 (
    ITERATOR begin
 ) const {
-   T val = 0;
-   size_t end = (*begin == size()-1 ? numberOfWeights() : labelOffset_[*begin+1] );
-
-   //std::cout<<"label "<<*begin<<"\n";
-   //std::cout<<"s e "<<labelOffset_[*begin]<<" "<<end<<"\n";
-   for(size_t i=labelOffset_[*begin];i<end;++i){
-        //std::cout<<"    i="<<i<<" wi="<<weightIds_[i]<<" w="<< weights_->getWeight(weightIds_[i])  <<" f="<<features_[i]<<"\n";
+    T val = 0;
+    const size_t oBegin = labelOffset_[*begin];
+    const size_t oEnd = (*begin == size()-1 ? numberOfWeights() : labelOffset_[*begin+1] );
+    for(size_t i=oBegin;i<oEnd;++i){
         val += weights_->getWeight(weightIds_[i]) * features_[i];
-   }
-   return val;
+    }
+    return val;
 }
 
 

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