def example_checkerboard(): # generate a checkerboard x = np.ones((10, 12)) x[::2, ::2] = -1 x[1::2, 1::2] = -1 x_noisy = x + np.random.normal(0, 1.5, size=x.shape) x_thresh = x_noisy > .0 # create unaries unaries = x_noisy # as we convert to int, we need to multipy to get sensible values unaries = (10 * np.dstack([unaries, -unaries]).copy("C")).astype(np.int32) # create potts pairwise pairwise = 100 * np.eye(2, dtype=np.int32) # do simple cut result_qpbo = binary_grid(unaries, pairwise) # make non-labeled zero (instead of negative) result_qpbo_vis = result_qpbo.copy() result_qpbo_vis[result_qpbo < 0] = 0 result_qpbo_vis[result_qpbo == 0] = -1 # plot results plt.subplot(231, title="original") plt.imshow(x, interpolation='nearest') plt.subplot(232, title="noisy version") plt.imshow(x_noisy, interpolation='nearest') plt.subplot(233, title="rounded to integers") plt.imshow(unaries[:, :, 0], interpolation='nearest') plt.subplot(234, title="thresholding result") plt.imshow(x_thresh, interpolation='nearest') plt.subplot(235, title="qpbo") plt.imshow(result_qpbo_vis, interpolation='nearest') plt.show()
def test_binary_grid_unaries(): # test handling on unaries for binary grid CRFs for ds in toy.binary: X, Y = ds(n_samples=1) x, y = X[0], Y[0] crf = GridCRF(inference_method="lp") w_unaries_only = np.zeros(5) w_unaries_only[:2] = 1.0 # test that inference with unaries only is the # same as argmax inf_unaries = crf.inference(x, w_unaries_only) pw_z = np.zeros((2, 2), dtype=np.int32) un = np.ascontiguousarray(-1000 * x).astype(np.int32) unaries = binary_grid(un, pw_z) assert_array_equal(inf_unaries, unaries) assert_array_equal(inf_unaries, np.argmax(x, axis=2)) try: assert np.mean(inf_unaries == y) > 0.5 except: print(ds) # check that the right thing happens on noise-free data X, Y = ds(n_samples=1, noise=0) inf_unaries = crf.inference(X[0], w_unaries_only) assert_array_equal(inf_unaries, Y[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_param = w[0] pairwise_params = np.array([[0, w[1]], [w[1], 0]]) if (x[:, :, 1] != 0).any(): raise ValueError("For simplicty, in binary CRFS," "all entries in the second feature should be 0.") unaries = - 1000 * unary_param * x.copy() pairwise = -1000 * pairwise_params y = binary_grid(unaries.astype(np.int32), pairwise.astype(np.int32)) return y
def test_multinomial_grid_binary(): # test handling on unaries for multinomial grid CRFs # on binary datasets for ds in toy_datasets.binary: X, Y = ds(n_samples=1) x, y = X[0], Y[0] crf = MultinomialGridCRF() w_unaries_only = np.zeros(5) w_unaries_only[:2] = 1. # test that inference with unaries only is the # same as argmax inf_unaries = crf.inference(x, w_unaries_only) pw_z = np.zeros((2, 2), dtype=np.int32) un = np.ascontiguousarray(-1000 * x).astype(np.int32) unaries = binary_grid(un, pw_z) assert_array_equal(inf_unaries, unaries) assert_array_equal(inf_unaries, np.argmax(x, axis=2))
def test_multinomial_grid_binary(): # test handling on unaries for multinomial grid CRFs # on binary datasets for ds in toy_datasets.binary: X, Y = ds(n_samples=1) x, y = X[0], Y[0] crf = MultinomialGridCRF() w_unaries_only = np.zeros(5) w_unaries_only[:2] = 1. # test that inference with unaries only is the # same as argmax inf_unaries = crf.inference(x, w_unaries_only) pw_z = np.zeros((2, 2), dtype=np.int32) un = np.ascontiguousarray( -1000 * x).astype(np.int32) unaries = binary_grid(un, pw_z) assert_array_equal(inf_unaries, unaries) assert_array_equal(inf_unaries, np.argmax(x, axis=2))
def example_binary(): # generate trivial data x = np.ones((10, 12)) x[:, 6:] = -1 x_noisy = x + np.random.normal(0, 0.8, size=x.shape) x_thresh = x_noisy > .0 # create unaries unaries = x_noisy # as we convert to int, we need to multipy to get sensible values unaries = (10 * np.dstack([unaries, -unaries]).copy("C")).astype(np.int32) # create potts pairwise pairwise = -10 * np.eye(2, dtype=np.int32) # do simple cut result_qpbo = binary_grid(unaries, pairwise) #result_qpbo = alpha_expansion_grid(unaries, pairwise) #result_gc = cut_simple(unaries, pairwise) # use the gerneral graph algorithm # first, we construct the grid graph inds = np.arange(x.size).reshape(x.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) # we flatten the unaries result_graph = binary_graph(edges, unaries.reshape(-1, 2), pairwise) # plot results plt.subplot(231, title="original") plt.imshow(x, interpolation='nearest') plt.subplot(232, title="noisy version") plt.imshow(x_noisy, interpolation='nearest') plt.subplot(233, title="rounded to integers") plt.imshow(unaries[:, :, 0], interpolation='nearest') plt.subplot(234, title="thresholding result") plt.imshow(x_thresh, interpolation='nearest') plt.subplot(235, title="qpbo") plt.imshow(result_qpbo, interpolation='nearest') plt.subplot(236, title="qpbo graph") plt.imshow(result_graph.reshape(x.shape), interpolation='nearest') plt.show()
def test_binary_grid_unaries(): # test handling on unaries for binary grid CRFs for ds in toy_datasets.binary: X, Y = ds(n_samples=1) x, y = X[0], Y[0] crf = BinaryGridCRF() w_unaries_only = np.zeros(2) w_unaries_only[0] = 1. # test that inference with unaries only is the # same as argmax inf_unaries = crf.inference(x, w_unaries_only) pw_z = np.zeros((2, 2), dtype=np.int32) un = np.ascontiguousarray(-1000 * x).astype(np.int32) unaries = binary_grid(un, pw_z) assert_array_equal(inf_unaries, unaries) assert_array_equal(inf_unaries, np.argmax(x, axis=2)) try: assert (np.mean(inf_unaries == y) > 0.5) except: print(ds)
def test_binary_grid_unaries(): # test handling on unaries for binary grid CRFs for ds in toy_datasets.binary: X, Y = ds(n_samples=1) x, y = X[0], Y[0] crf = BinaryGridCRF() w_unaries_only = np.zeros(2) w_unaries_only[0] = 1. # test that inference with unaries only is the # same as argmax inf_unaries = crf.inference(x, w_unaries_only) pw_z = np.zeros((2, 2), dtype=np.int32) un = np.ascontiguousarray( -1000 * x).astype(np.int32) unaries = binary_grid(un, pw_z) assert_array_equal(inf_unaries, unaries) assert_array_equal(inf_unaries, np.argmax(x, axis=2)) try: assert(np.mean(inf_unaries == y) > 0.5) except: print(ds)