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(model=crf, max_iter=500, C=1000., verbose=2, #check_constraints=True, n_jobs=-1, break_on_bad=True, #base_svm='1-slack', inference_cache=20, tol=.1) clf = LatentSubgradientSSVM( model=crf, max_iter=500, C=1000., verbose=2, n_jobs=-1, learning_rate=0.1, show_loss_every=10) 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"]]: for X_, Y_, H, name in [[X_train, Y_train, [None] * len(X_test), "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) fig, ax = plt.subplots(3, 2) ax[0, 0].matshow(y, vmin=0, vmax=crf.n_labels - 1) ax[0, 0].set_title("ground truth") ax[0, 1].matshow(np.argmax(x, axis=-1), vmin=0, vmax=crf.n_labels - 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(crf.inference(x, clf.w), vmin=0, vmax=crf.n_states - 1) ax[2, 0].set_title("prediction latent") ax[2, 1].matshow(y_pred, vmin=0, vmax=crf.n_labels - 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)
def test_directional_bars(): for inference_method in ['lp']: X, Y = toy.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], inference_method=inference_method) clf = LatentSubgradientSSVM(model=crf, max_iter=500, C=10. ** 5, verbose=2) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y)
def test_directional_bars(): # this test is very fragile :-/ X, Y = toy.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 = LatentSubgradientSSVM(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)
def test_with_crosses(): # very simple dataset. k-means init is perfect for n_states_per_label in [2, [1, 2]]: # test with 2 states for both foreground and background, # as well as with single background state #for inference_method in ['ad3', 'qpbo', 'lp']: for inference_method in ['lp']: X, Y = toy.generate_crosses(n_samples=10, noise=5, n_crosses=1, total_size=8) n_labels = 2 crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=n_states_per_label, inference_method=inference_method) clf = LatentSubgradientSSVM(model=crf, max_iter=250, C=10. ** 5, verbose=20, learning_rate=0.0001, show_loss_every=10, momentum=0.98, decay_exponent=0) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y)