def segmentGC(pred, beta): ''' This function implements a call to the standard Graph Cut segmentation in the OpenGM library (http://hci.iwr.uni-heidelberg.de/opengm2/). Potts model is assumed, with a 4-neighborhood for 2D data and a 6-neighborhood for 3D data to define the pairwise terms. Parameters: -- pred - the unary terms, used directly (no Log applied, do it outside if needed) This input is assumed to be 3D! -- beta - the weight of the pairwise potentials, usually called lambda Return: -- binary volume, as produced by OpenGM ''' nz, ny, nx = pred.shape numVar = pred.size numLabels = 2 numberOfStates = np.ones(numVar, dtype=opengm.index_type) * numLabels gm = opengm.graphicalModel(numberOfStates, operator='adder') # Adding unary function and factors functions = np.zeros((numVar, 2)) predflat = pred.reshape((numVar, 1)) if (predflat.dtype == np.uint8): predflat = predflat.astype(np.float32) predflat = predflat / 256. functions[:, 0] = predflat[:, 0] functions[:, 1] = 1 - predflat[:, 0] unary_fids = gm.addFunctions(functions) gm.addFactors(unary_fids, np.arange(0, numVar)) # add one binary function (potts fuction) potts = opengm.PottsFunction([2, 2], 0.0, beta) binary_fid = gm.addFunction(potts) # add binary factors indices = np.arange(numVar, dtype=np.uint32).reshape((nz, ny, nx)) z_edges = np.concatenate([indices[:nz - 1, :, :], indices[1:, :, :]] ).reshape((2, (nz - 1) * ny * nx)).transpose() y_edges = np.concatenate([indices[:, :ny - 1, :], indices[:, 1:, :]] ).reshape((2, nz * (ny - 1) * nx)).transpose() x_edges = np.concatenate([indices[:, :, :nx - 1], indices[:, :, 1:]] ).reshape((2, nz * ny * (nx - 1))).transpose() gm.addFactors(binary_fid, z_edges) gm.addFactors(binary_fid, y_edges) gm.addFactors(binary_fid, x_edges) grcut = opengm.inference.GraphCut(gm) grcut.infer() argmin = grcut.arg() res = argmin.reshape((nz, ny, nx)) if hasattr(pred, 'axistags'): res = vigra.taggedView(res, pred.axistags) return res
def intra_encounter_matching(): import numpy as np from scipy.sparse import coo_matrix, csgraph qreq_, cm_list = testdata_workflow() # qaids = [cm.qaid for cm in cm_list] # top_aids = [cm.get_top_aids(5) for cm in cm_list] aid_pairs = np.array([(cm.qaid, daid) for cm in cm_list for daid in cm.get_top_aids(5)]) top_scores = ut.flatten([cm.get_top_scores(5) for cm in cm_list]) N = aid_pairs.max() + 1 mat = coo_matrix((top_scores, aid_pairs.T), shape=(N, N)) csgraph.connected_components(mat) tree = csgraph.minimum_spanning_tree(mat) # NOQA import plottool as pt dense = mat.todense() pt.imshow(dense / dense.max() * 255) pt.show_if_requested() # baseline jobid import opengm # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/OpenGM%20tutorial.ipynb numVar = 10 unaries = np.ones([numVar, 3], dtype=opengm.value_type) gm = opengm.gm(np.ones(numVar, dtype=opengm.label_type) * 3) unary_fids = gm.addFunctions(unaries) gm.addFactors(unary_fids, np.arange(numVar)) infParam = opengm.InfParam(workflow=ut.ensure_ascii('(IC)(TTC-I,CC-I)'), ) inf = opengm.inference.Multicut(gm, parameter=infParam) visitor = inf.verboseVisitor(printNth=1, multiline=False) inf.infer(visitor) arg = inf.arg() # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) # regularizer = opengm.pottsFunction([3, 3], 0.0, beta) # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) unaries = np.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() # NOQA """
def add_potts_lattice_layer(gm, pixel_unaries, beta, offset=0): """ Adds a lattice layer to a gm where all pairwise functions are Potts model Parameters: - pixel_unaries - a 3D array of shape (width, height, n_labels). - beta - the smoothing parameter for the Potts model. Penalty for label dissimilarity """ n_labels = pixel_unaries.shape[-1] add_lattice_layer(gm, pixel_unaries, pixel_regularizer=opengm.PottsFunction( [n_labels, n_labels], 0.0, beta), offset=offset) return gm
def forward(self, unary_pots): """ Receive input tensor, return output tensor""" self.save_for_backward(unary_pots) print("In forward") b, r, c, k = unary_pots.size() if (False): if torch.cuda.is_available(): unaries = unary_pots.cpu().numpy() else: unaries = unary_pots.numpy() unaries = unaries.reshape([b * r * c, k]) numVar = r * c gm = opengm.gm(np.ones(numVar, dtype=opengm.label_type) * k) uf_id = gm.addFunctions(unaries) potts = opengm.PottsFunction([k, k], 0.0, 0.4) pf_id = gm.addFunction(potts) vis = np.arange(0, numVar, dtype=np.uint64) # add all unary factors at once gm.addFactors(uf_id, vis) # add pairwise factors ### Row Factors for i in range(0, r): for j in range(0, c - 1): gm.addFactor(pf_id, [i * c + j, i * c + j + 1]) ### Column Factors for i in range(0, r - 1): for j in range(c): gm.addFactor(pf_id, [i * c + j, (i + 1) * c + j]) print("Graphical Model Constructed") inf = opengm.inference.AlphaExpansionFusion(gm) inf.infer() labels = inf.arg() return torch.from_numpy(np.asarray(labels).astype('float')) else: return torch.zeros(b, r, c)
def test_add_factors_generic(self): def mygen(): yield 0 yield 1 gm = opengm.gm([2, 4]) f = opengm.PottsFunction([2, 4], 0.0, 1.0) fid = gm.addFunction(f) vis_list = [ [0, 1], (0, 1), (x for x in xrange(2)), mygen(), opengm.IndexVector(x for x in xrange(0, 2)), numpy.arange(0, 2, dtype=numpy.uint64) ] for i, vis in enumerate(vis_list): fIndex = gm.addFactor(fid, vis) assert(gm.numberOfFactors == i + 1) assert(fIndex == i) assert(gm[fIndex].numberOfVariables == 2) assert(gm[fIndex].shape[0] == 2) assert(gm[fIndex].shape[1] == 4) assert(gm[fIndex].variableIndices[0] == 0) assert(gm[fIndex].variableIndices[1] == 1)
def _update_state_opengm(model, weight_key='cut_prob', name_label_key='name_label'): import opengm import scipy.special graph = model.graph n_annots = len(model.graph) n_names = n_annots nodes = sorted(graph.nodes()) edges = [tuple(sorted(e)) for e in graph.edges()] edges = ut.sortedby2(edges, edges) index_type = opengm.index_type node_state_card = np.ones(n_annots, dtype=index_type) * n_names numberOfStates = node_state_card annot_idxs = list(range(n_annots)) lookup_annot_idx = ut.dzip(nodes, annot_idxs) gm = opengm.graphicalModel(numberOfStates, operator='adder') # annot_idxs = list(range(n_annots)) # edge_idxs = list(range(n_annots, n_annots + n_edges)) # if use_unaries: # unaries = np.ones((n_annots, n_names)) / n_names # # unaries[0][0] = 1 # # unaries[0][1:] = 0 # for annot_idx in annot_idxs: # fid = gm.addFunction(unaries[annot_idx]) # gm.addFactor(fid, annot_idx) # Add Potts function for each edge pairwise_factor_idxs = [] for count, (aid1, aid2) in enumerate(edges, start=len(list(gm.factors()))): varx1, varx2 = ut.take(lookup_annot_idx, [aid1, aid2]) var_indicies = np.array([varx1, varx2]) p_same = graph.get_edge_data(aid1, aid2)['cut_prob'] # p_diff = 1 - p_same eps = 1E-9 p_same = np.clip(p_same, eps, 1.0 - eps) same_weight = scipy.special.logit(p_same) # valueEqual = -same_weight valueEqual = 0 valueNotEqual = same_weight if not np.isfinite(valueNotEqual): """ python -m plottool.draw_func2 --exec-plot_func --show --range=-1,1 --func=scipy.special.logit """ print('valueNotEqual = %r' % (valueNotEqual, )) print('p_same = %r' % (p_same, )) raise ValueError('valueNotEqual') pairwise_factor_idxs.append(count) potts_func = opengm.PottsFunction((n_names, n_names), valueEqual=valueEqual, valueNotEqual=valueNotEqual) potts_func_id = gm.addFunction(potts_func) gm.addFactor(potts_func_id, var_indicies) model.gm = gm
def crftest(): """ pip install pyqpbo pip install pystruct http://taku910.github.io/crfpp/#install cd ~/tmp #wget https://drive.google.com/folderview?id=0B4y35FiV1wh7fngteFhHQUN2Y1B5eUJBNHZUemJYQV9VWlBUb3JlX0xBdWVZTWtSbVBneU0&usp=drive_web#list 7z x CRF++-0.58.tar.gz 7z x CRF++-0.58.tar cd CRF++-0.58 chmod +x configure ./configure make """ import pystruct import pystruct.models inference_method_options = ['lp', 'max-product'] inference_method = inference_method_options[1] # graph = pystruct.models.GraphCRF( # n_states=None, # n_features=None, # inference_method=inference_method, # class_weight=None, # directed=False, # ) num_annots = 5 num_names = num_annots aids = np.arange(5) rng = np.random.RandomState(0) hidden_nids = rng.randint(0, num_names, num_annots) unique_nids, groupxs = ut.group_indices(hidden_nids) # Indicator vector indicating the name node_features = np.zeros((num_annots, num_names)) node_features[(aids, hidden_nids)] = 1 toy_params = {True: {'mu': 1.0, 'sigma': 2.2}, False: {'mu': 7.0, 'sigma': 0.9}} if False: import vtool as vt import wbia.plottool as pt pt.ensureqt() xdata = np.linspace(0, 100, 1000) tp_pdf = vt.gauss_func1d(xdata, **toy_params[True]) fp_pdf = vt.gauss_func1d(xdata, **toy_params[False]) pt.plot_probabilities([tp_pdf, fp_pdf], ['TP', 'TF'], xdata=xdata) def metric(aidx1, aidx2, hidden_nids=hidden_nids, toy_params=toy_params): if aidx1 == aidx2: return 0 rng = np.random.RandomState(int(aidx1 + aidx2)) same = hidden_nids[int(aidx1)] == hidden_nids[int(aidx2)] mu, sigma = ut.dict_take(toy_params[same], ['mu', 'sigma']) return np.clip(rng.normal(mu, sigma), 0, np.inf) pairwise_aidxs = list(ut.iprod(range(num_annots), range(num_annots))) pairwise_labels = np.array( # NOQA [hidden_nids[a1] == hidden_nids[a2] for a1, a2 in pairwise_aidxs] ) pairwise_scores = np.array([metric(*zz) for zz in pairwise_aidxs]) pairwise_scores_mat = pairwise_scores.reshape(num_annots, num_annots) # NOQA graph = pystruct.models.EdgeFeatureGraphCRF( # NOQA n_states=num_annots, n_features=num_names, n_edge_features=1, inference_method=inference_method, ) import opengm numVar = 10 unaries = np.ones([numVar, 3], dtype=opengm.value_type) gm = opengm.gm(np.ones(numVar, dtype=opengm.label_type) * 3) unary_fids = gm.addFunctions(unaries) gm.addFactors(unary_fids, np.arange(numVar)) infParam = opengm.InfParam(workflow=ut.ensure_ascii('(IC)(TTC-I,CC-I)')) inf = opengm.inference.Multicut(gm, parameter=infParam) visitor = inf.verboseVisitor(printNth=1, multiline=False) inf.infer(visitor) arg = inf.arg() # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) # regularizer = opengm.pottsFunction([3, 3], 0.0, beta) # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) unaries = np.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() # NOQA
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)
import opengm import numpy unaries = numpy.random.rand(100, 100, 2) potts = opengm.PottsFunction([2, 2], 0.0, 0.4) gm = opengm.grid2d2Order(unaries=unaries, regularizer=potts) class IcmPurePython(): def __init__(self, gm): self.gm = gm self.numVar = gm.numberOfVariables self.movemaker = opengm.movemaker(gm) self.adj = gm.variablesAdjacency() self.localOpt = numpy.zeros(self.numVar, dtype=numpy.bool) def infer(self, verbose=False): changes = True while (changes): changes = False for v in self.gm.variables(): if (self.localOpt[v] == False): l = self.movemaker.label(v) nl = self.movemaker.moveOptimallyMin(v) self.localOpt[v] = True if (nl != l): if (verbose): print self.movemaker.value() self.localOpt[self.adj[v]] = False changes = True def arg(self):
energyNotEqual = 0.2 sigma = 0.2 resizeFactor = 2 img = vigra.impex.readImage('118035.jpg') shape = img.shape imgLab = vigra.colors.transform_RGB2Lab(img) shape = (shape[0] * resizeFactor, shape[1] * resizeFactor) imgLab = vigra.sampling.resize(imgLab, shape, order=3) gradMag = vigra.filters.gaussianGradientMagnitude(imgLab, gradScale) unaries = numpy.zeros([shape[0], shape[1], 2]) unaries[:, :, 1] = numpy.exp(-1.0 * gradMag[:, :, 0] * sigma) unaries[:, :, 0] = 1.0 - unaries[:, :, 1] regularizer = opengm.PottsFunction(2, 2, 0.0, energyNotEqual) gm = opengm.grid2d2Order(unaries=unaries, regularizer=regularizer, order='numpy', operator='adder') inf = opengm.inference.GraphCut(gm) inf.infer() argmin = inf.arg().reshape(shape[0:2]) plt.figure(1) ax = plt.subplot(2, 1, 1) plt.imshow(unaries[:, :, 1].T, interpolation="nearest") plt.set_cmap(cm.copper) plt.colorbar()
def build_factor_graph(G, nodes, edges, n_annots, n_names, lookup_annot_idx, use_unaries=True, edge_probs=None, operator='multiplier'): node_state_card = np.ones(n_annots, dtype=index_type) * n_names numberOfStates = node_state_card # n_edges = len(edges) # n_edge_states = 2 # edge_state_card = np.ones(n_edges, dtype=index_type) * n_edge_states # numberOfStates = np.hstack([node_state_card, edge_state_card]) # gm = opengm.graphicalModel(numberOfStates, operator='adder') gm = opengm.graphicalModel(numberOfStates, operator=operator) annot_idxs = list(range(n_annots)) # edge_idxs = list(range(n_annots, n_annots + n_edges)) import scipy.special if use_unaries: unaries = np.ones((n_annots, n_names)) / n_names # unaries[0][0] = 1 # unaries[0][1:] = 0 for annot_idx in annot_idxs: fid = gm.addFunction(unaries[annot_idx]) gm.addFactor(fid, annot_idx) # Add Potts function for each edge pairwise_factor_idxs = [] for count, (aid1, aid2) in enumerate(edges, start=len(list(gm.factors()))): varx1, varx2 = ut.take(lookup_annot_idx, [aid1, aid2]) var_indicies = np.array([varx1, varx2]) if edge_probs is None: p_same, p_diff = get_edge_id_probs(G, aid1, aid2, n_names) else: p_same, p_diff = edge_probs[count] use_logit = operator == 'adder' if use_logit: eps = 1E-9 p_same = np.clip(p_same, eps, 1.0 - eps) same_weight = scipy.special.logit(p_same) # valueEqual = -same_weight valueEqual = 0 valueNotEqual = same_weight if not np.isfinite(valueNotEqual): """ python -m plottool.draw_func2 --exec-plot_func --show --range=-1,1 --func=scipy.special.logit """ print('valueNotEqual = %r' % (valueNotEqual, )) print('p_same = %r' % (p_same, )) raise ValueError('valueNotEqual') else: valueEqual = p_same valueNotEqual = p_diff p_same, p_diff = get_edge_id_probs(G, aid1, aid2, n_names) pairwise_factor_idxs.append(count) potts_func = opengm.PottsFunction((n_names, n_names), valueEqual=valueEqual, valueNotEqual=valueNotEqual) potts_func_id = gm.addFunction(potts_func) gm.addFactor(potts_func_id, var_indicies) gm.pairwise_factor_idxs = pairwise_factor_idxs gm.G = G return gm
def robust_pn_potts_model(pixel_unaries, segment_map, beta, gamma, gamma_max, k): """ Creates a potts model with higher order factors defined over segments. This follows the robust Pn Potts model parameterization that is compatible with alpha-expansion Russel(2012) - section 3.4.1 - pixel_unaries - a 3D array of shape (width, height, n_labels). beta is pixel smoothing parameter (same as potts model) gamma is baseline inconsistency penalty gamma_max is max inconsistency penalty k is the increment in penalty with each inconsistent pixel Must have gamma <= gamma_max and k >= 0 The label space is extended to include a new free label (l_f). The last label in the results must be disregarded. If any of the resulting pixels take the free label there is something wrong... Resulting model is compatible with alpha-expansion and alpha-beta-swap using graph cuts """ n_labels = pixel_unaries.shape[-1] n_segments = np.max(segment_map) + 1 # add the free label to the unaries with extremely large cost pixel_unaries = np.dstack( (pixel_unaries, np.ones(pixel_unaries.shape[:2]))) # create a potts model over the new label space pixel_regularizer = opengm.PottsFunction([n_labels + 1, n_labels + 1], 0.0, beta) # the segment unaries are defined in terms of the new label space. # Penalty of gamma for any label and gamma_max for free label segment_unary = gamma * np.ones((1, n_labels + 1)) segment_unary[-1] = gamma_max segment_unaries = np.tile( segment_unary, (n_segments, 1) ) #this is not the most memory efficient way to do this.. hack for now # inter-layer potentials are defined as # 0 if the labels are equal # 0.5 k if either are free but not both # k otherwise (both are free or both are non-free but non-equal) def inter_layer(x, y): if x == y: return 0. elif (x == n_labels or y == n_labels) and x != y: return 0.5 else: return 1. inter_layer_potential = k * np.fromfunction( np.vectorize(inter_layer), shape=(n_labels + 1, n_labels + 1)) # the unary potentials must also be augmented to use this symmetric parameterization # that is compatible with alpha-expansion. The pixel one actually doesn't need # to be done because of the infinite penalty already associated with the free label # pixel_unary_augment = np.zeros((n_labels + 1)) # pixel_unary_augment[-1] = 0.5*k # pixel_unaries += pixel_unary_augment segment_unary_augment = np.zeros((n_labels + 1)) segment_unary_augment[-1] = -0.5 * k segment_unaries += segment_unary_augment return segment_overlap_graph(pixel_unaries, segment_map, segment_unaries, pixel_regularizer=pixel_regularizer, segment_regularizer=None, inter_layer_regularizer=inter_layer_potential)
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)
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)
import opengm import numpy unaries = numpy.random.rand(15, 15, 3) potts = opengm.PottsFunction([3, 3], 0.0, 0.15) gm = opengm.grid2d2Order(unaries=unaries, regularizer=potts) inf = opengm.inference.Icm(gm) inf.infer(inf.verboseVisitor(), False) print inf.arg().reshape(15, 15)
import opengm import numpy import matplotlib import time from matplotlib import pyplot as plt from matplotlib import animation shape=[100,100] numLabels=10 unaries=numpy.random.rand(shape[0], shape[1],numLabels) potts=opengm.PottsFunction([numLabels,numLabels],0.0,0.4) gm=opengm.grid2d2Order(unaries=unaries,regularizer=potts) # alpha beta swap as solver inf=opengm.inference.AlphaBetaSwap(gm,parameter=opengm.InfParam(steps=20)) inf=opengm.inference.AlphaExpansion(gm,parameter=opengm.InfParam(steps=20)) inf=opengm.inference.BeliefPropagation(gm,parameter=opengm.InfParam()) inf=opengm.inference.Icm(gm,parameter=opengm.InfParam()) class PyCallback(object): def __init__(self,shape,numLabels): self.shape=shape self.numLabels=numLabels def begin(self,inference): print "begin" self.visitNr=1 self.gm=inference.gm() self.labelVector=opengm.LabelVector() self.labelVector.resize(self.gm.numberOfVariables) matplotlib.interactive(True)
fid = gm.addFunction(f) functionIds.append(fid) gm.addFactor(fid, [3, 4, 5]) # fill sparse function from dense function f = opengm.SparseFunction() f.assignDense(numpy.identity(4), defaultValue=0) fid = gm.addFunction(f) functionIds.append(fid) gm.addFactor(fid, [4, 5]) print "\nsparse function: \n", f #--------------------------------------------------------------- # Potts Function #--------------------------------------------------------------- f = opengm.PottsFunction(shape=[2, 4], valueEqual=0.0, valueNotEqual=1.0) fid = gm.addFunction(f) functionIds.append(fid) gm.addFactor(fid, [0, 5]) print "\npotts function: \n", f #--------------------------------------------------------------- # Truncated Absolute Difference Function #--------------------------------------------------------------- f = opengm.TruncatedAbsoluteDifferenceFunction( shape=[3, 4], truncate=2, weight=0.2, ) fid = gm.addFunction(f) functionIds.append(fid)
def backward(self, grad_output): """Calculate the gradients of left and right""" print("Entering Backward Pass Through CRF\n Max Grad Outputs", torch.max(grad_output), torch.min(grad_output)) unary_pots, = self.saved_tensors true_labels = self.labels true_labels = true_labels.data.cpu().numpy() #unary_pots = unary_pots_temp[:,:,0:10,0:10] b, r, c, k = unary_pots.size() # # r=10 # # c=10 # print(unary_pots.size()) # print(true_labels.shape) gamma = 0.1 tau = 10 unary_flat = unary_pots.contiguous().view([b * r * c, k]) numVar = r * c index_arr = torch.zeros(r * c, k) for i in range(k): index_arr[:, i] = i for j in range(numVar): for i in range(k): unary_flat[j, i] = unary_flat[j, i] - gamma * min( abs(i - true_labels[j]), tau) if torch.cuda.is_available(): unaries = unary_flat.cpu().numpy() else: unaries = unary_flat.numpy() gm = opengm.gm(np.ones(numVar, dtype=opengm.label_type) * k) uf_id = gm.addFunctions(unaries) potts = opengm.PottsFunction([k, k], 0.2, 1.0) pf_id = gm.addFunction(potts) vis = np.arange(0, numVar, dtype=np.uint64) # add all unary factors at once gm.addFactors(uf_id, vis) # add pairwise factors ### Row Factors for i in range(0, r): for j in range(0, c - 1): gm.addFactor(pf_id, [i * c + j, i * c + j + 1]) ### Column Factors for i in range(0, r - 1): for j in range(c): gm.addFactor(pf_id, [i * c + j, (i + 1) * c + j]) print("Graphical Model Constructed") infParam = opengm.InfParam(steps=5) inf = opengm.inference.AlphaExpansionFusion(gm, parameter=infParam) inf.infer() print("Inference done") del_x_bar = inf.arg() sub_grad_unaries = np.zeros((b * r * c, k)) energy = 0 for i in range(numVar): energy = energy - unaries[i][del_x_bar[i]] + unaries[i][int( true_labels[i])] + gamma * min( abs(del_x_bar[i] - true_labels[i]), tau) for i in range(0, r): for j in range(0, c - 1): if (del_x_bar[i * c + j] == del_x_bar[i * c + j + 1]): energy = energy - 0.2 else: energy = energy - 1.0 if (true_labels[i * c + j] == true_labels[i * c + j + 1]): energy = energy + 0.2 else: energy = energy + 1.0 for i in range(0, r - 1): for j in range(c): if (del_x_bar[i * c + j] == del_x_bar[(i + 1) * c + j]): energy = energy - 0.2 else: energy = energy - 1.0 if (true_labels[i * c + j] == true_labels[i * c + j + 1]): energy = energy + 0.2 else: energy = energy + 1.0 print("Energy", energy) for i in range(numVar): sub_grad_unaries[i, int(del_x_bar[i])] = -1 #print true_labels[i] if (true_labels[i] == -1): continue sub_grad_unaries[i, int(true_labels[i])] = 1 grad_in = sub_grad_unaries.reshape([b, r, c, k]) print("Leaving Backward through CRF\n Min Grad Inputs", torch.max(grad_output), torch.min(grad_output)) return torch.from_numpy(grad_in).type('torch.cuda.FloatTensor') # width=100 # height=200 # numVar=width*height # numLabels=2 # # construct gm # gm=opengm.gm(np.ones(numVar,dtype=opengm.label_type)*numLabels) # # construct an array with all numeries (random in this example) # unaries=np.random.rand(width,height,numLabels) # # reshape unaries is such way, that the first axis is for the different functions # unaries2d=unaries.reshape([numVar,numLabels]) # # add all unary functions at once (#numVar unaries) # fids=gm.addFunctions(unaries2d) # # numpy array with the variable indices for all factors # vis=np.arange(0,numVar,dtype=numpy.uint64) # # add all unary factors at once # gm.addFactors(fids,vis) # print("Graphical Model Constructed")