def loss_augmented_inference(self, x, y, w, relaxed=False, return_energy=False): # we do not support relaxed inference yet relaxed = False return_energy = False self.inference_calls += 1 self._check_size_w(w) unary_potentials = self._get_unary_potentials(x, w) pairwise_potentials = self._get_pairwise_potentials(x, w) edges = self._get_edges(x) if y.full_labeled: loss_augment_weighted_unaries(unary_potentials, y.full, y.weights.astype(np.double)) h = inference_dispatch(unary_potentials, pairwise_potentials, edges, self.inference_method, relaxed=relaxed, return_energy=return_energy, n_iter=self.n_iter) return Label(h, None, y.weights, True) else: # this is weak labeled example # use pygco with label costs label_cost = np.zeros(self.n_states) c = np.sum(y.weights) / float(self.n_states) for label in y.weak: label_cost[label] = c for label in range(0, self.n_states): if label not in y.weak: unary_potentials[:, label] += y.weights edges = edges.copy().astype(np.int32) pairwise_potentials = (1000 * pairwise_potentials).copy().astype( np.int32) pairwise_cost = {} for i in range(0, edges.shape[0]): cost = pairwise_potentials[i, 0, 0] if cost >= 0: pairwise_cost[(edges[i, 0], edges[i, 1])] = cost from pygco import cut_from_graph_gen_potts shape_org = unary_potentials.shape[:-1] unary_potentials = (-1000 * unary_potentials).copy().astype( np.int32) unary_potentials = unary_potentials.reshape(-1, self.n_states) label_cost = (1000 * label_cost).copy().astype(np.int32) h = cut_from_graph_gen_potts(unary_potentials, pairwise_cost, label_cost=label_cost, n_iter=self.n_iter) h = h[0].reshape(shape_org) return Label(h, None, y.weights, False)
def latent(self, x, y, w): unary_potentials = self._get_unary_potentials(x, w) # forbid h that is incompoatible with y # by modifying unary params other_states = list(self.all_states - set(y)) unary_potentials[:, other_states] = -1e6 pairwise_potentials = self._get_pairwise_potentials(x, w) edges = self._get_edges(x) h = inference_dispatch(unary_potentials, pairwise_potentials, edges, self.inference_method, relaxed=False) return h
def latent(self, x, y, w): if y.full_labeled: return y unary_potentials = self._get_unary_potentials(x, w) # forbid h that is incompoatible with y # by modifying unary params other_states = list(self.all_states - set(y.weak)) unary_potentials[:, other_states] = -1000 pairwise_potentials = self._get_pairwise_potentials(x, w) edges = self._get_edges(x) h = inference_dispatch(unary_potentials, pairwise_potentials, edges, self.inference_method, relaxed=False, n_iter=self.n_iter) return Label(h, y.weak, y.weights, False)
def latent(self, x, y, w): unary_potentials = self._get_unary_potentials(x, w) # forbid h that is incompoatible with y # by modifying unary params other_states = list(self.all_states - set(y)) unary_potentials[:, other_states] = -1e6 pairwise_potentials = self._get_pairwise_potentials(x, w) edges = self._get_edges(x) h = inference_dispatch(unary_potentials, pairwise_potentials, edges, self.inference_method, relaxed=False) return h
def inference(self, x, w, relaxed=False, return_energy=False): """Inference for x using parameters w. Finds (approximately) armin_y np.dot(w, psi(x, y)) using self.inference_method. Parameters ---------- x : tuple Instance of a graph with unary & pairwise potentials. x=(unaries, edges, pairwise) unaries are an nd-array of shape (n_nodes, n_states), edges are an nd-array of shape (n_edges, 2) pairwise are an nd-array of shape (n_edges, n_states, n_states) w : ndarray, shape=(size_psi,) Parameters for the CRF energy function. relaxed : bool, default=False We do not support it yet. return_energy : bool, default=False Whether to return the energy of the solution (x, y) that was found. Returns ------- y_pred : ndarray By default an integer ndarray of shape=(width, height) of variable assignments for x is returned. """ # we do not support relaxed inference yet relaxed = False return_energy = False self._check_size_w(w) self.inference_calls += 1 unary_potentials = self._get_unary_potentials(x, w) pairwise_potentials = self._get_pairwise_potentials(x, w) edges = self._get_edges(x) h = inference_dispatch(unary_potentials, pairwise_potentials, edges, self.inference_method, relaxed=relaxed, return_energy=return_energy, n_iter=self.n_iter) return Label(h, None, None, True)