예제 #1
0
def example_multinomial():
    # generate dataset with three stripes
    np.random.seed(15)
    x = np.zeros((10, 12, 3))
    x[:, :4, 0] = -1
    x[:, 4:8, 1] = -1
    x[:, 8:, 2] = -1
    unaries = x + 1.5 * np.random.normal(size=x.shape)
    x = np.argmin(x, axis=2)
    unaries = (unaries * 10).astype(np.int32)
    x_thresh = np.argmin(unaries, axis=2)

    # potts potential
    #pairwise_potts = -2 * np.eye(3, dtype=np.int32)
    # potential that penalizes 0-1 and 1-2 less thann 0-2
    pairwise_1d = -15 * np.eye(3, dtype=np.int32) - 8
    pairwise_1d[-1, 0] = 5
    pairwise_1d[0, -1] = 5
    print(pairwise_1d)
    result_1d = alpha_expansion_grid(unaries, pairwise_1d)
    result_gco = cut_simple(unaries, pairwise_1d)
    plt.subplot(141, title="original")
    plt.imshow(x, interpolation="nearest")
    plt.subplot(142, title="thresholded unaries")
    plt.imshow(x_thresh, interpolation="nearest")
    plt.subplot(143, title="potts potentials")
    plt.imshow(result_gco, interpolation="nearest")
    plt.subplot(144, title="1d topology potentials")
    plt.imshow(result_1d, interpolation="nearest")
    plt.show()
예제 #2
0
파일: example.py 프로젝트: fgregg/pyqpbo
def example_multinomial():
    # generate dataset with three stripes
    np.random.seed(15)
    x = np.zeros((10, 12, 3))
    x[:, :4, 0] = -1
    x[:, 4:8, 1] = -1
    x[:, 8:, 2] = -1
    unaries = x + 1.5 * np.random.normal(size=x.shape)
    x = np.argmin(x, axis=2)
    unaries = (unaries * 10).astype(np.int32)
    x_thresh = np.argmin(unaries, axis=2)

    # potts potential
    #pairwise_potts = -2 * np.eye(3, dtype=np.int32)
    # potential that penalizes 0-1 and 1-2 less thann 0-2
    pairwise_1d = -15 * np.eye(3, dtype=np.int32) - 8
    pairwise_1d[-1, 0] = 5
    pairwise_1d[0, -1] = 5
    print(pairwise_1d)
    result_1d = alpha_expansion_grid(unaries, pairwise_1d)
    result_gco = cut_simple(unaries, pairwise_1d)
    plt.subplot(141, title="original")
    plt.imshow(x, interpolation="nearest")
    plt.subplot(142, title="thresholded unaries")
    plt.imshow(x_thresh, interpolation="nearest")
    plt.subplot(143, title="potts potentials")
    plt.imshow(result_gco, interpolation="nearest")
    plt.subplot(144, title="1d topology potentials")
    plt.imshow(result_1d, interpolation="nearest")
    plt.show()
예제 #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_grid(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
예제 #4
0
파일: crf.py 프로젝트: cshen/pystruct
 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))
     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_grid(unaries, pairwise)
     return y
예제 #5
0
def potts_example():
    img1 = np.asarray(Image.open("scene1.row3.col1.ppm")) / 255.
    img2 = np.asarray(Image.open("scene1.row3.col2.ppm")) / 255.
    unaries = (stereo_unaries(img1, img2) * 100).astype(np.int32)
    n_disps = unaries.shape[2]

    #newshape = unaries.shape[:2]
    potts_cut = alpha_expansion_grid(unaries, -5 * np.eye(n_disps,
        dtype=np.int32), n_iter=5)
    x, y = np.ogrid[:n_disps, :n_disps]
    one_d_topology = np.abs(x - y).astype(np.int32).copy("C")
    one_d_cut = alpha_expansion_grid(unaries, 5 * one_d_topology, n_iter=5)
    plt.subplot(231, xticks=(), yticks=())
    plt.imshow(img1)
    plt.subplot(232, xticks=(), yticks=())
    plt.imshow(img1)
    plt.subplot(233, xticks=(), yticks=())
    plt.imshow(np.argmax(unaries, axis=2), interpolation='nearest')
    plt.subplot(223, xticks=(), yticks=())
    plt.imshow(potts_cut, interpolation='nearest')
    plt.subplot(224, xticks=(), yticks=())
    plt.imshow(one_d_cut, interpolation='nearest')
    plt.show()
예제 #6
0
def potts_example():
    img1 = np.asarray(Image.open("scene1.row3.col1.ppm")) / 255.
    img2 = np.asarray(Image.open("scene1.row3.col2.ppm")) / 255.
    unaries = (stereo_unaries(img1, img2) * 100).astype(np.int32)
    n_disps = unaries.shape[2]

    #newshape = unaries.shape[:2]
    potts_cut = alpha_expansion_grid(unaries,
                                     -5 * np.eye(n_disps, dtype=np.int32),
                                     n_iter=5)
    x, y = np.ogrid[:n_disps, :n_disps]
    one_d_topology = np.abs(x - y).astype(np.int32).copy("C")
    one_d_cut = alpha_expansion_grid(unaries, 5 * one_d_topology, n_iter=5)
    plt.subplot(231, xticks=(), yticks=())
    plt.imshow(img1)
    plt.subplot(232, xticks=(), yticks=())
    plt.imshow(img1)
    plt.subplot(233, xticks=(), yticks=())
    plt.imshow(np.argmax(unaries, axis=2), interpolation='nearest')
    plt.subplot(223, xticks=(), yticks=())
    plt.imshow(potts_cut, interpolation='nearest')
    plt.subplot(224, xticks=(), yticks=())
    plt.imshow(one_d_cut, interpolation='nearest')
    plt.show()
예제 #7
0
def test_multinomial_grid_unaries():
    # test handling on unaries for multinomial grid CRFs
    # on multinomial datasets
    for ds in toy_datasets.multinomial:
        X, Y = ds(n_samples=1)
        x, y = X[0], Y[0]
        n_labels = len(np.unique(Y))
        crf = MultinomialGridCRF(n_states=n_labels)
        w_unaries_only = np.zeros(crf.size_psi)
        w_unaries_only[:n_labels] = 1.
        # test that inference with unaries only is the
        # same as argmax
        inf_unaries = crf.inference(x, w_unaries_only)

        pw_z = np.zeros((n_labels, n_labels), dtype=np.int32)
        un = np.ascontiguousarray(-1000 * x).astype(np.int32)
        unaries = alpha_expansion_grid(un, pw_z)
        assert_array_equal(inf_unaries, unaries)
        assert_array_equal(inf_unaries, np.argmax(x, axis=2))
예제 #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()
예제 #9
0
파일: test_crfs.py 프로젝트: cshen/pystruct
def test_multinomial_grid_unaries():
    # test handling on unaries for multinomial grid CRFs
    # on multinomial datasets
    for ds in toy_datasets.multinomial:
        X, Y = ds(n_samples=1)
        x, y = X[0], Y[0]
        n_labels = len(np.unique(Y))
        crf = MultinomialGridCRF(n_states=n_labels)
        w_unaries_only = np.zeros(crf.size_psi)
        w_unaries_only[:n_labels] = 1.
        # test that inference with unaries only is the
        # same as argmax
        inf_unaries = crf.inference(x, w_unaries_only)

        pw_z = np.zeros((n_labels, n_labels), dtype=np.int32)
        un = np.ascontiguousarray(
                -1000 * x).astype(np.int32)
        unaries = alpha_expansion_grid(un, pw_z)
        assert_array_equal(inf_unaries, unaries)
        assert_array_equal(inf_unaries, np.argmax(x, axis=2))
예제 #10
0
파일: example.py 프로젝트: fgregg/pyqpbo
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()