def inference_qpbo(unary_potentials, pairwise_potentials, edges, **kwargs): """Inference with PyQPBO backend. Used QPBO-I based move-making for undergenerating inference. Parameters ---------- unary_potentials : nd-array Unary potentials of energy function. pairwise_potentials : nd-array Pairwise potentials of energy function. edges : nd-array Edges of energy function. Returns ------- labels : nd-array Approximate (usually) MAP variable assignment. """ from pyqpbo import alpha_expansion_general_graph shape_org = unary_potentials.shape[:-1] n_states, pairwise_potentials = \ _validate_params(unary_potentials, pairwise_potentials, edges) unary_potentials = (-1000 * unary_potentials).copy().astype(np.int32) unary_potentials = unary_potentials.reshape(-1, n_states) pairwise_potentials = (-1000 * pairwise_potentials).copy().astype(np.int32) edges = edges.astype(np.int32).copy() y = alpha_expansion_general_graph(edges, unary_potentials, pairwise_potentials, random_seed=1) return y.reshape(shape_org)
def inference_qpbo(unary_potentials, pairwise_potentials, edges): from pyqpbo import alpha_expansion_general_graph shape_org = unary_potentials.shape[:-1] n_states, pairwise_potentials = \ _validate_params(unary_potentials, pairwise_potentials, edges) unary_potentials = (-1000 * unary_potentials).copy().astype(np.int32) unary_potentials = unary_potentials.reshape(-1, n_states) pairwise_potentials = (-1000 * pairwise_potentials).copy().astype(np.int32) edges = edges.astype(np.int32) y = alpha_expansion_general_graph(edges, unary_potentials, pairwise_potentials, random_seed=1) return y.reshape(shape_org)
def graphCut(img, centroids, unary): link = linkWeight(img) unary = (alpha * unary).astype('int32') pairwise = pairwiseCost(centroids) numIter = -1 edge, edgeCost = edgeAndWeight(img, pairwise) # unary2D = np.zeros((img.H,img.W,K)).astype('int32') # for i in range(0,img.H): # for j in range(0,img.W): # unary2D[i,j,:] = unary[i*img.W+j,:] img.labels = pyqpbo.alpha_expansion_general_graph(edge, unary, edgeCost) # img.labels = pyqpbo.alpha_expansion_grid(unary2D, pairwise) # img.labels = pygco.cut_simple(unary2D, pairwise, n_iter=1) # img.labels = pygco.cut_simple_vh(unary2D, pairwise, costV, costH) # img.labels = pygco.cut_from_graph(link, unary, pairwise, n_iter=numIter, algorithm='swap')
def inference_qpbo(unary_potentials, pairwise_potentials, edges, **kwargs): """Inference with PyQPBO backend. Used QPBO-I based move-making for undergenerating inference. Parameters ---------- unary_potentials : nd-array, shape (n_nodes, n_states) 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. Returns ------- labels : nd-array Approximate (usually) MAP variable assignment. """ from pyqpbo import alpha_expansion_general_graph shape_org = unary_potentials.shape[:-1] n_states, pairwise_potentials = \ _validate_params(unary_potentials, pairwise_potentials, edges) unary_potentials = (-1000 * unary_potentials).copy().astype(np.int32) unary_potentials = unary_potentials.reshape(-1, n_states) pairwise_potentials = (-1000 * pairwise_potentials).copy().astype(np.int32) edges = edges.astype(np.int32).copy() y = alpha_expansion_general_graph(edges, unary_potentials, pairwise_potentials, random_seed=1) return y.reshape(shape_org)