def test_learning(): crf = IgnoreVoidCRF(n_states=3, n_features=2, void_label=2, inference_method='lp') ssvm = SubgradientStructuredSVM(crf, verbose=10, C=100, n_jobs=1, max_iter=50, learning_rate=0.01) ssvm.fit(X, Y) for x in X: y_hat_exhaustive = exhaustive_inference(crf, x, ssvm.w) y_hat = crf.inference(x, ssvm.w) assert_array_equal(y_hat, y_hat_exhaustive) constr = [ find_constraint(crf, x, y, ssvm.w, y_hat=y_hat) for x, y, y_hat in zip(X, Y, ssvm.predict(X)) ] losses = [c[3] for c in constr] slacks = [c[2] for c in constr] assert_true(np.all(np.array(slacks) >= np.array(losses)))
def test_binary_crf_exhaustive(): # tests qpbo inference against brute force # on random data / weights np.random.seed(0) for i in xrange(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_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_inference(): crf = IgnoreVoidCRF(n_states=3, n_features=2, void_label=2, inference_method='lp') # set some random weights, do inference and check that everything is ok np.random.seed(0) for x, y in zip(X, Y): w = np.random.normal(size=crf.size_psi) y_hat, energy = crf.inference(x, w, relaxed=True, return_energy=True) energy_svm = np.dot(w, crf.psi(x, y_hat)) assert_almost_equal(energy_svm, -energy) y_hat_exhaustive = exhaustive_inference(crf, x, w) y_hat = crf.inference(x, w) assert_array_equal(y_hat, y_hat_exhaustive)
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)]).copy() crf = GridCRF() w = np.random.uniform(-1, 1, size=7) # check map inference y_hat = crf.inference(x, w) y_ex = exhaustive_inference(crf, x, w) #print(y_hat) #print(y_ex) #print("++++++++++++++++++++++") assert_array_equal(y_hat, y_ex)
def test_learning(): crf = IgnoreVoidCRF(n_states=3, n_features=2, void_label=2, inference_method='lp') ssvm = SubgradientStructuredSVM(crf, verbose=10, C=100, n_jobs=1, max_iter=50, learning_rate=0.01) ssvm.fit(X, Y) for x in X: y_hat_exhaustive = exhaustive_inference(crf, x, ssvm.w) y_hat = crf.inference(x, ssvm.w) assert_array_equal(y_hat, y_hat_exhaustive) constr = [find_constraint(crf, x, y, ssvm.w, y_hat=y_hat) for x, y, y_hat in zip(X, Y, ssvm.predict(X))] losses = [c[3] for c in constr] slacks = [c[2] for c in constr] assert_true(np.all(np.array(slacks) >= np.array(losses)))