def test_loss_augmentation(): X, Y = generate_blocks(n_samples=1) x, y = X[0], Y[0] w = np.array([1, 0, 0, 1, 0, -4, 0]) # unary # pairwise 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.joint_feature(x, y_hat)))
def test_loss_augmentation(): X, Y = toy.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='lp') 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)))
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)))
def test_binary_crf_exhaustive_loss_augmented(): # tests qpbo inference against brute force # on random data / weights np.random.seed(0) for inference_method in get_installed(["qpbo", "lp"]): crf = GridCRF(n_states=2, n_features=2, inference_method=inference_method) for i in xrange(10): # generate data and weights y = np.random.randint(2, size=(3, 2)) x = np.random.uniform(-1, 1, size=(3, 2)) x = np.dstack([-x, np.zeros_like(x)]) w = np.random.uniform(-1, 1, size=7) # check loss augmented map inference y_hat = crf.loss_augmented_inference(x, y, w) y_ex = exhaustive_loss_augmented_inference(crf, x, y, w) assert_array_equal(y_hat, y_ex)
def test_binary_crf_exhaustive_loss_augmented(): # tests qpbo inference against brute force # on random data / weights np.random.seed(0) for inference_method in get_installed(['qpbo', 'lp']): crf = GridCRF(n_states=2, n_features=2, inference_method=inference_method) for i in xrange(10): # generate data and weights y = np.random.randint(2, size=(3, 2)) x = np.random.uniform(-1, 1, size=(3, 2)) x = np.dstack([-x, np.zeros_like(x)]) w = np.random.uniform(-1, 1, size=7) # check loss augmented map inference y_hat = crf.loss_augmented_inference(x, y, w) y_ex = exhaustive_loss_augmented_inference(crf, x, y, w) assert_array_equal(y_hat, y_ex)
def test_binary_crf_exhaustive_loss_augmented(): # tests graph cut inference against brute force # on random data / weights np.random.seed(0) for inference_method in ['qpbo', 'lp']: crf = GridCRF(inference_method=inference_method) for i in xrange(50): # generate data and weights y = np.random.randint(2, size=(3, 3)) x = np.random.uniform(-1, 1, size=(3, 3)) x = np.dstack([-x, np.zeros_like(x)]) w = np.random.uniform(-1, 1, size=7) # check loss augmented map inference y_hat = crf.loss_augmented_inference(x, y, w) y_ex = exhaustive_loss_augmented_inference(crf, x, y, w) #print(y_hat) #print(y_ex) #print("++++++++++++++++++++++") assert_array_equal(y_hat, y_ex)