def svm(): # Training Parameters # Defines how high the cost is of a misclassification # versus making the decision plane more complex. # Low COST makes decisions very simple but creates classification errors COST = 0.9 # Used for generalisation # - Low GAMMA means high generalisation # - High GAMMA is closer to original dataset GAMMA = 6 KERNEL = RBF svm_model.predict = lambda self, x: svm_predict([0], [x], self)[0][0] # Get the data SPARSE_LENGTH = 16 sparseCodings = sparse_coding.generateFull(SPARSE_LENGTH) dataset, data, outputs, classes = sparse_coding.toSVMProblem(sparseCodings) # Set the parameters for the SVM parameters = svm_parameter() parameters.kernel_type = KERNEL parameters.C = COST parameters.gamma = GAMMA # Train the SVM solver = svm_train(dataset, parameters) # Create the output path if it doesn't exist generated_dir = path.abspath( path.join( "generated", "Q2Task1-TrainedSVM-{}".format(strftime("%Y-%m-%d_%H-%M-%S")))) if not path.exists(generated_dir): makedirs(generated_dir) uniqueFileName = path.normpath(path.join(generated_dir, "data.pkl")) svm_save_model(uniqueFileName, solver) # Compare the results to the extected values figure = plot.figure() axis = figure.add_subplot(111) colors = ['r', 'y', 'g', 'c', 'b', 'k'] for sample in sparseCodings: classifier = sparse_coding.getClassifier(sample) activationResult = svm_predict([0.], [sample], solver, '-q')[0][0] axis.bar(classifier, activationResult, color=colors[classifier % len(colors)]) plot.savefig(path.normpath(path.join(generated_dir, "activations.png"))) plot.show()
def valid(self,datasets,opt,opp,method = fold,part_ids = None,seed = None,test_data = None): if seed is None: # If seed is not set. UNIX time is used as seed. seed = time.time() saving_seed = "%s/log/%s.log.seed" % (self._dir,self._name) with open(saving_seed,"w") as fp: # Save used seed value. fp.write("seed:%f\n" % seed) if part_ids is None: part_ids = datasets.pids groups = [(test,train) for test,train in method(part_ids,seed = seed)] for cnt,pdtsts in enumerate(groups): # cnt is number of cluster. if test_data is None: test = False ltest,dtest,itest = test2svm_prob(datasets.mkTest(pdtsts[0])) else: test = True ltest,dtest,itest = test2svm_prob(test_data.mkTest(test_data.pids)) print "start %s validation" % (cnt) ptrn,itrain = train2svm_prob(datasets.mkTrain(pdtsts[1])) #opt = svm.svm_parameter(opt) model = svmutil.svm_train(ptrn,opt) plbl,pacc,pval = svmutil.svm_predict(ltest,dtest,model,opp) # create saving direcotry #self._mkdir(cnt) # create log files self._save_log(itest,plbl,pval,cnt,test) model_name = "%s/model/%s.model.%s" % (self._dir,self._name,cnt)
def plotSVM(solver, lowerBound, upperBound, step): assert lowerBound < upperBound X = arange(lowerBound, upperBound, step) Y = arange(lowerBound, upperBound, step) X,Y = meshgrid(X,Y) Z = zeros(X.shape) for i in range(len(X)): for j in range(len(Y)): #Classify the result result = int( svm_predict([0.], [[ X[i][j], Y[i][j] ]], solver, '-q')[0][0] ) if result == 0: Z[i][j] = 0 #lower limit else: Z[i][j] = 100 #higher limit plot.imshow(Z) plot.gcf() plot.clim() plot.title("SVM Activation") return plot
def plotSVM(solver, lowerBound, upperBound, step): assert lowerBound < upperBound X = arange(lowerBound, upperBound, step) Y = arange(lowerBound, upperBound, step) X, Y = meshgrid(X, Y) Z = zeros(X.shape) for i in range(len(X)): for j in range(len(Y)): #Classify the result result = int( svm_predict([0.], [[X[i][j], Y[i][j]]], solver, '-q')[0][0]) if result == 0: Z[i][j] = 0 #lower limit else: Z[i][j] = 100 #higher limit plot.imshow(Z) plot.gcf() plot.clim() plot.title("SVM Activation") return plot
def classify_audio_file(audio_file,cfg, ext=None): if isinstance(cfg,types.StringTypes): cfg_path, _name=os.path.split(cfg) cfg=eval(open(cfg).read()) if cfg_path: if not cfg['range_file'].startswith(os.sep): cfg['range_file']=os.path.join(cfg_path, cfg['range_file']) if not cfg['model_file'].startswith(os.sep): cfg['model_file']=os.path.join(cfg_path, cfg['model_file']) classes=cfg.get('classes') r=load_scaling_range(cfg['range_file']) a,sr=audiolib.load_audio_sample(audio_file, ext) logger.debug('Loaded audio sample of length %.2f' % (len(a)/float(sr))) segs=audiolib.segment_audio(cfg['segalg'],a, sr, limit=cfg['threshold'], **cfg['params']) [cfg['start_index']:cfg['end_index']] scaled=[] for s in segs: features= audiolib.calc_mfcc(s, sr, nbins=cfg['nbins']) scaled.append(scale_sample(features, r)) if model_cache.has_key(cfg['model_file']): m=model_cache[cfg['model_file']] else: with module_lock: m=svmutil.svm_load_model(cfg['model_file']) model_cache[cfg['model_file']]=m p_labels, p_acc, p_vals=svmutil.svm_predict([0]*len(scaled), scaled, m, '-q') #logger.debug('prediction additional info ' +str(p_acc)+str(p_vals)) res=''.join([cls2char(int(n), classes) for n in p_labels]) return res
def classify_audio_file(audio_file, cfg, ext=None): if isinstance(cfg, types.StringTypes): cfg_path, _name = os.path.split(cfg) cfg = eval(open(cfg).read()) if cfg_path: if not cfg['range_file'].startswith(os.sep): cfg['range_file'] = os.path.join(cfg_path, cfg['range_file']) if not cfg['model_file'].startswith(os.sep): cfg['model_file'] = os.path.join(cfg_path, cfg['model_file']) classes = cfg.get('classes') r = load_scaling_range(cfg['range_file']) a, sr = audiolib.load_audio_sample(audio_file, ext) logger.debug('Loaded audio sample of length %.2f' % (len(a) / float(sr))) segs = audiolib.segment_audio( cfg['segalg'], a, sr, limit=cfg['threshold'], **cfg['params'])[cfg['start_index']:cfg['end_index']] scaled = [] for s in segs: features = audiolib.calc_mfcc(s, sr, nbins=cfg['nbins']) scaled.append(scale_sample(features, r)) if model_cache.has_key(cfg['model_file']): m = model_cache[cfg['model_file']] else: with module_lock: m = svmutil.svm_load_model(cfg['model_file']) model_cache[cfg['model_file']] = m p_labels, p_acc, p_vals = svmutil.svm_predict([0] * len(scaled), scaled, m, '-q') #logger.debug('prediction additional info ' +str(p_acc)+str(p_vals)) res = ''.join([cls2char(int(n), classes) for n in p_labels]) return res
def valid(self,datasets,opt,opp,method = fold,seed = None): # Should groups and ngroups be idch ? groups = [(test,train) for test,train in method(datasets.pids,seed = seed)] ngroups = [(test,train) for test,train in method(datasets.nids,seed = seed)] for cnt,(pdtsts,ndtsts) in enumerate(zip(groups,ngroups)): # cnt is number of cluster. ltest,dtest,itest = test2svm_prob(datasets.mkTest(pdtsts[0],ndtsts[0])) ptrn,itrain = train2svm_prob(datasets.mkTrain(pdtsts[1],ndtsts[1])) print "start %s validation" % (cnt) #opt = svm.svm_parameter(opt) model = svmutil.svm_train(ptrn,opt) plbl,pacc,pval = svmutil.svm_predict(ltest,dtest,model,opp) # create saving direcotry #self._mkdir(cnt) # create log files self._save_log(itest,plbl,pval,cnt) model_name = "%s/model/%s.model.%s" % (self._dir,self._name,cnt)
import sys import os path = os.path.abspath(os.path.dirname(sys.argv[0])) sys.path.append(path) from svm.svm import * from svm.svmutil import svm_train, svm_predict, svm_save_model from support import csv #from graphpy import SVM2D # importing this doesn't work for some reason from time import strftime, time from matplotlib import pyplot as plot from numpy import arange, meshgrid, zeros KERNEL = RBF # The kernel in the .dll file to use svm_model.predict = lambda self, x: svm_predict([0], [x], self)[0][0] def plotSVM(solver, lowerBound, upperBound, step): assert lowerBound < upperBound X = arange(lowerBound, upperBound, step) Y = arange(lowerBound, upperBound, step) X,Y = meshgrid(X,Y) Z = zeros(X.shape) for i in range(len(X)): for j in range(len(Y)): #Classify the result result = int( svm_predict([0.], [[ X[i][j], Y[i][j] ]], solver, '-q')[0][0] )
import sys import os path = os.path.abspath(os.path.dirname(sys.argv[0])) sys.path.append(path) from svm.svm import * from svm.svmutil import svm_train, svm_predict, svm_save_model from support import csv #from graphpy import SVM2D # importing this doesn't work for some reason from time import strftime, time from matplotlib import pyplot as plot from numpy import arange, meshgrid, zeros KERNEL = RBF # The kernel in the .dll file to use svm_model.predict = lambda self, x: svm_predict([0], [x], self)[0][0] def plotSVM(solver, lowerBound, upperBound, step): assert lowerBound < upperBound X = arange(lowerBound, upperBound, step) Y = arange(lowerBound, upperBound, step) X, Y = meshgrid(X, Y) Z = zeros(X.shape) for i in range(len(X)): for j in range(len(Y)): #Classify the result