Example #1
0
 def latent(self, x, y, w):
     # augment unary potentials for latent states
     x_wide = np.repeat(x, self.n_states_per_label, axis=1)
     # do usual inference
     unary_params = w[:self.n_states]
     pairwise_flat = np.asarray(w[self.n_states:])
     pairwise_params = np.zeros((self.n_states, self.n_states))
     pairwise_params[np.tri(self.n_states, dtype=np.bool)] = pairwise_flat
     pairwise_params = (pairwise_params + pairwise_params.T
                        - np.diag(np.diag(pairwise_params)))
     unaries = (- 10 * unary_params * x_wide).astype(np.int32)
     # forbid h that is incompoatible with y
     # by modifying unary params
     other_states = (np.arange(self.n_states) / self.n_states_per_label !=
                     y[:, np.newaxis])
     unaries[other_states] = +1000000
     pairwise = (-10 * pairwise_params).astype(np.int32)
     h = alpha_expansion_graph(self.edges, unaries, pairwise)
     if (h / self.n_states_per_label != y).any():
         if np.any(w):
             print("inconsistent h and y")
             #tracer()
             h = y * self.n_states_per_label
         else:
             h = y * self.n_states_per_label
     return h
Example #2
0
def _inference_qpbo(x, unary_params, pairwise_params, edges):
    unaries = (-1000 * unary_params * x).astype(np.int32)
    unaries = unaries.reshape(-1, x.shape[-1])
    pairwise = (-1000 * pairwise_params).astype(np.int32)
    edges = edges.astype(np.int32)
    y = alpha_expansion_graph(edges, unaries, pairwise, random_seed=1)
    return y.reshape(x.shape[:2])
Example #3
0
 def latent(self, x, y, w):
     # augment unary potentials for latent states
     x_wide = np.repeat(x, self.n_states_per_label, axis=1)
     # do usual inference
     unary_params = w[:self.n_states]
     pairwise_flat = np.asarray(w[self.n_states:])
     pairwise_params = np.zeros((self.n_states, self.n_states))
     pairwise_params[np.tri(self.n_states, dtype=np.bool)] = pairwise_flat
     pairwise_params = pairwise_params + pairwise_params.T\
             - np.diag(np.diag(pairwise_params))
     unaries = (- 10 * unary_params * x_wide).astype(np.int32)
     # forbid h that is incompoatible with y
     # by modifying unary params
     other_states = (np.arange(self.n_states) / self.n_states_per_label !=
             y[:, np.newaxis])
     unaries[other_states] = +1000000
     pairwise = (-10 * pairwise_params).astype(np.int32)
     h = alpha_expansion_graph(self.edges, unaries, pairwise)
     if (h / self.n_states_per_label != y).any():
         if np.any(w):
             print("inconsistent h and y")
             #tracer()
             h = y * self.n_states_per_label
         else:
             h = y * self.n_states_per_label
     return h
def main():
    X, Y = make_dataset_big_checker(n_samples=1)
    size_y = Y[0].size
    shape_y = Y[0].shape
    n_labels = len(np.unique(Y))
    inds = np.arange(size_y).reshape(shape_y)
    horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()]
    vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()]
    downleft = np.c_[inds[:-1, :-1].ravel(), inds[1:, 1:].ravel()]
    downright = np.c_[inds[:-1, 1:].ravel(), inds[1:, :-1].ravel()]
    edges = np.vstack([horz, vert, downleft, downright]).astype(np.int32)
    graph = sparse.coo_matrix((np.ones(edges.shape[0]),
        (edges[:, 0], edges[:, 1])), shape=(size_y, size_y)).tocsr()
    graph = graph + graph.T

    crf = MultinomialFixedGraphCRFNoBias(n_states=n_labels, graph=graph)
    #crf = MultinomialGridCRF(n_labels=4)
    #clf = StructuredPerceptron(problem=crf, max_iter=50)
    clf = StructuredSVM(problem=crf, max_iter=20, C=1000000, verbose=20,
            check_constraints=True,
            positive_constraint=np.arange(crf.size_psi - 1))
    #clf = SubgradientStructuredSVM(problem=crf, max_iter=100, C=10000)
    X_flat = [x.reshape(-1, n_labels).copy("C") for x in X]
    Y_flat = [y.ravel() for y in Y]
    clf.fit(X_flat, Y_flat)
    #clf.fit(X, Y)
    Y_pred = clf.predict(X_flat)
    #Y_pred = clf.predict(X)

    i = 0
    loss = 0
    for x, y, y_pred in zip(X, Y, Y_pred):
        y_pred = y_pred.reshape(x.shape[:2])
        #loss += np.sum(y != y_pred)
        loss += np.sum(np.logical_xor(y, y_pred))
        if i > 4:
            continue
        fig, plots = plt.subplots(1, 4)
        plots[0].imshow(y, interpolation='nearest')
        plots[0].set_title("gt")
        pw_z = np.zeros((n_labels, n_labels), dtype=np.int32)
        un = np.ascontiguousarray(
                -1000 * x.reshape(-1, n_labels)).astype(np.int32)
        unaries = alpha_expansion_graph(edges, un, pw_z)
        plots[1].imshow(unaries.reshape(y.shape), interpolation='nearest')
        plots[1].set_title("unaries only")
        plots[2].imshow(y_pred, interpolation='nearest')
        plots[2].set_title("prediction")
        loss_augmented = clf.problem.loss_augmented_inference(
                x.reshape(-1, n_labels), y.ravel(), clf.w)
        loss_augmented = loss_augmented.reshape(y.shape)
        plots[3].imshow(loss_augmented, interpolation='nearest')
        plots[3].set_title("loss augmented")
        fig.savefig("data_%03d.png" % i)
        plt.close(fig)
        i += 1
    print("loss: %f" % loss)
Example #5
0
 def inference(self, x, w):
     if w.shape != (self.size_psi,):
         raise ValueError("Got w of wrong shape. Expected %s, got %s" %
                 (self.size_psi, w.shape))
     self.inference_calls += 1
     unary_params = w[:self.n_states]
     pairwise_flat = np.asarray(w[self.n_states:])
     pairwise_params = np.zeros((self.n_states, self.n_states))
     pairwise_params[np.tri(self.n_states, dtype=np.bool)] = pairwise_flat
     pairwise_params = pairwise_params + pairwise_params.T\
             - np.diag(np.diag(pairwise_params))
     unaries = (-1000 * unary_params * x).astype(np.int32)
     pairwise = (-1000 * pairwise_params).astype(np.int32)
     y = alpha_expansion_graph(self.edges, unaries, pairwise, random_seed=1)
     return y
Example #6
0
 def inference(self, x, w):
     if w.shape != (self.size_psi,):
         raise ValueError("Got w of wrong shape. Expected %s, got %s" %
                 (self.size_psi, w.shape))
     self.inference_calls += 1
     pairwise_flat = np.asarray(w[:-1])
     unary = w[-1]
     pairwise_params = np.zeros((self.n_states, self.n_states))
     pairwise_params[np.tri(self.n_states, k=-1, dtype=np.bool)] = pairwise_flat
     pairwise_params = pairwise_params + pairwise_params.T\
             - np.diag(np.diag(pairwise_params))
     unaries = (-1000 * unary * x).astype(np.int32)
     pairwise = (-1000 * pairwise_params).astype(np.int32)
     y = alpha_expansion_graph(self.edges, unaries, pairwise, random_seed=1)
     from pyqpbo import binary_graph
     y_ = binary_graph(self.edges, unaries, pairwise)
     if (y_ != y).any():
         tracer()
     return y
Example #7
0
def example_multinomial_checkerboard():
    # generate a checkerboard
    np.random.seed(1)
    x = np.zeros((12, 10, 3))
    x[::2, ::2, 0] = -2
    x[1::2, 1::2, 1] = -2
    x[:, :, 2] = -1
    x_noisy = x + np.random.normal(0, 1.0, size=x.shape)
    x_org = np.argmin(x, axis=2)

    # create unaries
    unaries = (10 * x_noisy).astype(np.int32)
    x_thresh = np.argmin(unaries, axis=2)
    # as we convert to int, we need to multipy to get sensible values
    # create potts pairwise
    pairwise = 100 * np.eye(3, dtype=np.int32)

    # do alpha expansion
    result_qpbo = alpha_expansion_grid(unaries, pairwise, n_iter=4)

    # use the gerneral graph algorithm
    # first, we construct the grid graph
    inds = np.arange(x_org.size).reshape(x_org.shape)
    horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()]
    vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()]
    edges = np.vstack([horz, vert]).astype(np.int32)
    result_qpbo_graph = alpha_expansion_graph(edges,
                                              unaries.reshape(-1, 3),
                                              pairwise,
                                              n_iter=4)

    # plot results
    plt.subplot(221, title="original")
    plt.imshow(x_org, interpolation='nearest')
    plt.subplot(222, title="thresholding result")
    plt.imshow(x_thresh, interpolation='nearest')
    plt.subplot(223, title="qpbo")
    plt.imshow(result_qpbo, interpolation='nearest')
    plt.subplot(224, title="qpbo graph")
    plt.imshow(result_qpbo_graph.reshape(x_org.shape), interpolation='nearest')

    plt.show()
Example #8
0
def example_multinomial_checkerboard():
    # generate a checkerboard
    np.random.seed(1)
    x = np.zeros((12, 10, 3))
    x[::2, ::2, 0] = -2
    x[1::2, 1::2, 1] = -2
    x[:, :, 2] = -1
    x_noisy = x + np.random.normal(0, 1.0, size=x.shape)
    x_org = np.argmin(x, axis=2)

    # create unaries
    unaries = (10 * x_noisy).astype(np.int32)
    x_thresh = np.argmin(unaries, axis=2)
    # as we convert to int, we need to multipy to get sensible values
    # create potts pairwise
    pairwise = 100 * np.eye(3, dtype=np.int32)

    # do alpha expansion
    result_qpbo = alpha_expansion_grid(unaries, pairwise, n_iter=4)

    # use the gerneral graph algorithm
    # first, we construct the grid graph
    inds = np.arange(x_org.size).reshape(x_org.shape)
    horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()]
    vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()]
    edges = np.vstack([horz, vert]).astype(np.int32)
    result_qpbo_graph = alpha_expansion_graph(edges, unaries.reshape(-1, 3),
                                              pairwise, n_iter=4)

    # plot results
    plt.subplot(221, title="original")
    plt.imshow(x_org, interpolation='nearest')
    plt.subplot(222, title="thresholding result")
    plt.imshow(x_thresh, interpolation='nearest')
    plt.subplot(223, title="qpbo")
    plt.imshow(result_qpbo, interpolation='nearest')
    plt.subplot(224, title="qpbo graph")
    plt.imshow(result_qpbo_graph.reshape(x_org.shape), interpolation='nearest')

    plt.show()
def main():
    X, Y = make_dataset_big_checker(n_samples=1)
    size_y = Y[0].size
    shape_y = Y[0].shape
    n_labels = len(np.unique(Y))
    inds = np.arange(size_y).reshape(shape_y)
    horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()]
    vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()]
    downleft = np.c_[inds[:-1, :-1].ravel(), inds[1:, 1:].ravel()]
    downright = np.c_[inds[:-1, 1:].ravel(), inds[1:, :-1].ravel()]
    edges = np.vstack([horz, vert, downleft, downright]).astype(np.int32)
    graph = sparse.coo_matrix(
        (np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
        shape=(size_y, size_y)).tocsr()
    graph = graph + graph.T

    crf = MultinomialFixedGraphCRFNoBias(n_states=n_labels, graph=graph)
    #crf = MultinomialGridCRF(n_labels=4)
    #clf = StructuredPerceptron(problem=crf, max_iter=50)
    clf = StructuredSVM(problem=crf,
                        max_iter=20,
                        C=1000000,
                        verbose=20,
                        check_constraints=True,
                        positive_constraint=np.arange(crf.size_psi - 1))
    #clf = SubgradientStructuredSVM(problem=crf, max_iter=100, C=10000)
    X_flat = [x.reshape(-1, n_labels).copy("C") for x in X]
    Y_flat = [y.ravel() for y in Y]
    clf.fit(X_flat, Y_flat)
    #clf.fit(X, Y)
    Y_pred = clf.predict(X_flat)
    #Y_pred = clf.predict(X)

    i = 0
    loss = 0
    for x, y, y_pred in zip(X, Y, Y_pred):
        y_pred = y_pred.reshape(x.shape[:2])
        #loss += np.sum(y != y_pred)
        loss += np.sum(np.logical_xor(y, y_pred))
        if i > 4:
            continue
        fig, plots = plt.subplots(1, 4)
        plots[0].imshow(y, interpolation='nearest')
        plots[0].set_title("gt")
        pw_z = np.zeros((n_labels, n_labels), dtype=np.int32)
        un = np.ascontiguousarray(-1000 * x.reshape(-1, n_labels)).astype(
            np.int32)
        unaries = alpha_expansion_graph(edges, un, pw_z)
        plots[1].imshow(unaries.reshape(y.shape), interpolation='nearest')
        plots[1].set_title("unaries only")
        plots[2].imshow(y_pred, interpolation='nearest')
        plots[2].set_title("prediction")
        loss_augmented = clf.problem.loss_augmented_inference(
            x.reshape(-1, n_labels), y.ravel(), clf.w)
        loss_augmented = loss_augmented.reshape(y.shape)
        plots[3].imshow(loss_augmented, interpolation='nearest')
        plots[3].set_title("loss augmented")
        fig.savefig("data_%03d.png" % i)
        plt.close(fig)
        i += 1
    print("loss: %f" % loss)