Infer/Optimize a Graphical Model

Belief propagation (Bp)

import opengm
import numpy

from time import time

shape=[20, 20]
nl = 100
unaries=numpy.random.rand(*shape+[nl])
potts=opengm.PottsFunction([nl]*2,0.0,0.4)
gm=opengm.grid2d2Order(unaries=unaries,regularizer=potts)



inf=opengm.inference.BeliefPropagation(gm,parameter=opengm.InfParam(steps=10,damping=0.5,convergenceBound=0.001))
# start inference (in this case unverbose infernce)

t0=time()
inf.infer()
t1=time()

print t1-t0

# get the result states
argmin=inf.arg()
# print the argmin (on the grid)
#print argmin.reshape(*shape)

ICM

import opengm
import numpy
#---------------------------------------------------------------
# MinSum  with ICM
#---------------------------------------------------------------

n=10
nl=10
unaries=numpy.random.rand(n , n, nl)
potts=opengm.PottsFunction([nl,nl],0.0,0.05)
gm=opengm.grid2d2Order(unaries=unaries,regularizer=potts)
#---------------------------------------------------------------
# Minimize
#---------------------------------------------------------------
#get an instance of the optimizer / inference-algorithm


inf=opengm.inference.Icm(gm)
# start inference (in this case verbose infernce)
visitor=inf.verboseVisitor(printNth=10000,multiline=True)
inf.infer(visitor)
# get the result states
argmin=inf.arg()
# print the argmin (on the grid)
print argmin.reshape(n,n)

GraphCut

import opengm
import numpy



unaries=numpy.random.rand(10, 10,2)
potts=opengm.PottsFunction([2,2],0.0,0.4)
gm=opengm.grid2d2Order(unaries=unaries,regularizer=potts)


inf=opengm.inference.GraphCut(gm)
inf.infer()
arg=inf.arg()