def train_svm(self, dataset, kernel='linear', C=1.0, gamma=0.0): """ Train classifiers for class """ print '%d trains class %s'%(mpi.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'%(mpi.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 = self.svm.predict_proba(x) 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 cPickle.dump(self.svm, open(svm_filename, 'w')) return ap
def train_svm(self, dataset, kernel='linear', C=1.0, gamma=0.0): """ Train classifiers for class """ print '%d trains class %s' % (mpi.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' % (mpi.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 = self.svm.predict_proba(x) 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 cPickle.dump(self.svm, open(svm_filename, 'w')) return ap
def load_svm(self): filename = config.get_gist_svm_filename(self.cls,self.train_d) if os.path.exists(filename): print 'load svm %s'%filename try: svm = cPickle.loads(open(filename).read()) self.svm = svm except: print 'could not load svm. Probably we are in crossvalidation and there is much going on' svm = None else: print 'gist svm for',self.cls,'does not exist at %s'%filename svm = None return svm
def load_svm(self): filename = config.get_gist_svm_filename(self.cls, self.train_d) if os.path.exists(filename): print 'load svm %s' % filename try: svm = cPickle.loads(open(filename).read()) self.svm = svm except: print 'could not load svm. Probably we are in crossvalidation and there is much going on' svm = None else: print 'gist svm for', self.cls, 'does not exist at %s' % filename svm = None return svm