Ejemplo n.º 1
0
def main(C=1):
    dataset = NYUSegmentation()
    # load training data
    data_train = load_nyu('train', 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 = make_hierarchical_data(dataset, data_train)
    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.EdgeFeatureLatentNodeCRF(n_hidden_states=5,
                                          n_edge_features=5,
                                          inference_method='qpbo',
                                          class_weight=class_weights,
                                          symmetric_edge_features=[0, 1],
                                          latent_node_features=False,
                                          n_labels=4)
    experiment_name = "rgbd_normal_angles_fold1_strong_reweight%f" % C
    base_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
                                      }))
    latent_logger = SaveLogger("lssvm_" + experiment_name + "_%d.pickle",
                               save_every=1)
    ssvm = learners.LatentSSVM(base_ssvm, logger=latent_logger, latent_iter=3)

    ssvm.fit(data_train.X, data_train.Y)
    print("fit finished!")
    return
def train(trainSetX, trainSetY, testSetX, testSetY):
    modelLogger = SaveLogger('imagesegmentation-horse-hog_96_lbp_test.model',
                             save_every=1)

    # Load trained CRF model
    print 'Loading trained model for  CRF'
    #clf = modelLogger.load()

    # Uncomment if we want to train from scratch first layer CRF
    print 'Training CRF...'
    start_time = time.time()
    crf = EdgeFeatureGraphCRF()  #antisymmetric_edge_features=[1,2]
    clf = FrankWolfeSSVM(model=crf,
                         C=10.,
                         tol=.1,
                         verbose=3,
                         show_loss_every=1,
                         logger=modelLogger)  # #max_iter=50
    ##clf = OneSlackSSVM(model=crf, verbose=1, show_loss_every=1, logger=modelLogger)
    clf.fit(numpy.array(trainSetX), numpy.array(trainSetY))
    print 'Training CRF took ' + str(time.time() - start_time) + ' seconds'

    #print("Overall super pixelwise accuracy (training set): %f" % clf.score(numpy.array(trainSetX), numpy.array(trainSetY) ))
    #print("Overall super pixelwise accuracy (test set): %f" % clf.score(numpy.array(testSetX), numpy.array(testSetY) ))

    print 'SUPERPIXELWISE ACCURACY'
    print '-----------------------------------------------------------------------'
    print ''
    print 'TRAINING SET RESULTS'
    train_ypred = evaluatePerformance(clf, numpy.array(trainSetX),
                                      numpy.array(trainSetY))
    print ''
    print 'TEST SET RESULTS'
    evaluatePerformance(clf, numpy.array(testSetX), numpy.array(testSetY))
    print '-----------------------------------------------------------------------'
def test_logging():
    iris = load_iris()
    X, y = iris.data, iris.target

    X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X]
    Y = y.reshape(-1, 1)

    X_train, X_test, y_train, y_test = train_test_split(X_, Y, random_state=1)
    _, file_name = mkstemp()

    pbl = GraphCRF(n_features=4, n_states=3, inference_method=inference_method)
    logger = SaveLogger(file_name)
    svm = NSlackSSVM(pbl, C=100, n_jobs=1, logger=logger)
    svm.fit(X_train, y_train)

    score_current = svm.score(X_test, y_test)
    score_auto_saved = logger.load().score(X_test, y_test)

    alt_file_name = file_name + "alt"
    logger.save(svm, alt_file_name)
    logger.file_name = alt_file_name
    logger.load()
    score_manual_saved = logger.load().score(X_test, y_test)

    assert_less(.97, score_current)
    assert_less(.97, score_auto_saved)
    assert_less(.97, score_manual_saved)
    assert_almost_equal(score_auto_saved, score_manual_saved)
Ejemplo n.º 4
0
def plot_results():
    data = load_data("val", independent=False)
    data = make_hierarchical_data(data, lateral=False, latent=True)
    logger = SaveLogger("test_latent_2.0001.pickle", save_every=100)
    ssvm = logger.load()
    plot_results_hierarchy(data,
                           ssvm.predict(data.X),
                           folder="latent_results_val_50_states_no_lateral")
Ejemplo n.º 5
0
 def fresh_train(self, x, y, iterations=10):
     self.model = EdgeFeatureGraphCRF(inference_method="max-product")
     self.learner = SubgradientSSVM(
         model=self.model,
         max_iter=iterations,
         logger=SaveLogger(model_file.format(self.userId + "-learner")))
     self.learner.fit(x, y, warm_start=False)
     self.save()
Ejemplo n.º 6
0
 def fresh_train(self, x, y, iterations=10):
     self.model = ChainCRF(inference_method="max-product")
     self.learner = SubgradientSSVM(
         model=self.model,
         max_iter=iterations,
         logger=SaveLogger(
             MODEL_PATH_TEMPLATE.format(self.userId + "-learner")),
         show_loss_every=50)
     self.learner.fit(x, y, warm_start=False)
     self.save()
Ejemplo n.º 7
0
def test_n_slack_svm_as_crf_pickling():
    iris = load_iris()
    X, y = iris.data, iris.target

    X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X]
    Y = y.reshape(-1, 1)

    X_train, X_test, y_train, y_test = train_test_split(X_, Y, random_state=1)
    _, file_name = mkstemp()

    pbl = GraphCRF(n_features=4, n_states=3, inference_method='lp')
    logger = SaveLogger(file_name)
    svm = NSlackSSVM(pbl, C=100, n_jobs=1, logger=logger)
    svm.fit(X_train, y_train)

    assert_less(.97, svm.score(X_test, y_test))
    assert_less(.97, logger.load().score(X_test, y_test))
Ejemplo n.º 8
0
def test_svm_as_crf_pickling_batch():

    iris = load_iris()
    X, y = iris.data, iris.target

    X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X]
    Y = y.reshape(-1, 1)

    X_train, X_test, y_train, y_test = train_test_split(X_, Y, random_state=1)
    _, file_name = mkstemp()

    pbl = GraphCRF(n_features=4, n_states=3, inference_method='unary')
    logger = SaveLogger(file_name)
    svm = FrankWolfeSSVM(pbl, C=10, logger=logger, max_iter=50, batch_mode=False)
    svm.fit(X_train, y_train)

    assert_less(.97, svm.score(X_test, y_test))
    assert_less(.97, logger.load().score(X_test, y_test))
Ejemplo n.º 9
0
 def load(self, expiration_timestamp=None):
     """
     Load myself from disk
     If an expiration timestamp is given, the model stored on disk must be fresher than timestamp
     return self or raise a ModelException
     """
     super(Model_SSVM_AD3, self).load(expiration_timestamp)
     self.ssvm = self._loadIfFresh(self.getModelFilename(),
                                   expiration_timestamp,
                                   lambda x: SaveLogger(x).load())
     try:
         self._EdgeBaselineModel = self._loadIfFresh(
             self.getEdgeBaselineFilename(), expiration_timestamp,
             self.gzip_cPickle_load)
         self.setNbClass(
             self.ssvm.model.n_states)  #required the compute the edge label
     except:
         self._EdgeBaselineModel = None
     return self
Ejemplo n.º 10
0
def svm_on_segments(C=.1, learning_rate=.001, subgradient=False):
    data_file = "data_train_XY.pickle"
    ds = PascalSegmentation()
    if os.path.exists(data_file):
        X_, Y_ = cPickle.load(open(data_file))
    else:
        # load and prepare data
        data_train = load_pascal("train", sp_type="cpmc")
        data_train = make_cpmc_hierarchy(ds, data_train)
        data_train = discard_void(ds, data_train)
        X_, Y_ = data_train.X, data_train.Y
        cPickle.dump((X_, Y_), open(data_file, 'wb'), -1)

    class_weights = 1. / np.bincount(np.hstack(Y_))
    class_weights *= 21. / np.sum(class_weights)
    experiment_name = ("latent_25_cpmc_%f_qpbo_n_slack_blub3" % C)
    logger = SaveLogger(experiment_name + ".pickle", save_every=10)
    model = LatentNodeCRF(n_hidden_states=25,
                          inference_method='qpbo',
                          class_weight=class_weights,
                          latent_node_features=False)
    if subgradient:
        ssvm = learners.LatentSubgradientSSVM(model,
                                              C=C,
                                              verbose=1,
                                              show_loss_every=10,
                                              logger=logger,
                                              n_jobs=-1,
                                              learning_rate=learning_rate,
                                              decay_exponent=1,
                                              momentum=0.,
                                              max_iter=100000,
                                              decay_t0=100)
    else:
        latent_logger = SaveLogger("lssvm_" + experiment_name + "_%d.pickle",
                                   save_every=1)
        #base_ssvm = learners.OneSlackSSVM(
        #model, verbose=2, C=C, max_iter=100, n_jobs=-1, tol=0.001,
        #show_loss_every=200, inference_cache=50, logger=logger,
        #cache_tol='auto', inactive_threshold=1e-5, break_on_bad=False,
        #switch_to=('ogm', {'alg': 'dd'}))
        base_ssvm = learners.NSlackSSVM(model,
                                        verbose=4,
                                        C=C,
                                        n_jobs=-1,
                                        tol=0.1,
                                        show_loss_every=20,
                                        logger=logger,
                                        inactive_threshold=1e-8,
                                        break_on_bad=False,
                                        batch_size=36,
                                        inactive_window=10,
                                        switch_to=('ad3', {
                                            'branch_and_bound': True
                                        }))
        ssvm = learners.LatentSSVM(base_ssvm,
                                   logger=latent_logger,
                                   latent_iter=3)
    #warm_start = True
    warm_start = False
    if warm_start:
        ssvm = logger.load()
        ssvm.logger = SaveLogger(experiment_name + "_retrain.pickle",
                                 save_every=10)
        ssvm.max_iter = 10000
        ssvm.decay_exponent = 1
        #ssvm.decay_t0 = 1000
        #ssvm.learning_rate = 0.00001
        #ssvm.momentum = 0

    X_, Y_ = shuffle(X_, Y_)
    #ssvm.fit(data_train.X, data_train.Y)
    ssvm.fit(X_, Y_)
    #H_init = [np.hstack([y, np.random.randint(21, 26)]) for y in Y_]
    #ssvm.fit(X_, Y_, H_init=H_init)
    print("fit finished!")
Ejemplo n.º 11
0
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,
                           batch_size=100)
ssvm.fit(data_train['X'], data_train['Y'])

data_val = cPickle.load(open("data_val_dict.pickle"))
y_pred = ssvm.predict(data_val['X'])

# we throw away void superpixels and flatten everything
y_pred, y_true = np.hstack(y_pred), np.hstack(data_val['Y'])
y_pred = y_pred[y_true != 255]
y_true = y_true[y_true != 255]

print("Score on validation set: %f" % np.mean(y_true == y_pred))
Ejemplo n.º 12
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
Ejemplo n.º 13
0
    def train(self,
              lGraph_trn,
              lGraph_vld,
              bWarmStart=True,
              expiration_timestamp=None,
              verbose=0):
        """
        Train a CRF model using the list of labelled graph as training
        if bWarmStart if True, try to continue from previous training, IF the stored model is older than expiration_timestamp!!
            , otherwise, starts from scratch
        return nothing
        """
        if self.bGridSearch:
            return self.gridsearch(lGraph_trn, verbose=verbose)

        traceln("\t- computing features on training set")
        traceln("\t\t #nodes=%d  #edges=%d " %
                Graph.getNodeEdgeTotalNumber(lGraph_trn))
        lX, lY = self.get_lX_lY(lGraph_trn)
        lX_vld, lY_vld = self.get_lX_lY(lGraph_vld)
        bMakeSlim = not bWarmStart  # for warm-start mode, we do not make the model slimer!"

        traceln("\t- retrieving or creating model...")
        self.ssvm = None
        sModelFN = self.getModelFilename()
        if bWarmStart:
            try:
                try:
                    self.ssvm = self._loadIfFresh(
                        sModelFN + "._last_", expiration_timestamp,
                        lambda x: SaveLogger(x).load())
                    traceln(
                        "\t- warmstarting with last saved model (not necessarily best one)!"
                    )
                except:
                    self.ssvm = self._loadIfFresh(
                        sModelFN, expiration_timestamp,
                        lambda x: SaveLogger(x).load())
                    traceln("\t- warmstarting from last best model!")
                #we allow to change the max_iter of the model
                try:
                    self.ssvm.max_iter  #to make sure we do something that makes sense...
                    if self.ssvm.max_iter != self.max_iter:
                        traceln(
                            "\t- changing max_iter value from (stored) %d to %d"
                            % (self.ssvm.max_iter, self.max_iter))
                        self.ssvm.max_iter = self.max_iter
                except AttributeError:
                    traceln("\t- cannot access or change the max_iter value")

                try:
                    self.ssvm.n_jobs  #to make sure we do something that makes sense...
                    if self.ssvm.n_jobs != self.njobs:
                        traceln(
                            "\t- changing n_jobs value from (stored) %d to %d"
                            % (self.ssvm.n_jobs, self.njobs))
                        self.ssvm.n_jobs = self.njobs
                except AttributeError:
                    traceln("\t- cannot access or change the n_jobs value")

            except Exception as e:
                self.ssvm = None
                traceln("\t- Cannot warmstart: %s" % e)
            #self.ssvm is either None or containing a nice ssvm model!!

        chronoOn("train")
        traceln("\t- training graph-based model")
        traceln("\t\t solver parameters:", " inference_cache=",
                self.inference_cache, " C=", self.C, " tol=", self.tol,
                " n_jobs=", self.njobs)

        if not self.ssvm:
            traceln("\t- creating a new SSVM-trained CRF model")

            traceln("\t\t- computing class weight:")
            if self.balanced:
                traceln("\t\tusing balanced weights")
                self.setBalancedWeights()
            clsWeights = self.computeClassWeight(lY)
            traceln("\t\t\t --> %s" % clsWeights)

            #clsWeights = np.array([1, 4.5])
            # These weights are tuned for best performance of LR and SVM and hence consistently used here
            crf = self._getCRFModel(clsWeights)

            self.ssvm = OneSlackSSVM(crf,
                                     inference_cache=self.inference_cache,
                                     C=self.C,
                                     tol=self.tol,
                                     n_jobs=self.njobs,
                                     logger=SaveLogger(
                                         sModelFN, save_every=self.save_every),
                                     max_iter=self.max_iter,
                                     show_loss_every=10,
                                     verbose=verbose)
            bWarmStart = False

        if lGraph_vld:
            self.ssvm.fit_with_valid(lX,
                                     lY,
                                     lX_vld,
                                     lY_vld,
                                     warm_start=bWarmStart,
                                     valid_every=self.save_every)
        else:
            # old classical method
            self.ssvm.fit(lX, lY, warm_start=bWarmStart)
        traceln("\t [%.1fs] done (graph-CRF model is trained) \n" %
                chronoOff("train"))

        #traceln(self.getModelInfo())

        #cleaning useless data that takes MB on disk
        if bMakeSlim:
            self.ssvm.alphas = None
            self.ssvm.constraints_ = None
            self.ssvm.inference_cache_ = None
            traceln(
                "\t\t(model made slimmer. Not sure you can efficiently warm-start it later on. See option -w.)"
            )

        #the baseline model(s) if any
        self._trainBaselines(lX, lY)

        #do some garbage collection
        del lX, lY
        gc.collect()

        return
Ejemplo n.º 14
0
    def gridsearch(self, lGraph, verbose=0):
        """
        do a grid search instead of a normal training
        """
        traceln("--- GRID SEARCH FOR CRF MODEL ---")
        traceln("\t- computing features on training set")
        traceln("\t\t #nodes=%d  #edges=%d " %
                Graph.getNodeEdgeTotalNumber(lGraph))
        chronoOn()
        lX, lY = self.get_lX_lY(lGraph)

        dPrm = {}
        dPrm['C'] = self.C if type(self.C) == list else [self.C]
        dPrm['tol'] = self.tol if type(self.tol) == list else [self.tol]
        dPrm['inference_cache'] = self.inference_cache if type(
            self.inference_cache) == list else [self.inference_cache]
        dPrm['max_iter'] = self.max_iter if type(
            self.max_iter) == list else [self.max_iter]

        traceln("\t- creating a SSVM-trained CRF model")

        traceln("\t\t- computing class weight:")
        clsWeights = self.computeClassWeight(lY)
        traceln("\t\t\t%s" % clsWeights)

        crf = self._getCRFModel(clsWeights)

        self._ssvm = OneSlackSSVM(
            crf
            #, inference_cache=self.inference_cache, C=self.C, tol=self.tol
            ,
            n_jobs=self.njobs
            #, logger=SaveLogger(sModelFN, save_every=self.save_every)
            #, max_iter=self.max_iter
            ,
            show_loss_every=10
            #                            , verbose=verbose)
            ,
            verbose=1)

        self._gs_ssvm = GridSearchCV(self._ssvm,
                                     dPrm,
                                     n_jobs=1,
                                     verbose=verbose)
        self.ssvm = None

        chronoOn()
        traceln("\t - training by grid search a graph-based model")
        traceln("\t\t solver parameters for grid search:", " inference_cache=",
                self.inference_cache, " C=", self.C, " tol=", self.tol,
                " n_jobs=", self.njobs, " max_iter=", self.max_iter)
        self._gs_ssvm.fit(lX, lY)
        traceln(
            "\t [%.1fs] done (graph-based model is trained with best parameters, selected by grid search) \n"
            % chronoOff())

        self.ssvm = self._gs_ssvm.best_estimator_  #Estimator that was chosen by the search

        try:
            #win32
            dBestParams = self._gs_ssvm.best_params_
        except:
            #do not know how to get this... in
            dBestParams = {
                'C': self.ssvm.C,
                'inference_cache': self.ssvm.inference_cache,
                'max_iter': self.ssvm.max_iter,
                'tol': self.ssvm.tol
            }

        self.storeBestParams(dBestParams)
        traceln("\t", "- " * 20)
        traceln("\tBest parameters: ", dBestParams)
        traceln("\t", "- " * 20)

        try:
            self.ssvm.alphas = None
            self.ssvm.constraints_ = None
            self.ssvm.inference_cache_ = None
            traceln(
                "\t\t(model made slimmer. Not sure you can efficiently warm-start it later on. See option -w.)"
            )
        except Exception as e:
            traceln(
                "\t\t(COULD NOT make the model slimmer. Got exception: %s" %
                str(e))

        logger = SaveLogger(self.getModelFilename())
        logger(self.ssvm)  #save this model!

        traceln(self.getModelInfo())

        #Also save the details of this grid search
        sFN = self.getModelFilename()[:-4] + "GridSearchCV.pkl"
        try:
            self.gzip_cPickle_dump(sFN, self._gs_ssvm)
            traceln("\n\n--- GridSearchCV details: (also in %s)" % sFN)
            traceln("--- Best parameters set found on development set:")
            traceln(self._gs_ssvm.best_params_)
            traceln("--- Grid scores on development set:")
            means = self._gs_ssvm.cv_results_['mean_test_score']
            stds = self._gs_ssvm.cv_results_['std_test_score']
            for mean, std, params in zip(means, stds,
                                         self._gs_ssvm.cv_results_['params']):
                traceln("%0.3f (+/-%0.03f) for %r" % (mean, std * 2, params))
            traceln("--- ---")
        except Exception as e:
            traceln(
                "WARNING: error while dealing with the GridSearchCV object.")
            traceln(e)

        #the baseline model(s) if any
        self._trainBaselines(lX, lY)

        #do some garbage collection
        del lX, lY
        gc.collect()
        return
Ejemplo n.º 15
0
def main():

    tweets_data_train = []
    with open('Train\dataset_train.pkl', 'rb') as r:
        tweets_set = pickle.load(r)

    for i in range(0, len(tweets_set)):
        for j in range(0, len(tweets_set[i])):
            t = tweets_set[i][j][1].encode('ascii', 'ignore')
            tweets_data_train.append(t)

    features_train_transformed = get_extra_features(tweets_data_train)
    print(features_train_transformed.shape)
    features_train_transformed.dump("Train\extra_features_train.pkl")

    extra_features_train = numpy.load("Train\extra_features_train.pkl")
    print "EXTRA FEATURES FOR TRAIN DATA IS SUCCESSFULLY EXTRACTED"

    tweets_data_test = []
    with open('Test\dataset_test.pkl', 'rb') as r:
        tweets_set = pickle.load(r)
    for i in range(0, len(tweets_set)):
        for j in range(0, len(tweets_set[i])):
            t = tweets_set[i][j][1].encode('ascii', 'ignore')
            tweets_data_test.append(t)

    features_test_transformed = get_extra_features(tweets_data_test)
    features_test_transformed.dump("Test\extra_features_test.pkl")

    extra_features_test = numpy.load("Test\extra_features_test.pkl")
    print "EXTRA FEATURES FOR TEST DATA IS SUCCESSFULLY EXTRACTED"

    #TFIDF VECTORIZER
    features_train_tfidf, features_test_tfidf = get_main_features(
        tweets_data_train, tweets_data_test)

    with open('Train\edges_train.pkl', 'rb') as e:
        edges_train = pickle.load(e)
    with open('Train\labels_train.pkl', 'rb') as l:
        labels_tr = pickle.load(l)
    with open('Test\edges_test.pkl', 'rb') as e:
        edges_test = pickle.load(e)
    with open('Test\labels_test.pkl', 'rb') as l:
        labels_te = pickle.load(l)

    #edges=numpy.array(edges)
    labels_tr = numpy.array(labels_tr)
    labels_te = numpy.array(labels_te)
    #labels_1D=numpy.zeros(1)

    labels_train = array_to_list(labels_tr)
    labels_test = array_to_list(labels_te)
    labels_test = numpy.array(labels_test)

    #labels_1D=numpy.delete(labels_1D,(0),0)
    """

	selector=SelectPercentile(f_classif,percentile=70)
	selector.fit(features_train_tfidf,labels_1D)
	features_train_transformed=selector.transform(features_train_tfidf).toarray()
	features_test_transformed=selector.transform(features_test_tfidf).toarray()
	print "Features Selection is done successfully """

    print features_test_tfidf.shape, extra_features_test.shape

    features_train_transformed = numpy.concatenate(
        (features_train_tfidf, extra_features_train), axis=1)
    features_test_transformed = numpy.concatenate(
        (features_test_tfidf, extra_features_test), axis=1)
    print "TFIDF FEATURES ARE SUCCESSFULLY CREATED"

    features_train = get_features_and_edges(features_train_transformed,
                                            edges_train)
    features_test = get_features_and_edges(features_test_transformed,
                                           edges_test)

    labels_train = numpy.array(labels_train)
    print labels_train.shape
    model_name = "GraphCRF_model"
    model = GraphCRF(directed=True)
    ssvm = FrankWolfeSSVM(model=model,
                          C=1.0,
                          max_iter=100,
                          logger=SaveLogger(model_name + ".pickle",
                                            save_every=100))
    start_time = time.time()
    final_model = ssvm.fit(features_train, labels_train)
    print("--- Time taken to train the classifier is %s seconds " %
          (time.time() - start_time))
    print "YAAY ! A GRAPH CRF MODEL IS SUCCESSFULLY CREATED AND TRAINED"

    print("Charliehedbo event is the Test Data")
    pickle.dump(final_model, open('Saved_Model/sdqc_final_model.pkl', 'wb'))
    ssvm = pickle.load(open('Saved_Model/sdqc_final_model.pkl', 'rb'))
    #ssvm = SaveLogger(model_name+".pickle").load()
    X_test = []
    y_test = []
    for i in range(0, len(features_test)):
        if features_test[i][0].shape[0] >= 3:
            X_test.append(features_test[i])
            y_test.append(labels_test[i])
    #print X_test

    #print ("Accuracy score with Graph CRF : %f" % ssvm.score(X_test,y_test))

    predictions = ssvm.predict(X_test)
    #PREDICTIONS AND y_TEST ARE LIST OF ARRAYS
    true = numpy.zeros(1)
    prediction = numpy.zeros(1)
    for i in range(0, len(predictions)):
        true = numpy.hstack((true, y_test[i]))
        prediction = numpy.hstack((prediction, predictions[i]))

    true = numpy.delete(true, (0), axis=0)
    prediction = numpy.delete(prediction, (0), axis=0)
    print "TOTAL", true.shape[0]
    print accuracy_score(true, prediction)
    with open('SDQC_Result.pkl', 'wb') as w:
        pickle.dump(prediction, w)
    print(
        classification_report(
            true,
            prediction,
            target_names=["support", "deny", "query", "comment"]))
    print confusion_matrix(true, prediction, labels=[0, 1, 2, 3])
    plot_cmat(true, prediction)
Ejemplo n.º 16
0
def main():
    argv = sys.argv
    print("loading %s ..." % argv[1])
    ssvm1 = SaveLogger(file_name=argv[1]).load()
    ssvm2 = SaveLogger(file_name=argv[2]).load()

    data_str = 'val'
    if len(argv) <= 3:
        raise ValueError("Need a folder name for plotting.")
    print("loading data...")
    data = load_nyu(data_str, n_sp=500)
    dataset = NYUSegmentation()
    print("done")
    data1 = add_edges(data, kind="pairwise")
    data2 = add_edges(data, kind="pairwise")
    data1 = add_edge_features(dataset, data1)
    data2 = add_edge_features(dataset, data2, depth_diff=True)
    Y_pred1 = ssvm1.predict(data1.X)
    Y_pred2 = ssvm2.predict(data2.X)
    folder = argv[3]

    if not os.path.exists(folder):
        os.mkdir(folder)

    np.random.seed(0)
    for image_name, superpixels, y_pred1, y_pred2 in zip(data.file_names,
                                                         data.superpixels,
                                                         Y_pred1, Y_pred2):
        if np.all(y_pred1 == y_pred2):
            continue
        gt = dataset.get_ground_truth(image_name)
        perf1 = eval_on_pixels(dataset, [gt], [y_pred1[superpixels]],
                              print_results=False)[0]
        perf1 = np.mean(perf1[np.isfinite(perf1)])

        perf2 = eval_on_pixels(dataset, [gt], [y_pred2[superpixels]],
                              print_results=False)[0]
        perf2 = np.mean(perf2[np.isfinite(perf2)])
        if np.abs(perf1 - perf2) < 2:
            continue

        image = dataset.get_image(image_name)
        fig, axes = plt.subplots(2, 3, figsize=(12, 6))
        axes[0, 0].imshow(image)
        axes[0, 0].imshow((y_pred1 != y_pred2)[superpixels], vmin=0, vmax=1,
                          alpha=.7)

        axes[0, 1].set_title("ground truth")
        axes[0, 1].imshow(image)
        axes[0, 1].imshow(gt, alpha=.7, cmap=dataset.cmap, vmin=0,
                          vmax=dataset.cmap.N)
        axes[1, 0].set_title("%.2f" % perf1)
        axes[1, 0].imshow(image)
        axes[1, 0].imshow(y_pred1[superpixels], vmin=0, vmax=dataset.cmap.N,
                          alpha=.7, cmap=dataset.cmap)

        axes[1, 1].set_title("%.2f" % perf2)
        axes[1, 1].imshow(image)
        axes[1, 1].imshow(y_pred2[superpixels], alpha=.7, cmap=dataset.cmap,
                          vmin=0, vmax=dataset.cmap.N)
        present_y = np.unique(np.hstack([y_pred1, y_pred2, np.unique(gt)]))
        present_y = np.array([y_ for y_ in present_y if y_ !=
                              dataset.void_label])
        axes[0, 2].imshow(present_y[:, np.newaxis], interpolation='nearest',
                          cmap=dataset.cmap, vmin=0, vmax=dataset.cmap.N)
        for i, c in enumerate(present_y):
            axes[0, 2].text(1, i, dataset.classes[c])
        for ax in axes.ravel():
            ax.set_xticks(())
            ax.set_yticks(())
        axes[1, 2].set_visible(False)
        fig.savefig(folder + "/%s.png" % image_name, bbox_inches="tight")
        plt.close(fig)
from pystruct.models import LatentGridCRF, GridCRF, LatentGraphCRF, GraphCRF, EdgeFeatureGraphCRF
from pystruct.learners import LatentSSVM, OneSlackSSVM, SubgradientSSVM, FrankWolfeSSVM
from pystruct.utils import make_grid_edges, SaveLogger
from skimage import img_as_ubyte
from matplotlib import pyplot as plt
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from skimage.transform import resize

from skimage.filters import threshold_otsu

# Pixel Classes - Black, White
horsePixelClasses = [numpy.array([0., 0., 0.]), numpy.array([1., 1., 1.])]

# Load trained Model
horseModelLogger = SaveLogger('save/imagesegmentation-horse-hog_96_lbp.model',
                              save_every=1)
horseCRF = horseModelLogger.load()


######################################
# Compute S_0 score
######################################
def foregroundQualityScore(a, b):
    TP = TN = FP = FN = 0.0

    for i in range(0, len(a)):
        if a[i] == b[i]:
            if a[i] == 0:
                TN += 1
            else:
                TP += 1
Ejemplo n.º 18
0
# Script to predict edge map

# Importing Libraries
from pystruct.utils import SaveLogger
import scipy.io as sio

# Getting parameters
params = sio.loadmat(
    '/home/dell/Desktop/sarvaswa/objectness-release-v2.2/Trial_Pascal/testSet/exp1/salprop-v1.0/matpy/params.mat'
)
matpyfiles = params['params'][0]['matpyfiles'][0]
featureFile = matpyfiles['featureFile'][0][0][0]
edgesFile = matpyfiles['edgesFile'][0][0][0]
predictFile = matpyfiles['predictFile'][0][0][0]
modelFile = params['params'][0]['modelFileCRF'][0][0]

# Loading required Files
modelLogger = SaveLogger(modelFile)
crf = modelLogger.load()
feat = sio.loadmat(featureFile)
feat = feat['feat']
edges = sio.loadmat(edgesFile)
edges = edges['edges'] - 1

# Make Prediction
inData = [(feat, edges)]
prediction = crf.predict(inData)

# Save Prediction;
sio.savemat(predictFile, mdict={'prediction': prediction})
Ejemplo n.º 19
0
def main():
    argv = sys.argv
    print("loading %s ..." % argv[1])
    ssvm = SaveLogger(file_name=argv[1]).load()
    plot_learning(ssvm)
Ejemplo n.º 20
0
    G = [np.empty((0, 2), dtype=np.int) for x in trainDirty[0:n_train]]
else:
    edgeList = edges((28, 28), dist=dist, diag=diag)
    G = [edgeList for x in trainDirty[0:n_train]]

X_flat = [np.vstack(i) for i in trainDirty[0:n_train]]
Y_flat = np.array(trainLabels[0:n_train])

crf = GraphCRF(inference_method=inference)
svm = NSlackSSVM(model=crf,
                 max_iter=num_iter,
                 C=C,
                 n_jobs=6,
                 verbose=1,
                 logger=SaveLogger(folderName + "model_%d.pickle",
                                   save_every=save_every,
                                   verbose=0))

asdf = zip(X_flat, G)

svm.fit(asdf, Y_flat)

#%%####### TESTING ##################

if dist == 0:
    G2 = G = [np.empty((0, 2), dtype=np.int) for x in testDirty[0:n_test]]
else:
    G2 = [edgeList for x in testDirty[0:n_test]]

X_flat2 = [np.vstack(i) for i in testDirty[0:n_test]]
Y_flat2 = np.array(testLabels[0:n_test])
Ejemplo n.º 21
0
    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)

    # predict score on test dataset
    y_pred = ssvm.predict(X_test)
    y_pred, y_test = np.hstack(y_pred), np.hstack(y_test)
    y_pred = y_pred[y_test != 0]
    y_test = y_test[y_test != 0]

    print("Score on validation set: %f" % np.mean(y_test == y_pred))
Ejemplo n.º 22
0
	def validate(self):
		""" Tweaks C for the svc. self.validation_set is used for validating """
		validation_features = \
			bookfunctions.get_features_from_pages_data(self.validation_set,
			self.number_of_blocks, self.overlap, self.svm_path)

		if self.use_page_classifier:
			# FIXME: number of blocks is fixed to what the page classifier has
			# learned on in my test case, for now.
			page_validation_features = bookfunctions.get_all_features(self.validation_set, \
				(5,5))
			s = page_validation_features.shape
			# Reshape all features to 1 feature vector
			page_validation_features.shape = (s[0], s[1] * s[2] * s[3])


		validation_labels = bookfunctions.get_all_labels(self.validation_set, \
			self.number_of_blocks, overlap=self.overlap)

		print """validation set features size after concatenate %s. validation
		labels size: %s""" % (str(np.shape(validation_features)), \
			str(np.shape(validation_labels)))
		best_f = 0

		# Count the number of class labels in order to set the class weights
		class_weights = 1. / np.bincount(self.train_labels.flatten())
		# Normalize class weights in order to have a scalable tolerance
		# parameter
		class_weights *= float(np.shape(self.train_features)[3]) / np.sum(class_weights)
		print "class weights: %s" % str(class_weights)
		self.crf = WeightedGridCRF(neighborhood=4, class_weight=class_weights)

		for i in range(1, 5):
			c = 10**i
			self.logger = SaveLogger(get_log_path('model', c, self.use_svm, \
				self.overlap, self.use_page_classifier), save_every=15)

			print "validating with c = " + str(c)
			temp_classifier = ssvm.OneSlackSSVM(model=self.crf, C=c, n_jobs=-1,
				verbose=2, logger=self.logger, tol=.01)

			# Fit the classifier:
			temp_classifier.fit(self.train_features, self.train_labels)

			# Write the ssvm parameters!
			with open(get_log_path('param', c, self.use_svm, self.overlap,
				self.use_page_classifier), 'w') as f:
				f.write(str(temp_classifier.get_params()))

			print "validation features shape: %s" + str(np.shape(validation_features))

			validation_predicted_labels = temp_classifier.predict(validation_features)
			validation_predicted_labels = np.array(validation_predicted_labels)
			
			if self.use_page_classifier:
				# Get the page predictions, which have pretttyy high accuracy
				validation_predicted_pages = self.page_classifier.predict( \
					page_validation_features)
				for i, page in enumerate(validation_predicted_pages):
					if page != 0:
						# Replace any page that has no images according to the
						# page classifier, with a page that is fully classified
						# as 1.
						validation_predicted_labels[i] = \
							np.ones((validation_predicted_labels.shape[1],
								validation_predicted_labels.shape[2]))

			print "C = %d" % (c)
			prfs = precision_recall_fscore_support(validation_labels.flatten(), \
				validation_predicted_labels.flatten())
			print """
				Precision:
					Image: %f
					Text: %f
				Recall:
					Image: %f
					Text: %f
				Fscore:
					Image: %f
					Text: %f
				Support:
					Image: %f
					Text: %f
				""" % tuple(np.ndarray.flatten(np.array(prfs)))

			f = prfs[2][0]

			if f > best_f:
				best_f = f
				self.classifier = temp_classifier

		print "F-score for best c: %s" % str(best_f)
		return best_f
Ejemplo n.º 23
0
def svm_on_segments(C=.1, learning_rate=.001, subgradient=True):
    # load and prepare data
    lateral = True
    latent = True
    test = False
    #data_train = load_data(which="piecewise")
    #data_train = add_edges(data_train, independent=False)
    #data_train = add_kraehenbuehl_features(data_train, which="train_30px")
    #data_train = add_kraehenbuehl_features(data_train, which="train")
    #if lateral:
    #data_train = add_edge_features(data_train)
    data_train = load_data_global_probs(latent=latent)
    X_org_ = data_train.X
    #data_train = make_hierarchical_data(data_train, lateral=lateral,
    #latent=latent, latent_lateral=True)
    data_train = discard_void(data_train, 21, latent_features=True)
    X_, Y_ = data_train.X, data_train.Y
    # remove edges
    if not lateral:
        X_org_ = [(x[0], np.zeros((0, 2), dtype=np.int)) for x in X_org_]

    if test:
        data_val = load_data('val', which="piecewise")
        data_val = add_edges(data_val, independent=False)
        data_val = add_kraehenbuehl_features(data_val)
        data_val = make_hierarchical_data(data_val,
                                          lateral=lateral,
                                          latent=latent)
        data_val = discard_void(data_val, 21)

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

    n_states = 21
    class_weights = 1. / np.bincount(np.hstack(Y_))
    class_weights *= 21. / np.sum(class_weights)
    experiment_name = ("latent5_features_C%f_top_node" % C)
    logger = SaveLogger(experiment_name + ".pickle", save_every=10)
    if latent:
        model = LatentNodeCRF(n_labels=n_states,
                              n_features=data_train.X[0][0].shape[1],
                              n_hidden_states=5,
                              inference_method='qpbo' if lateral else 'dai',
                              class_weight=class_weights,
                              latent_node_features=True)
        if subgradient:
            ssvm = learners.LatentSubgradientSSVM(model,
                                                  C=C,
                                                  verbose=1,
                                                  show_loss_every=10,
                                                  logger=logger,
                                                  n_jobs=-1,
                                                  learning_rate=learning_rate,
                                                  decay_exponent=1,
                                                  momentum=0.,
                                                  max_iter=100000)
        else:
            latent_logger = SaveLogger("lssvm_" + experiment_name +
                                       "_%d.pickle",
                                       save_every=1)
            base_ssvm = learners.OneSlackSSVM(model,
                                              verbose=2,
                                              C=C,
                                              max_iter=100000,
                                              n_jobs=-1,
                                              tol=0.001,
                                              show_loss_every=200,
                                              inference_cache=50,
                                              logger=logger,
                                              cache_tol='auto',
                                              inactive_threshold=1e-5,
                                              break_on_bad=False,
                                              switch_to_ad3=True)
            ssvm = learners.LatentSSVM(base_ssvm, logger=latent_logger)
        warm_start = False
        if warm_start:
            ssvm = logger.load()
            ssvm.logger = SaveLogger(experiment_name + "_retrain.pickle",
                                     save_every=10)
            ssvm.max_iter = 100000
            ssvm.learning_rate = 0.00001
            ssvm.momentum = 0
    else:
        #model = GraphCRF(n_states=n_states,
        #n_features=data_train.X[0][0].shape[1],
        #inference_method='qpbo' if lateral else 'dai',
        #class_weight=class_weights)
        model = EdgeFeatureGraphCRF(
            n_states=n_states,
            n_features=data_train.X[0][0].shape[1],
            inference_method='qpbo' if lateral else 'dai',
            class_weight=class_weights,
            n_edge_features=4,
            symmetric_edge_features=[0, 1],
            antisymmetric_edge_features=[2])
        ssvm = learners.OneSlackSSVM(model,
                                     verbose=2,
                                     C=C,
                                     max_iter=100000,
                                     n_jobs=-1,
                                     tol=0.0001,
                                     show_loss_every=200,
                                     inference_cache=50,
                                     logger=logger,
                                     cache_tol='auto',
                                     inactive_threshold=1e-5,
                                     break_on_bad=False)

    #ssvm = logger.load()

    X_, Y_ = shuffle(X_, Y_)
    #ssvm.fit(data_train.X, data_train.Y)
    #ssvm.fit(X_, Y_, warm_start=warm_start)
    ssvm.fit(X_, Y_)
    print("fit finished!")
Ejemplo n.º 24
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)
Ejemplo n.º 25
0
    ssvm_path = '../models_backup/model_c_10_svm_%d_overlap_%d.py' % (int(use_svm), \
     int(overlap))
    output_folder = '../output_testset'

    # read all images from input folder
    #pages_data = bookfunctions.get_pages_and_data_from_book(input_folder)
    pages_data = bookfunctions.get_pages_and_data_from_folder(input_folder)
    #pages_data = pages_data[379:381]

    true_labels = bookfunctions.get_all_labels(pages_data, \
     number_of_blocks, overlap)
    features = bookfunctions.get_features_from_pages_data(pages_data, \
     number_of_blocks, overlap, svm_path)

    # put features in ssvm
    logger = SaveLogger(ssvm_path)
    ssvm = logger.load()
    predicted_labels = np.array(ssvm.predict(features))
    prfs = precision_recall_fscore_support(true_labels.flatten(), \
     predicted_labels.flatten())
    cm = confusion_matrix(true_labels.flatten(), predicted_labels.flatten())
    print """
		Precision:
			Image: %f
			Text: %f
		Recall:
			Image: %f
			Text: %f
		Fscore:
			Image: %f
			Text: %f
Ejemplo n.º 26
0
    from common.trace import traceln

from crf.Model_SSVM_AD3 import Model_SSVM_AD3

# --- MAIN: DISPLAY STORED MODEL INFO ------------------------------------------------------------------

if __name__ == "__main__":
    try:
        sModelDir, sModelName = sys.argv[1:3]
    except:
        print("Usage: %s <model-dir> <model-name>" % sys.argv[0])
        print("Display some info regarding the stored model")
        exit(1)

    mdl = Model_SSVM_AD3(sModelName, sModelDir)
    print("Loading %s" % mdl.getModelFilename())
    if False:
        mdl.load()  #loads all sub-models!!
    else:
        mdl.ssvm = mdl._loadIfFresh(mdl.getModelFilename(), None,
                                    lambda x: SaveLogger(x).load())

    print(mdl.getModelInfo())

    fig = plt.figure()
    fig.canvas.set_window_title("dir=%s  model=%s  " % (sModelDir, sModelName))
    plt.plot(mdl.ssvm.loss_curve_)
    plt.xlabel("Iteration / 10")
    plt.ylabel("Loss")
    plt.show()

# --- MAIN: DISPLAY STORED MODEL INFO ------------------------------------------------------------------

if __name__ == "__main__":
    try:
        sModelDir, sModelName = sys.argv[1:3]
    except:
        print("Usage: %s <model-dir> <model-name>"%sys.argv[0])
        print("Display some info regarding the stored model")
        exit(1)
        
    mdl = Model_SSVM_AD3(sModelName, sModelDir)
    print("Loading %s"%mdl.getModelFilename())
    if False:
        mdl.load()  #loads all sub-models!!
    else:
        mdl.ssvm = mdl._loadIfFresh(mdl.getModelFilename(), None, lambda x: SaveLogger(x).load())

    print(mdl.getModelInfo())
    
    fig = plt.figure()
    fig.canvas.set_window_title("dir=%s  model=%s  "%(sModelDir, sModelName))
    plt.plot(mdl.ssvm.loss_curve_)
    plt.xlabel("Iteration / 10")
    plt.ylabel("Loss")
    plt.show()

    
    
Ejemplo n.º 28
0
def main():
    argv = sys.argv
    print("loading %s ..." % argv[1])
    ssvm = SaveLogger(file_name=argv[1]).load()
    if hasattr(ssvm, 'problem'):
        ssvm.model = ssvm.problem
    print(ssvm)
    if hasattr(ssvm, 'base_ssvm'):
        ssvm = ssvm.base_ssvm
    print("Iterations: %d" % len(ssvm.objective_curve_))
    print("Objective: %f" % ssvm.objective_curve_[-1])
    inference_run = None
    if hasattr(ssvm, 'cached_constraint_'):
        inference_run = ~np.array(ssvm.cached_constraint_)
        print("Gap: %f" %
              (np.array(ssvm.primal_objective_curve_)[inference_run][-1] -
               ssvm.objective_curve_[-1]))

    if len(argv) <= 2:
        argv.append("acc")

    if len(argv) <= 3:
        dataset = 'nyu'
    else:
        dataset = argv[3]

    if argv[2] == 'acc':

        ssvm.n_jobs = 1

        for data_str, title in zip(["train", "val"],
                                   ["TRAINING SET", "VALIDATION SET"]):
            print(title)
            edge_type = "pairwise"

            if dataset == 'msrc':
                ds = MSRC21Dataset()
                data = msrc_helpers.load_data(data_str, which="piecewise_new")
                #data = add_kraehenbuehl_features(data, which="train_30px")
                data = msrc_helpers.add_kraehenbuehl_features(data, which="train")
            elif dataset == 'pascal':
                ds = PascalSegmentation()
                data = pascal_helpers.load_pascal(data_str, sp_type="cpmc")
                #data = pascal_helpers.load_pascal(data_str)
            elif dataset == 'nyu':
                ds = NYUSegmentation()
                data = nyu_helpers.load_nyu(data_str, n_sp=500, sp='rgbd')
            else:
                raise ValueError("Excepted dataset to be 'nyu', 'pascal' or 'msrc',"
                                 " got %s." % dataset)

            if type(ssvm.model).__name__ == "LatentNodeCRF":
                print("making data hierarchical")
                data = pascal_helpers.make_cpmc_hierarchy(ds, data)
                #data = make_hierarchical_data(
                    #ds, data, lateral=True, latent=True, latent_lateral=False,
                    #add_edge_features=False)
            else:
                data = add_edges(data, edge_type)

            if type(ssvm.model).__name__ == 'EdgeFeatureGraphCRF':
                data = add_edge_features(ds, data, depth_diff=True, normal_angles=True)

            if type(ssvm.model).__name__ == "EdgeFeatureLatentNodeCRF":
                data = add_edge_features(ds, data)
                data = make_hierarchical_data(
                    ds, data, lateral=True, latent=True, latent_lateral=False,
                    add_edge_features=True)
            #ssvm.model.inference_method = "qpbo"
            Y_pred = ssvm.predict(data.X)

            if isinstance(ssvm.model, LatentNodeCRF):
                Y_pred = [ssvm.model.label_from_latent(h) for h in Y_pred]
            Y_flat = np.hstack(data.Y)

            print("superpixel accuracy: %.2f"
                  % (np.mean((np.hstack(Y_pred) == Y_flat)[Y_flat != ds.void_label]) * 100))

            if dataset == 'msrc':
                res = msrc_helpers.eval_on_pixels(data, Y_pred,
                                                  print_results=True)
                print("global: %.2f, average: %.2f" % (res['global'] * 100,
                                                       res['average'] * 100))
                #msrc_helpers.plot_confusion_matrix(res['confusion'])
            else:
                hamming, jaccard = eval_on_sp(ds, data, Y_pred,
                                              print_results=True)
                print("Jaccard: %.2f, Hamming: %.2f" % (jaccard.mean(),
                                                        hamming.mean()))

        plt.show()

    elif argv[2] == 'plot':
        data_str = 'val'
        if len(argv) <= 4:
            raise ValueError("Need a folder name for plotting.")
        if dataset == "msrc":
            ds = MSRC21Dataset()
            data = msrc_helpers.load_data(data_str, which="piecewise")
            data = add_edges(data, independent=False)
            data = msrc_helpers.add_kraehenbuehl_features(
                data, which="train_30px")
            data = msrc_helpers.add_kraehenbuehl_features(
                data, which="train")

        elif dataset == "pascal":
            ds = PascalSegmentation()
            data = pascal_helpers.load_pascal("val")
            data = add_edges(data)

        elif dataset == "nyu":
            ds = NYUSegmentation()
            data = nyu_helpers.load_nyu("test")
            data = add_edges(data)

        if type(ssvm.model).__name__ == 'EdgeFeatureGraphCRF':
            data = add_edge_features(ds, data, depth_diff=True, normal_angles=True)
        Y_pred = ssvm.predict(data.X)

        plot_results(ds, data, Y_pred, argv[4])
Ejemplo n.º 29
0
    def train(self, lGraph, bWarmStart=True, expiration_timestamp=None, verbose=0):
        """
        Train a CRF model using the list of labelled graph as training
        if bWarmStart if True, try to continue from previous training, IF the stored model is older than expiration_timestamp!!
            , otherwise, starts from scratch
        return nothing
        """
        if self.bGridSearch:
            return self.gridsearch(lGraph, verbose=verbose)
    
        traceln("\t- computing features on training set")
        traceln("\t\t #nodes=%d  #edges=%d "%Graph.getNodeEdgeTotalNumber(lGraph))
        chronoOn()
        lX, lY = self.get_lX_lY(lGraph)
        self._computeModelCaracteristics(lX)    #we discover here dynamically the number of features of nodes and edges
        traceln("\t\t %s"%self._getNbFeatureAsText())
        traceln("\t [%.1fs] done\n"%chronoOff())
        
        bMakeSlim = not bWarmStart  # for warm-start mode, we do not make the model slimer!"
        
        traceln("\t- retrieving or creating model...")
        self.ssvm = None
        sModelFN = self.getModelFilename()
        if bWarmStart:
            try:
                self.ssvm = self._loadIfFresh(sModelFN, expiration_timestamp, lambda x: SaveLogger(x).load())
                traceln("\t- warmstarting!")
                #we allow to change the max_iter of the model
                try:
                    self.ssvm.max_iter #to make sure we do something that makes sense...
                    if self.ssvm.max_iter != self.max_iter:
                        traceln("\t- changing max_iter value from (stored) %d to %d"%(self.ssvm.max_iter, self.max_iter))
                        self.ssvm.max_iter = self.max_iter
                except AttributeError:
                    traceln("\t- cannot access or change the max_iter value")
                    
                try:
                    self.ssvm.n_jobs #to make sure we do something that makes sense...
                    if self.ssvm.n_jobs != self.njobs:
                        traceln("\t- changing n_jobs value from (stored) %d to %d"%(self.ssvm.n_jobs, self.njobs))
                        self.ssvm.n_jobs = self.njobs
                except AttributeError:
                    traceln("\t- cannot access or change the n_jobs value")

            except Exception as e:
                self.ssvm = None
                traceln("\t- Cannot warmstart: %s"%e)
            #self.ssvm is either None or containing a nice ssvm model!!
        
        if not self.ssvm:
            traceln("\t- creating a new SSVM-trained CRF model")
            
            traceln("\t\t- computing class weight:")
            clsWeights = self.computeClassWeight(lY)
            traceln("\t\t\t --> %s" % clsWeights)
            
            crf = self._getCRFModel(clsWeights)
    
            self.ssvm = OneSlackSSVM(crf
                                , inference_cache=self.inference_cache, C=self.C, tol=self.tol, n_jobs=self.njobs
                                , logger=SaveLogger(sModelFN, save_every=self.save_every)
                                , max_iter=self.max_iter                                        
                                , show_loss_every=10, verbose=verbose)
            bWarmStart = False
        
        chronoOn()
        traceln("\t- training graph-based model")
        traceln("\t\t solver parameters:"
                    , " inference_cache=",self.inference_cache
                    , " C=",self.C, " tol=",self.tol, " n_jobs=",self.njobs)
        self.ssvm.fit(lX, lY, warm_start=bWarmStart)
        traceln("\t [%.1fs] done (graph-based model is trained) \n"%chronoOff())
        
        traceln(self.getModelInfo())
        
        #cleaning useless data that takes MB on disk
        if bMakeSlim:
            self.ssvm.alphas = None  
            self.ssvm.constraints_ = None
            self.ssvm.inference_cache_ = None    
            traceln("\t\t(model made slimmer. Not sure you can efficiently warm-start it later on. See option -w.)")   
                 
        #the baseline model(s) if any
        self._trainBaselines(lX, lY)
        
        #do some garbage collection
        del lX, lY
        gc.collect()
        return