Beispiel #1
0
def test_blocks_crf_unaries():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    unary_weights = np.repeat(np.eye(2), 2, axis=0)
    pairwise_weights = np.array([0,
                                 0,  0,
                                 0,  0,  0,
                                 0,  0,  0, 0])
    w = np.hstack([unary_weights.ravel(), pairwise_weights])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    assert_array_equal(h_hat / 2, np.argmax(x, axis=-1))
def main():
    X, Y = toy.generate_crosses(n_samples=40, noise=8, n_crosses=2,
                                total_size=10)
    X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.5)
    n_labels = len(np.unique(Y_train))
    crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=2,
                        inference_method='lp')
    clf = LatentSSVM(problem=crf, max_iter=50, C=1000., verbose=2,
                     check_constraints=True, n_jobs=-1, break_on_bad=True,
                     plot=True)
    clf.fit(X_train, Y_train)

    for X_, Y_, H, name in [[X_train, Y_train, clf.H_init_, "train"],
                            [X_test, Y_test, [None] * len(X_test), "test"]]:
        Y_pred = clf.predict(X_)
        i = 0
        loss = 0
        for x, y, h_init, y_pred in zip(X_, Y_, H, Y_pred):
            loss += np.sum(y != y_pred / crf.n_states_per_label)
            fig, ax = plt.subplots(3, 2)
            ax[0, 0].matshow(y * crf.n_states_per_label,
                             vmin=0, vmax=crf.n_states - 1)
            ax[0, 0].set_title("ground truth")
            unary_params = np.repeat(np.eye(2), 2, axis=1)
            pairwise_params = np.zeros(10)
            w_unaries_only = np.hstack([unary_params.ravel(),
                                        pairwise_params.ravel()])
            unary_pred = crf.inference(x, w_unaries_only)
            ax[0, 1].matshow(unary_pred, vmin=0, vmax=crf.n_states - 1)
            ax[0, 1].set_title("unaries only")
            if h_init is None:
                ax[1, 0].set_visible(False)
            else:
                ax[1, 0].matshow(h_init, vmin=0, vmax=crf.n_states - 1)
                ax[1, 0].set_title("latent initial")
            ax[1, 1].matshow(crf.latent(x, y, clf.w),
                             vmin=0, vmax=crf.n_states - 1)
            ax[1, 1].set_title("latent final")
            ax[2, 0].matshow(y_pred, vmin=0, vmax=crf.n_states - 1)
            ax[2, 0].set_title("prediction")
            ax[2, 1].matshow((y_pred // crf.n_states_per_label)
                             * crf.n_states_per_label,
                             vmin=0, vmax=crf.n_states - 1)
            ax[2, 1].set_title("prediction")
            for a in ax.ravel():
                a.set_xticks(())
                a.set_yticks(())
            fig.savefig("data_%s_%03d.png" % (name, i), bbox_inches="tight")
            i += 1
        print("loss %s set: %f" % (name, loss))
    print(clf.w)
Beispiel #3
0
def test_blocks_crf():
    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])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    assert_array_equal(y, h_hat / 2)

    h = crf.latent(x, y, w)
    assert_equal(crf.loss(h, h_hat), 0)
def main():
    X, Y = toy.generate_crosses(n_samples=20, noise=5, n_crosses=1,
                                total_size=8)
    X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.5)
    n_labels = len(np.unique(Y_train))
    crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=[1, 2],
                        inference_method='lp')
    clf = LatentSSVM(problem=crf, max_iter=50, C=1000., verbose=2,
                     check_constraints=True, n_jobs=-1, break_on_bad=True)
    clf.fit(X_train, Y_train)

    i = 0
    for X_, Y_, H, name in [[X_train, Y_train, clf.H_init_, "train"],
                            [X_test, Y_test, [None] * len(X_test), "test"]]:
        Y_pred = clf.predict(X_)
        score = clf.score(X_, Y_)
        for x, y, h_init, y_pred in zip(X_, Y_, H, Y_pred):
            fig, ax = plt.subplots(4, 1)
            ax[0].matshow(y, vmin=0, vmax=crf.n_labels - 1)
            ax[0].set_title("Ground truth")
            ax[1].matshow(np.argmax(x, axis=-1), vmin=0, vmax=crf.n_labels - 1)
            ax[1].set_title("Unaries only")
            #if h_init is None:
                #ax[1, 0].set_visible(False)
            #else:
                #ax[1, 0].matshow(h_init, vmin=0, vmax=crf.n_states - 1)
                #ax[1, 0].set_title("latent initial")
            #ax[2].matshow(crf.latent(x, y, clf.w),
                          #vmin=0, vmax=crf.n_states - 1)
            #ax[2].set_title("latent final")
            ax[2].matshow(crf.inference(x, clf.w), vmin=0, vmax=crf.n_states
                          - 1)
            ax[2].set_title("Prediction for h")
            ax[3].matshow(y_pred, vmin=0, vmax=crf.n_labels - 1)
            ax[3].set_title("Prediction for y")
            for a in ax.ravel():
                a.set_xticks(())
                a.set_yticks(())
            plt.subplots_adjust(hspace=.5)
            fig.savefig("data_%s_%03d.png" % (name, i), bbox_inches="tight",
                        dpi=400)
            i += 1
        print("score %s set: %f" % (name, score))
    print(clf.w)
Beispiel #5
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))
Beispiel #6
0
def main():
    X, Y = toy.generate_crosses(n_samples=40,
                                noise=8,
                                n_crosses=2,
                                total_size=10)
    X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.5)
    n_labels = len(np.unique(Y_train))
    crf = LatentGridCRF(n_labels=n_labels,
                        n_states_per_label=2,
                        inference_method='lp')
    clf = LatentSSVM(problem=crf,
                     max_iter=50,
                     C=1000.,
                     verbose=2,
                     check_constraints=True,
                     n_jobs=-1,
                     break_on_bad=True,
                     plot=True)
    clf.fit(X_train, Y_train)

    for X_, Y_, H, name in [[X_train, Y_train, clf.H_init_, "train"],
                            [X_test, Y_test, [None] * len(X_test), "test"]]:
        Y_pred = clf.predict(X_)
        i = 0
        loss = 0
        for x, y, h_init, y_pred in zip(X_, Y_, H, Y_pred):
            loss += np.sum(y != y_pred / crf.n_states_per_label)
            fig, ax = plt.subplots(3, 2)
            ax[0, 0].matshow(y * crf.n_states_per_label,
                             vmin=0,
                             vmax=crf.n_states - 1)
            ax[0, 0].set_title("ground truth")
            unary_params = np.repeat(np.eye(2), 2, axis=1)
            pairwise_params = np.zeros(10)
            w_unaries_only = np.hstack(
                [unary_params.ravel(),
                 pairwise_params.ravel()])
            unary_pred = crf.inference(x, w_unaries_only)
            ax[0, 1].matshow(unary_pred, vmin=0, vmax=crf.n_states - 1)
            ax[0, 1].set_title("unaries only")
            if h_init is None:
                ax[1, 0].set_visible(False)
            else:
                ax[1, 0].matshow(h_init, vmin=0, vmax=crf.n_states - 1)
                ax[1, 0].set_title("latent initial")
            ax[1, 1].matshow(crf.latent(x, y, clf.w),
                             vmin=0,
                             vmax=crf.n_states - 1)
            ax[1, 1].set_title("latent final")
            ax[2, 0].matshow(y_pred, vmin=0, vmax=crf.n_states - 1)
            ax[2, 0].set_title("prediction")
            ax[2, 1].matshow(
                (y_pred // crf.n_states_per_label) * crf.n_states_per_label,
                vmin=0,
                vmax=crf.n_states - 1)
            ax[2, 1].set_title("prediction")
            for a in ax.ravel():
                a.set_xticks(())
                a.set_yticks(())
            fig.savefig("data_%s_%03d.png" % (name, i), bbox_inches="tight")
            i += 1
        print("loss %s set: %f" % (name, loss))
    print(clf.w)