X_train_, X_test_, X_train, X_test, y_train, y_test, y_org_train, y_org_test =\ train_test_split(X_, X, Y, y_org, test_size=.5) # first, do it with a standard CRF / SVM pbl = GraphCRF(n_features=64, n_states=2, inference_method='lp') svm = StructuredSVM(pbl, verbose=1, check_constraints=True, C=1000, n_jobs=1, batch_size=-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)) print(svm.score(X_train_, y_train)) print(svm.score(X_test_, y_test)) # now with latent CRF SVM latent_pbl = LatentGraphCRF(n_features=64, n_labels=2, n_states_per_label=5, inference_method='dai') latent_svm = LatentSubgradientSSVM(model=latent_pbl, max_iter=5000, C=1, verbose=2, n_jobs=1, learning_rate=0.1, show_loss_every=10, momentum=0.0, decay_exponent=0.5) #latent_svm = LatentSSVM(latent_pbl, verbose=2, check_constraints=True, C=100, #n_jobs=1, batch_size=-1, tol=.1, latent_iter=2) latent_svm.fit(X_train_, y_train) print(latent_svm.score(X_train_, y_train)) print(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)))
#inactive_window=0, learning_rate=0.01, momentum=0) latent_svm = LatentSubgradientSSVM(model=latent_crf, max_iter=200, C=100, verbose=1, n_jobs=1, show_loss_every=10, learning_rate=0.01, momentum=0) # 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] #G = [make_grid_edges(x) for x in X] #H_init = [np.hstack([y.ravel(), 2 + y[1: -1, 1: -1].ravel()]) for y in Y] H_init = [np.hstack([y.ravel(), np.random.randint(2, 4, size=2 * 2)]) for y in Y] plot_boxes(H_init) X_ = zip(X_flat, G, [2 * 2 for x in X_flat]) latent_svm.fit(X_, Y_flat, H_init) print("Training score with latent nodes: %f " % latent_svm.score(X_, Y_flat)) H = latent_svm.predict_latent(X_) plot_boxes(H) plt.show()