Example #1
0
def test_blocks_crf_directional():
    # test latent directional CRF on blocks
    # test that all results are the same as equivalent LatentGridCRF
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    pairwise_weights = np.array([0, 0, 0, -4, -4, 0, -4, -4, 0, 0])
    unary_weights = np.repeat(np.eye(2), 2, axis=0)
    w = np.hstack([unary_weights.ravel(), pairwise_weights])
    pw_directional = np.array(
        [0, 0, -4, -4, 0, 0, -4, -4, -4, -4, 0, 0, -4, -4, 0, 0, 0, 0, -4, -4, 0, 0, -4, -4, -4, -4, 0, 0, -4, -4, 0, 0]
    )
    w_directional = np.hstack([unary_weights.ravel(), pw_directional])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    directional_crf = LatentDirectionalGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    h_hat_d = directional_crf.inference(x, w_directional)
    assert_array_equal(h_hat, h_hat_d)

    h = crf.latent(x, y, w)
    h_d = directional_crf.latent(x, y, w_directional)
    assert_array_equal(h, h_d)

    h_hat = crf.loss_augmented_inference(x, y, w)
    h_hat_d = directional_crf.loss_augmented_inference(x, y, w_directional)
    assert_array_equal(h_hat, h_hat_d)

    psi = crf.psi(x, h_hat)
    psi_d = directional_crf.psi(x, h_hat)
    assert_array_equal(np.dot(psi, w), np.dot(psi_d, w_directional))
def test_k_means_initialization_directional_grid_crf():
    X, Y = generate_big_checker(n_samples=10)
    crf = LatentDirectionalGridCRF(n_states_per_label=1, n_features=2,
                                   n_labels=2)
    #crf.initialize(X, Y)
    H = crf.init_latent(X, Y)
    assert_array_equal(Y, H)
Example #3
0
def test_k_means_initialization_directional_grid_crf():
    X, Y = generate_big_checker(n_samples=10)
    crf = LatentDirectionalGridCRF(n_states_per_label=1, n_features=2,
                                   n_labels=2)
    #crf.initialize(X, Y)
    H = crf.init_latent(X, Y)
    assert_array_equal(Y, H)
Example #4
0
def test_blocks_crf_directional():
    # test latent directional CRF on blocks
    # test that all results are the same as equivalent LatentGridCRF
    X, Y = generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    pairwise_weights = np.array([0, 0, 0, -4, -4, 0, -4, -4, 0, 0])
    unary_weights = np.repeat(np.eye(2), 2, axis=0)
    w = np.hstack([unary_weights.ravel(), pairwise_weights])
    pw_directional = np.array([
        0, 0, -4, -4, 0, 0, -4, -4, -4, -4, 0, 0, -4, -4, 0, 0, 0, 0, -4, -4,
        0, 0, -4, -4, -4, -4, 0, 0, -4, -4, 0, 0
    ])
    w_directional = np.hstack([unary_weights.ravel(), pw_directional])
    crf = LatentGridCRF(n_states_per_label=2, inference_method='lp')
    crf.initialize(X, Y)
    directional_crf = LatentDirectionalGridCRF(n_states_per_label=2,
                                               inference_method='lp')
    directional_crf.initialize(X, Y)
    h_hat = crf.inference(x, w)
    h_hat_d = directional_crf.inference(x, w_directional)
    assert_array_equal(h_hat, h_hat_d)

    h = crf.latent(x, y, w)
    h_d = directional_crf.latent(x, y, w_directional)
    assert_array_equal(h, h_d)

    h_hat = crf.loss_augmented_inference(x, y, w)
    h_hat_d = directional_crf.loss_augmented_inference(x, y, w_directional)
    assert_array_equal(h_hat, h_hat_d)

    joint_feature = crf.joint_feature(x, h_hat)
    joint_feature_d = directional_crf.joint_feature(x, h_hat)
    assert_array_equal(np.dot(joint_feature, w),
                       np.dot(joint_feature_d, w_directional))
Example #5
0
def test_directional_bars():
    # this test is very fragile :-/
    X, Y = generate_easy(n_samples=20, noise=2, box_size=2, total_size=6,
                         seed=2)
    n_labels = 2
    crf = LatentDirectionalGridCRF(n_labels=n_labels,
                                   n_states_per_label=[1, 4])
    clf = SubgradientLatentSSVM(model=crf, max_iter=75, C=10.,
                                learning_rate=1, momentum=0,
                                decay_exponent=0.5, decay_t0=10)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)

    assert_array_equal(np.array(Y_pred), Y)
Example #6
0
def test_directional_bars():
    X, Y = generate_easy(n_samples=10,
                         noise=5,
                         box_size=2,
                         total_size=6,
                         seed=1)
    n_labels = 2
    crf = LatentDirectionalGridCRF(n_labels=n_labels,
                                   n_states_per_label=[1, 4])
    clf = LatentSSVM(
        OneSlackSSVM(model=crf,
                     max_iter=500,
                     C=10.,
                     inference_cache=50,
                     tol=.01))
    clf.fit(X, Y)
    Y_pred = clf.predict(X)

    assert_array_equal(np.array(Y_pred), Y)
Example #7
0
def test_k_means_initialization_directional_grid_crf():
    X, Y = toy.generate_big_checker(n_samples=10)
    crf = LatentDirectionalGridCRF(n_labels=2, n_states_per_label=1, inference_method="lp")
    H = crf.init_latent(X, Y)
    assert_array_equal(Y, H)