def test_switch_to_ad3(): # smoketest only # test if switching between qpbo and ad3 works inside latent svm # use less perfect initialization if not get_installed(['qpbo']) or not get_installed(['ad3']): return X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2, inference_method='qpbo') crf.initialize(X, Y) H_init = crf.init_latent(X, Y) np.random.seed(0) mask = np.random.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) base_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, max_iter=10000, switch_to=('ad3', {'branch_and_bound': True}), C=10. ** 3) clf = LatentSSVM(base_ssvm) clf.fit(X, Y, H_init=H_init) assert_equal(clf.model.inference_method[0], 'ad3') Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(.98 < clf.score(X_test, Y_test) < 1)
def test_with_crosses_bad_init(): # use less perfect initialization rnd = np.random.RandomState(0) X, Y = toy.generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] n_labels = 2 crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=2) H_init = crf.init_latent(X, Y) mask = rnd.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) one_slack = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, max_iter=10000) n_slack = NSlackSSVM(crf) subgradient = SubgradientSSVM(crf, max_iter=150, learning_rate=.01, momentum=0) for base_ssvm in [one_slack, n_slack, subgradient]: base_ssvm.C = 10. ** 2 clf = LatentSSVM(base_ssvm) clf.fit(X, Y, H_init=H_init) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(.98 < clf.score(X_test, Y_test) < 1)
def test_with_crosses_bad_init(): # use less perfect initialization 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=2, inference_method='lp') H_init = crf.init_latent(X, Y) mask = np.random.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) one_slack = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, max_iter=10000) n_slack = StructuredSVM(crf) subgradient = SubgradientSSVM(crf, max_iter=150, learning_rate=5) for base_ssvm in [one_slack, n_slack, subgradient]: base_ssvm.C = 10. ** 3 base_ssvm.n_jobs = -1 clf = LatentSSVM(base_ssvm) clf.fit(X, Y, H_init=H_init) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y)
def main(): # get some data X, Y = toy.generate_bars(n_samples=40, noise=10, total_size=10, separate_labels=False) X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.5) # train a latent grid crf n_labels = len(np.unique(Y_train)) crf = LatentDirectionalGridCRF(n_labels=n_labels, n_states_per_label=[1, 6], inference_method='lp') clf = LatentSSVM(problem=crf, max_iter=50, C=10., verbose=2, check_constraints=True, n_jobs=-1, break_on_bad=False, tol=-10, base_svm='1-slack') clf.fit(X_train, Y_train) # the rest is plotting 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) 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") unary_pred = np.argmax(x, axis=-1) ax[0, 1].matshow(unary_pred, 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") 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))
def test_directional_bars(): X, Y = 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]) clf = LatentSSVM(OneSlackSSVM(model=crf, max_iter=500, C=10.0, inference_cache=50, tol=0.01)) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y)
def main(): # get some data X, Y = toy.generate_bars(n_samples=40, noise=10, total_size=10, separate_labels=False) X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.5) # train a latent grid crf n_labels = len(np.unique(Y_train)) crf = LatentDirectionalGridCRF(n_labels=n_labels, n_states_per_label=[1, 6], inference_method="lp") clf = LatentSSVM( problem=crf, max_iter=50, C=10.0, verbose=2, check_constraints=True, n_jobs=-1, break_on_bad=False, tol=-10, base_svm="1-slack", ) clf.fit(X_train, Y_train) # the rest is plotting 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) 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") unary_pred = np.argmax(x, axis=-1) ax[0, 1].matshow(unary_pred, 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") 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))
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)
def test_with_crosses_base_svms(): # very simple dataset. k-means init is perfect for base_svm in ['1-slack', 'n-slack', 'subgradient']: 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=[1, 2], inference_method='lp') clf = LatentSSVM(problem=crf, max_iter=150, C=10. ** 5, verbose=2, check_constraints=True, n_jobs=-1, break_on_bad=True, base_svm=base_svm, learning_rate=5) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y)
def test_switch_to_ad3(): # smoketest only # test if switching between qpbo and ad3 works inside latent svm # use less perfect initialization if not get_installed(["qpbo"]) or not get_installed(["ad3"]): return X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2, inference_method="qpbo") crf.initialize(X, Y) H_init = crf.init_latent(X, Y) np.random.seed(0) mask = np.random.uniform(size=H_init.shape) > 0.7 H_init[mask] = 2 * (H_init[mask] / 2) base_ssvm = OneSlackSSVM( crf, inactive_threshold=1e-8, cache_tol=0.0001, inference_cache=50, max_iter=10000, switch_to=("ad3", {"branch_and_bound": True}), C=10.0 ** 3, ) clf = LatentSSVM(base_ssvm) # evil hackery to get rid of ad3 output try: devnull = open("/dev/null", "w") oldstdout_fno = os.dup(sys.stdout.fileno()) os.dup2(devnull.fileno(), 1) replaced_stdout = True except: replaced_stdout = False clf.fit(X, Y, H_init=H_init) if replaced_stdout: os.dup2(oldstdout_fno, 1) assert_equal(clf.model.inference_method[0], "ad3") Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(0.98 < clf.score(X_test, Y_test) < 1)
def test_states(states, x, y, x_t, y_t, i, jobs): latent_pbl = GraphLDCRF(n_states_per_label=states, inference_method="qpbo") base_ssvm = NSlackSSVM(latent_pbl, C=1, tol=0.01, inactive_threshold=1e-3, batch_size=10, verbose=0, n_jobs=jobs) latent_svm = LatentSSVM(base_ssvm=base_ssvm, latent_iter=3) latent_svm.fit(x, y) test = latent_svm.score(x_t, y_t) train = latent_svm.score(x, y) plot_cm(latent_svm, y_t, x_t, str(states), i) print states, "Test:", test, "Train:", train return test, train
def test_with_crosses_perfect_init(): # 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 X, Y = 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) clf = LatentSSVM( OneSlackSSVM(model=crf, max_iter=500, C=10, check_constraints=False, break_on_bad=False, inference_cache=50) ) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) assert_equal(clf.score(X, Y), 1)
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 = LatentSSVM(problem=crf, max_iter=500, C=10. ** 5, verbose=2, check_constraints=True, n_jobs=-1, break_on_bad=True, base_svm='1-slack') clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y)
def test_binary_blocks_cutting_plane_latent_node(): #testing cutting plane ssvm on easy binary dataset # we use the LatentNodeCRF without latent nodes and check that it does the # same as GraphCRF X, Y = generate_blocks(n_samples=3) crf = GraphCRF() clf = NSlackSSVM(model=crf, max_iter=20, C=100, check_constraints=True, break_on_bad=False, n_jobs=1) x1, x2, x3 = X y1, y2, y3 = Y n_states = len(np.unique(Y)) # delete some rows to make it more fun x1, y1 = x1[:, :-1], y1[:, :-1] x2, y2 = x2[:-1], y2[:-1] # generate graphs X_ = [x1, x2, x3] G = [make_grid_edges(x) for x in X_] # reshape / flatten x and y X_ = [x.reshape(-1, n_states) for x in X_] Y = [y.ravel() for y in [y1, y2, y3]] X = zip(X_, G) clf.fit(X, Y) Y_pred = clf.predict(X) for y, y_pred in zip(Y, Y_pred): assert_array_equal(y, y_pred) latent_crf = LatentNodeCRF(n_labels=2, n_hidden_states=0) latent_svm = LatentSSVM(NSlackSSVM(model=latent_crf, max_iter=20, C=100, check_constraints=True, break_on_bad=False, n_jobs=1), latent_iter=3) X_latent = zip(X_, G, np.zeros(len(X_))) latent_svm.fit(X_latent, Y, H_init=Y) Y_pred = latent_svm.predict(X_latent) for y, y_pred in zip(Y, Y_pred): assert_array_equal(y, y_pred) assert_array_almost_equal(latent_svm.w, clf.w)
def test_with_crosses_base_svms(): # very simple dataset. k-means init is perfect n_labels = 2 crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=[1, 2]) one_slack = OneSlackSSVM(crf, inference_cache=50) n_slack = NSlackSSVM(crf) subgradient = SubgradientSSVM(crf, max_iter=400, learning_rate=0.01, decay_exponent=0, decay_t0=10) X, Y = generate_crosses(n_samples=10, noise=5, n_crosses=1, total_size=8) for base_ssvm in [one_slack, n_slack, subgradient]: base_ssvm.C = 100.0 clf = LatentSSVM(base_ssvm=base_ssvm) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) assert_equal(clf.score(X, Y), 1)
def test_with_crosses_bad_init(): # use less perfect initialization 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=2, inference_method='lp') clf = LatentSSVM(problem=crf, max_iter=50, C=10. ** 3, verbose=2, check_constraints=True, n_jobs=-1, break_on_bad=True) H_init = crf.init_latent(X, Y) mask = np.random.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) clf.fit(X, Y, H_init=H_init) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y)
def test_latent_node_boxes_standard_latent(): # learn the "easy" 2x2 boxes dataset. # a 2x2 box is placed randomly in a 4x4 grid # we add a latent variable for each 2x2 patch # that should make the model fairly simple X, Y = make_simple_2x2(seed=1, n_samples=40) latent_crf = LatentNodeCRF(n_labels=2, n_hidden_states=2, n_features=1) one_slack = OneSlackSSVM(latent_crf) n_slack = NSlackSSVM(latent_crf) subgradient = SubgradientSSVM(latent_crf, max_iter=100) for base_svm in [one_slack, n_slack, subgradient]: base_svm.C = 10 latent_svm = LatentSSVM(base_svm, latent_iter=10) G = [make_grid_edges(x) for x in X] # make edges for hidden states: edges = make_edges_2x2() G = [np.vstack([make_grid_edges(x), edges]) for x in X] # reshape / flatten x and y X_flat = [x.reshape(-1, 1) for x in X] Y_flat = [y.ravel() for y in Y] X_ = zip(X_flat, G, [2 * 2 for x in X_flat]) latent_svm.fit(X_[:20], Y_flat[:20]) assert_array_equal(latent_svm.predict(X_[:20]), Y_flat[:20]) assert_equal(latent_svm.score(X_[:20], Y_flat[:20]), 1) # test that score is not always 1 assert_true(.98 < latent_svm.score(X_[20:], Y_flat[20:]) < 1)
def test_latent_node_boxes_edge_features(): # learn the "easy" 2x2 boxes dataset. # smoketest using a single constant edge feature X, Y = make_simple_2x2(seed=1, n_samples=40) latent_crf = EdgeFeatureLatentNodeCRF(n_labels=2, n_hidden_states=2, n_features=1) base_svm = OneSlackSSVM(latent_crf) base_svm.C = 10 latent_svm = LatentSSVM(base_svm, latent_iter=10) G = [make_grid_edges(x) for x in X] # make edges for hidden states: edges = make_edges_2x2() G = [np.vstack([make_grid_edges(x), edges]) for x in X] # reshape / flatten x and y X_flat = [x.reshape(-1, 1) for x in X] Y_flat = [y.ravel() for y in Y] #X_ = zip(X_flat, G, [2 * 2 for x in X_flat]) # add edge features X_ = [(x, g, np.ones((len(g), 1)), 4) for x, g in zip(X_flat, G)] latent_svm.fit(X_[:20], Y_flat[:20]) assert_array_equal(latent_svm.predict(X_[:20]), Y_flat[:20]) assert_equal(latent_svm.score(X_[:20], Y_flat[:20]), 1) # test that score is not always 1 assert_true(.98 < latent_svm.score(X_[20:], Y_flat[20:]) < 1)
def test_with_crosses_base_svms(): # very simple dataset. k-means init is perfect n_labels = 2 crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=[1, 2], inference_method='lp') one_slack = OneSlackSSVM(crf) n_slack = StructuredSVM(crf) subgradient = SubgradientSSVM(crf, max_iter=150, learning_rate=5) for base_ssvm in [one_slack, n_slack, subgradient]: base_ssvm.C = 10. ** 5 base_ssvm.n_jobs = -1 X, Y = toy.generate_crosses(n_samples=10, noise=5, n_crosses=1, total_size=8) clf = LatentSSVM(base_ssvm=base_ssvm) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y)
def test_binary_blocks_cutting_plane_latent_node(): #testing cutting plane ssvm on easy binary dataset # we use the LatentNodeCRF without latent nodes and check that it does the # same as GraphCRF X, Y = toy.generate_blocks(n_samples=3) crf = GraphCRF(inference_method='lp') clf = StructuredSVM(model=crf, max_iter=20, C=100, verbose=0, check_constraints=True, break_on_bad=False, n_jobs=1) x1, x2, x3 = X y1, y2, y3 = Y n_states = len(np.unique(Y)) # delete some rows to make it more fun x1, y1 = x1[:, :-1], y1[:, :-1] x2, y2 = x2[:-1], y2[:-1] # generate graphs X_ = [x1, x2, x3] G = [make_grid_edges(x) for x in X_] # reshape / flatten x and y X_ = [x.reshape(-1, n_states) for x in X_] Y = [y.ravel() for y in [y1, y2, y3]] X = zip(X_, G) clf.fit(X, Y) Y_pred = clf.predict(X) for y, y_pred in zip(Y, Y_pred): assert_array_equal(y, y_pred) latent_crf = LatentNodeCRF(n_labels=2, inference_method='lp', n_hidden_states=0) latent_svm = LatentSSVM(StructuredSVM(model=latent_crf, max_iter=20, C=100, verbose=0, check_constraints=True, break_on_bad=False, n_jobs=1), latent_iter=3) X_latent = zip(X_, G, np.zeros(len(X_))) latent_svm.fit(X_latent, Y, H_init=Y) Y_pred = latent_svm.predict(X_latent) for y, y_pred in zip(Y, Y_pred): assert_array_equal(y, y_pred) assert_array_almost_equal(latent_svm.w, clf.w)
def test_directional_bars(): X, Y = 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]) clf = LatentSSVM( OneSlackSSVM(model=crf, max_iter=500, C=10., inference_cache=50, tol=.01)) 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 = LatentSSVM(problem=crf, max_iter=50, C=10. ** 5, verbose=2, check_constraints=True, n_jobs=-1, break_on_bad=True) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y)
def test_latent_node_boxes_standard_latent(): # learn the "easy" 2x2 boxes dataset. # a 2x2 box is placed randomly in a 4x4 grid # we add a latent variable for each 2x2 patch # that should make the model fairly simple X, Y = make_simple_2x2(seed=1, n_samples=40) latent_crf = LatentNodeCRF(n_labels=2, n_hidden_states=2, n_features=1) one_slack = OneSlackSSVM(latent_crf) n_slack = NSlackSSVM(latent_crf) subgradient = SubgradientSSVM(latent_crf, max_iter=100) for base_svm in [one_slack, n_slack, subgradient]: base_svm.C = 10 latent_svm = LatentSSVM(base_svm, latent_iter=10) G = [make_grid_edges(x) for x in X] # make edges for hidden states: edges = make_edges_2x2() G = [np.vstack([make_grid_edges(x), edges]) for x in X] # reshape / flatten x and y X_flat = [x.reshape(-1, 1) for x in X] Y_flat = [y.ravel() for y in Y] X_ = list(zip(X_flat, G, [2 * 2 for x in X_flat])) latent_svm.fit(X_[:20], Y_flat[:20]) assert_array_equal(latent_svm.predict(X_[:20]), Y_flat[:20]) assert_equal(latent_svm.score(X_[:20], Y_flat[:20]), 1) # test that score is not always 1 assert_true(.98 < latent_svm.score(X_[20:], Y_flat[20:]) < 1)
def test_with_crosses_bad_init(): # use less perfect initialization rnd = np.random.RandomState(0) X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2) crf.initialize(X, Y) H_init = crf.init_latent(X, Y) mask = rnd.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) one_slack_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, C=100) clf = LatentSSVM(one_slack_ssvm) clf.fit(X, Y, H_init=H_init) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(.98 < clf.score(X_test, Y_test) < 1)
def test_with_crosses_bad_init(): # use less perfect initialization rnd = np.random.RandomState(0) X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2) crf.initialize(X, Y) H_init = crf.init_latent(X, Y) mask = rnd.uniform(size=H_init.shape) > 0.7 H_init[mask] = 2 * (H_init[mask] / 2) one_slack_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=0.0001, inference_cache=50, C=100) clf = LatentSSVM(one_slack_ssvm) clf.fit(X, Y, H_init=H_init) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(0.98 < clf.score(X_test, Y_test) < 1)
def test_latent_node_boxes_standard_latent(): # learn the "easy" 2x2 boxes dataset. # a 2x2 box is placed randomly in a 4x4 grid # we add a latent variable for each 2x2 patch # that should make the model fairly simple X, Y = toy.make_simple_2x2(seed=1) latent_crf = LatentNodeCRF(n_labels=2, inference_method='lp', n_hidden_states=2, n_features=1) one_slack = OneSlackSSVM(latent_crf) n_slack = StructuredSVM(latent_crf) subgradient = SubgradientSSVM(latent_crf, max_iter=100, learning_rate=0.01, momentum=0) for base_svm in [one_slack, n_slack, subgradient]: base_svm.C = 10 latent_svm = LatentSSVM(base_svm, latent_iter=10) G = [make_grid_edges(x) for x in X] # make edges for hidden states: edges = [] node_indices = np.arange(4 * 4).reshape(4, 4) for i, (x, y) in enumerate(itertools.product([0, 2], repeat=2)): for j in xrange(x, x + 2): for k in xrange(y, y + 2): edges.append([i + 4 * 4, node_indices[j, k]]) G = [np.vstack([make_grid_edges(x), edges]) for x in X] # reshape / flatten x and y X_flat = [x.reshape(-1, 1) for x in X] Y_flat = [y.ravel() for y in Y] X_ = zip(X_flat, G, [2 * 2 for x in X_flat]) latent_svm.fit(X_, Y_flat) assert_equal(latent_svm.score(X_, Y_flat), 1)
def test_switch_to_ad3(): # smoketest only # test if switching between qpbo and ad3 works inside latent svm # use less perfect initialization if not get_installed(['qpbo']) or not get_installed(['ad3']): return X, Y = toy.generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] n_labels = 2 crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=2, inference_method='qpbo') H_init = crf.init_latent(X, Y) np.random.seed(0) mask = np.random.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) base_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, max_iter=10000, switch_to='ad3bb', C=10. ** 3) clf = LatentSSVM(base_ssvm) clf.fit(X, Y, H_init=H_init) # we actually switch back from ad3bb to the original assert_equal(clf.model.inference_method, "qpbo") # unfortunately this test only works with ad3 clf.base_ssvm.model.inference_method = 'ad3bb' Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(.98 < clf.score(X_test, Y_test) < 1)
def test_switch_to_ad3(): # smoketest only # test if switching between qpbo and ad3 works inside latent svm # use less perfect initialization if not get_installed(['qpbo']) or not get_installed(['ad3']): return X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2, inference_method='qpbo') crf.initialize(X, Y) H_init = crf.init_latent(X, Y) np.random.seed(0) mask = np.random.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) base_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, max_iter=10000, switch_to=('ad3', { 'branch_and_bound': True }), C=10.**3) clf = LatentSSVM(base_ssvm) # evil hackery to get rid of ad3 output try: devnull = open('/dev/null', 'w') oldstdout_fno = os.dup(sys.stdout.fileno()) os.dup2(devnull.fileno(), 1) replaced_stdout = True except: replaced_stdout = False clf.fit(X, Y, H_init=H_init) if replaced_stdout: os.dup2(oldstdout_fno, 1) assert_equal(clf.model.inference_method[0], 'ad3') Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(.98 < clf.score(X_test, Y_test) < 1)
def test_latent_node_boxes_standard_latent_features(): # learn the "easy" 2x2 boxes dataset. # we make it even easier now by adding features that encode the correct # latent state. This basically tests that the features are actually used X, Y = make_simple_2x2(seed=1, n_samples=20, n_flips=6) latent_crf = LatentNodeCRF(n_labels=2, n_hidden_states=2, n_features=1, latent_node_features=True) one_slack = OneSlackSSVM(latent_crf) n_slack = NSlackSSVM(latent_crf) subgradient = SubgradientSSVM(latent_crf, max_iter=100, learning_rate=0.01, momentum=0) for base_svm in [one_slack, n_slack, subgradient]: base_svm.C = 10 latent_svm = LatentSSVM(base_svm, latent_iter=10) G = [make_grid_edges(x) for x in X] # make edges for hidden states: edges = make_edges_2x2() G = [np.vstack([make_grid_edges(x), edges]) for x in X] # reshape / flatten x and y X_flat = [x.reshape(-1, 1) for x in X] # augment X with the features for hidden units X_flat = [ np.vstack([x, y[::2, ::2].reshape(-1, 1)]) for x, y in zip(X_flat, Y) ] Y_flat = [y.ravel() for y in Y] X_ = zip(X_flat, G, [2 * 2 for x in X_flat]) latent_svm.fit(X_[:10], Y_flat[:10]) assert_array_equal(latent_svm.predict(X_[:10]), Y_flat[:10]) assert_equal(latent_svm.score(X_[:10], Y_flat[:10]), 1) # we actually become prefect ^^ assert_true(.98 < latent_svm.score(X_[10:], Y_flat[10:]) <= 1)
def test_states(states, x, y, x_t, y_t, jobs): latent_pbl = GraphLDCRF(n_states_per_label=states, inference_method='qpbo') base_ssvm = NSlackSSVM(latent_pbl, C=1, tol=.01, inactive_threshold=1e-3, batch_size=10, verbose=0, n_jobs=jobs) latent_svm = LatentSSVM(base_ssvm=base_ssvm, latent_iter=3) latent_svm.fit(x, y) test = latent_svm.score(x_t, y_t) train = latent_svm.score(x, y) print states, 'Test:', test, 'Train:', train return test, train
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)
def test_with_crosses_base_svms(): # very simple dataset. k-means init is perfect n_labels = 2 crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=[1, 2]) one_slack = OneSlackSSVM(crf, inference_cache=50) n_slack = NSlackSSVM(crf) subgradient = SubgradientSSVM(crf, max_iter=400, learning_rate=.01, decay_exponent=0, decay_t0=10) X, Y = generate_crosses(n_samples=10, noise=5, n_crosses=1, total_size=8) for base_ssvm in [one_slack, n_slack, subgradient]: base_ssvm.C = 100. clf = LatentSSVM(base_ssvm=base_ssvm) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) assert_equal(clf.score(X, Y), 1)
def test_latent_node_boxes_standard_latent_features(): # learn the "easy" 2x2 boxes dataset. # we make it even easier now by adding features that encode the correct # latent state. This basically tests that the features are actually used X, Y = make_simple_2x2(seed=1, n_samples=20, n_flips=6) latent_crf = LatentNodeCRF(n_labels=2, n_hidden_states=2, n_features=1, latent_node_features=True) one_slack = OneSlackSSVM(latent_crf) n_slack = NSlackSSVM(latent_crf) subgradient = SubgradientSSVM(latent_crf, max_iter=100, learning_rate=0.01, momentum=0) for base_svm in [one_slack, n_slack, subgradient]: base_svm.C = 10 latent_svm = LatentSSVM(base_svm, latent_iter=10) G = [make_grid_edges(x) for x in X] # make edges for hidden states: edges = make_edges_2x2() G = [np.vstack([make_grid_edges(x), edges]) for x in X] # reshape / flatten x and y X_flat = [x.reshape(-1, 1) for x in X] # augment X with the features for hidden units X_flat = [np.vstack([x, y[::2, ::2].reshape(-1, 1)]) for x, y in zip(X_flat, Y)] Y_flat = [y.ravel() for y in Y] X_ = zip(X_flat, G, [2 * 2 for x in X_flat]) latent_svm.fit(X_[:10], Y_flat[:10]) assert_array_equal(latent_svm.predict(X_[:10]), Y_flat[:10]) assert_equal(latent_svm.score(X_[:10], Y_flat[:10]), 1) # we actually become prefect ^^ assert_true(.98 < latent_svm.score(X_[10:], Y_flat[10:]) <= 1)
def test_with_crosses_perfect_init(): # 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 X, Y = 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) clf = LatentSSVM( OneSlackSSVM(model=crf, max_iter=500, C=10, check_constraints=False, break_on_bad=False, inference_cache=50)) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) assert_equal(clf.score(X, Y), 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)
# a CRF problem with no edges. pbl = GraphCRF(inference_method='unary') # We use batch_size=-1 as a binary problem can be solved in one go. svm = NSlackSSVM(pbl, C=1, batch_size=-1) svm.fit(X_train_, y_train) # Now, use a latent-variabile CRF model with SVM training. # 5 states per label is enough capacity to encode the 5 digit classes. latent_pbl = LatentGraphCRF(n_states_per_label=5, inference_method='unary') base_ssvm = NSlackSSVM(latent_pbl, C=1, tol=.01, inactive_threshold=1e-3, batch_size=10) latent_svm = LatentSSVM(base_ssvm=base_ssvm, latent_iter=2) latent_svm.fit(X_train_, y_train) print("Score with binary SVM:") print("Train: {:2.2f}".format(svm.score(X_train_, y_train))) print("Test: {:2.2f}".format(svm.score(X_test_, y_test))) print("Score with latent SVM:") print("Train: {:2.2f}".format(latent_svm.score(X_train_, y_train))) print("Test: {:2.2f}".format(latent_svm.score(X_test_, y_test))) h_pred = np.hstack(latent_svm.predict_latent(X_test_)) print("Latent class counts: %s" % repr(np.bincount(h_pred))) # plot first few digits from each latent class
print("Training score binary grid CRF: %f" % svm.score(asdf, Y_flat)) # using one latent variable for each 2x2 rectangle latent_crf = LatentNodeCRF(n_labels=2, n_features=1, n_hidden_states=2, inference_method='lp') ssvm = OneSlackSSVM(model=latent_crf, max_iter=200, C=100, verbose=1, n_jobs=-1, show_loss_every=10, inference_cache=50) latent_svm = LatentSSVM(ssvm) # make edges for hidden states: edges = [] node_indices = np.arange(4 * 4).reshape(4, 4) for i, (x, y) in enumerate(itertools.product([0, 2], repeat=2)): for j in xrange(x, x + 2): for k in xrange(y, y + 2): edges.append([i + 4 * 4, node_indices[j, k]]) G = [np.vstack([make_grid_edges(x), edges]) for x in X] # Random initialization H_init = [ np.hstack([y.ravel(), np.random.randint(2, 4, size=2 * 2)]) for y in Y ]
pbl = GraphCRF(inference_method='unary') # We use batch_size=-1 as a binary problem can be solved in one go. svm = NSlackSSVM(pbl, C=1, batch_size=-1) svm.fit(X_train_, y_train) # Now, use a latent-variabile CRF model with SVM training. # 5 states per label is enough capacity to encode the 5 digit classes. latent_pbl = LatentGraphCRF(n_states_per_label=5, inference_method='unary') base_ssvm = NSlackSSVM(latent_pbl, C=1, tol=.01, inactive_threshold=1e-3, batch_size=10) latent_svm = LatentSSVM(base_ssvm=base_ssvm, latent_iter=2) latent_svm.fit(X_train_, y_train) print("Score with binary SVM:") print("Train: {:2.2f}".format(svm.score(X_train_, y_train))) print("Test: {:2.2f}".format(svm.score(X_test_, y_test))) print("Score with latent SVM:") print("Train: {:2.2f}".format(latent_svm.score(X_train_, y_train))) print("Test: {:2.2f}".format(latent_svm.score(X_test_, y_test))) h_pred = np.hstack(latent_svm.predict_latent(X_test_)) print("Latent class counts: %s" % repr(np.bincount(h_pred))) # plot first few digits from each latent class
svm = NSlackSSVM(model=crf, max_iter=200, C=1, n_jobs=1) G = [make_grid_edges(x) for x in X] X_grid_edges = list(zip(X_flat, G)) svm.fit(X_grid_edges, Y_flat) plot_boxes(svm.predict(X_grid_edges), title="Non-latent SSVM predictions") print("Training score binary grid CRF: %f" % svm.score(X_grid_edges, Y_flat)) # using one latent variable for each 2x2 rectangle latent_crf = LatentNodeCRF(n_labels=2, n_features=1, n_hidden_states=2, inference_method='lp') ssvm = OneSlackSSVM(model=latent_crf, max_iter=200, C=100, n_jobs=-1, show_loss_every=10, inference_cache=50) latent_svm = LatentSSVM(ssvm) # make edges for hidden states: edges = [] node_indices = np.arange(4 * 4).reshape(4, 4) for i, (x, y) in enumerate(itertools.product([0, 2], repeat=2)): for j in range(x, x + 2): for k in range(y, y + 2): edges.append([i + 4 * 4, node_indices[j, k]]) G = [np.vstack([make_grid_edges(x), edges]) for x in X] # Random initialization H_init = [np.hstack([y.ravel(), np.random.randint(2, 4, size=2 * 2)]) for y in Y] plot_boxes(H_init, title="Top: Random initial hidden states. Bottom: Ground"
X_train_, X_test_, X_train, X_test, y_train, y_test = \ train_test_split(X_, X, Y, test_size=.5) # first, do it with a standard CRF / SVM pbl = GraphCRF(n_features=2, n_states=2, inference_method='lp') svm = StructuredSVM(pbl, verbose=1, check_constraints=True, C=100, n_jobs=1) svm.fit(X_train_, y_train) y_pred = np.vstack(svm.predict(X_test_)) print("Score with pystruct crf svm: %f" % np.mean(y_pred == y_test)) # now with latent CRF SVM latent_pbl = LatentGraphCRF(n_features=2, n_labels=2, n_states_per_label=2, inference_method='lp') latent_svm = LatentSSVM(latent_pbl, verbose=1, check_constraints=True, C=100, n_jobs=1) # we explicitly initialize randomly to make it a bit more interesting latent_svm.fit(X_train_, y_train, H_init=np.random.randint(2, size=y_train.shape) + 2 * y_train) y_pred_latent = np.vstack(latent_svm.predict(X_test_)) # plot the results fig, axes = plt.subplots(1, 5, figsize=(12, 4)) cm = plt.cm.Paired axes[0].set_title("Ground truth") axes[0].scatter(X_test[:, 0], X_test[:, 1], c=y_test, s=50, cmap=cm) axes[1].set_title("SVM prediction") axes[1].scatter(X_test[:, 0], X_test[:, 1], c=y_pred, s=50, cmap=cm) axes[2].set_title("Latent SVM prediction") axes[2].scatter(X_test[:, 0], X_test[:, 1], c=y_pred_latent // 2, s=50, cmap=cm)
def trainModel_Latent(num_iter=5, latent_iter=3, inference="qpbo", trainer="NSlack", num_train=2, num_test=1, C=0.1, edges="180x180_dist1_diag0", inputs=[1, 1, 1, 1, 1, 1], features="all", directed=False, savePred=False): padding = (30, 30, 30, 30) if directed == True: features += '+directed' resultsDir = os.getcwd() + '/CRFResults' nameLen = len(os.listdir(resultsDir)) edgeFeature = edges filename = str(nameLen) + '_CRF_iter_' + str( num_iter ) + "_" + inference + "_" + trainer + "_" + features + "_" + str( num_train) + "_" + str(num_test) + "_" + edgeFeature print "Loading training slices" start = time.clock() train = extractSlices2(train_path, num_train, padding, inputs=inputs) end = time.clock() train_load_time = (end - start) / 60.0 [trainLayers, trainTruth, sliceShape] = train print "Training slices loaded in %f" % (train_load_time) n_features = len(trainLayers[0][0, 0]) print "Layer shape is : " print trainLayers[0].shape print "Training the model" edges = np.load("/home/bmi/CRF/edges/" + edges + ".npy") G = [edges for x in trainLayers] print trainLayers[0].shape trainLayers = np.array([ x.reshape((sliceShape[0] * sliceShape[1], n_features)) for x in trainLayers ]) trainTruth = np.array([ x.reshape((sliceShape[0] * sliceShape[1], )).astype(int) for x in trainTruth ]) if inference == 'ogm': crf = GraphCRF(inference_method=('ogm', { 'alg': 'fm' }), directed=directed) else: crf = LatentGraphCRF(n_states_per_label=2, inference_method=inference) if trainer == "Frank": base_svm = FrankWolfeSSVM(model=crf, max_iter=num_iter, C=C, n_jobs=-1, verbose=1) svm = LatentSSVM(base_ssvm=base_svm) elif trainer == "NSlack": base_svm = NSlackSSVM(model=crf, max_iter=num_iter, C=C, n_jobs=-1, verbose=1) svm = LatentSSVM(base_ssvm=base_svm, latent_iter=latent_iter) else: svm = OneSlackSSVM(model=crf, max_iter=num_iter, C=C, n_jobs=-1, verbose=1) start = time.clock() asdf = zip(trainLayers, G) svm.fit(asdf, trainTruth) end = time.clock() train_time = (end - start) / 60.0 print "The training took %f" % (train_time) print "Model parameter size :" print svm.w.shape print "making predictions on train data" predTrain = svm.predict(asdf) trainDice = [] for i in range(len(trainLayers)): diceScore = accuracy(predTrain[i], trainTruth[i]) trainDice.append(diceScore) meanTrainDice = sum(trainDice) / len(trainLayers) del trainLayers, trainTruth ################################################################################################ overallDicePerPatient = [] # For overall test Dice extDicePerPatient = [] PatientTruthLayers = [] PatientPredLayers = [] PREC = [] RECALL = [] F1 = [] LayerwiseDiceTotal = [] testResultFile = open(os.getcwd() + "/CRFResults/" + filename + ".csv", 'a') testResultFile.write( "folderName,numLayers, Overall Dice, precision , recall, F1" + "\n") counter = 0 print "Loading the test slices" for folder in os.listdir(test_path): path = test_path + "/" + folder layerDiceScores = '' # print path data = extractTestSlices2(path, padding, inputs=inputs) if data != 0: [testLayers, testTruth, sliceShape, startSlice, endSlice] = data # trueTestLayers=testLayers GTest = [edges for x in testLayers] testLayers = np.array([ x.reshape((sliceShape[0] * sliceShape[1], n_features)) for x in testLayers ]) testTruth = np.array([ x.reshape((sliceShape[0] * sliceShape[1], )).astype(int) for x in testTruth ]) asdfTest = zip(testLayers, GTest) predTest = svm.predict(asdfTest) LayerwiseDice = [] for i in range(len(testLayers)): diceScore = accuracy(predTest[i], testTruth[i]) layerDiceScores += "," + str(diceScore) if math.isnan(diceScore): if sum(predTest[i]) == 0 and sum(testTruth[i]) == 0: LayerwiseDice.append(1.0) continue LayerwiseDice.append(diceScore) LayerwiseDiceTotal.append(LayerwiseDice) overallTestDice = accuracy(np.hstack(predTest), np.hstack(testTruth)) extDice = np.mean( np.array(LayerwiseDice) [range(10) + range(len(LayerwiseDice) - 10, len(LayerwiseDice))]) prec, recall, f1 = precision_score(np.hstack(testTruth), np.hstack(predTest)), recall_score( np.hstack(testTruth), np.hstack(predTest)), f1_score( np.hstack(testTruth), np.hstack(predTest)) print "Patient %d : Overall test DICE for %s is : %f and extDice is %f" % ( counter, folder, overallTestDice, extDice) print "Precision : %f Recall : %f F1 : %f " % (prec, recall, f1) print "__________________________________________" # testResultFile.write(folder+","+str(len(testLayers))+","+str(meanTestDice)+","+str(overallTestDice) ","+str(np.max(testDice)) +","+ str(np.min(testDice))+"\n" ) testResultFile.write(folder + "," + str(len(testLayers)) + "," + str(overallTestDice) + "," + str(prec) + "," + str(recall) + "," + str(extDice) + layerDiceScores + "\n") overallDicePerPatient.append(overallTestDice) extDicePerPatient.append(extDice) PREC.append(prec), RECALL.append(recall), F1.append(f1) PatientTruthLayers.append(testTruth) PatientPredLayers.append(predTest) counter += 1 if counter == num_test and num_test != -1: break ###################################################################################################### print "Done testing slices" overallDice = sum(overallDicePerPatient) / len(PatientTruthLayers) overallPrec = sum(PREC) / len(PatientTruthLayers) overallRecall = sum(RECALL) / len(PatientTruthLayers) overallExtDice = np.mean(extDicePerPatient) print "Overall DICE : %f Precision : %f Recall : %f extDice : %f " % ( overallDice, overallPrec, overallRecall, overallExtDice) print "############################################" # testOutput=np.array([PatientPredLayers,PatientTruthLayers,trueTestLayers]) testOutput = np.array([PatientPredLayers, PatientTruthLayers]) ########### Saving the models ###################################################################### # print "Saving the model" # modelDir = os.getcwd()+"/CRFModel/" # svmModel = open(modelDir+filename+"_model"+".pkl",'wb') # cPickle.dump(svm,svmModel,protocol=cPickle.HIGHEST_PROTOCOL) # svmModel.close() # # print "saving the predictions" # predFileTest = open(os.getcwd()+"/CRFPred/"+filename+"_pred.pkl",'wb') # cPickle.dump(testOutput,predFileTest,protocol=cPickle.HIGHEST_PROTOCOL) # predFileTest.close() layerDataLog = open(os.getcwd() + "/CRFModel/" + filename + "_layer.pkl", 'wb') cPickle.dump(LayerwiseDiceTotal, layerDataLog, protocol=cPickle.HIGHEST_PROTOCOL) layerDataLog.close() resultLog = os.getcwd() + "/CRFResults/TestResultFinal.csv" resultFile = open(resultLog, 'a') resultFile.write(time.ctime() + "," + str(num_iter) + "," + str(num_train) + "," + str(num_test) + "," + inference + "," + trainer + "," + str(C) + "," + str(train_time) + "," + str(meanTrainDice) + "," + str(overallDice) + "," + str(np.std(overallDicePerPatient)) + "," + edgeFeature + "," + "None" + "," + features + "," + filename + "," + str(overallPrec) + "," + str(overallRecall) + "," + str(overallExtDice) + "," + "Flair+T2Latent with 2 states" + "\n") resultFile.close() testResultFile.close() return
from pystruct.models import LatentGridCRF from pystruct.learners import LatentSSVM, OneSlackSSVM from pystruct.datasets import generate_crosses X, Y = 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) crf = LatentGridCRF(n_states_per_label=[1, 2]) base_ssvm = OneSlackSSVM(model=crf, C=10., n_jobs=-1, inference_cache=20, tol=.1) clf = LatentSSVM(base_ssvm=base_ssvm) clf.fit(X_train, Y_train) print("Score training set: %f" % clf.score(X_train, Y_train)) print("Score test set: %f" % clf.score(X_test, Y_test)) Y_pred = clf.predict(X_test) x, y, y_pred = X_test[1], Y_test[1], Y_pred[1] 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") ax[1, 0].set_visible(False) ax[1, 1].matshow(crf.latent(x, y, clf.w), vmin=0, vmax=crf.n_states - 1) ax[1, 1].set_title("latent final")
from sklearn.cross_validation import train_test_split from pystruct.models import LatentGridCRF from pystruct.learners import LatentSSVM, OneSlackSSVM from pystruct.datasets import generate_crosses X, Y = 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, allow_nd=True) crf = LatentGridCRF(n_states_per_label=[1, 2]) base_ssvm = OneSlackSSVM(model=crf, C=10., n_jobs=-1, inference_cache=20, tol=.1) clf = LatentSSVM(base_ssvm=base_ssvm) clf.fit(X_train, Y_train) print("loss training set: %f" % clf.score(X_train, Y_train)) print("loss test set: %f" % clf.score(X_test, Y_test)) Y_pred = clf.predict(X_test) x, y, y_pred = X_test[1], Y_test[1], Y_pred[1] 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") ax[1, 0].set_visible(False) ax[1, 1].matshow(crf.latent(x, y, clf.w),