Esempio n. 1
0
def train_image_classify_svm(cc, cls, C=1.0, gamma=0.0, force_new=False):
    current_fold = cc.d.current_fold
    filename = config.get_classifier_svm_name(cls, C, gamma, current_fold)
    if not force_new and os.path.exists(filename):
        print 'svm for class %s, C=%f, gamma=%f exists already' % (cls, C,
                                                                   gamma)
        return

    pyr_feat_size = get_pyr_feat_size(cc.L, cc.dense_codebook.shape[0])

    cc.tictocer.tic('overall')

    print 'compute classifier(C=%f, gamma=%f) for class %s' % (C, gamma, cls)
    pos_images = cc.d.get_pos_samples_for_fold_class(cls)
    if pos_images.size == 0:
        return
    neg_images = cc.d.get_neg_samples_for_fold_class(cls, pos_images.size)

    # 1. extract all the pyramids
    # ======== POSTIVE IMAGES ===========
    print 'compute feature vector for positive images'
    num_slices = 3
    pos_pyrs = np.zeros(
        (len(pos_images),
         pyr_feat_size + cc.sparse_codebook.shape[0] * (1 + num_slices)))
    for idx, img in enumerate(pos_images):
        pos_pyrs[idx, :] = get_feature_vector(cc, img)

    # ======== NEGATIVE IMAGES ===========
    print 'compute feature vector for negative images'
    neg_pyrs = np.zeros(
        (len(neg_images),
         pyr_feat_size + cc.sparse_codebook.shape[0] * (1 + num_slices)))
    for idx, img in enumerate(neg_images):
        neg_pyrs[idx, :] = get_feature_vector(cc, img)

    # 2. Compute SVM for class
    X = np.vstack((pos_pyrs, neg_pyrs))
    Y = [1] * pos_pyrs.shape[0] + [-1] * neg_pyrs.shape[0]

    if X.shape[0] > 0:
        print 'train svm for class %s, C=%f, gamma=%f' % (cls, C, gamma)
        cc.tictocer.tic()
        clf = train_svm(X, Y, kernel='rbf', gamma=gamma, C=C)
        print '\ttook %f seconds' % cc.tictocer.toc(quiet=True)

        print 'save as', filename
        save_svm(clf, filename)
    else:
        print 'Don\'t compute SVM, no examples given'

    print 'training all classifier SVMs took:', cc.tictocer.toc(
        'overall', quiet=True), 'seconds on', comm_rank
Esempio n. 2
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. 3
0
def train_image_classify_svm(cc, cls, C=1.0, gamma=0.0, force_new=False):
  current_fold = cc.d.current_fold
  filename = config.get_classifier_svm_name(cls, C, gamma, current_fold)
  if not force_new and os.path.exists(filename):
    print 'svm for class %s, C=%f, gamma=%f exists already'%(cls,C,gamma)
    return
      
  pyr_feat_size = get_pyr_feat_size(cc.L, cc.dense_codebook.shape[0])  
  
  cc.tictocer.tic('overall') 
  
  print 'compute classifier(C=%f, gamma=%f) for class %s'%(C, gamma, cls)
  pos_images = cc.d.get_pos_samples_for_fold_class(cls)
  if pos_images.size == 0:
    return
  neg_images = cc.d.get_neg_samples_for_fold_class(cls, pos_images.size)
    
  # 1. extract all the pyramids    
  # ======== POSTIVE IMAGES ===========
  print 'compute feature vector for positive images'
  num_slices = 3
  pos_pyrs = np.zeros((len(pos_images),pyr_feat_size + cc.sparse_codebook.shape[0]*(1+num_slices)))      
  for idx, img in enumerate(pos_images):
    pos_pyrs[idx, :] = get_feature_vector(cc, img)

  # ======== NEGATIVE IMAGES ===========
  print 'compute feature vector for negative images'
  neg_pyrs = np.zeros((len(neg_images),pyr_feat_size + cc.sparse_codebook.shape[0]*(1+num_slices)))  
  for idx, img in enumerate(neg_images):
    neg_pyrs[idx, :] = get_feature_vector(cc, img)
  
  # 2. Compute SVM for class
  X = np.vstack((pos_pyrs, neg_pyrs))
  Y = [1]*pos_pyrs.shape[0] + [-1]*neg_pyrs.shape[0] 
  
  if X.shape[0] > 0:
    print 'train svm for class %s, C=%f, gamma=%f'%(cls,C,gamma)
    cc.tictocer.tic()    
    clf = train_svm(X, Y, kernel='rbf', gamma=gamma, C=C)
    print '\ttook %f seconds'%cc.tictocer.toc(quiet=True)
    
    print 'save as', filename
    save_svm(clf, filename)
  else:
    print 'Don\'t compute SVM, no examples given'
  
  print 'training all classifier SVMs took:', cc.tictocer.toc('overall', quiet=True), 'seconds on', comm_rank