def __init__(self, wavfolder, trainingdata): """Initialise the classifier and train it on some WAV files. 'wavfolder' is the base folder, to be prepended to all WAV paths. 'trainingdata' is a dictionary of wavpath:label pairs.""" self.mfccMaker = melScaling(int(fs), framelen / 2, 40) self.mfccMaker.update() self.count = 0 allfeatures = { wavpath: self.file_to_features(os.path.join(wavfolder, wavpath)) for wavpath in trainingdata } #print('ALLfeatures:',allfeatures)####checking features### np.set_printoptions(threshold=np.nan) foobar = open("all_features_together_dataset_13.txt", "w") for label, aggf in allfeatures.items(): foobar.write("\"" + label + "\"") foobar.write(str(aggf)) foobar.close() # Determine the normalisation stats, and remember them allconcat = np.vstack(list(allfeatures.values( ))) #stack arrays in sequence vertically(row wise)) self.means = np.mean(allconcat, 0) #compute mean self.invstds = np.std(allconcat, 0) #compute the standard deviation for i, val in enumerate(self.invstds): if val == 0.0: self.invstds[i] = 1.0 else: self.invstds[i] = 1.0 / val # For each label, compile a normalised concatenated list of features aggfeatures = {} for wavpath, features in allfeatures.items(): label = trainingdata[wavpath] normed = self.__normalise(features) if label not in aggfeatures: aggfeatures[label] = normed else: aggfeatures[label] = np.vstack((aggfeatures[label], normed)) # For each label's aggregated features, train a GMM and remember it self.gmms = {} for label, aggf in aggfeatures.items(): if verbose: print( " Training a GMM for label %s, using data of shape %s" % (label, str(np.shape(aggf)))) self.gmms[label] = GMM(n_components=2) # , cvtype='full') self.gmms[label].fit(aggf) if verbose: print(" Trained %i classes from %i input files" % (len(self.gmms), len(trainingdata)))
def __init__(self, wavfolder, trainingdata): """Initialise the classifier and train it on some WAV files. 'wavfolder' is the base folder, to be prepended to all WAV paths. 'trainingdata' is a dictionary of wavpath:label pairs.""" self.mfccMaker = melScaling(int(fs), framelen / 2, 40) self.mfccMaker.update() allfeatures = { wavpath: self.file_to_features(os.path.join(wavfolder, wavpath)) for wavpath in trainingdata } # Determine the normalisation stats, and remember them allconcat = np.vstack(list(allfeatures.values())) self.means = np.mean(allconcat, 0) self.invstds = np.std(allconcat, 0) for i, val in enumerate(self.invstds): if val == 0.0: self.invstds[i] = 1.0 else: self.invstds[i] = 1.0 / val # For each label, compile a normalised concatenated list of features aggfeatures = {} for wavpath, features in allfeatures.items(): label = trainingdata[wavpath] normed = self.__normalise(features) if label not in aggfeatures: aggfeatures[label] = normed else: aggfeatures[label] = np.vstack((aggfeatures[label], normed)) # For each label's aggregated features, train a GMM and remember it self.GaussianMixtures = {} for label, aggf in aggfeatures.items(): if verbose: print( " Training a GMM for label %s, using data of shape %s" % (label, str(np.shape(aggf)))) self.GaussianMixtures[label] = GaussianMixture( n_components=10) # , cvtype='full') self.GaussianMixtures[label].fit(aggf) if verbose: print(" Trained %i classes from %i input files" % (len(self.GaussianMixtures), len(trainingdata)))
def __init__(self, wavfolder, trainingdata): """Initialise the classifier and train it on some WAV files. 'wavfolder' is the base folder, to be prepended to all WAV paths. 'trainingdata' is a dictionary of wavpath:label pairs.""" self.mfccMaker = melScaling(int(fs), framelen/2, 40) self.mfccMaker.update() allfeatures = {wavpath:self.file_to_features(os.path.join(wavfolder, wavpath)) for wavpath in trainingdata} ###print('ALLfeatures:',allfeatures)####checking features### # Determine the normalisation stats, and remember them allconcat = np.vstack(list(allfeatures.values()))#stack arrays in sequence vertically(row wise)) self.means = np.mean(allconcat, 0)#compute mean self.invstds = np.std(allconcat, 0)#compute the standard deviation for i,val in enumerate(self.invstds): if val == 0.0: self.invstds[i] = 1.0 else: self.invstds[i] = 1.0 / val # For each label, compile a normalised concatenated list of features aggfeatures = {} for wavpath, features in allfeatures.items(): label = trainingdata[wavpath] normed = self.__normalise(features) if label not in aggfeatures: aggfeatures[label] = normed else: aggfeatures[label] = np.vstack((aggfeatures[label], normed)) # For each label's aggregated features, train a GMM and remember it self.gmms = {} for label, aggf in aggfeatures.items(): if verbose: print(" Training a GMM for label %s, using data of shape %s" % (label, str(np.shape(aggf)))) self.gmms[label] = GMM(n_components=10) # , cvtype='full') self.gmms[label].fit(aggf) if verbose: print(" Trained %i classes from %i input files" % (len(self.gmms), len(trainingdata)))
def __init__(self): self.mfccMaker = melScaling(int(fs), framelen / 2, 40) self.mfccMaker.update()
def __init__(self): self.mfccMaker = melScaling(int(fs), framelen/2, 40) self.mfccMaker.update()
def __init__(self, wavfolder, trainingdata): """Initialise the classifier and train it on some WAV files. 'wavfolder' is the base folder, to be prepended to all WAV paths. 'trainingdata' is a dictionary of wavpath:label pairs.""" self.mfccMaker = melScaling(int(fs), framelen / 2, 40) self.mfccMaker.update() self.target = {} self.labelarr = [] self.labelarr2 = {} self.labelarr3 = {} self.target2 = [] i = 0 for wavpath in trainingdata: label = trainingdata[wavpath] if label not in self.labelarr2.values(): print " target classes :" + label self.labelarr.append(i) self.labelarr2[i] = label self.labelarr3[label] = i i = i + 1 else: self.labelarr.append(self.labelarr3[label]) allfeatures = { wavpath: self.file_to_features(os.path.join(wavfolder, wavpath), trainingdata[wavpath]) for wavpath in trainingdata } # print("**********features :",self.file_to_features(os.path.join(wavfolder,wavpath))) print ("allfeatures :" + str(np.shape(allfeatures.values()))) # print("target :",self.target2) print ("target values", self.labelarr) print ("target labels", self.labelarr2) ###print('ALLfeatures:',allfeatures)####checking features### # Determine the normalisation stats, and remember them allconcat = np.vstack(list(allfeatures.values())) # stack arrays in sequence vertically(row wise)) self.means = np.mean(allconcat, 0) # compute mean self.invstds = np.std(allconcat, 0) # compute the standard deviation for i, val in enumerate(self.invstds): if val == 0.0: self.invstds[i] = 1.0 else: self.invstds[i] = 1.0 / val # For each label, compile a normalised concatenated list of features aggfeatures = {} for wavpath, features in allfeatures.items(): label = trainingdata[wavpath] normed = self.__normalise(features) if label not in aggfeatures: aggfeatures[label] = normed else: aggfeatures[label] = np.vstack((aggfeatures[label], normed)) # For each label's aggregated features, train a SVM and remember it # self.svms = {} self.X = [] for label, aggf in aggfeatures.items(): if verbose: print (" Training a SVM for label %s, using data of shape %s" % (label, str(np.shape(aggf)))) # self.svms[label] = svm.SVC() # print("aggf : ",aggf) if not np.any(self.X): self.X = aggf else: # print("X: ",np.shape(self.X)) # print("aggf: ",np.shape(aggf)) self.X = np.vstack((self.X, aggf)) # self.svms[label].fit(aggf,self.target[label]) # print("X :",self.X) self.clf = svm.SVC() # print("target : ",self.target2) self.clf.fit(self.X, self.target2)
def __init__(self, wavfolder, trainingdata): """Initialise the classifier and train it on some WAV files. 'wavfolder' is the base folder, to be prepended to all WAV paths. 'trainingdata' is a dictionary of wavpath:label pairs.""" #open audio file as an AudioFile object for wavpath in trainingdata: audio_file = open((os.path.join(wavfolder, wavpath))) file_info=self.get_info(audio_file) #Creates a WaveReader object from the AudioFile Object pcm_data = audio_file.to_pcm() #Creates a FrameList object from WaveReader object. Currently reads all #frames in file frame_list = pcm_data.read(file_info["frames"]) #Convert samples to floats (-1.0 - +1.0) float_frame_list = frame_list.to_float() #eventually do some signal processing here... #Convert back to integer FrameList output_framelist = float_frame_list.to_int(file_info["bits"]) #now back to raw bytes output_data = output_framelist.to_bytes(False, True) self.mfccMaker = melScaling(int(fs), framelen/2, 40) self.mfccMaker.update() self.target={} self.labelarr=[] self.labelarr2={} self.labelarr3={} self.target2=[] i=0 for wavpath in trainingdata: label = trainingdata[wavpath] if label not in self.labelarr2.values(): print " target classes :"+label self.labelarr.append(i) self.labelarr2[i]=label self.labelarr3[label]=i i=i+1 else: self.labelarr.append(self.labelarr3[label]) allfeatures = {wavpath:self.file_to_features(os.path.join(wavfolder, wavpath),trainingdata[wavpath]) for wavpath in trainingdata} #print("**********features :",self.file_to_features(os.path.join(wavfolder,wavpath))) print("allfeatures :"+str(np.shape(allfeatures.values()))) #print("target :",self.target2) print('target values', self.labelarr) print('target labels', self.labelarr2) ###print('ALLfeatures:',allfeatures)####checking features### # Determine the normalisation stats, and remember them allconcat = np.vstack(list(allfeatures.values()))#stack arrays in sequence vertically(row wise)) self.means = np.mean(allconcat, 0)#compute mean self.invstds = np.std(allconcat, 0)#compute the standard deviation for i,val in enumerate(self.invstds): if val == 0.0: self.invstds[i] = 1.0 else: self.invstds[i] = 1.0 / val # For each label, compile a normalised concatenated list of features aggfeatures = {} for wavpath, features in allfeatures.items(): label = trainingdata[wavpath] normed = self.__normalise(features) if label not in aggfeatures: aggfeatures[label] = normed else: aggfeatures[label] = np.vstack((aggfeatures[label], normed)) # For each label's aggregated features, train a SVM and remember it #self.svms = {} self.X=[] for label, aggf in aggfeatures.items(): if verbose: print(" Training a SVM for label %s, using data of shape %s" % (label, str(np.shape(aggf)))) #self.svms[label] = svm.SVC() #print("aggf : ",aggf) if not np.any(self.X): self.X=aggf else: #print("X: ",np.shape(self.X)) #print("aggf: ",np.shape(aggf)) self.X=np.vstack((self.X,aggf)) #self.svms[label].fit(aggf,self.target[label]) #print("X :",self.X) self.clf=svm.SVC() #print("target : ",self.target2) self.clf.fit(self.X,self.target2)
def __init__(self, wavfolder, trainingdata): """Initialise the classifier and train it on some WAV files. 'wavfolder' is the base folder, to be prepended to all WAV paths. 'trainingdata' is a dictionary of wavpath:label pairs.""" #open audio file as an AudioFile object for wavpath in trainingdata: audio_file = open((os.path.join(wavfolder, wavpath))) file_info=self.get_info(audio_file) #Creates a WaveReader object from the AudioFile Object pcm_data = audio_file.to_pcm() #Creates a FrameList object from WaveReader object. Currently reads all #frames in file frame_list = pcm_data.read(file_info["frames"]) #Convert samples to floats (-1.0 - +1.0) float_frame_list = frame_list.to_float() #eventually do some signal processing here... #Convert back to integer FrameList output_framelist = float_frame_list.to_int(file_info["bits"]) #now back to raw bytes output_data = output_framelist.to_bytes(False, True) self.mfccMaker = melScaling(int(fs), framelen/2, 40) self.mfccMaker.update() allfeatures = {wavpath:self.file_to_features(os.path.join(wavfolder, wavpath)) for wavpath in trainingdata} ###print('ALLfeatures:',allfeatures)####checking features### # Determine the normalisation stats, and remember them allconcat = np.vstack(list(allfeatures.values()))#stack arrays in sequence vertically(row wise)) self.means = np.mean(allconcat, 0)#compute mean self.invstds = np.std(allconcat, 0)#compute the standard deviation for i,val in enumerate(self.invstds): if val == 0.0: self.invstds[i] = 1.0 else: self.invstds[i] = 1.0 / val # For each label, compile a normalised concatenated list of features aggfeatures = {} for wavpath, features in allfeatures.items(): label = trainingdata[wavpath] normed = self.__normalise(features) if label not in aggfeatures: aggfeatures[label] = normed else: aggfeatures[label] = np.vstack((aggfeatures[label], normed)) # For each label's aggregated features, train a GMM and remember it self.gmms = {} for label, aggf in aggfeatures.items(): if verbose: print(" Training a GMM for label %s, using data of shape %s" % (label, str(np.shape(aggf)))) self.gmms[label] = GMM(n_components=10) # , cvtype='full') self.gmms[label].fit(aggf) if verbose: print(" Trained %i classes from %i input files" % (len(self.gmms), len(trainingdata)))