def __init__(self,Gs,ls):
     self._K = ls.shape[1]
     self._Gs = Gs
     self._N = Gs.shape[1]
     self._ls = ls
     self._svm = LibSvm('c_svc','rbf',\
         gamma=1.0/self._N,C=100,probability=True)                
class Svm(object):   
    
    def __init__(self,Gs,ls):
        self._K = ls.shape[1]
        self._Gs = Gs
        self._N = Gs.shape[1]
        self._ls = ls
        self._svm = LibSvm('c_svc','rbf',\
            gamma=1.0/self._N,C=100,probability=True)                
    
    def train(self):
        try:
            labels = np.argmax(self._ls,axis=1)
            idx = np.where(labels == 0)[0]
            ls = np.ones(len(idx),dtype=np.int)
            Gs = self._Gs[idx,:]
            for k in range(1,self._K):
                idx = np.where(labels == k)[0]
                ls = np.concatenate((ls, \
                (k+1)*np.ones(len(idx),dtype=np.int)))
                Gs = np.concatenate((Gs,\
                               self._Gs[idx,:]),axis=0)         
            self._svm.learn(Gs,ls)  
            return True 
        except Exception as e:
            print 'Error: %s'%e 
            return False    
    
    def classify(self,Gs):
        probs = np.transpose(self._svm. \
                             pred_probability(Gs))       
        classes = np.argmax(probs,axis=0)+1
        return (classes, probs) 
Exemple #3
0
 def BuildModel(self, data, labels):
   # Create and train the classifier.
   svm = LibSvm(kernel_type=self.kernel,
                C=self.C,
                gamma=self.gamma)
   svm.learn(data, labels)
   return svm
Exemple #4
0
def svmtrain(pos, neg):
    y = np.hstack((np.ones(len(pos),dtype=int),np.zeros(len(neg),dtype=int)))
    x = np.vstack((pos, neg))
    # The values of gamma and C come from grid.py in libsvm/tools
    svm = LibSvm(kernel_type='rbf', gamma=8.0, C=128.0)
    svm.learn(x,y)
    svm.save_model("svm.model")
Exemple #5
0
 def BuildModel(self, data, labels):
   # Create and train the classifier.
   svm = LibSvm(kernel_type=self.kernel,
                C=self.C,
                gamma=self.gamma)
   svm.learn(data, labels)
   return svm
Exemple #6
0
class VoiceCmd:
    def __init__(self, config):
        self.config = config
        self.commands = {}
        self.command_names = {}
        commands_folder = self.config["commands_folder"]
        for index, command_name in enumerate(os.listdir(commands_folder)):
            cmd = Command(command_name, index, os.path.join(commands_folder, command_name))
            self.commands[command_name] = cmd
            self.command_names[index] = command_name

        self.svm = LibSvm(svm_type="c_svc", kernel_type="linear")
        x, y = [], []
        for command in self.commands.itervalues():
            for feature in command.objects:
                x.append(feature)
                y.append(command.index)
        self.svm.learn(x, y)

    def run(self):
        print "Speak now."
        signal = self.read_voice()
        # signal = load_wav('commands/calculator/command1.wav')
        print "Recording stopped"
        decision = self.predict(signal)
        print decision
        self.commands[decision].execute()

    def predict(self, signal):
        return self.command_names[int(self.svm.pred(signal))]

    def distance(self, template, query):
        return dtw(template, query)[0]

    def read_voice(self):
        rate, fpb, seconds, channels = 44100, 1024, 3, 1
        read = []
        p = pyaudio.PyAudio()
        stream = p.open(format=pyaudio.paInt16, channels=channels, rate=rate, input=True, frames_per_buffer=fpb)
        chunks_needed = rate / fpb * seconds
        for i in xrange(chunks_needed):
            data = stream.read(fpb)
            read.append(data)
        stream.close()
        p.terminate()
        data = "".join(read)

        debug = wave.open("debug.wav", "wb")
        debug.setnchannels(channels)
        debug.setsampwidth(p.get_sample_size(pyaudio.paInt16))
        debug.setframerate(rate)
        debug.writeframes(data)
        debug.close()

        out = unpack_wav(data, chunks_needed * fpb, channels)
        return out
Exemple #7
0
def bench_mlpy(X, y, T, valid):
#
#       .. MLPy ..
#
    from mlpy import LibSvm
    start = datetime.now()
    clf = LibSvm(kernel_type='rbf', C=1., gamma=1./sigma)
    clf.learn(X, y.astype(np.float64))
    score = np.mean(clf.pred(T) == valid)
    return score, datetime.now() - start
def bench_mlpy(X, y, T, valid):
#
#       .. MLPy ..
#
    from mlpy import LibSvm
    start = datetime.now()
    clf = LibSvm(kernel_type='rbf', C=1., gamma=1. / sigma)
    clf.learn(X, y.astype(np.float64))
    score = np.mean(clf.pred(T) == valid)
    return score, datetime.now() - start
 def __init__(self,Gs,ls):
     self._K = ls.shape[1]
     self._Gs = Gs
     self._N = Gs.shape[1]
     self._ls = ls
     self._svm = LibSvm('c_svc','rbf',\
         gamma=1.0/self._N,C=100,probability=True)                
Exemple #10
0
    def __init__(self, dictionary, mode):
        """
        Initial the Retrival Object, including parameters (sample rate, hashing density,
        freqency offset, number of keys used in hashing) and initializing hashing table.
        """
        self.sr = 8000
        # the target hashes-per-sec
        self.density = 20
        # number of hash keys are corresponded to the composition of landmarks
        # They are <freq-bin number, delta frequency, delta time span>
        self.nkey = 3
        # The fist two elements relate with spectrumgram and FFT bands
        # The last is relevant with the targetdt in find_landmarks
        # The calculate of hashlength is log2(original value)
        self.hashlength = [8,6,8]
        # time span for sampling
        self.hopt = 0.01
        # Configures for extract time slot from database
        self.window = 4500      # extract features within 45s
        self.step = 20          # the searching step is 200ms
        # load the trained SVM classifier
        self.svm = LibSvm.load_model("svm.model")
        # Take frequncy offset into consideration
        self.freqoffset = 4

        self.numoftable = 10
        self.dict = dictionary
        self.mode = mode
Exemple #11
0
    def __init__(self, config):
        self.config = config
        self.commands = {}
        self.command_names = {}
        commands_folder = self.config['commands_folder']
        for index, command_name in enumerate(os.listdir(commands_folder)):
            cmd = Command(command_name, index,
                          os.path.join(commands_folder, command_name))
            self.commands[command_name] = cmd
            self.command_names[index] = command_name

        self.svm = LibSvm(svm_type='c_svc', kernel_type='linear')
        x, y = [], []
        for command in self.commands.itervalues():
            for feature in command.objects:
                x.append(feature)
                y.append(command.index)
        self.svm.learn(x, y)
class Svm(object):   
      
    def __init__(self,Gs,ls):
        self._K = ls.shape[1]
        self._Gs = Gs
        self._N = Gs.shape[1]
        self._ls = ls
        self._svm = LibSvm('c_svc','rbf',\
            gamma=1.0/self._N,C=100,probability=True)                
      
    def train(self):
        try:
            labels = np.argmax(self._ls,axis=1)
            idx = np.where(labels == 0)[0]
            ls = np.ones(len(idx),dtype=np.int)
            Gs = self._Gs[idx,:]
            for k in range(1,self._K):
                idx = np.where(labels == k)[0]
                ls = np.concatenate((ls, \
                (k+1)*np.ones(len(idx),dtype=np.int)))
                Gs = np.concatenate((Gs,\
                               self._Gs[idx,:]),axis=0)         
            self._svm.learn(Gs,ls)  
            return True 
        except Exception as e:
            print 'Error: %s'%e 
            return None   
      
    def classify(self,Gs):
        probs = np.transpose(self._svm. \
                             pred_probability(Gs))       
        classes = np.argmax(probs,axis=0)+1
        return (classes, np.transpose(probs)) 
              
    def test(self,Gs,ls):
        m = np.shape(Gs)[0]
        classes, _ = self.classify(Gs)
        classes = np.asarray(classes,np.int16)
        labels = np.argmax(np.transpose(ls),axis=0)+1
        misscls = np.where(classes-labels)[0]
        return len(misscls)/float(m)              
Exemple #13
0
    def metric(self):
        totalTimer = Timer()
        with totalTimer:
            model = LibSvm(**self.build_opts)
            model.learn(self.data_split[0], self.data_split[1])

            if len(self.data) >= 2:
                predictions = model.pred(self.data[1])

        metric = {}
        metric["runtime"] = totalTimer.ElapsedTime()

        if len(self.data) == 3:
            confusionMatrix = Metrics.ConfusionMatrix(self.data[2],
                                                      predictions)
            metric['ACC'] = Metrics.AverageAccuracy(confusionMatrix)
            metric['MCC'] = Metrics.MCCMultiClass(confusionMatrix)
            metric['Precision'] = Metrics.AvgPrecision(confusionMatrix)
            metric['Recall'] = Metrics.AvgRecall(confusionMatrix)
            metric['MSE'] = Metrics.SimpleMeanSquaredError(
                self.data[2], predictions)

        return metric
Exemple #14
0
    def __init__(self, config):
        self.config = config
        self.commands = {}
        self.command_names = {}
        commands_folder = self.config["commands_folder"]
        for index, command_name in enumerate(os.listdir(commands_folder)):
            cmd = Command(command_name, index, os.path.join(commands_folder, command_name))
            self.commands[command_name] = cmd
            self.command_names[index] = command_name

        self.svm = LibSvm(svm_type="c_svc", kernel_type="linear")
        x, y = [], []
        for command in self.commands.itervalues():
            for feature in command.objects:
                x.append(feature)
                y.append(command.index)
        self.svm.learn(x, y)
Exemple #15
0
class VoiceCmd:
    def __init__(self, config):
        self.config = config
        self.commands = {}
        self.command_names = {}
        commands_folder = self.config['commands_folder']
        for index, command_name in enumerate(os.listdir(commands_folder)):
            cmd = Command(command_name, index,
                          os.path.join(commands_folder, command_name))
            self.commands[command_name] = cmd
            self.command_names[index] = command_name

        self.svm = LibSvm(svm_type='c_svc', kernel_type='linear')
        x, y = [], []
        for command in self.commands.itervalues():
            for feature in command.objects:
                x.append(feature)
                y.append(command.index)
        self.svm.learn(x, y)

    def run(self):
        print "Speak now."
        signal = self.read_voice()
        #signal = load_wav('commands/calculator/command1.wav')
        print "Recording stopped"
        decision = self.predict(signal)
        print decision
        self.commands[decision].execute()

    def predict(self, signal):
        return self.command_names[int(self.svm.pred(signal))]

    def distance(self, template, query):
        return dtw(template, query)[0]

    def read_voice(self):
        rate, fpb, seconds, channels = 44100, 1024, 3, 1
        read = []
        p = pyaudio.PyAudio()
        stream = p.open(format=pyaudio.paInt16,
                        channels=channels,
                        rate=rate,
                        input=True,
                        frames_per_buffer=fpb)
        chunks_needed = rate / fpb * seconds
        for i in xrange(chunks_needed):
            data = stream.read(fpb)
            read.append(data)
        stream.close()
        p.terminate()
        data = ''.join(read)

        debug = wave.open('debug.wav', 'wb')
        debug.setnchannels(channels)
        debug.setsampwidth(p.get_sample_size(pyaudio.paInt16))
        debug.setframerate(rate)
        debug.writeframes(data)
        debug.close()

        out = unpack_wav(data, chunks_needed * fpb, channels)
        return out