def main(): print("Please be patient. Will take 5-20 minutes.") snakes = load_snakes() X_train, Y_train = snakes['X_train'], snakes['Y_train'] X_train = [one_hot_colors(x) for x in X_train] Y_train_flat = [y_.ravel() for y_ in Y_train] X_train_directions, X_train_edge_features = prepare_data(X_train) # first, train on X with directions only: crf = EdgeFeatureGraphCRF(inference_method='qpbo') ssvm = OneSlackSSVM(crf, inference_cache=50, C=.1, tol=.1, switch_to='ad3', n_jobs=-1) ssvm.fit(X_train_directions, Y_train_flat) # Evaluate using confusion matrix. # Clearly the middel of the snake is the hardest part. X_test, Y_test = snakes['X_test'], snakes['Y_test'] X_test = [one_hot_colors(x) for x in X_test] Y_test_flat = [y_.ravel() for y_ in Y_test] X_test_directions, X_test_edge_features = prepare_data(X_test) Y_pred = ssvm.predict(X_test_directions) print("Results using only directional features for edges") print("Test accuracy: %.3f" % accuracy_score(np.hstack(Y_test_flat), np.hstack(Y_pred))) print(confusion_matrix(np.hstack(Y_test_flat), np.hstack(Y_pred))) # now, use more informative edge features: crf = EdgeFeatureGraphCRF(inference_method='qpbo') ssvm = OneSlackSSVM(crf, inference_cache=50, C=.1, tol=.1, switch_to='ad3', n_jobs=-1) ssvm.fit(X_train_edge_features, Y_train_flat) Y_pred2 = ssvm.predict(X_test_edge_features) print("Results using also input features for edges") print("Test accuracy: %.3f" % accuracy_score(np.hstack(Y_test_flat), np.hstack(Y_pred2))) print(confusion_matrix(np.hstack(Y_test_flat), np.hstack(Y_pred2))) # plot stuff fig, axes = plt.subplots(2, 2) axes[0, 0].imshow(snakes['X_test'][0], interpolation='nearest') axes[0, 0].set_title('Input') y = Y_test[0].astype(np.int) bg = 2 * (y != 0) # enhance contrast axes[0, 1].matshow(y + bg, cmap=plt.cm.Greys) axes[0, 1].set_title("Ground Truth") axes[1, 0].matshow(Y_pred[0].reshape(y.shape) + bg, cmap=plt.cm.Greys) axes[1, 0].set_title("Prediction w/o edge features") axes[1, 1].matshow(Y_pred2[0].reshape(y.shape) + bg, cmap=plt.cm.Greys) axes[1, 1].set_title("Prediction with edge features") for a in axes.ravel(): a.set_xticks(()) a.set_yticks(()) plt.show() from IPython.core.debugger import Tracer Tracer()()
from plot_snakes import one_hot_colors, prepare_data def REPORT(l_Y_GT, lY_Pred, t=None): if t: print "\t( predict DONE IN %.1fs)"%t _flat_GT, _flat_P = (np.hstack([y.ravel() for y in l_Y_GT]), np.hstack([y.ravel() for y in lY_Pred])) confmat = confusion_matrix(_flat_GT, _flat_P) print confmat print "\ttrace =", confmat.trace() print "\tAccuracy= %.3f"%accuracy_score(_flat_GT, _flat_P) print("Please be patient. Learning will take 5-20 minutes.") snakes = load_snakes() X_train, Y_train = snakes['X_train'], snakes['Y_train'] #X_train, Y_train = X_train[:5], Y_train[:5] X_train = [one_hot_colors(x) for x in X_train] Y_train_flat = [y_.ravel() for y_ in Y_train] X_train_directions, X_train_edge_features = prepare_data(X_train) print "%d picture for training"%len(X_train) # Evaluate using confusion matrix. # Clearly the middel of the snake is the hardest part. X_test, Y_test = snakes['X_test'], snakes['Y_test'] X_test = [one_hot_colors(x) for x in X_test] Y_test_flat = [y_.ravel() for y_ in Y_test] X_test_directions, X_test_edge_features = prepare_data(X_test)
def main(): print("Please be patient. Will take 5-20 minutes.") snakes = load_snakes() X_train, Y_train = snakes['X_train'], snakes['Y_train'] X_train = [one_hot_colors(x) for x in X_train] Y_train_flat = [y_.ravel() for y_ in Y_train] X_train_directions, X_train_edge_features = prepare_data(X_train) if 'ogm' in get_installed(): inference = ('ogm', {'alg': 'fm'}) else: inference = 'qpbo' # first, train on X with directions only: crf = EdgeFeatureGraphCRF(inference_method=inference) ssvm = OneSlackSSVM(crf, inference_cache=50, C=.1, tol=.1, max_iter=100, n_jobs=1) ssvm.fit(X_train_directions, Y_train_flat) # Evaluate using confusion matrix. # Clearly the middel of the snake is the hardest part. X_test, Y_test = snakes['X_test'], snakes['Y_test'] X_test = [one_hot_colors(x) for x in X_test] Y_test_flat = [y_.ravel() for y_ in Y_test] X_test_directions, X_test_edge_features = prepare_data(X_test) Y_pred = ssvm.predict(X_test_directions) print("Results using only directional features for edges") print("Test accuracy: %.3f" % accuracy_score(np.hstack(Y_test_flat), np.hstack(Y_pred))) print(confusion_matrix(np.hstack(Y_test_flat), np.hstack(Y_pred))) # now, use more informative edge features: crf = EdgeFeatureGraphCRF(inference_method=inference) ssvm = OneSlackSSVM(crf, inference_cache=50, C=.1, tol=.1, switch_to='ad3', n_jobs=1) ssvm.fit(X_train_edge_features, Y_train_flat) Y_pred2 = ssvm.predict(X_test_edge_features) print("Results using also input features for edges") print("Test accuracy: %.3f" % accuracy_score(np.hstack(Y_test_flat), np.hstack(Y_pred2))) print(confusion_matrix(np.hstack(Y_test_flat), np.hstack(Y_pred2))) # plot stuff fig, axes = plt.subplots(2, 2) axes[0, 0].imshow(snakes['X_test'][0], interpolation='nearest') axes[0, 0].set_title('Input') y = Y_test[0].astype(np.int) bg = 2 * (y != 0) # enhance contrast axes[0, 1].matshow(y + bg, cmap=plt.cm.Greys) axes[0, 1].set_title("Ground Truth") axes[1, 0].matshow(Y_pred[0].reshape(y.shape) + bg, cmap=plt.cm.Greys) axes[1, 0].set_title("Prediction w/o edge features") axes[1, 1].matshow(Y_pred2[0].reshape(y.shape) + bg, cmap=plt.cm.Greys) axes[1, 1].set_title("Prediction with edge features") for a in axes.ravel(): a.set_xticks(()) a.set_yticks(()) plt.show() from IPython.core.debugger import Tracer Tracer()()
# vertical edge_features_directions = edge_list_to_features([right, down]) # edge feature that contains features from the nodes that the edge connects edge_features = np.zeros((edges.shape[0], features.shape[1], 4)) edge_features[:len(right), :, 0] = features[right[:, 0]] edge_features[:len(right), :, 1] = features[right[:, 1]] edge_features[len(right):, :, 0] = features[down[:, 0]] edge_features[len(right):, :, 1] = features[down[:, 1]] edge_features = edge_features.reshape(edges.shape[0], -1) X_directions.append((features, edges, edge_features_directions)) X_edge_features.append((features, edges, edge_features)) return X_directions, X_edge_features print("Please be patient. Will take 5-20 minutes.") snakes = load_snakes() X_train, Y_train = snakes['X_train'], snakes['Y_train'] X_train = [one_hot_colors(x) for x in X_train] Y_train_flat = [y_.ravel() for y_ in Y_train] X_train_directions, X_train_edge_features = prepare_data(X_train) if 'ogm' in get_installed(): inference = ('ogm', {'alg': 'fm'}) else: inference = 'qpbo' # first, train on X with directions only: crf = EdgeFeatureGraphCRF(inference_method=inference) ssvm = OneSlackSSVM(crf, inference_cache=50,
def test_dataset_loading(): # test that we can read the datasets. load_scene() load_letters() load_snakes()