def test_energy():
    # make sure that energy as computed by ssvm is the same as by lp
    np.random.seed(0)
    for inference_method in get_installed(["lp", "ad3"]):
        found_fractional = False
        crf = DirectionalGridCRF(n_states=3, inference_method=inference_method)
        while not found_fractional:
            x = np.random.normal(size=(7, 8, 3))
            unary_params = np.random.normal(size=(3, 3))
            pw1 = np.random.normal(size=(3, 3))
            pw2 = np.random.normal(size=(3, 3))
            w = np.hstack([unary_params.ravel(), pw1.ravel(), pw2.ravel()])
            res, energy = crf.inference(x, w, relaxed=True, return_energy=True)
            found_fractional = np.any(np.max(res[0], axis=-1) != 1)

            psi = crf.psi(x, res)
            energy_svm = np.dot(psi, w)

            assert_almost_equal(energy, -energy_svm)
            if not found_fractional:
                # exact discrete labels, test non-relaxed version
                res, energy = crf.inference(x, w, relaxed=False,
                                            return_energy=True)
                psi = crf.psi(x, res)
                energy_svm = np.dot(psi, w)

                assert_almost_equal(energy, -energy_svm)
Exemplo n.º 2
0
def test_energy():
    # make sure that energy as computed by ssvm is the same as by lp
    np.random.seed(0)
    for inference_method in get_installed(["lp", "ad3"]):
        found_fractional = False
        crf = DirectionalGridCRF(n_states=3,
                                 n_features=3,
                                 inference_method=inference_method)
        while not found_fractional:
            x = np.random.normal(size=(7, 8, 3))
            unary_params = np.random.normal(size=(3, 3))
            pw1 = np.random.normal(size=(3, 3))
            pw2 = np.random.normal(size=(3, 3))
            w = np.hstack([unary_params.ravel(), pw1.ravel(), pw2.ravel()])
            res, energy = crf.inference(x, w, relaxed=True, return_energy=True)
            found_fractional = np.any(np.max(res[0], axis=-1) != 1)

            psi = crf.psi(x, res)
            energy_svm = np.dot(psi, w)

            assert_almost_equal(energy, -energy_svm)
            if not found_fractional:
                # exact discrete labels, test non-relaxed version
                res, energy = crf.inference(x,
                                            w,
                                            relaxed=False,
                                            return_energy=True)
                psi = crf.psi(x, res)
                energy_svm = np.dot(psi, w)

                assert_almost_equal(energy, -energy_svm)
def test_psi_continuous():
    # FIXME
    # first make perfect prediction, including pairwise part
    X, Y = toy.generate_blocks_multinomial(noise=2, n_samples=1, seed=1)
    x, y = X[0], Y[0]
    n_states = x.shape[-1]

    pw_horz = -1 * np.eye(n_states)
    xx, yy = np.indices(pw_horz.shape)
    # linear ordering constraint horizontally
    pw_horz[xx > yy] = 1

    # high cost for unequal labels vertically
    pw_vert = -1 * np.eye(n_states)
    pw_vert[xx != yy] = 1
    pw_vert *= 10

    # create crf, assemble weight, make prediction
    for inference_method in get_installed(["lp", "ad3"]):
        crf = DirectionalGridCRF(n_states=3, inference_method=inference_method)
        w = np.hstack([np.eye(3).ravel(), -pw_horz.ravel(), -pw_vert.ravel()])
        y_pred = crf.inference(x, w, relaxed=True)

        # compute psi for prediction
        psi_y = crf.psi(x, y_pred)
        assert_equal(psi_y.shape, (crf.size_psi,))
Exemplo n.º 4
0
def test_psi_continuous():
    # FIXME
    # first make perfect prediction, including pairwise part
    X, Y = generate_blocks_multinomial(noise=2, n_samples=1, seed=1)
    x, y = X[0], Y[0]
    n_states = x.shape[-1]

    pw_horz = -1 * np.eye(n_states)
    xx, yy = np.indices(pw_horz.shape)
    # linear ordering constraint horizontally
    pw_horz[xx > yy] = 1

    # high cost for unequal labels vertically
    pw_vert = -1 * np.eye(n_states)
    pw_vert[xx != yy] = 1
    pw_vert *= 10

    # create crf, assemble weight, make prediction
    for inference_method in get_installed(["lp", "ad3"]):
        crf = DirectionalGridCRF(inference_method=inference_method)
        crf.initialize(X, Y)
        w = np.hstack([np.eye(3).ravel(), -pw_horz.ravel(), -pw_vert.ravel()])
        y_pred = crf.inference(x, w, relaxed=True)

        # compute psi for prediction
        psi_y = crf.psi(x, y_pred)
        assert_equal(psi_y.shape, (crf.size_psi, ))
def test_psi_discrete():
    X, Y = toy.generate_blocks_multinomial(noise=2, n_samples=1, seed=1)
    x, y = X[0], Y[0]
    for inference_method in get_installed(["lp", "ad3", "qpbo"]):
        crf = DirectionalGridCRF(n_states=3, inference_method=inference_method)
        psi_y = crf.psi(x, y)
        assert_equal(psi_y.shape, (crf.size_psi,))
        # first horizontal, then vertical
        # we trust the unaries ;)
        pw_psi_horz, pw_psi_vert = psi_y[crf.n_states *
                                         crf.n_features:].reshape(
                                             2, crf.n_states, crf.n_states)
        xx, yy = np.indices(y.shape)
        assert_array_equal(pw_psi_vert, np.diag([9 * 4, 9 * 4, 9 * 4]))
        vert_psi = np.diag([10 * 3, 10 * 3, 10 * 3])
        vert_psi[0, 1] = 10
        vert_psi[1, 2] = 10
        assert_array_equal(pw_psi_horz, vert_psi)
Exemplo n.º 6
0
def test_psi_discrete():
    X, Y = generate_blocks_multinomial(noise=2, n_samples=1, seed=1)
    x, y = X[0], Y[0]
    for inference_method in get_installed(["lp", "ad3", "qpbo"]):
        crf = DirectionalGridCRF(inference_method=inference_method)
        crf.initialize(X, Y)
        psi_y = crf.psi(x, y)
        assert_equal(psi_y.shape, (crf.size_psi, ))
        # first horizontal, then vertical
        # we trust the unaries ;)
        pw_psi_horz, pw_psi_vert = psi_y[crf.n_states *
                                         crf.n_features:].reshape(
                                             2, crf.n_states, crf.n_states)
        xx, yy = np.indices(y.shape)
        assert_array_equal(pw_psi_vert, np.diag([9 * 4, 9 * 4, 9 * 4]))
        vert_psi = np.diag([10 * 3, 10 * 3, 10 * 3])
        vert_psi[0, 1] = 10
        vert_psi[1, 2] = 10
        assert_array_equal(pw_psi_horz, vert_psi)