def _inference_dai(x, unary_params, pairwise_params, edges): ## build graph n_states = x.shape[-1] log_unaries = unary_params * x.reshape(-1, n_states) max_entry = max(np.max(log_unaries), 1) unaries = np.exp(log_unaries / max_entry) y = mrf(unaries, edges, np.exp(pairwise_params / max_entry), alg='jt') y = y.reshape(x.shape[:2]) return y
def inference_dai(unary_potentials, pairwise_potentials, edges): from daimrf import mrf shape_org = unary_potentials.shape[:-1] n_states, pairwise_potentials = \ _validate_params(unary_potentials, pairwise_potentials, edges) n_states = unary_potentials.shape[-1] log_unaries = unary_potentials.reshape(-1, n_states) max_entry = max(np.max(log_unaries), 1) unaries = np.exp(log_unaries / max_entry) y = mrf(unaries, edges.astype(np.int64), np.exp(pairwise_potentials / max_entry), alg='jt') y = y.reshape(shape_org) return y
def inference_dai(unary_potentials, pairwise_potentials, edges, return_energy=False, alg='jt', **kwargs): """Inference with LibDAI backend. 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. alg : string, (default='jt') Inference algorithm to use. Defaults to Junction Tree. THIS WILL BLOW UP for loopy graphs. Returns ------- labels : nd-array Approximate (usually) MAP variable assignment. """ from daimrf import mrf shape_org = unary_potentials.shape[:-1] n_states, pairwise_potentials = \ _validate_params(unary_potentials, pairwise_potentials, edges) n_states = unary_potentials.shape[-1] log_unaries = unary_potentials.reshape(-1, n_states) max_entry = max(np.max(log_unaries), 1) unaries = np.exp(log_unaries / max_entry) y = mrf(unaries, edges.astype(np.int64), np.exp(pairwise_potentials / max_entry), alg=alg) y = y.reshape(shape_org) if return_energy: return y, compute_energy(unary_potentials, pairwise_potentials, edges, y) return y