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': .9 } } if False: import vtool as vt import 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( [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) graph = pystruct.models.EdgeFeatureGraphCRF( 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()
# Read image img = numpy.array(numpy.squeeze(vigra.impex.readImage(imgPath)), dtype=opengm.value_type) #[0:100,0:40] shape = img.shape # get graphical model an starting point gm, startingPoint = denoiseModel(img, norm=norm, weight=weight, inpaintPixels=numpy.where(img == 0), numLabels=numLabels, randInpaitStartingPoint=True) Inf = opengm.inference.LazyFlipper param = opengm.InfParam(maxSubgraphSize=1) inf = Inf(gm=gm, accumulator='minimizer', parameter=param) # set up starting point assert len(startingPoint) == gm.numberOfVariables assert int(startingPoint.max()) < numLabels inf.setStartingPoint(startingPoint) # start inference with visitor print "inf icm " inf.infer(inf.verboseVisitor(5000)) print "inf " param = opengm.InfParam() inf2 = Inf(gm=gm, accumulator='minimizer', parameter=param) print "inf"
def cut_step( G, nodes, edges, n_annots, n_names, lookup_annot_idx, edge_probs, pass_values, fail_values, ): # 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 space = np.full((n_annots, ), fill_value=n_names, dtype=opengm.index_type) gm = opengm.gm(space, operator='adder') # Use one potts function for each edge gm = build_factor_graph( G, nodes, edges, n_annots, n_names, lookup_annot_idx, use_unaries=False, edge_probs=edge_probs, operator='adder', ) with ut.Indenter('[CUTS]'): ut.cprint('Brute Force Labels: (energy minimization)', 'blue') infr = opengm.inference.Bruteforce(gm, accumulator='minimizer') infr.infer() labels = rectify_labels(G, infr.arg()) logger.info( pd.DataFrame(labels, columns=['nid'], index=pd.Series(nodes)).T) logger.info('value = %r' % (infr.value(), )) mc_params = opengm.InfParam( maximalNumberOfConstraintsPerRound=1000000, initializeWith3Cycles=True, edgeRoundingValue=1e-08, timeOut=36000000.0, cutUp=1e75, reductionMode=3, numThreads=0, # allowCutsWithin=? # workflow=workflow verbose=False, verboseCPLEX=False, ) infr = opengm.inference.Multicut(gm, parameter=mc_params, accumulator='minimizer') infr.infer() labels = infr.arg() labels = rectify_labels(G, infr.arg()) ut.cprint('Multicut Labels: (energy minimization)', 'blue') logger.info( pd.DataFrame(labels, columns=['nid'], index=pd.Series(nodes)).T) logger.info('value = %r' % (infr.value(), )) if pass_values is not None: gotany = False for pval in pass_values: if all(labels == pval): gotany = True break if not gotany: ut.cprint('INCORRECT DID NOT GET PASS VALUES', 'red') logger.info('pass_values = %r' % (pass_values, )) if fail_values is not None: for fail in fail_values: if all(labels == fail): ut.cprint('INCORRECT', 'red')
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")
def inference_ogm(unary_potentials, pairwise_potentials, edges, return_energy=False, alg='dd', init=None, reserveNumFactorsPerVariable=2, **kwargs): """Inference with OpenGM backend. Parameters ---------- unary_potentials : nd-array, shape (n_nodes, n_nodes) Unary potentials of energy function. pairwise_potentials : nd-array, shape (n_states, n_states) or (n_states, n_states, n_edges). Pairwise potentials of energy function. If the first case, edge potentials are assumed to be the same for all edges. In the second case, the sequence needs to correspond to the edges. edges : nd-array, shape (n_edges, 2) Graph edges for pairwise potentials, given as pair of node indices. As pairwise potentials are not assumed to be symmetric, the direction of the edge matters. alg : string Possible choices currently are: * 'bp' for Loopy Belief Propagation. * 'dd' for Dual Decomposition via Subgradients. * 'trws' for Vladimirs TRWs implementation. * 'trw' for OGM TRW. * 'gibbs' for Gibbs sampling. * 'lf' for Lazy Flipper * 'fm' for Fusion Moves (alpha-expansion fusion) * 'dyn' for Dynamic Programming (message passing in trees) * 'gc' for Graph Cut * 'alphaexp' for Alpha Expansion using Graph Cuts * 'mqpbo' for multi-label qpbo init : nd-array Initial solution for starting inference (ignored by some algorithms). reserveNumFactorsPerVariable : reserve a certain number of factors for each variable can speed up the building of a graphical model. ( For a 2d grid with second order factors one should set this to 5 4 2-factors and 1 unary factor for most pixels ) Returns ------- labels : nd-array Approximate (usually) MAP variable assignment. """ import opengm n_states, pairwise_potentials = \ _validate_params(unary_potentials, pairwise_potentials, edges) n_nodes = len(unary_potentials) gm = opengm.gm(np.ones(n_nodes, dtype=opengm.label_type) * n_states) nFactors = int(n_nodes + edges.shape[0]) gm.reserveFactors(nFactors) gm.reserveFunctions(nFactors, 'explicit') # all unaries as one numpy array # (opengm's value_type == float64 but all types are accepted) unaries = np.require(unary_potentials, dtype=opengm.value_type) * -1.0 # add all unart functions at once fidUnaries = gm.addFunctions(unaries) visUnaries = np.arange(n_nodes, dtype=opengm.label_type) # add all unary factors at once gm.addFactors(fidUnaries, visUnaries) # add all pariwise functions at once # - first axis of secondOrderFunctions iterates over the function) secondOrderFunctions = -np.require(pairwise_potentials, dtype=opengm.value_type) fidSecondOrder = gm.addFunctions(secondOrderFunctions) gm.addFactors(fidSecondOrder, edges.astype(np.uint64)) if alg == 'bp': inference = opengm.inference.BeliefPropagation(gm) elif alg == 'dd': inference = opengm.inference.DualDecompositionSubgradient(gm) elif alg == 'trws': inference = opengm.inference.TrwsExternal(gm) elif alg == 'trw': inference = opengm.inference.TreeReweightedBp(gm) elif alg == 'gibbs': inference = opengm.inference.Gibbs(gm) elif alg == 'lf': inference = opengm.inference.LazyFlipper(gm) elif alg == 'icm': inference = opengm.inference.Icm(gm) elif alg == 'dyn': inference = opengm.inference.DynamicProgramming(gm) elif alg == 'fm': inference = opengm.inference.AlphaExpansionFusion(gm) elif alg == 'gc': inference = opengm.inference.GraphCut(gm) elif alg == 'loc': inference = opengm.inference.Loc(gm) elif alg == 'mqpbo': inference = opengm.inference.Mqpbo(gm) elif alg == 'alphaexp': inference = opengm.inference.AlphaExpansion(gm) elif alg == 'lp': parameter = opengm.InfParam(integerConstraint=True) inference = opengm.inference.LpCplex(gm, parameter=parameter) if init is not None: inference.setStartingPoint(init) inference.infer() # we convert the result to int from unsigned int # because otherwise we are sure to shoot ourself in the foot res = inference.arg().astype(np.int) if return_energy: return res, gm.evaluate(res) return res
qx = pq1[1] - pq2[1] qy = pq1[1] - pq3[1] # score = np.abs(N1[0] - N3[0] - (N1[1] - N2[1])) # score = np.abs(N1[1] - N3[1] - (N1[0] - N2[0])) integrability_score = np.abs(py - qx) # smooth_score = px ** 2 + py ** 2 + qx ** 2 + qy ** 2 score = integrability_score f[st1, st2, st3] = score # f = np.exp(f) # f = f / np.sum(f) fid = gm.addFunction(f * w_tri) gm.addFactor(fid, (i, neighbour[0], neighbour[1])) inf = opengm.inference.BeliefPropagation(gm, accumulator='minimizer', parameter=opengm.InfParam(steps=100, convergenceBound=0.001, damping=0.5)) # # inf = opengm.inference.TreeReweightedBp(gm, accumulator='minimizer', # parameter=opengm.InfParam(steps=100, convergenceBound=0.001)) infereMonitor = InfereMonitor(noofNodes, nState) visitor = inf.pythonVisitor(infereMonitor, visitNth=1) inf.infer(visitor) labels = inf.arg() # reconstruct labels to images N_reconstruct = np.zeros_like(N_valid)
for x2 in range(2): for x3 in range(2): for x4 in range(2): labelsum=x1+x2+x3+x4 if labelsum is not 2 and labelsum is not 0 : fClosedness[x1,x2,x3,x4]=high fidClosedness=gm.addFunction(fClosedness) # for each boundary in the grid, i.e. for each variable # of the model, add one 1st order functions # and one 1st order factor # and for each junction of four inter-pixel edges on the grid, # one factor is added that connects the corresponding variable # indices and refers to the closedness-function for yt in range(tGridSize[1]): for xt in range(tGridSize[0]): # unaries if (xt % 2 + yt % 2) == 1 : gradient = abs( data[xt / 2, yt / 2]- data[xt/ 2 + xt % 2, yt / 2 + yt % 2]) f=numpy.array([beta*gradient , (1.0-beta)*(1.0-gradient)]) gm.addFactor(gm.addFunction(f),[cToVi.convert(xt,yt)]) # high order factors (4.th order) if xt % 2 + yt % 2 == 2 : vi=[cToVi.convert(xt + 1, yt),cToVi.convert(xt - 1, yt),cToVi.convert(xt, yt + 1), cToVi.convert(xt, yt - 1)] vi=sorted(vi); gm.addFactor(fidClosedness,vi) inf=opengm.inference.LazyFlipper(gm,parameter=opengm.InfParam(maxSubgraphSize=4)) inf.inference.infer() arg=inf.inference.arg() printSolution(data,arg,cToVi)
numLabels = 50 # use 256 for full-model (slow) # Read image img = numpy.array(numpy.squeeze(vigra.impex.readImage(imgPath)), dtype=opengm.value_type) #[0:100,0:40] shape = img.shape # get graphical model an starting point gm, startingPoint = denoiseModel(img, norm=norm, weight=weight, inpaintPixels=numpy.where(img == 0), numLabels=numLabels, randInpaitStartingPoint=True) inf = opengm.inference.BeliefPropagation(gm, parameter=opengm.InfParam()) print "inf" inf.setStartingPoint(inf.arg()) # set up visitor callback = PyCallback(shape, numLabels) visitor = inf.pythonVisitor(callback, visitNth=1) inf.infer(visitor) # get the result arg = inf.arg() arg = arg.reshape(shape) # plot final result matplotlib.interactive(False) # Two subplots, the axes array is 1-d f, axarr = plt.subplots(1, 2)
for vi2 in range(vi1 + 1, length): highOrderFunction = rnd(numLabels, numLabels, numLabels) * beta gm.addFactor(gm.addFunction(highOrderFunction), [vi0, vi1, vi2]) else: raise RuntimeError("wrong model type") # inference parameter if ilp: ad3Solver = 'ad3_ilp' else: ad3Solver = 'ad3_lp' param = opengm.InfParam(solverType=ad3Solver, adaptEta=True, steps=1000, residualThreshold=1e-6, verbose=1) inf = opengm.inference.Ad3(gm, parameter=param) # do inference inf.infer() # get results arg = inf.arg() posteriors = inf.posteriors() # grid or chain ? if model == '2OrderSubmodublarGrid': #print as grind print posteriors print arg.reshape([length, length]) else:
def bp_step(G, nodes, edges , n_annots, n_names, lookup_annot_idx): gm = build_factor_graph(G, nodes, edges , n_annots, n_names, lookup_annot_idx, use_unaries=False, edge_probs=None, operator='multiplier') with ut.Indenter('[BELIEF]'): ut.cprint('Brute Force Labels: (probability maximization)', 'blue') infr = opengm.inference.Bruteforce(gm, accumulator='maximizer') infr.infer() labels = rectify_labels(G, infr.arg()) print(pd.DataFrame(labels, columns=['nid'], index=pd.Series(nodes)).T) print('value = %r' % (infr.value(),)) lpb_parmas = opengm.InfParam(damping=0.00, steps=10000, # convergenceBound=0, isAcyclic=False) # http://www.andres.sc/publications/opengm-2.0.2-beta-manual.pdf # I believe multiplier + integrator = marginalization # Manual says multiplier + adder = marginalization # Manual says multiplier + maximizer = probability maximization # infr = opengm.inference.TreeReweightedBp( LBP_algorithm = opengm.inference.BeliefPropagation # LBP_algorithm = opengm.inference.TreeReweightedBp ut.cprint('Belief Propogation (maximization)', 'blue') infr = LBP_algorithm( gm, parameter=lpb_parmas, accumulator='maximizer' ) infr.infer() labels = rectify_labels(G, infr.arg()) pairwise_factor_idxs = gm.pairwise_factor_idxs factor_marginals = infr.factorMarginals(pairwise_factor_idxs) # print('factor_marginals =\n%r' % (factor_marginals,)) edge_marginals_same_diff_ = [(np.diag(f).sum(), f[~np.eye(f.shape[0], dtype=bool)].sum()) for f in factor_marginals] edge_marginals_same_diff_ = np.array(edge_marginals_same_diff_) edge_marginals_same_diff = edge_marginals_same_diff_.copy() edge_marginals_same_diff /= edge_marginals_same_diff.sum(axis=1, keepdims=True) print('Unnormalized Edge Marginals:') print(pd.DataFrame(edge_marginals_same_diff, columns=['same', 'diff'], index=pd.Series(edges))) # print('Edge marginals after Belief Propogation') # print(pd.DataFrame(edge_marginals_same_diff, columns=['same', 'diff'], index=pd.Series(edges))) print('Labels:') print(pd.DataFrame(labels, columns=['nid'], index=pd.Series(nodes)).T) print('value = %r' % (infr.value(),)) ut.cprint('Belief Propogation (marginalization)', 'blue') infr = LBP_algorithm( gm, parameter=lpb_parmas, accumulator='integrator' ) infr.infer() labels = rectify_labels(G, infr.arg()) pairwise_factor_idxs = gm.pairwise_factor_idxs factor_marginals = infr.factorMarginals(pairwise_factor_idxs) # print('factor_marginals =\n%r' % (factor_marginals,)) edge_marginals_same_diff_ = [(np.diag(f).sum(), f[~np.eye(f.shape[0], dtype=bool)].sum()) for f in factor_marginals] edge_marginals_same_diff_ = np.array(edge_marginals_same_diff_) edge_marginals_same_diff = edge_marginals_same_diff_.copy() edge_marginals_same_diff /= edge_marginals_same_diff.sum(axis=1, keepdims=True) print('Unnormalized Edge Marginals:') print(pd.DataFrame(edge_marginals_same_diff, columns=['same', 'diff'], index=pd.Series(edges))) # print('Edge marginals after Belief Propogation') # print(pd.DataFrame(edge_marginals_same_diff, columns=['same', 'diff'], index=pd.Series(edges))) print('Labels:') print(pd.DataFrame(labels, columns=['nid'], index=pd.Series(nodes)).T) print('value = %r' % (infr.value(),)) # import plottool_ibeis as pt # viz_factor_graph(gm) # # _ = pt.show_nx(G) # print("SHOW") # pt.plt.show() # marginals = infr.marginals(annot_idxs) # print('node marginals are') # print(pd.DataFrame(marginals, index=pd.Series(nodes))) return edge_marginals_same_diff
def test_graphcut_maxflow_ibfs(self): if opengm.configuration.withMaxflowIbfs : solverClass = opengm.inference.GraphCut params=[ opengm.InfParam(minStCut='ibfs') ] genericSolverCheck(solverClass, params=params,gms=[self.gridGm, self.chainGm], semiRings=self.minSum,testPythonVisitor=False)
def test_partition_move(self): solverClass = opengm.inference.PartitionMove params = [None, opengm.InfParam()] genericSolverCheck(solverClass, params=params, gms=[self.mcGm], semiRings=self.minSum,testPythonVisitor=False)
def test_alpha_expansion_fusion(self): if opengm.configuration.withQpbo: solverClass = opengm.inference.AlphaExpansionFusion params = [None, opengm.InfParam(steps=10)] genericSolverCheck(solverClass, params=params, gms=[ self.gridGm3, self.chainGm3], semiRings=self.minSum)
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)
def segmentation_example(): import vigra import opengm import sklearn import sklearn.mixture import numpy as np from vigra import graphs import matplotlib as mpl import plottool as pt pt.ensure_pylab_qt4() # load image and convert to LAB img_fpath = str(ut.grab_test_imgpath(str('lena.png'))) img = vigra.impex.readImage(img_fpath) imgLab = vigra.colors.transform_RGB2Lab(img) superpixelDiameter = 15 # super-pixel size slicWeight = 15.0 # SLIC color - spatial weight labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight, superpixelDiameter) labels = vigra.analysis.labelImage(labels) - 1 # get 2D grid graph and RAG gridGraph = graphs.gridGraph(img.shape[0:2]) rag = graphs.regionAdjacencyGraph(gridGraph, labels) # Node Features nodeFeatures = rag.accumulateNodeFeatures(imgLab) nodeFeaturesImg = rag.projectNodeFeaturesToGridGraph(nodeFeatures) nodeFeaturesImg = vigra.taggedView(nodeFeaturesImg, "xyc") nodeFeaturesImgRgb = vigra.colors.transform_Lab2RGB(nodeFeaturesImg) nCluster = 5 g = sklearn.mixture.GMM(n_components=nCluster) g.fit(nodeFeatures[:, :]) clusterProb = g.predict_proba(nodeFeatures) # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Irregular%20Factor%20Graphs.ipynb # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Hard%20and%20Soft%20Constraints.ipynb clusterProbImg = rag.projectNodeFeaturesToGridGraph( clusterProb.astype(np.float32)) clusterProbImg = vigra.taggedView(clusterProbImg, "xyc") ndim_data = clusterProbImg.reshape((-1, nCluster)) pca = sklearn.decomposition.PCA(n_components=3) print(ndim_data.shape) pca.fit(ndim_data) print(ut.repr2(pca.explained_variance_ratio_, precision=2)) oldshape = (clusterProbImg.shape[0:2] + (-1, )) clusterProgImg3 = pca.transform(ndim_data).reshape(oldshape) print(clusterProgImg3.shape) # graphical model with as many variables # as superpixels, each has 3 states gm = opengm.gm(np.ones(rag.nodeNum, dtype=opengm.label_type) * nCluster) # convert probabilites to energies probs = np.clip(clusterProb, 0.00001, 0.99999) costs = -1.0 * np.log(probs) # add ALL unaries AT ONCE fids = gm.addFunctions(costs) gm.addFactors(fids, np.arange(rag.nodeNum)) # add a potts function beta = 40.0 # strength of potts regularizer regularizer = opengm.pottsFunction([nCluster] * 2, 0.0, beta) fid = gm.addFunction(regularizer) # get variable indices of adjacent superpixels # - or "u" and "v" node id's for edges uvIds = rag.uvIds() uvIds = np.sort(uvIds, axis=1) # add all second order factors at once gm.addFactors(fid, uvIds) # get super-pixels with slic on LAB image Inf = opengm.inference.BeliefPropagation parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001) 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) pt.imshow(clusterProgImg3.swapaxes(0, 1)) # plot superpixels cmap = mpl.colors.ListedColormap(np.random.rand(nseg, 3)) pt.imshow(labels.swapaxes(0, 1).squeeze(), cmap=cmap) pt.imshow(nodeFeaturesImgRgb) cmap = mpl.colors.ListedColormap(np.random.rand(nCluster, 3)) for arg in callback.labels: arg = vigra.taggedView(arg, "n") argImg = rag.projectNodeFeaturesToGridGraph(arg.astype(np.uint32)) argImg = vigra.taggedView(argImg, "xy") # plot superpixels pt.imshow(argImg.swapaxes(0, 1).squeeze(), cmap=cmap)
#dataset = learning.DatasetWithGeneralizedHammingLoss(0) dataset.load(out_dir, out_prefix) nWeights = dataset.getNumberOfWeights() print 'nWeights', nWeights print 'nModels', dataset.getNumberOfModels() # for grid search learner lowerBounds = np.ones(nWeights) * -1.0 upperBounds = np.ones(nWeights) * 1.0 nTestPoints = np.ones(nWeights).astype('uint64') * 3 #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()) weights = dataset.getWeights() for w in range(nWeights): print weights[w] for i in range(dataset.getNumberOfModels()): print 'loss of', i, '=', dataset.getLoss(i, infCls=opengm.inference.Icm, parameter=opengm.InfParam()) print 'total loss =', dataset.getLoss(i, infCls=opengm.inference.Icm, parameter=opengm.InfParam())
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)
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) visitNth=1 if False : visitNth=1000 inf=opengm.inference.Icm(gm) elif False: inf=opengm.inference.TrwsExternal(gm) else: inf=opengm.inference.BeliefPropagation(gm,parameter=opengm.InfParam(damping=0.9)) class PyCallback(object): def __init__(self,shape,numLabels): self.shape=shape self.numLabels=numLabels self.cmap = matplotlib.colors.ListedColormap ( numpy.random.rand ( self.numLabels,3)) matplotlib.interactive(True) def begin(self,inference): print "begin of inference" def end(self,inference): print "end of inference" def visit(self,inference): print "visit" gm=inference.gm() labelVector=inference.arg()
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
fUnary=fUnary, fBinary=fBinary, addConstFeature=False) learner = learning.subgradientSSVM(dataset, learningRate=0.05, C=100, learningMode='batch',maxIterations=1000) #learner = learning.structMaxMarginLearner(dataset, 0.1, 0.001, 0) learner.learn(infCls=opengm.inference.LazyFlipper, parameter=opengm.InfParam(maxSubgraphSize=3)) # predict on test test for (rgbImg, gtImg, gm) in test_set : # infer for test image inf = opengm.inference.QpboExternal(gm) inf.infer() arg = inf.arg() arg = arg.reshape( numpy.squeeze(gtImg.shape)) vigra.segShow(rgbImg, arg+2) vigra.show()
import opengm import numpy chainLength = 5 numLabels = 100 numberOfStates = numpy.ones(chainLength, dtype=opengm.label_type) * numLabels gm = opengm.gm(numberOfStates, operator='adder') #add some random unaries for vi in range(chainLength): unaryFuction = numpy.random.random(numLabels) gm.addFactor(gm.addFunction(unaryFuction), [vi]) #add one 2.order function f = opengm.differenceFunction(shape=[numLabels] * 2, weight=0.1) print type(f), f fid = gm.addFunction(f) #add factors on a chain for vi in range(chainLength - 1): gm.addFactor(fid, [vi, vi + 1]) inf = opengm.inference.BeliefPropagation(gm, parameter=opengm.InfParam( steps=40, convergenceBound=0, damping=0.9)) inf.infer(inf.verboseVisitor()) print inf.arg()
addConstFeature=True) if True: dataset.save("simple_dataset", 'simple_') if True: dataset = learning.createDataset(0, numInstances=0) dataset.load("simple_dataset", 'simple_') if True: learner = learning.subgradientSSVM(dataset, learningRate=0.1, C=100, learningMode='batch', maxIterations=1000, averaging=-1) learner.learn(infCls=opengm.inference.TrwsExternal, parameter=opengm.InfParam()) else: learner = learning.maxLikelihoodLearner(dataset, temp=0.0000001) learner.learn() # predict on test test for (rgbImg, sp, gm) in test_set: # infer for test image inf = opengm.inference.TrwsExternal(gm) inf.infer() arg = inf.arg() + 1 assert sp.min() == 0 assert sp.max() == arg.shape[0] - 1 gg = vigra.graphs.gridGraph(rgbImg.shape[0:2])
import opengm import numpy #--------------------------------------------------------------- # MinSum with SelfFusion #--------------------------------------------------------------- numpy.random.seed(42) n = 100 nl = 100 unaries = numpy.random.rand(n, n, nl) potts = opengm.PottsFunction([nl, nl], 0.0, 0.5) gm = opengm.grid2d2Order(unaries=unaries, regularizer=potts) #--------------------------------------------------------------- # Minimize #--------------------------------------------------------------- #get an instance of the optimizer / inference-algorithm infParam = opengm.InfParam(generator='trws') inf = opengm.inference.SelfFusion(gm, parameter=infParam) # start inference (in this case verbose infernce) visitor = inf.verboseVisitor(printNth=1, multiline=True) inf.infer(visitor) # get the result states argmin = inf.arg() # print the argmin (on the grid) print argmin.reshape(n, n)