Beispiel #1
0
def test_binary_grid_unaries():
    # test handling on unaries for binary grid CRFs
    for ds in binary:
        X, Y = ds(n_samples=1)
        x, y = X[0], Y[0]
        for inference_method in get_installed():
            # dai is to expensive
            crf = GridCRF(inference_method=inference_method)
            crf.initialize(X, Y)
            w_unaries_only = np.zeros(7)
            w_unaries_only[:4] = np.eye(2).ravel()
            # test that inference with unaries only is the
            # same as argmax
            inf_unaries = crf.inference(x, w_unaries_only)

            assert_array_equal(
                inf_unaries, np.argmax(x, axis=2),
                "Wrong unary inference for %s" % inference_method)
            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],
                               "Wrong unary result for %s" % inference_method)
Beispiel #2
0
def test_binary_blocks():
    X, Y = generate_blocks(n_samples=10)
    crf = GridCRF()
    clf = StructuredPerceptron(model=crf, max_iter=40)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
def test_multinomial_blocks_frankwolfe():
    X, Y = generate_blocks_multinomial(n_samples=10, noise=0.5, seed=0)
    crf = GridCRF(inference_method='qpbo')
    clf = FrankWolfeSSVM(model=crf, C=1, max_iter=50, verbose=3)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Beispiel #4
0
def test_blocks_multinomial_crf():
    X, Y = generate_blocks_multinomial(n_samples=1, size_x=9, seed=0)
    x, y = X[0], Y[0]
    w = np.array([
        1.,
        0.,
        0.,  # unaryA
        0.,
        1.,
        0.,
        0.,
        0.,
        1.,
        .4,  # pairwise
        -.3,
        .3,
        -.5,
        -.1,
        .3
    ])
    for inference_method in get_installed():
        crf = GridCRF(inference_method=inference_method)
        crf.initialize(X, Y)
        y_hat = crf.inference(x, w)
        assert_array_equal(y, y_hat)
Beispiel #5
0
def test_multinomial_blocks():
    X, Y = generate_blocks_multinomial(n_samples=10, noise=0.3, seed=0)
    crf = GridCRF(n_states=X.shape[-1])
    clf = StructuredPerceptron(model=crf, max_iter=10)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Beispiel #6
0
def test_switch_to_ad3():
    # test if switching between qpbo and ad3 works

    if not get_installed(['qpbo']) or not get_installed(['ad3']):
        return
    X, Y = generate_blocks_multinomial(n_samples=5, noise=1.5, seed=0)
    crf = GridCRF(n_states=3, inference_method='qpbo')

    ssvm = NSlackSSVM(crf, max_iter=10000)

    ssvm_with_switch = NSlackSSVM(crf, max_iter=10000, switch_to=('ad3'))
    ssvm.fit(X, Y)
    ssvm_with_switch.fit(X, Y)
    assert_equal(ssvm_with_switch.model.inference_method, 'ad3')
    # we check that the dual is higher with ad3 inference
    # as it might use the relaxation, that is pretty much guraranteed
    assert_greater(ssvm_with_switch.objective_curve_[-1],
                   ssvm.objective_curve_[-1])

    # test that convergence also results in switch
    ssvm_with_switch = NSlackSSVM(crf,
                                  max_iter=10000,
                                  switch_to=('ad3'),
                                  tol=10)
    ssvm_with_switch.fit(X, Y)
    assert_equal(ssvm_with_switch.model.inference_method, 'ad3')
Beispiel #7
0
def test_binary_grid_unaries():
    # test handling on unaries for binary grid CRFs
    for ds in binary:
        X, Y = ds(n_samples=1)
        x, y = X[0], Y[0]
        for inference_method in get_installed():
            #NOTE: ad3+ fails because it requires a different data structure
            if inference_method == 'ad3+': continue            
            crf = GridCRF(inference_method=inference_method)
            crf.initialize(X, Y)
            w_unaries_only = np.zeros(7)
            w_unaries_only[:4] = np.eye(2).ravel()
            # test that inference with unaries only is the
            # same as argmax
            inf_unaries = crf.inference(x, w_unaries_only)

            assert_array_equal(inf_unaries, np.argmax(x, axis=2),
                               "Wrong unary inference for %s"
                               % inference_method)
            assert(np.mean(inf_unaries == y) > 0.5)

            # 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],
                               "Wrong unary result for %s"
                               % inference_method)
Beispiel #8
0
def test_one_slack_constraint_caching():
    # testing cutting plane ssvm on easy multinomial dataset
    X, Y = generate_blocks_multinomial(n_samples=10, noise=0.5, seed=0,
                                       size_x=9)
    n_labels = len(np.unique(Y))
    exact_inference = get_installed([('ad3', {'branch_and_bound': True}), "lp"])[0]
    crf = GridCRF(n_states=n_labels, inference_method=exact_inference)
    clf = OneSlackSSVM(model=crf, max_iter=150, C=1,
                       check_constraints=True, break_on_bad=True,
                       inference_cache=50, inactive_window=0)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
    assert_equal(len(clf.inference_cache_), len(X))
    # there should be 13 constraints, which are less than the 94 iterations
    # that are done
    # check that we didn't change the behavior of how we construct the cache
    constraints_per_sample = [len(cache) for cache in clf.inference_cache_]
    if exact_inference == "lp":
        assert_equal(len(clf.inference_cache_[0]), 18)
        assert_equal(np.max(constraints_per_sample), 18)
        assert_equal(np.min(constraints_per_sample), 18)
    else:
        assert_equal(len(clf.inference_cache_[0]), 13)
        assert_equal(np.max(constraints_per_sample), 20)
        assert_equal(np.min(constraints_per_sample), 11)
Beispiel #9
0
def test_one_slack_constraint_caching():
    #testing cutting plane ssvm on easy multinomial dataset
    X, Y = generate_blocks_multinomial(n_samples=10,
                                       noise=0.5,
                                       seed=0,
                                       size_x=9)
    n_labels = len(np.unique(Y))
    crf = GridCRF(n_states=n_labels, inference_method='lp')
    clf = OneSlackSSVM(model=crf,
                       max_iter=150,
                       C=1,
                       check_constraints=True,
                       break_on_bad=True,
                       inference_cache=50,
                       inactive_window=0,
                       verbose=10)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
    assert_equal(len(clf.inference_cache_), len(X))
    # there should be 11 constraints, which are less than the 94 iterations
    # that are done
    assert_equal(len(clf.inference_cache_[0]), 11)
    # check that we didn't change the behavior of how we construct the cache
    constraints_per_sample = [len(cache) for cache in clf.inference_cache_]
    assert_equal(np.max(constraints_per_sample), 19)
    assert_equal(np.min(constraints_per_sample), 11)
Beispiel #10
0
 def define_model(self):
     if self.models == 'GridCRF':
         from pystruct.models import GridCRF
         self.crf = GridCRF(
             neighborhood=self.models_parameters['neighborhood'],
             inference_method=self.models_parameters['inference']
         )
     if self.models == 'GraphCRF':
         self.prepare_data_to_graph_crf()
         logging.info('Class weight: ' + str([self.total_label_one_avg,1-self.total_label_one_avg]))
         from pystruct.models import GraphCRF
         self.crf = GraphCRF(
             inference_method=self.models_parameters['inference'],
             directed =  self.models_parameters['directed'],
             class_weight =  [self.total_label_one_avg,1-self.total_label_one_avg]
             # class_weight=[0.01, 0.99]
         )
     if self.models == 'EdgeFeatureGraphCRF':
         self.prepare_data_to_edge_feature_graph_crf()
         logging.info('Class weight: ' + str([self.total_label_one_avg, 1 - self.total_label_one_avg]))
         from pystruct.models import EdgeFeatureGraphCRF
         self.crf = EdgeFeatureGraphCRF(
             inference_method=self.models_parameters['inference'],
             # directed=self.models_parameters['directed'],
             class_weight=[self.total_label_one_avg, 1 - self.total_label_one_avg]
             # class_weight=[0.01, 0.99]
         )
     return
Beispiel #11
0
def test_multinomial_checker_cutting_plane():
    X, Y = generate_checker_multinomial(n_samples=10, noise=.1)
    n_labels = len(np.unique(Y))
    crf = GridCRF(n_states=n_labels, inference_method=inference_method)
    clf = NSlackSSVM(model=crf, max_iter=20, C=100000, check_constraints=True)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
def test_multinomial_checker_subgradient():
    X, Y = generate_checker_multinomial(n_samples=10, noise=0.4)
    n_labels = len(np.unique(Y))
    crf = GridCRF(n_states=n_labels, inference_method=inference_method)
    clf = SubgradientSSVM(model=crf, max_iter=50)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Beispiel #13
0
def test_binary_blocks_batches_n_slack():
    #testing cutting plane ssvm on easy binary dataset
    X, Y = generate_blocks(n_samples=5)
    crf = GridCRF(inference_method=inference_method)
    clf = NSlackSSVM(model=crf, max_iter=20, batch_size=1, C=100)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
def test_binary_blocks():
    #testing subgradient ssvm on easy binary dataset
    X, Y = generate_blocks(n_samples=5)
    crf = GridCRF(inference_method=inference_method)
    clf = SubgradientSSVM(model=crf)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
def test_binary_blocks_perceptron_online():
    #testing subgradient ssvm on easy binary dataset
    X, Y = generate_blocks(n_samples=10)
    inference_method = get_installed(['qpbo', 'ad3', 'lp'])[0]
    crf = GridCRF(inference_method=inference_method)
    clf = StructuredPerceptron(model=crf, max_iter=20)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Beispiel #16
0
def test_binary_blocks_cutting_plane():
    #testing cutting plane ssvm on easy binary dataset
    X, Y = generate_blocks(n_samples=5)
    crf = GridCRF(inference_method=inference_method)
    clf = NSlackSSVM(model=crf, max_iter=20, C=100,
                     check_constraints=True, break_on_bad=False)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
def test_multinomial_blocks_subgradient():
    #testing cutting plane ssvm on easy multinomial dataset
    X, Y = generate_blocks_multinomial(n_samples=10, noise=0.6, seed=1)
    n_labels = len(np.unique(Y))
    crf = GridCRF(n_states=n_labels, inference_method=inference_method)
    clf = SubgradientSSVM(model=crf, max_iter=50)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Beispiel #18
0
def test_multinomial_blocks_cutting_plane():
    #testing cutting plane ssvm on easy multinomial dataset
    X, Y = generate_blocks_multinomial(n_samples=40, noise=0.5, seed=0)
    n_labels = len(np.unique(Y))
    crf = GridCRF(n_states=n_labels, inference_method=inference_method)
    clf = NSlackSSVM(model=crf, max_iter=100, C=100, check_constraints=False,
                     batch_size=1)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Beispiel #19
0
def test_binary_ssvm_attractive_potentials():
    # test that submodular SSVM can learn the block dataset
    X, Y = generate_blocks(n_samples=10)
    crf = GridCRF(inference_method=inference_method)
    submodular_clf = NSlackSSVM(model=crf, max_iter=200, C=100,
                                check_constraints=True,
                                negativity_constraint=[5])
    submodular_clf.fit(X, Y)
    Y_pred = submodular_clf.predict(X)
    assert_array_equal(Y, Y_pred)
    assert_true(submodular_clf.w[5] < 0)
Beispiel #20
0
def test_multinomial_blocks_one_slack():
    #testing cutting plane ssvm on easy multinomial dataset
    X, Y = generate_blocks_multinomial(n_samples=10, noise=0.5, seed=0)
    n_labels = len(np.unique(Y))
    crf = GridCRF(n_states=n_labels, inference_method=inference_method)
    clf = OneSlackSSVM(model=crf, max_iter=150, C=1,
                       check_constraints=True, break_on_bad=True, tol=.1,
                       inference_cache=50)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Beispiel #21
0
def test_multinomial_blocks_frankwolfe():
    X, Y = generate_blocks_multinomial(n_samples=50, noise=0.5, seed=0)
    crf = GridCRF(inference_method='qpbo')
    clf = FrankWolfeSSVM(model=crf,
                         C=1,
                         line_search=True,
                         batch_mode=False,
                         check_dual_every=500)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Beispiel #22
0
def test_binary_blocks_crf_n8_lp():
    X, Y = generate_blocks(n_samples=1, noise=1)
    x, y = X[0], Y[0]
    w = np.array([1, 0,  # unary
                  0, 1,
                  1,     # pairwise
                  -1.4, 1])
    crf = GridCRF(neighborhood=8)
    crf.initialize(X, Y)
    y_hat = crf.inference(x, w)
    assert_array_equal(y, y_hat)
Beispiel #23
0
def test_binary_blocks_crf():
    X, Y = generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    w = np.array([1, 0,  # unary
                  0, 1,
                  0,     # pairwise
                  -4, 0])
    for inference_method in get_installed(['dai', 'qpbo', 'lp', 'ad3', 'ogm']):
        crf = GridCRF(inference_method=inference_method)
        crf.initialize(X, Y)
        y_hat = crf.inference(x, w)
        assert_array_equal(y, y_hat)
Beispiel #24
0
def test_averaged():
    # Under a lot of noise, averaging helps.  This fails with less noise.
    X, Y = generate_blocks_multinomial(n_samples=15, noise=3, seed=0)
    X_train, Y_train = X[:10], Y[:10]
    X_test, Y_test = X[10:], Y[10:]
    crf = GridCRF()
    clf = StructuredPerceptron(model=crf, max_iter=3)
    clf.fit(X_train, Y_train)
    no_avg_test = clf.score(X_test, Y_test)
    clf.set_params(average=True)
    clf.fit(X_train, Y_train)
    avg_test = clf.score(X_test, Y_test)
    assert_greater(avg_test, no_avg_test)
Beispiel #25
0
def test_binary_crf_exhaustive():
    # tests qpbo inference against brute force
    # on random data / weights
    np.random.seed(0)
    for i in range(10):
        x = np.random.uniform(-1, 1, size=(3, 2))
        x = np.dstack([-x, np.zeros_like(x)]).copy()
        crf = GridCRF(n_features=2, n_states=2)
        w = np.random.uniform(-1, 1, size=7)
        # check map inference
        y_hat = crf.inference(x, w)
        y_ex = exhaustive_inference(crf, x, w)
        assert_array_equal(y_hat, y_ex)
def test_binary_blocks():
    #testing subgradient ssvm on easy binary dataset
    X, Y = generate_blocks(n_samples=5)
    crf = GridCRF(inference_method=inference_method)
    clf = SubgradientSSVM(model=crf,
                          C=100,
                          learning_rate=1,
                          decay_exponent=1,
                          momentum=0,
                          decay_t0=10)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Beispiel #27
0
def test_loss_augmentation():
    X, Y = generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    w = np.array([1, 0,  # unary
                  0, 1,
                  0,     # pairwise
                  -4, 0])
    crf = GridCRF()
    crf.initialize(X, Y)
    y_hat, energy = crf.loss_augmented_inference(x, y, w, return_energy=True)

    assert_almost_equal(energy + crf.loss(y, y_hat),
                        -np.dot(w, crf.psi(x, y_hat)))
Beispiel #28
0
def test_energy_lp():
    # make sure that energy as computed by ssvm is the same as by lp
    np.random.seed(0)
    found_fractional = False
    for inference_method in get_installed(["lp", "ad3"]):
        crf = GridCRF(n_states=3, n_features=4,
                      inference_method=inference_method)
        while not found_fractional:
            x = np.random.normal(size=(2, 2, 4))
            w = np.random.uniform(size=crf.size_psi)
            inf_res, energy_lp = crf.inference(x, w, relaxed=True,
                                               return_energy=True)
            assert_almost_equal(energy_lp,
                                -np.dot(w, crf.psi(x, inf_res)))
            found_fractional = np.any(np.max(inf_res[0], axis=-1) != 1)
Beispiel #29
0
def test_blocks_multinomial_crf():
    X, Y = generate_blocks_multinomial(n_samples=1, size_x=9, seed=0)
    x, y = X[0], Y[0]
    w = np.array([1., 0., 0.,  # unaryA
                  0., 1., 0.,
                  0., 0., 1.,
                 .4,           # pairwise
                 -.3, .3,
                 -.5, -.1, .3])
    for inference_method in get_installed():
        #NOTE: ad3+ fails because it requires a different data structure
        if inference_method == 'ad3+': continue
        crf = GridCRF(inference_method=inference_method)
        crf.initialize(X, Y)
        y_hat = crf.inference(x, w)
        assert_array_equal(y, y_hat)
Beispiel #30
0
def test_max_product_binary_blocks():
    X, Y = generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    w = np.array([
        1,
        0,  # unary
        0,
        1,
        0,  # pairwise
        -4,
        0
    ])
    crf = GridCRF(inference_method='max-product')
    crf.initialize(X, Y)
    y_hat = crf.inference(x, w)
    assert_array_equal(y, y_hat)