示例#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
示例#2
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
示例#3
0
 def save_svm(self, model, filename):
   save_svm(model, filename)