Пример #1
0
def main():
    X, Y = generate_blocks(n_samples=1)
    # X, Y = generate_checker()
    # X, Y = generate_easy(n_samples=1)
    # X, Y = generate_big_checker()
    crf = BinaryGridCRF()
    # clf = StructuredPerceptron(problem=crf, max_iter=100)
    clf = StructuredSVM(problem=crf, max_iter=200, C=100, verbose=10, check_constraints=True)
    # clf = SubgradientStructuredSVM(problem=crf, max_iter=2000, C=100)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    i = 0
    loss = 0
    for x, y, y_pred in zip(X, Y, Y_pred):
        loss += np.sum(y != y_pred)
        fig, plots = plt.subplots(2, 2)
        plots[0, 0].set_title("x")
        plots[0, 0].imshow(x[:, :, 0], interpolation="nearest")
        plots[0, 1].set_title("y")
        plots[0, 1].imshow(y, interpolation="nearest")
        plots[1, 0].set_title("unaries")
        w_unaries = np.zeros(4)
        w_unaries[0] = 1.0
        y_unaries = crf.inference(x, w_unaries)
        plots[1, 0].imshow(y_unaries, interpolation="nearest")
        plots[1, 1].set_title("full crf")
        plots[1, 1].imshow(y_pred, interpolation="nearest")
        for plot in plots.ravel():
            plot.set_axis_off()
        fig.savefig("data_%03d.png" % i)
        plt.close(fig)
        i += 1
    print("loss: %f" % loss)
Пример #2
0
def test_binary_crf_exhaustive():
    # tests graph cut inference against brute force
    # on random data / weights
    np.random.seed(0)
    for i in xrange(50):
        x = np.random.uniform(-1, 1, size=(3, 3))
        x = np.dstack([-x, np.zeros_like(x)])
        crf = BinaryGridCRF()
        w = np.random.uniform(-1, 1, size=2)
        # check map inference
        y_hat = crf.inference(x, w)
        y_ex = exhausive_inference_binary(crf, x, w)
        print(y_hat)
        print(y_ex)
        print("++++++++++++++++++++++")
        assert_array_equal(y_hat, y_ex)
Пример #3
0
def test_binary_crf_exhaustive():
    # tests graph cut inference against brute force
    # on random data / weights
    np.random.seed(0)
    for i in xrange(50):
        x = np.random.uniform(-1, 1, size=(3, 3))
        x = np.dstack([-x, np.zeros_like(x)])
        crf = BinaryGridCRF()
        w = np.random.uniform(-1, 1, size=2)
        # check map inference
        y_hat = crf.inference(x, w)
        y_ex = exhausive_inference_binary(crf, x, w)
        print(y_hat)
        print(y_ex)
        print("++++++++++++++++++++++")
        assert_array_equal(y_hat, y_ex)
Пример #4
0
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)
Пример #5
0
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)
Пример #6
0
def main():
    X, Y = generate_blocks(n_samples=1)
    #X, Y = generate_checker()
    #X, Y = generate_easy(n_samples=1)
    #X, Y = generate_big_checker()
    crf = BinaryGridCRF()
    #clf = StructuredPerceptron(problem=crf, max_iter=100)
    clf = StructuredSVM(problem=crf,
                        max_iter=200,
                        C=100,
                        verbose=10,
                        check_constraints=True)
    #clf = SubgradientStructuredSVM(problem=crf, max_iter=2000, C=100)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    i = 0
    loss = 0
    for x, y, y_pred in zip(X, Y, Y_pred):
        loss += np.sum(y != y_pred)
        fig, plots = plt.subplots(2, 2)
        plots[0, 0].set_title("x")
        plots[0, 0].imshow(x[:, :, 0], interpolation='nearest')
        plots[0, 1].set_title("y")
        plots[0, 1].imshow(y, interpolation='nearest')
        plots[1, 0].set_title("unaries")
        w_unaries = np.zeros(4)
        w_unaries[0] = 1.
        y_unaries = crf.inference(x, w_unaries)
        plots[1, 0].imshow(y_unaries, interpolation='nearest')
        plots[1, 1].set_title("full crf")
        plots[1, 1].imshow(y_pred, interpolation='nearest')
        for plot in plots.ravel():
            plot.set_axis_off()
        fig.savefig("data_%03d.png" % i)
        plt.close(fig)
        i += 1
    print("loss: %f" % loss)