Пример #1
0
    def inference(self, x, w, relaxed=False, return_energy=False):
        self.inference_calls += 1
        # extract unary weights
        unary_params = self.get_unary_weights(w)
        # extract pairwise weights of shape n_edge_types x n_states x n_states
        pairwise_params = self.get_pairwise_weights(w)
        edges = _make_grid_edges(x, neighborhood=self.neighborhood,
                                 return_lists=True)
        n_edges = [len(e) for e in edges]
        # replicate pairwise weights for edges of certain type
        edge_weights = [np.repeat(pw[np.newaxis, :, :], n, axis=0)
                        for pw, n in zip(pairwise_params, n_edges)]
        edge_weights = np.vstack(edge_weights)
        edges = np.vstack(edges)

        #if self.inference_method == "qpbo":
            #return _inference_qpbo(x, unary_params, pairwise_params,
                                   #self.neighborhood)
        #elif self.inference_method == "dai":
            #return _inference_dai(x, unary_params, pairwise_params,
                                  #self.neighborhood)
        if self.inference_method == "lp":
            return _inference_lp(x, unary_params, edge_weights, edges, relaxed,
                                 return_energy=return_energy)
        #elif self.inference_method == "ad3":
            #return _inference_ad3(x, unary_params, pairwise_params,
                                  #self.neighborhood, relaxed)
        else:
            raise ValueError("inference_method must be 'qpbo' or 'dai', got %s"
                             % self.inference_method)
Пример #2
0
 def inference(self, x, w, relaxed=False):
     unary_params = self.get_unary_weights(w)
     pairwise_params = self.get_pairwise_weights(w)
     self.inference_calls += 1
     edges = _make_grid_edges(x, neighborhood=self.neighborhood)
     if self.inference_method == "qpbo":
         return _inference_qpbo(x, unary_params, pairwise_params,
                                edges)
     elif self.inference_method == "dai":
         return _inference_dai(x, unary_params, pairwise_params,
                               edges)
     elif self.inference_method == "lp":
         return _inference_lp(x, unary_params, pairwise_params,
                              edges, relaxed)
     elif self.inference_method == "ad3":
         return _inference_ad3(x, unary_params, pairwise_params,
                               edges, relaxed)
     else:
         raise ValueError("inference_method must be 'qpbo' or 'dai', got %s"
                          % self.inference_method)
Пример #3
0
    def psi(self, x, y):
        # x is unaries
        # y is a labeling
        if isinstance(y, tuple):
            # y can also be continuous (from lp)
            # in this case, it comes with accumulated edge marginals
            y, pw = y
            x_flat = x.reshape(-1, x.shape[-1])
            y_flat = y.reshape(-1, y.shape[-1])
            unaries_acc = np.sum(x_flat * y_flat, axis=0)
            # pw contains separate entries for all edges
            # we need to find out which belong to which kind
            edges = _make_grid_edges(x, neighborhood=self.neighborhood,
                                     return_lists=True)
            n_edges = [len(e) for e in edges]
            n_edges.insert(0, 0)
            edge_boundaries = np.cumsum(n_edges)
            pw_accumulated = []
            for i, j in zip(edge_boundaries[:-1], edge_boundaries[1:]):
                pw_accumulated.append(pw[i:j].sum(axis=0))
            pw = np.hstack(pw_accumulated)
        else:
            ## unary features:
            gx, gy = np.ogrid[:x.shape[0], :x.shape[1]]
            selected_unaries = x[gx, gy, y]
            unaries_acc = np.bincount(y.ravel(), selected_unaries.ravel(),
                                      minlength=self.n_states)

            ##accumulated pairwise
            #make one hot encoding
            labels = np.zeros((y.shape[0], y.shape[1], self.n_states),
                              dtype=np.int)
            labels[gx, gy, y] = 1
            pw = np.vstack(pairwise_grid_features(labels, self.neighborhood))

        feature = np.hstack([unaries_acc, pw.ravel()])
        return feature
Пример #4
0
 def init_latent(self, X, Y):
     edges = _make_grid_edges(X[0], neighborhood=self.neighborhood,
                              return_lists=True)
     return kmeans_init(X, Y, edges,
                        n_states_per_label=self.n_states_per_label)