Exemplo n.º 1
0
def main(C=1):
    dataset = NYUSegmentation()
    # load training data
    data_train = load_nyu(n_sp=500, sp='rgbd')
    data_train = add_edges(data_train)
    data_train = add_edge_features(dataset,
                                   data_train,
                                   depth_diff=True,
                                   normal_angles=True)

    data_train = discard_void(dataset, data_train)

    n_states = 4.
    print("number of samples: %s" % len(data_train.X))
    class_weights = 1. / np.bincount(np.hstack(data_train.Y))
    class_weights *= n_states / np.sum(class_weights)
    #class_weights = np.ones(n_states)
    print(class_weights)
    #model = crfs.GraphCRF(n_states=n_states,
    #n_features=data_train.X[0][0].shape[1],
    #inference_method='qpbo', class_weight=class_weights)
    model = crfs.EdgeFeatureGraphCRF(inference_method='qpbo',
                                     class_weight=class_weights,
                                     n_edge_features=5,
                                     symmetric_edge_features=[0, 1])
    experiment_name = "rgbd_test%f" % C
    ssvm = learners.OneSlackSSVM(model,
                                 verbose=2,
                                 C=C,
                                 max_iter=100000,
                                 n_jobs=-1,
                                 tol=0.001,
                                 show_loss_every=100,
                                 inference_cache=50,
                                 cache_tol='auto',
                                 logger=SaveLogger(experiment_name + ".pickle",
                                                   save_every=100),
                                 inactive_threshold=1e-5,
                                 break_on_bad=False,
                                 inactive_window=50,
                                 switch_to=("ad3", {
                                     'branch_and_bound': True
                                 }))

    ssvm.fit(data_train.X, data_train.Y)
    print("fit finished!")
    return
Exemplo n.º 2
0
def main():
    ds = PascalSegmentation()
    # load training data
    edge_type = "pairwise"
    which = "kTrain"
    data_train = load_pascal(which=which, sp_type="cpmc")

    data_train = add_edges(data_train, edge_type)
    data_train = add_edge_features(ds, data_train)
    data_train = discard_void(ds, data_train, ds.void_label)

    X, Y = data_train.X, data_train.Y

    class_weights = 1. / np.bincount(np.hstack(Y))
    class_weights *= 21. / np.sum(class_weights)

    model = crfs.EdgeFeatureGraphCRF(class_weight=class_weights,
                                     symmetric_edge_features=[0, 1],
                                     antisymmetric_edge_features=[2],
                                     inference_method='qpbo')

    ssvm = learners.NSlackSSVM(model, C=0.01, n_jobs=-1)
    ssvm.fit(X, Y)
Exemplo n.º 3
0
def main(C=1, test=False):
    ds = PascalSegmentation()
    # load training data
    edge_type = "pairwise"
    if test:
        which = "train"
    else:
        which = "kTrain"
    data_train = load_pascal(which=which, sp_type="cpmc")

    data_train = add_edges(data_train, edge_type)
    data_train = add_edge_features(ds, data_train)
    data_train = discard_void(ds, data_train, ds.void_label)

    print("number of samples: %s" % len(data_train.X))
    class_weights = 1. / np.bincount(np.hstack(data_train.Y))
    class_weights *= 21. / np.sum(class_weights)
    print(class_weights)
    #model = crfs.GraphCRF(n_states=n_states,
    #n_features=data_train.X[0][0].shape[1],
    #inference_method='qpbo', class_weight=class_weights)
    model = crfs.EdgeFeatureGraphCRF(inference_method='qpbo',
                                     class_weight=class_weights,
                                     symmetric_edge_features=[0, 1],
                                     antisymmetric_edge_features=[2])
    experiment_name = "cpmc_edge_features_trainval_new_%f" % C
    #warm_start = True
    warm_start = False
    ssvm = learners.OneSlackSSVM(model,
                                 verbose=2,
                                 C=C,
                                 max_iter=100000,
                                 n_jobs=-1,
                                 tol=0.0001,
                                 show_loss_every=50,
                                 inference_cache=50,
                                 cache_tol='auto',
                                 logger=SaveLogger(experiment_name + ".pickle",
                                                   save_every=100),
                                 inactive_threshold=1e-5,
                                 break_on_bad=False,
                                 inactive_window=50,
                                 switch_to=None)
    #ssvm = learners.SubgradientSSVM(
    #model, verbose=3, C=C, max_iter=10000, n_jobs=-1, show_loss_every=10,
    #logger=SaveLogger(experiment_name + ".pickle", save_every=10),
    #momentum=0, learning_rate=0.1, decay_exponent=1, decay_t0=100)

    if warm_start:
        ssvm = SaveLogger(experiment_name + ".pickle").load()
        ssvm.logger = SaveLogger(file_name=experiment_name + "_refit.pickle",
                                 save_every=10)
        #ssvm.learning_rate = 0.000001

        ssvm.model.inference_method = 'ad3bb'
        #ssvm.n_jobs = 1

    ssvm.fit(data_train.X, data_train.Y, warm_start=warm_start)
    return

    print("fit finished!")
    if test:
        data_val = load_pascal('val')
    else:
        data_val = load_pascal('kVal')

    data_val = add_edges(data_val, edge_type)
    data_val = add_edge_features(ds, data_val, more_colors=True)
    eval_on_sp(ds, data_val, ssvm.predict(data_val.X), print_results=True)
Exemplo n.º 4
0
from pystruct import learners
import pystruct.models as crfs
from pystruct.utils import SaveLogger

data_train = cPickle.load(open("data_train_dict.pickle"))
C = 0.01

n_states = 21
print("number of samples: %s" % len(data_train['X']))
class_weights = 1. / np.bincount(np.hstack(data_train['Y']))
class_weights *= 21. / np.sum(class_weights)
print(class_weights)

model = crfs.EdgeFeatureGraphCRF(inference_method='qpbo',
                                 class_weight=class_weights,
                                 symmetric_edge_features=[0, 1],
                                 antisymmetric_edge_features=[2])

experiment_name = "edge_features_one_slack_trainval_%f" % C

ssvm = learners.NSlackSSVM(model,
                           verbose=2,
                           C=C,
                           max_iter=100000,
                           n_jobs=-1,
                           tol=0.0001,
                           show_loss_every=5,
                           logger=SaveLogger(experiment_name + ".pickle",
                                             save_every=100),
                           inactive_threshold=1e-3,
                           inactive_window=10,
Exemplo n.º 5
0
    import pickle
import pdb, time, os
from pystruct import learners
import pystruct.models as crfs
from pystruct.utils import SaveLogger
output_folder = 'out'
method = 'max-product'
evaluate_val = True
import cv2

data_train = pickle.load(open("train_data_davis_sub.p", "rb"))
C = 0.01
n_states = 2
print("number of samples: %s" % len(data_train['X']))

model = crfs.EdgeFeatureGraphCRF(inference_method=method)

experiment_name = "edge_features_one_slack_trainval_%f" % C

ssvm = learners.NSlackSSVM(
    model, verbose=2, C=C, max_iter=25, n_jobs=-1,
    tol=0.0001, show_loss_every=5,
    logger=SaveLogger(experiment_name + ".pickle", save_every=5),
    inactive_threshold=1e-3, inactive_window=10, batch_size=100)
start = time.time()
ssvm.fit(data_train['X'], data_train['Y'])
print('average training time: %f s/iter'%((time.time()-start)/25.0))
data_val = pickle.load(open("val_data_davis_sub.p", "rb"))
start = time.time()
if evaluate_val:
    y_pred = ssvm.predict(data_val['X'])
Exemplo n.º 6
0
def main(C=1, test=False):
    # load training data
    #independent = True
    independent = False
    data_train = load_data(which="piecewise")
    data_train = add_edges(data_train,
                           independent=independent,
                           fully_connected=True)
    data_train = add_kraehenbuehl_features(data_train, which="train_30px")
    data_train = add_kraehenbuehl_features(data_train, which="train")

    #data_train = load_data_global_probs()

    if not independent:
        data_train = add_edge_features(data_train)

    data_train = discard_void(data_train, 21)

    if test:
        data_val = load_data("val", which="piecewise_train")
        data_val = add_edges(data_val, independent=independent)
        data_val = add_kraehenbuehl_features(data_val, which="train_30px")
        data_val = add_kraehenbuehl_features(data_val, which="train")
        data_val = add_edge_features(data_val)
        data_val = discard_void(data_val, 21)
        data_train = concatenate_datasets(data_train, data_val)

        #X_.extend(data_val.X)
        #Y_.extend(data_val.Y)

    n_states = 21
    print("number of samples: %s" % len(data_train.X))
    class_weights = 1. / np.bincount(np.hstack(data_train.Y))
    #class_weights[21] = 0
    class_weights *= 21. / np.sum(class_weights)
    #class_weights = np.ones(n_states)
    print(class_weights)
    #model = crfs.GraphCRF(n_states=n_states,
    #n_features=data_train.X[0][0].shape[1],
    #inference_method='qpbo', class_weight=class_weights)
    model = crfs.EdgeFeatureGraphCRF(n_states=n_states,
                                     n_features=data_train.X[0][0].shape[1],
                                     inference_method='qpbo',
                                     class_weight=class_weights,
                                     n_edge_features=3,
                                     symmetric_edge_features=[0, 1],
                                     antisymmetric_edge_features=[2])
    experiment_name = "fully_connected_%f" % C
    #warm_start = True
    warm_start = False
    ssvm = learners.OneSlackSSVM(model,
                                 verbose=2,
                                 C=C,
                                 max_iter=100000,
                                 n_jobs=-1,
                                 tol=0.0001,
                                 show_loss_every=50,
                                 inference_cache=50,
                                 cache_tol='auto',
                                 logger=SaveLogger(experiment_name + ".pickle",
                                                   save_every=100),
                                 inactive_threshold=1e-5,
                                 break_on_bad=False,
                                 inactive_window=50,
                                 switch_to_ad3=False)
    #ssvm = learners.SubgradientSSVM(
    #model, verbose=3, C=C, max_iter=10000, n_jobs=-1, show_loss_every=10,
    #logger=SaveLogger(experiment_name + ".pickle", save_every=10),
    #momentum=0, learning_rate=0.001, decay_exponent=1)

    if warm_start:
        ssvm = SaveLogger(experiment_name + ".pickle").load()
        ssvm.logger = SaveLogger(file_name=experiment_name + "_refit.pickle",
                                 save_every=10)
        ssvm.learning_rate = 0.000001
        #ssvm.model.inference_method = 'ad3'
        #ssvm.n_jobs = 1

    ssvm.fit(data_train.X, data_train.Y, warm_start=warm_start)
    print("fit finished!")
    return
Exemplo n.º 7
0
    # split train and test data
    test_ratio = 0.2
    X_train, X_test, y_train, y_test = crosval.train_test_split(
        X, y, test_size=test_ratio, random_state=0)

    # setup parameter
    n_states = np.unique(np.hstack(y)).shape[0]
    class_weight = 1. / np.bincount(np.hstack(y))
    class_weight *= float(n_states) / np.sum(class_weight)
    n_jobs = 6
    C = 0.01

    # init CRF model
    # model = models.EdgeFeatureGraphCRF(inference_method='qpbo', class_weight=class_weight, symmetric_edge_features=[0, 1], antisymmetric_edge_features=[2, 3])
    model = models.EdgeFeatureGraphCRF(inference_method='qpbo',
                                       class_weight=class_weight,
                                       symmetric_edge_features=[0, 1, 2],
                                       antisymmetric_edge_features=[3, 4])
    # model = models.EdgeFeatureGraphCRF(class_weight=class_weight, symmetric_edge_features=[0, 1], antisymmetric_edge_features=[2, 3])

    # init learner
    ssvm = learners.NSlackSSVM(model,
                               verbose=2,
                               n_jobs=n_jobs,
                               C=C,
                               logger=SaveLogger(args.model_filename,
                                                 save_every=50))
    # ssvm = learners.NSlackSSVM(model, verbose=2, C=C, max_iter=100000, n_jobs=n_jobs, tol=0.0001, show_loss_every=5, logger=SaveLogger(args.model_filename, save_every=50), inactive_threshold=1e-3, inactive_window=10, batch_size=100)

    # train model
    ssvm.fit(X_train, y_train)
Exemplo n.º 8
0
                                                dtype=np.int), edgesFeatures))
test_count = 0
test_Y = []
for i in range(0, len(test_edges)):
    labels = np.zeros([1, test_edges[i][0].shape[0]], dtype=int)
    for j in range(0, test_edges[i][0].shape[0]):
        labels[0][j] = test_labels[test_count + j].astype(int)
    test_count = test_count + test_edges[i][0].shape[0]
    test_Y.append(labels[0])
C = 0.01
n_states = 2
class_weights = 1. / np.bincount(np.hstack(valid_Y))
class_weights *= 2. / np.sum(class_weights)
print(class_weights)

model = crfs.EdgeFeatureGraphCRF(class_weight=class_weights)

experiment_name = "edge_features_one_slack_trainval_%f" % C

ssvm = learners.NSlackSSVM(model,
                           verbose=2,
                           C=1,
                           max_iter=1000,
                           n_jobs=-1,
                           tol=0.0001,
                           show_loss_every=5,
                           inactive_threshold=1e-3,
                           inactive_window=10,
                           batch_size=100)
ssvm.fit(X_valid, valid_Y)
Exemplo n.º 9
0
print "Saving Data"
(X_train,Y_train) = cPickle.load(open("/home/prassanna/Development/workspace/CamVid_scripts/FrameworkDump3/model/crf_data_200.pkl","r"))

#Do Some Data modifying operations
#Energy * -1 perhaps?

print "Initializing CRF"
nr_states= 12;
class_counts = np.bincount(np.hstack(Y_train))
class_frequency = 1./ class_counts;
class_weights = class_frequency*(nr_states/np.sum(class_frequency))

C = 0.01
experiment_name = "edge_features_one_slack_trainval_%f" % C
model = crfs.EdgeFeatureGraphCRF( n_states = nr_states, inference_method='max-product',
                                 class_weight=class_weights,
                                 symmetric_edge_features=[0])
ssvm = learners.OneSlackSSVM(
    model, verbose=2, C=C, max_iter=100000, n_jobs=-1,
    tol=0.0001, show_loss_every=5,
    logger=SaveLogger(experiment_name + ".pickle", save_every=100),
    inactive_threshold=1e-3, inactive_window=10)                                 

#Fit CRF
ssvm.fit(X_train, Y_train)

cPickle.dump(ssvm, open("/home/prassanna/Development/workspace/CamVid_scripts/FrameworkDump3/model/trained_crf_fromdata.pkl", "w"))