Example #1
0
def draw_gm(gm, layout='sfdp', size=1.5):
  import opengm as ogm
  import networkx
  from networkx.drawing.nx_agraph import graphviz_layout
  networkx.graphviz_layout = graphviz_layout

  ogm.visualizeGm(gm, layout=layout, relNodeSize=size)
Example #2
0
def viz_factor_graph(gm):
    """
    ut.qtensure()
    gm = build_factor_graph(G, nodes, edges , n_annots, n_names, lookup_annot_idx,
                            use_unaries=True, edge_probs=None, operator='multiplier')
    """
    ut.qtensure()
    import networkx
    from networkx.drawing.nx_agraph import graphviz_layout
    networkx.graphviz_layout = graphviz_layout
    opengm.visualizeGm(gm, show=False, layout="neato", plotUnaries=True,
                        iterations=1000, plotFunctions=True,
                        plotNonShared=False, relNodeSize=1.0)

    _ = pt.show_nx(gm.G)  # NOQA
Example #3
0
f1=numpy.ones([2])
f2=numpy.ones([2,2])

"""
Grid:
    - 4x4=16 variables
    - second order factors in 4-neigbourhood
      all connected to the same function
    - higher order functions are shared
"""

size=3
gm=opengm.gm([2]*size*size)

fid=gm.addFunction(f2)
for y in range(size):   
    for x in range(size):
        gm.addFactor(gm.addFunction(f1),x*size+y)
        if(x+1<size):
            gm.addFactor(fid,[x*size+y,(x+1)*size+y])
        if(y+1<size):
            gm.addFactor(fid,[x*size+y,x*size+(y+1)])


opengm.visualizeGm( gm,layout='spring',iterations=3000,
                    show=False,plotFunctions=True,
                    plotNonShared=True,relNodeSize=0.4)
plt.savefig("grid.png",bbox_inches='tight',dpi=300) 
plt.close()
Example #4
0
f1=numpy.ones([2])
f2=numpy.ones([2,2])

"""
Grid:
    - 4x4=16 variables
    - second order factors in 4-neigbourhood
      all connected to the same function
    - higher order functions are shared
"""

size=3
gm=opengm.gm([2]*size*size)

fid=gm.addFunction(f2)
for y in range(size):   
    for x in range(size):
        gm.addFactor(gm.addFunction(f1),x*size+y)
        if(x+1<size):
            gm.addFactor(fid,[x*size+y,(x+1)*size+y])
        if(y+1<size):
            gm.addFactor(fid,[x*size+y,x*size+(y+1)])


opengm.visualizeGm( gm,layout='spring',iterations=3000,
                    show=True,plotFunctions=True,
                    plotNonShared=True,relNodeSize=0.4)
plt.show
plt.savefig("grid.png",bbox_inches='tight',dpi=300) 
plt.close()
import numpy
import opengm
import matplotlib.pyplot as plt

f1 = numpy.ones([2])
f2 = numpy.ones([2, 2])
f3 = numpy.ones([2, 2, 2])
"""
Triangle (non-shared) :
    - 3 variables
    - 3 unaries
    - 2 second order functions
    - 1 third order factor
    - functions are *non* - shared
"""
gm = opengm.gm([2, 2, 2])
gm.addFactor(gm.addFunction(f1), [0])
gm.addFactor(gm.addFunction(f1), [1])
gm.addFactor(gm.addFunction(f1), [2])
gm.addFactor(gm.addFunction(f2), [0, 1])
gm.addFactor(gm.addFunction(f2), [1, 2])
gm.addFactor(gm.addFunction(f2), [0, 2])
gm.addFactor(gm.addFunction(f3), [0, 1, 2])

opengm.visualizeGm(gm, show=False, plotFunctions=True, plotNonShared=True)
plt.savefig("triangle.png", bbox_inches='tight', dpi=300)
plt.close()
    n_pixel_labels = 2
    n_segment_labels = 2
    pixels = np.random.random((shape + (n_pixel_labels, )))

    segment_map = np.arange(n_pixels).reshape(shape)
    segment_values = np.random.random(
        (np.max(segment_map) + 1, n_segment_labels))

    t0 = time.time()
    gm = pixel_lattice_graph(
        pixels, opengm.pottsFunction([n_pixel_labels, n_pixel_labels], 0.0,
                                     0.5))
    t1 = time.time()

    opengm.visualizeGm(gm)
    print "graph build in", t1 - t0, "seconds"

    # test inference is possible
    inference = opengm.inference.GraphCut(gm=gm)
    t0 = time.time()
    inference.infer()
    t1 = time.time()

    print "inference completed in", t1 - t0, "seconds"

    t0 = time.time()
    gm = segment_adjacency_graph(segment_values,
                                 segment_map,
                                 segment_regularizer=opengm.pottsFunction(
                                     [n_segment_labels, n_segment_labels], 0.0,
Example #7
0
def dummy_cut_example():
    r"""
    CommandLine:
        python -m ibeis.workflow --exec-dummy_cut_example --show

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.workflow import *  # NOQA
        >>> result = dummy_cut_example()
        >>> print(result)
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> ut.show_if_requested()
    """
    import opengm
    import numpy as np
    import plottool as pt
    pt.ensure_pylab_qt4()
    # Matching Graph
    cost_matrix = np.array([
        [0.5, 0.6, 0.2, 0.4],
        [0.0, 0.5, 0.2, 0.9],
        [0.0, 0.0, 0.5, 0.1],
        [0.0, 0.0, 0.0, 0.5],
    ])
    cost_matrix += cost_matrix.T
    number_of_labels = 4
    num_annots = 4
    #cost_matrix = (cost_matrix * 2) - 1

    #gm = opengm.gm(number_of_labels)
    gm = opengm.gm(np.ones(num_annots) * number_of_labels)
    aids = np.arange(num_annots)
    aid_pairs = np.array([(a1, a2) for a1, a2 in ut.iprod(
        aids, aids) if a1 != a2], dtype=np.uint32)
    aid_pairs.sort(axis=1)

    # add a potts function
    # penalizes neighbors for having different labels
    # beta = 0   # 0.1  # strength of potts regularizer
    #beta = 0.1   # 0.1  # strength of potts regularizer

    # Places to look for the definition of this stupid class
    # ~/code/opengm/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx
    # /src/interfaces/python/opengm/opengmcore/function_injector.py

    #shape = [number_of_labels] * 2
    #regularizer = opengm.PottsGFunction(shape, 0.0, beta)
    # __init__( (object)arg1, (object)shape [, (object)values=()]) -> object :

    # values = np.arange(1, ut.num_partitions(num_annots) + 1)
    #regularizer = opengm.PottsGFunction(shape)
    #reg_fid = gm.addFunction(regularizer)

    # A Comparative Study of Modern Inference Techniques for Structured Discrete Energy Minimization Problems
    # http://arxiv.org/pdf/1404.0533.pdf

    # regularizer1 = opengm.pottsFunction([number_of_labels] * 2, valueEqual=0.0, valueNotEqual=beta)

    # gm.addFactors(reg_fid, aid_pairs)

    # 2nd order function
    pair_fid = gm.addFunction(cost_matrix)
    gm.addFactors(pair_fid, aid_pairs)

    if False:
        Inf = opengm.inference.BeliefPropagation
        parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001)
    else:
        Inf = opengm.inference.Multicut
        parameter = opengm.InfParam()

    inf = Inf(gm, parameter=parameter)

    class PyCallback(object):

        def __init__(self,):
            self.labels = []

        def begin(self, inference):
            print("begin of inference")

        def end(self, inference):
            self.labels.append(inference.arg())

        def visit(self, inference):
            gm = inference.gm()
            labelVector = inference.arg()
            print("energy  %r" % (gm.evaluate(labelVector),))
            self.labels.append(labelVector)

    callback = PyCallback()
    visitor = inf.pythonVisitor(callback, visitNth=1)
    inf.infer(visitor)
    print(callback.labels)

    print(cost_matrix)
    pt.imshow(cost_matrix, cmap='magma')
    opengm.visualizeGm(gm=gm)
    pass
Example #8
0
def dummy_multicut():
    """ """
    # Places to look for the definition of PottsGFunction class
    # ~/code/opengm/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx
    # /src/interfaces/python/opengm/opengmcore/function_injector.py
    # A Comparative Study of Modern Inference Techniques for Structured Discrete Energy Minimization Problems
    # http://arxiv.org/pdf/1404.0533.pdf
    # __init__( (object)arg1, (object)shape [, (object)values=()]) -> object :
    # values = np.arange(1, ut.num_partitions(num_annots) + 1)
    # http://hci.iwr.uni-heidelberg.de/opengm2/doxygen/opengm-2.1.1/classopengm_1_1PottsGFunction.html
    import opengm
    import numpy as np
    from itertools import product
    cost_matrix = np.array([
        [ 1. ,  0.2, -0.6, -0.2],
        [ 0.2,  1. , -0.6,  0.8],
        [-0.6, -0.6,  1. , -0.8],
        [-0.2,  0.8, -0.8,  1. ]])
    num_vars = len(cost_matrix)

    # Enumerate undirected edges (node index pairs)
    var_indices = np.arange(num_vars)
    varindex_pairs = np.array(
        [(a1, a2) for a1, a2 in product(var_indices, var_indices)
         if a1 != a2 and a1 > a2], dtype=np.uint32)
    varindex_pairs.sort(axis=1)

    # Create nodes in the graphical model.  In this case there are <num_vars>
    # nodes and each node can be assigned to one of <num_vars> possible labels
    num_nodes = num_vars
    space = np.full((num_nodes,), fill_value=num_vars, dtype=np.int)
    gm = opengm.gm(space)

    # Use one potts function for each edge
    for varx1, varx2 in varindex_pairs:
        cost = cost_matrix[varx1, varx2]
        potts_func = opengm.PottsFunction((num_vars, num_vars), valueEqual=0, valueNotEqual=cost)
        potts_func_id = gm.addFunction(potts_func)
        var_indicies = np.array([varx1, varx2])
        gm.addFactor(potts_func_id, var_indicies)

    #opengm.visualizeGm(gm=gm)

    InfAlgo = opengm.inference.Multicut
    parameter = opengm.InfParam()
    inf = InfAlgo(gm, parameter=parameter)
    inf.infer()
    labels = inf.arg()
    print(labels)

    import plottool as pt

    #varindex_pairs = np.vstack(np.triu_indices_from(cost_matrix)).T

    # Dummy unaries
    #for varx in var_indices:
    #    unary_func = np.ones(num_vars)
    #    unary_func_id = gm.addFunction(unary_func)
    #    gm.addFactor(unary_func_id, varx1)

    #pt.ensure_pylab_qt4()

    # add a potts function
    #shape = [num_vars] * 2
    # num_parts = 5  # possible number paritions with 4 variables
    # num_parts = ut.get_nth_bell_number(num_vars - 1)
    # Causes a segfault if values is passed in
    # values = np.arange(1, num_parts + 1).astype(np.float64)
    # gpotts_func = opengm.PottsGFunction(shape, values)
    #gpotts_func = opengm.PottsGFunction(shape)
    #gpotts_fid = gm.addFunction(gpotts_func)
    # Commenting out the next line results in a segfault
    #gm.addFactors(gpotts_fid, varindex_pairs)

    # 2nd order function
    # Seems to cause OpenGM error: Invalid Model for Multicut-Solver! Solver requires a generalized potts model!
    # pair_fid = gm.addFunction(cost_matrix)
    # gm.addFactors(pair_fid, varindex_pairs)

    InfAlgo = opengm.inference.Multicut
    # Not sure what parameters are allowed to be passed here.
    parameter = opengm.InfParam()
    inf = InfAlgo(gm, parameter=parameter)
    inf.infer()

    class PyCallback(object):

        def __init__(self,):
            self.labels = []

        def begin(self, inference):
            print("begin of inference")

        def end(self, inference):
            self.labels.append(inference.arg())

        def visit(self, inference):
            gm = inference.gm()
            labelVector = inference.arg()
            print("energy  %r" % (gm.evaluate(labelVector),))
            self.labels.append(labelVector)

    callback = PyCallback()
    visitor = inf.pythonVisitor(callback, visitNth=1)
    inf.infer(visitor)
    print(callback.labels)

    print(cost_matrix)
    pt.imshow(cost_matrix, cmap='magma')
    opengm.visualizeGm(gm=gm)
Example #9
0
import numpy 
import opengm
import matplotlib.pyplot as plt

f1=numpy.ones([2])
f2=numpy.ones([2,2])
f3=numpy.ones([2,2,2])
"""
Triangle (non-shared) :
    - 3 variables
    - 3 unaries
    - 2 second order functions
    - 1 third order factor
    - functions are *non* - shared
"""
gm=opengm.gm([2,2,2])
gm.addFactor(gm.addFunction(f1),[0])
gm.addFactor(gm.addFunction(f1),[1])
gm.addFactor(gm.addFunction(f1),[2])
gm.addFactor(gm.addFunction(f2),[0,1])
gm.addFactor(gm.addFunction(f2),[1,2])
gm.addFactor(gm.addFunction(f2),[0,2])
gm.addFactor(gm.addFunction(f3),[0,1,2])

opengm.visualizeGm( gm,show=False,plotFunctions=True,
                    plotNonShared=True)
plt.savefig("triangle.png",bbox_inches='tight',
             dpi=300)  
plt.close()
Example #10
0
import opengm
import matplotlib.pyplot as plt

f1=numpy.ones([2])
f2=numpy.ones([2,2])

#Chain (non-shared functions):
numVar=5
gm=opengm.gm([2]*numVar)
for vi in xrange(numVar):
    gm.addFactor(gm.addFunction(f1),vi)
    if(vi+1<numVar):
        gm.addFactor(gm.addFunction(f2),[vi,vi+1])

# visualize gm        
opengm.visualizeGm( gm,show=False,layout='spring',plotFunctions=True,
                    plotNonShared=True,relNodeSize=0.4)
plt.savefig("chain_non_shared.png",bbox_inches='tight',dpi=300)  
plt.close()

#Chain (shared high order functions):
numVar=5
gm=opengm.gm([2]*numVar)
fid2=gm.addFunction(f2)
for vi in xrange(numVar):
    gm.addFactor(gm.addFunction(f1),vi)
    if(vi+1<numVar):
        gm.addFactor(fid2,[vi,vi+1])

# visualize gm  
opengm.visualizeGm( gm,show=False,layout='spring',plotFunctions=True,
                    plotNonShared=True,relNodeSize=0.4)
Example #11
0
f1=numpy.ones([2])
f2=numpy.ones([2,2])

"""
Full Connected (non-shared):
    - all possible pairwise connections
    - functions are *non* - shared
"""
numVar=4
gm=opengm.gm([2]*numVar)
for vi0 in xrange(numVar):
    for vi1 in xrange(vi0+1,numVar):
        gm.addFactor(gm.addFunction(f2),[vi0,vi1])
opengm.visualizeGm( gm,show=False,layout='neato',
                    iterations=1000,plotFunctions=True,
                    plotNonShared=True,relNodeSize=0.4)
plt.savefig("full_non_shared.png",bbox_inches='tight',dpi=300)  
plt.close()


"""
Full Connected (shared):
    - 5 variables
    - 10 second order factors
      (all possible pairwise connections)
    - functions are *non* - shared
"""
numVar=4
gm=opengm.gm([2]*numVar)
fid2=gm.addFunction(f2)
Example #12
0
def dummy_cut_example():
    r"""
    CommandLine:
        python -m ibeis.workflow --exec-dummy_cut_example --show

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.workflow import *  # NOQA
        >>> result = dummy_cut_example()
        >>> print(result)
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> ut.show_if_requested()
    """
    import opengm
    import numpy as np
    import plottool as pt
    pt.ensure_pylab_qt4()
    # Matching Graph
    cost_matrix = np.array([
        [0.5, 0.6, 0.2, 0.4],
        [0.0, 0.5, 0.2, 0.9],
        [0.0, 0.0, 0.5, 0.1],
        [0.0, 0.0, 0.0, 0.5],
    ])
    cost_matrix += cost_matrix.T
    number_of_labels = 4
    num_annots = 4
    #cost_matrix = (cost_matrix * 2) - 1

    #gm = opengm.gm(number_of_labels)
    gm = opengm.gm(np.ones(num_annots) * number_of_labels)
    aids = np.arange(num_annots)
    aid_pairs = np.array([(a1, a2)
                          for a1, a2 in ut.iprod(aids, aids) if a1 != a2],
                         dtype=np.uint32)
    aid_pairs.sort(axis=1)

    # add a potts function
    # penalizes neighbors for having different labels
    # beta = 0   # 0.1  # strength of potts regularizer
    #beta = 0.1   # 0.1  # strength of potts regularizer

    # Places to look for the definition of this stupid class
    # ~/code/opengm/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx
    # /src/interfaces/python/opengm/opengmcore/function_injector.py

    #shape = [number_of_labels] * 2
    #regularizer = opengm.PottsGFunction(shape, 0.0, beta)
    # __init__( (object)arg1, (object)shape [, (object)values=()]) -> object :

    # values = np.arange(1, ut.num_partitions(num_annots) + 1)
    #regularizer = opengm.PottsGFunction(shape)
    #reg_fid = gm.addFunction(regularizer)

    # A Comparative Study of Modern Inference Techniques for Structured Discrete Energy Minimization Problems
    # http://arxiv.org/pdf/1404.0533.pdf

    # regularizer1 = opengm.pottsFunction([number_of_labels] * 2, valueEqual=0.0, valueNotEqual=beta)

    # gm.addFactors(reg_fid, aid_pairs)

    # 2nd order function
    pair_fid = gm.addFunction(cost_matrix)
    gm.addFactors(pair_fid, aid_pairs)

    if False:
        Inf = opengm.inference.BeliefPropagation
        parameter = opengm.InfParam(steps=10,
                                    damping=0.5,
                                    convergenceBound=0.001)
    else:
        Inf = opengm.inference.Multicut
        parameter = opengm.InfParam()

    inf = Inf(gm, parameter=parameter)

    class PyCallback(object):
        def __init__(self, ):
            self.labels = []

        def begin(self, inference):
            print("begin of inference")

        def end(self, inference):
            self.labels.append(inference.arg())

        def visit(self, inference):
            gm = inference.gm()
            labelVector = inference.arg()
            print("energy  %r" % (gm.evaluate(labelVector), ))
            self.labels.append(labelVector)

    callback = PyCallback()
    visitor = inf.pythonVisitor(callback, visitNth=1)
    inf.infer(visitor)
    print(callback.labels)

    print(cost_matrix)
    pt.imshow(cost_matrix, cmap='magma')
    opengm.visualizeGm(gm=gm)
    pass
Example #13
0
def dummy_multicut():
    """ """
    # Places to look for the definition of PottsGFunction class
    # ~/code/opengm/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx
    # /src/interfaces/python/opengm/opengmcore/function_injector.py
    # A Comparative Study of Modern Inference Techniques for Structured Discrete Energy Minimization Problems
    # http://arxiv.org/pdf/1404.0533.pdf
    # __init__( (object)arg1, (object)shape [, (object)values=()]) -> object :
    # values = np.arange(1, ut.num_partitions(num_annots) + 1)
    # http://hci.iwr.uni-heidelberg.de/opengm2/doxygen/opengm-2.1.1/classopengm_1_1PottsGFunction.html
    import opengm
    import numpy as np
    from itertools import product
    cost_matrix = np.array([[1., 0.2, -0.6, -0.2], [0.2, 1., -0.6, 0.8],
                            [-0.6, -0.6, 1., -0.8], [-0.2, 0.8, -0.8, 1.]])
    num_vars = len(cost_matrix)

    # Enumerate undirected edges (node index pairs)
    var_indices = np.arange(num_vars)
    varindex_pairs = np.array([(a1, a2)
                               for a1, a2 in product(var_indices, var_indices)
                               if a1 != a2 and a1 > a2],
                              dtype=np.uint32)
    varindex_pairs.sort(axis=1)

    # Create nodes in the graphical model.  In this case there are <num_vars>
    # nodes and each node can be assigned to one of <num_vars> possible labels
    num_nodes = num_vars
    space = np.full((num_nodes, ), fill_value=num_vars, dtype=np.int)
    gm = opengm.gm(space)

    # Use one potts function for each edge
    for varx1, varx2 in varindex_pairs:
        cost = cost_matrix[varx1, varx2]
        potts_func = opengm.PottsFunction((num_vars, num_vars),
                                          valueEqual=0,
                                          valueNotEqual=cost)
        potts_func_id = gm.addFunction(potts_func)
        var_indicies = np.array([varx1, varx2])
        gm.addFactor(potts_func_id, var_indicies)

    #opengm.visualizeGm(gm=gm)

    InfAlgo = opengm.inference.Multicut
    parameter = opengm.InfParam()
    inf = InfAlgo(gm, parameter=parameter)
    inf.infer()
    labels = inf.arg()
    print(labels)

    import plottool as pt

    #varindex_pairs = np.vstack(np.triu_indices_from(cost_matrix)).T

    # Dummy unaries
    #for varx in var_indices:
    #    unary_func = np.ones(num_vars)
    #    unary_func_id = gm.addFunction(unary_func)
    #    gm.addFactor(unary_func_id, varx1)

    #pt.ensure_pylab_qt4()

    # add a potts function
    #shape = [num_vars] * 2
    # num_parts = 5  # possible number paritions with 4 variables
    # num_parts = ut.get_nth_bell_number(num_vars - 1)
    # Causes a segfault if values is passed in
    # values = np.arange(1, num_parts + 1).astype(np.float64)
    # gpotts_func = opengm.PottsGFunction(shape, values)
    #gpotts_func = opengm.PottsGFunction(shape)
    #gpotts_fid = gm.addFunction(gpotts_func)
    # Commenting out the next line results in a segfault
    #gm.addFactors(gpotts_fid, varindex_pairs)

    # 2nd order function
    # Seems to cause OpenGM error: Invalid Model for Multicut-Solver! Solver requires a generalized potts model!
    # pair_fid = gm.addFunction(cost_matrix)
    # gm.addFactors(pair_fid, varindex_pairs)

    InfAlgo = opengm.inference.Multicut
    # Not sure what parameters are allowed to be passed here.
    parameter = opengm.InfParam()
    inf = InfAlgo(gm, parameter=parameter)
    inf.infer()

    class PyCallback(object):
        def __init__(self, ):
            self.labels = []

        def begin(self, inference):
            print("begin of inference")

        def end(self, inference):
            self.labels.append(inference.arg())

        def visit(self, inference):
            gm = inference.gm()
            labelVector = inference.arg()
            print("energy  %r" % (gm.evaluate(labelVector), ))
            self.labels.append(labelVector)

    callback = PyCallback()
    visitor = inf.pythonVisitor(callback, visitNth=1)
    inf.infer(visitor)
    print(callback.labels)

    print(cost_matrix)
    pt.imshow(cost_matrix, cmap='magma')
    opengm.visualizeGm(gm=gm)