Esempio n. 1
0
 def train_svm(self, dataset, kernel='linear', C=1.0, gamma=0.0):
   """
   Train classifiers for class  
   """  
   print '%d trains class %s'%(comm_rank, self.cls)
   t = time.time()
   pos = dataset.get_pos_samples_for_class(self.cls)
   neg = dataset.get_neg_samples_for_class(self.cls)
              
   pos_gist = self.gist_table[pos, :]
   neg_gist = self.gist_table[neg, :]      
   
   x = np.concatenate((pos_gist, neg_gist))
   y = [1]*pos.shape[0] + [-1]*neg.shape[0]
   print '%d compute svm for %s'%(comm_rank, self.cls)
   svm_filename = config.get_gist_svm_filename(self.cls, dataset)
   print svm_filename
   self.svm = train_svm(x, y, kernel, C, gamma)
   print '\ttook', time.time()-t,'sec'
   print 'the score on train-data is %f'%self.svm.score(x,y)
   table_t = svm_proba(x, self.svm)
   y2 = np.array(y)
   y2 = (y2+1)/2 # switch to 0/1
   ap,_,_ = Evaluation.compute_cls_pr(table_t[:,1], y2)
   print 'ap on train: %f'%ap    
   save_svm(self.svm, svm_filename)      
   return ap 
Esempio n. 2
0
def gist_evaluate_svms(d_train, d_val):
  
  gist_scores = np.zeros((len(d_val.images), len(d_val.classes)))
  gist_table = np.load(config.get_gist_dict_filename(d_train.name))
  
  kernels = ['rbf', 'linear', 'poly']
  Cs = [1,10,100]
  gammas = [0,0.3,1]
  setts = list(itertools.product(kernels, Cs, gammas))
  val_gt = d_val.get_cls_ground_truth()
  
  for cls_idx in range(len(d_val.classes)):
    cls = d_val.classes[cls_idx]  
    gist = GistClassifier(cls, d_train, gist_table=gist_table, d_val=d_val)
    filename = config.get_gist_crossval_filename(d_train, cls) 
    # doing some crossval right here!!!
    for set_idx in range(comm_rank, len(setts), comm_size):
      sett = setts[len(setts)-1-set_idx]
      kernel = sett[0]
      C = sett[1]
      gamma = sett[2]
      train_ap = gist.train_svm(d_train, kernel, C, gamma)
          
      val_gist_table = np.load(config.get_gist_dict_filename(d_val.name))     
      gist_scores = svm_proba(val_gist_table, gist.svm)[:,1]
      
      val_ap,_,_ = Evaluation.compute_cls_pr(gist_scores, val_gt.subset_arr(cls))
      w = open(filename, 'a')
      w.write('%s C=%d gamma=%f - train: %f, val: %f\n'%(kernel, C, gamma, train_ap, val_ap))
      w.close()
      print 'ap on val: %f'%val_ap
      
  print '%d at safebarrier'%comm_rank
  safebarrier(comm)
  gist_scores = comm.reduce(gist_scores)
  if comm_rank == 0:
    print gist_scores
    filename = config.get_gist_classifications_filename(d_val)    
    cPickle.dump(gist_scores, open(filename,'w'))
    res = Evaluation.compute_cls_pr(gist_scores, val_gt.arr)
    print res
Esempio n. 3
0
 def train(self, pos, neg, kernel, C):
   y = [1]*pos.shape[0] + [-1]*neg.shape[0]
   x = np.concatenate((pos,neg))
   model = train_svm(x, y, kernel, C)
   self.svm = model
   print 'model.score(C=%d): %f'%(C, model.score(x,y))
   table_t = svm_proba(x, model)
   y2 = np.array(y)
   y2 = (y2+1)/2 # switch to 0/1
   ap,_,_ = Evaluation.compute_cls_pr(table_t[:,1], y2)
   print 'ap on train set: %f'%ap
   filename = config.get_classifier_filename(self, self.cls, self.train_dataset)
   self.svm = model
   self.save_svm(model, filename)
   return model
Esempio n. 4
0
  def eval_cls(self, ext_detector):
    print 'evaluate svm for %s'%self.cls
    dataset = ext_detector.dataset
    assert(dataset.name in ['full_pascal_val','full_pascal_test'])
    print dataset.name

    table_cls = np.zeros(len(dataset.images))
    for img_idx, image in enumerate(dataset.images):
      print '%d eval on img %d/%d'%(comm_rank, img_idx, len(dataset.images))
      img_dets, _ = ext_detector.detect(image, astable=True)
      img_scores = img_dets.subset_arr('score')
      score = self.classify_image(img_scores)
      table_cls[img_idx] = score
      
    ap, _,_ = Evaluation.compute_cls_pr(table_cls, dataset.get_cls_ground_truth().subset_arr(self.cls))
    print 'ap on val for %s: %f'%(self.cls, ap)

    return table_cls
Esempio n. 5
0
    def eval_cls(self, ext_detector):
        print 'evaluate svm for %s' % self.cls
        dataset = ext_detector.dataset
        assert (dataset.name in ['full_pascal_val', 'full_pascal_test'])
        print dataset.name

        table_cls = np.zeros(len(dataset.images))
        for img_idx, image in enumerate(dataset.images):
            print '%d eval on img %d/%d' % (comm_rank, img_idx,
                                            len(dataset.images))
            img_dets, _ = ext_detector.detect(image, astable=True)
            img_scores = img_dets.subset_arr('score')
            score = self.classify_image(img_scores)
            table_cls[img_idx] = score

        ap, _, _ = Evaluation.compute_cls_pr(
            table_cls,
            dataset.get_cls_ground_truth().subset_arr(self.cls))
        print 'ap on val for %s: %f' % (self.cls, ap)

        return table_cls