def run_experiment_2(path=get_dropbox_path()+"yes-no-test/"): """ Run Experiment 2. Use the Yes/No sample collection, but only the "Y" and "NONE" samples. Use a single class classifier (for Yes). Run the offline version of the SVM classifier. Parameters: ----------- path - string the path to search for the samples """ print('Running Experiment 2') # return a list of the audio test samples samples = find_testsamples(path) classes=["Y","NONE"] sample_set = SampleSet(samples,classes=classes) sample_set.stats() (train,test) = sample_set.sample() # generate features and classes from TestSamples train_features = [] train_classes = [] for sample in train: train_features.append(feature_factory(sample)) train_classes.append(single_class_factory(sample,"Y")) #print(str(sample)) #print('class: '+str(single_class_factory(sample,"Y"))) # train the classifier print("Training classifier...") classifier = SVMClassifier() classifier.train(train_features,train_classes) print("Testing classifier...") # Test samples one by one score = 0 for sample in test: prediction = classifier.predict(feature_factory(sample)) test_class = single_class_factory(sample,"Y") if prediction != test_class: print(str(sample)) print("incorrect classification (prediction: "+str(prediction)+", class: "+str(test_class)+")") print("feature: "+str(feature_factory(sample))) else: print("correct classification") score=score+1 print('Classification score: '+str(score)+'/'+str(len(test))) print('Classification score: '+str(float(score)/float(len(test))))
def run_experiment_0(path=get_dropbox_path()+"old-test/"): """ Run Experiment 0. Use the old simple vowel sample collection (only A and E present). Use a single class classifier (for A). Run the offline version of the Template Classifier. Parameters: ----------- path - string the path to search for the Templates and TestRecordings. """ print('Running Experiment 0') print('Running template matching') classifier = TemplateClassifier() # train classifier templates = find_templates(path) if len(templates) < 1: raise ValueError('No templates found! aborting') print("Training on template: "+str(templates[0])) # train on a single template for now classifier.train(templates[0]) # test a single recording with classifier recordings = find_testrecordings(get_dropbox_path()) classifier.test_recording(recordings[0].path) print "Test: "+str(recordings[0]) # plot results plt.plot(classifier._get_raw_result()) plt.show() print 'Classifier test times: '+str(classifier._get_raw_test_times()) print 'Classifier read times: '+str(classifier._get_raw_read_times())
def run_experiment_1(include_none=False,path=get_dropbox_path()+"yes-no-test/"): """ Run Experiment 1. Use the Yes/No sample collection. Use a single class classifier (for Yes). Run the offline version of the SVM classifier. Parameters: ----------- include_none - bool whether or not to include the "NONE" class samples path - string the path to search for the samples """ print('Running Experiment 1') # return a list of the audio test samples samples = find_testsamples(path) classes=["Y","N"] if include_none: classes.append("NONE") sample_set = SampleSet(samples,classes=classes) sample_set.stats() (train,test) = sample_set.sample() # generate features and classes from TestSamples train_features = [] train_classes = [] for sample in train: train_features.append(feature_factory(sample)) train_classes.append(class_factory(sample)) print('class: '+str(class_factory(sample))) # train the classifier classifier = SVMClassifier() classifier.train(train_features,train_classes) # generate features and classes from TestSamples test_features = [] test_classes = [] for sample in test: test_features.append(feature_factory(sample)) test_classes.append(class_factory(sample)) # test the classifier classifier.test(test_features,test_classes)
def train_offline_svm_test_live(path=get_dropbox_path()): """ Train the offline version of the SVM classifier on all samples found in the path. Then run the classifier on 1 second inputs live. Parameters ---------- path - string The directory to search for TestSamples """ print('Training SVM classifier') # return a list of the audio test samples samples = find_testsamples(path) sample_set = SampleSet(samples,classes=["Y","NONE"]) sample_set.stats() (train,test) = sample_set.sample(in_order=False) # generate features and classes from TestSamples train_features = [] train_classes = [] for sample in train: train_features.append(feature_factory(sample)) train_classes.append(class_factory(sample)) # train the classifier classifier = SVMClassifier() classifier.train(train_features,train_classes) prompt ='Would you like to record a 3-sec sample to test?' if raw_input(prompt) == 'y': print('Recording now...') live_recording = record_sample(samplerate=22050,blocking=True) # process the recording into test samples test_samples = process_live_recording(live_recording,"UChiApt",str(time.time()),22050) print('Testing...') for sample in test_samples: feature = feature_factory(sample) print("Prediction: "+str(classifier.predict(feature))) else: print('Exiting...')
# This contains a collection of short examples about how to use the new # classes in the "dataset" Python module. Recall that a Python module is simply # a collection of classes and functions within a single file. from cssigps.dataset import * from get_dropbox_path import * if __name__ == '__main__': print('Running example3 - process test recordings to generate new samples') # generate samples for all of the current recordings path=get_dropbox_path()+"simple-yes-no-test/" recordings = find_testrecordings(path) print('\nFound '+str(len(recordings))+' recordings') if len(recordings) > 0: prompt='This will generate new sample wav files for each test recording.\n Do you want to continue? (y/n)' if raw_input(prompt) == 'y': for recording in recordings: print('\t'+str(recording)) recording.generate_samples('..'+os.sep+'samples'+os.sep) else: print('aborting...')
# generate features and classes from TestSamples train_features = [] train_classes = [] for sample in train: train_features.append(feature_factory(sample)) train_classes.append(class_factory(sample)) # train the classifier classifier = SVMClassifier() classifier.train(train_features,train_classes) prompt ='Would you like to record a 3-sec sample to test?' if raw_input(prompt) == 'y': print('Recording now...') live_recording = record_sample(samplerate=22050,blocking=True) # process the recording into test samples test_samples = process_live_recording(live_recording,"UChiApt",str(time.time()),22050) print('Testing...') for sample in test_samples: feature = feature_factory(sample) print("Prediction: "+str(classifier.predict(feature))) else: print('Exiting...') if __name__ == '__main__': print("Training the classifier against the Yes/No dataset") path = get_dropbox_path()+"yes-no-test/" train_offline_svm_test_live(path)
if __name__ == '__main__': # decide which experiment to run based on the command line or user-input response = "" if len(sys.argv) >= 2: response=sys.argv[1] if response in ["-h","--help"]: print_usage() quit() else: prompt = "Which experiment would you like to run? [0-2]" response = raw_input(prompt) # run experiment if response == "0": path=get_dropbox_path()+"old-test/" run_experiment_0(path) elif response == "1": run_experiment_1(include_none=True) elif response == "2": run_experiment_2() elif response == "3": run_experiment_3() elif response == "4": run_experiment_4() elif response == "5": path=get_dropbox_path()+"vowels-test/" run_offline_svm(path) elif response == "S": # run single class classifier c = sys.argv[2]
# This contains a collection of short examples about how to use the new # classes in the "dataset" Python module. Recall that a Python module is simply # a collection of classes and functions within a single file. from cssigps.dataset import * from cssigps.feature import * from get_dropbox_path import * if __name__ == '__main__': print('Running example4 - generates features for each test sample') path=get_dropbox_path() # return a list of the audio test samples samples = find_testsamples(path) print('\nFound '+str(len(samples))+' samples') # generate features print("\nGenerating MFCCFeature") for sample in samples: if sample.env == 'Q1': sample.check() print('\t'+str(sample)) # feature = FreqBinFeature(sample) feature = MFCCFeature(sample) #FreqBinFeature(sample) feature.stats() break