Potts Model Grid

load modules

In [8]:
import opengm
import vigra
import numpy
import time
import sys
load data

In [2]:

thresholding naive

In [3]:
threshold = 0.24
labelsNaive = img > threshold
vigra.imshow(labelsNaive)
vigra.show()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-eb9b65b0273e> in <module>()
      1 threshold = 0.24
----> 2 labelsNaive = img > threshold
      3 vigra.imshow(labelsNaive)
      4 vigra.show()

NameError: name 'img' is not defined
thresholding with potts model

In [4]:
nVar = img.size
    nLabelsPerVar = 2
    variableSpace = numpy.ones(nVar)*nLabelsPerVar
    gm = opengm.gm(variableSpace)
    
    
    t0 = time.time()
    # add unaries
    for y in range(img.shape[1]):
        for x in range(img.shape[0]):
            
            energy0 = img[x,y] -threshold
            energy1 = threshold - img[x,y]
            unaryFunction = numpy.array([energy0,energy1])
            
            # add unary function to graphical model
            functionId = gm.addFunction(unaryFunction)
            
            # add unary factor to graphical model
            variableIndex = y +x*img.shape[1]
            
            gm.addFactor(functionId,variableIndex)
            
    # add 2. order regularizer
    # ``Potts``- regularizer
    beta = 0.1
    pottsFunction = numpy.zeros([2,2])
    pottsFunction[0,1]=beta
    pottsFunction[1,0]=beta
    
    # add 2. order function to graphical model
    # but only ONCE
    pottsFunctionId = gm.addFunction(pottsFunction)
    
    
    for y in range(img.shape[1]):
        for x in range(img.shape[0]):
            
            # add "horizontal" second order factor
            if x+1 < img.shape[0]:
                variableIndex0 = y + x*img.shape[1]
                variableIndex1 = y + (x+1)*img.shape[1]
                
                gm.addFactor(pottsFunctionId,[variableIndex0,variableIndex1])
            
            # add "vertical"  facator
            if y+1 < img.shape[1]:
                variableIndex0 = y + x*img.shape[1]
                variableIndex1 = (y+1) + x*img.shape[1]
                # add "horizontal" second order factor
                gm.addFactor(pottsFunctionId,[variableIndex0,variableIndex1])
        
    t1 = time.time()
    print "build model in", t1-t0, "sek"


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-7a7d4eff72b1> in <module>()
----> 1 nVar = img.size
      2 nLabelsPerVar = 2
      3 variableSpace = numpy.ones(nVar)*nLabelsPerVar
      4 gm = opengm.gm(variableSpace)
      5 

NameError: name 'img' is not defined
Alternative

In [5]:
gm=None

t0=time.time()
beta = 0.1
unaries=numpy.ones(img.shape+(2,))
unaries[:,:,0] = img-threshold
unaries[:,:,1] = -img+threshold
potts=opengm.PottsFunction([2,2],0.0,beta)
gm=opengm.grid2d2Order(unaries=unaries,regularizer=potts)

t1=time.time()
print "build model in", t1-t0, "sek"


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-f6ca573e9169> in <module>()
      3 t0=time.time()
      4 beta = 0.1
----> 5 unaries=numpy.ones(img.shape+(2,))
      6 unaries[:,:,0] = img-threshold
      7 unaries[:,:,1] = -img+threshold

NameError: name 'img' is not defined
Inference

In [6]:
graphCut = opengm.inference.GraphCut(gm=gm)
graphCut.infer()
labels = graphCut.arg()
labels = labels.reshape(img.shape)


vigra.imshow(vigra.taggedView(labels,'xy'))
vigra.show()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-5325763b1b97> in <module>()
----> 1 graphCut = opengm.inference.GraphCut(gm=gm)
      2 graphCut.infer()
      3 labels = graphCut.arg()
      4 labels = labels.reshape(img.shape)
      5 

/usr/lib/python2.7/dist-packages/opengm/_inference_interface_generator.py in inference_init(self, gm, accumulator, parameter)
    154         # set up basic properties
    155         self.gm = gm
--> 156         self.operator = gm.operator
    157         if accumulator is None:
    158             self.accumulator = defaultAccumulator(gm)

AttributeError: 'NoneType' object has no attribute 'operator'