def main(): adv_ms = 10 len_ms = 20 offset_s = 0.35 trdir = os.getcwd() + "/train" tedir = os.getcwd() + "/test" components = 28 # get training files try: trainFiles = np.genfromtxt('trainFileList.txt', dtype='str') [train_Y, trainFiles] = get_labels_path(trainFiles) print('Train Files List Loaded') except IOError: trainFiles = sorted(get_corpus(trdir)) [trainFiles, trSilences] = fileFiltSave(trainFiles, 'trainFileList.txt', 'train') del trSilences [train_Y, trainFiles] = get_labels_path(trainFiles) print('Train Files List Saved') # get testing files try: testFiles = np.genfromtxt('testFileList.txt', dtype='str') testSilences = np.genfromtxt('teSilenceList.txt', dtype='str') print('Test Files List Loaded') except IOError: testFiles = np.array(sorted(get_corpus(tedir))) [testFiles, testSilences] = fileFiltSave(testFiles, 'testFileList.txt', 'teSilenceList.txt') print('Test Files List Saved') try: #load train data train_X = np.fromfile('trainData.dat', dtype=float) samplesN = trainFiles.shape[0] data_dim = components timesteps = int(train_X.shape[0] / samplesN / data_dim) train_X = np.reshape(train_X, (samplesN, timesteps, data_dim)) print('Train Data Features Loaded') # load test data test_X = np.fromfile('testData.dat', dtype=float) test_samplesN = testFiles.shape[0] test_X = np.reshape(test_X, (test_samplesN, timesteps, data_dim)) print('Test Data Features Loaded') except IOError: timer = Timer() pca = pca_analysis_of_spectra(trainFiles, adv_ms, len_ms, offset_s) print( "PCA feature generation and analysis time {}, feature extraction..." .format(timer.elapsed())) timer.reset() # Read features - each row is a feature vector train_X = extract_features_from_corpus(trainFiles, adv_ms, len_ms, offset_s, pca, components) print("Time to generate features {}".format(timer.elapsed())) timer.reset() [samplesN, data_dim] = train_X.shape timesteps = 1 train_X.tofile('trainData.dat') print('Train Data Features Saved') train_X = train_X.flatten() train_X = np.reshape(train_X, (samplesN, timesteps, data_dim)) # Read features - each row is a feature vector test_X = extract_features_from_corpus(testFiles, adv_ms, len_ms, offset_s, pca, components) [test_samplesN, data_dim] = test_X.shape test_X.tofile('testData.dat') print('Test Data Features Saved') test_X = test_X.flatten() test_X = np.reshape(test_X, (test_samplesN, timesteps, data_dim)) num_classes = len(set(train_Y)) try: json_file = open('model.json', 'r') loaded_model_json = json_file.read() json_file.close() model = model_from_json(loaded_model_json) # load weights into new model model.load_weights("LSTMgoogle.h5") print("Loaded model from disk") model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) except IOError: model = Sequential() model.add( LSTM(256, return_sequences=True, input_shape=( timesteps, data_dim))) # returns a sequence of vectors of dimension 256 model.add(LSTM(256, return_sequences=True) ) # returns a sequence of vectors of dimension 256 model.add(LSTM(256, return_sequences=True) ) # returns a sequence of vectors of dimension 256 model.add(LSTM(256)) # return a single vector of dimension 256 model.add(Dense(num_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) kfold = StratifiedKFold(2, shuffle=True) for (train_idx, test_idx) in kfold.split(train_X, train_Y): onehotlabels = np_utils.to_categorical(train_Y) model.fit(train_X[train_idx], onehotlabels[train_idx], batch_size=256, epochs=100, validation_data=(train_X[test_idx], onehotlabels[test_idx])) model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) model.save_weights("LSTMgoogle.h5") print('Model has been saved') classmap = [ 'yes', 'no', 'up', 'down', 'left', 'right', 'on', 'off', 'stop', 'go' ] filenames = [] for f in testFiles: arr = f.split('/') filenames.append(arr[-1]) result = model.predict(test_X) [n, bits] = result.shape print('obtained total predictions of ') print(n) dictionary = {} for f in testSilences: arr = f.split('/') name = arr[-1] dictionary[name] = 'silence' for i in range(0, n): classIdx = np.argmax(result[i, :]) confidence = np.max(result[i, :]) if classIdx < 10 and confidence > 0.95: dictionary[filenames[i]] = classmap[classIdx] else: dictionary[filenames[i]] = 'unknown' with open('submission.csv', 'w') as f: fieldnames = ['fname', 'label'] writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() data = [dict(zip(fieldnames, [k, v])) for k, v in dictionary.items()] writer.writerows(data)
def main(): files = get_corpus("C:/users/corpora/tidigits/wav/train") # for testing if False: files[50:] = [] # truncate test for speed print("%d files" % (len(files))) adv_ms = 10 len_ms = 20 # If > 0, extract +/- offset_s, if None take everything unless # voice activity detector is used offset_s = None print("Generating voice activity detection model") timer = Timer() vad = UnsupervisedVAD(files, adv_ms, len_ms) print("VAD training time {}, starting PCA analysis...".format( timer.elapsed())) timer.reset() pca = pca_analysis_of_spectra(files, adv_ms, len_ms, vad, offset_s) print("PCA feature generation and analysis time {}, feature extraction...". format(timer.elapsed())) timer.reset() # Read features - each row is a feature vector components = 40 examples = extract_tensors_from_corpus(files, adv_ms, len_ms, vad, offset_s, pca, components) # Find the length of the longest time series max_frames = max([e.shape[0] for e in examples]) print("Longest time series {} steps".format(max_frames)) print("Time to generate features {}".format(timer.elapsed())) timer.reset() labels = get_class(files) outputN = len(set(labels)) # Specify model architectures T = PaddedBatchGenerator.longest_sequence(examples) dim_recurrent = components dim_feedforward = T * components # This structure is used only for feed-forward networks # The RNN networks are built more traditionally as I need # to further develop the infrastructure for wrapping layers models_ff = [ # Each list is a model that will be executed [ (Dense, [30], { 'activation': 'relu', 'input_dim': dim_feedforward, 'kernel_regularizer': regularizers.l2(0.01) }), #(Dropout, [.25], {}), (Dense, [30], { 'activation': 'relu', 'kernel_regularizer': regularizers.l2(0.01) }), #(Dropout, [.25], {}), (Dense, [outputN], { 'activation': 'softmax', 'kernel_regularizer': regularizers.l2(0.01) }) ] ] models_rnn = [] print("Time to build matrix {}, starting cross validation".format( timer.elapsed())) # Use recurrent classifiers if true recurrent = True if recurrent: # Use the recurrent neural net list and train/evaluation fn models = models_rnn train_eval = classifier.recurrent.train_and_evaluate else: # Use the feed forward neural net list and train/evaluation fn models = models_ff train_eval = classifier.feedforward.train_and_evaluate batch_size = 100 epochs = 60 debug = False if debug: models = [models[-1]] # Only test the last model in the list results = [] for architecture in models: model = build_model(architecture) results.append( CrossValidator(examples, labels, model, train_eval, batch_size=batch_size, epochs=epochs))
from dsp.utils import get_corpus from dsp.audioframes import AudioFrames from dsp.rmsstream import RMSStream import os import numpy as np adv_ms = 10 len_ms = 20 dir0 = os.getcwd()+"/testAudio" list0 = get_corpus(dir0) for name in list0: stream = AudioFrames(name, adv_ms, len_ms) rms = RMSStream(stream) array = [] for v in rms: array.append(v) print(name) print(np.std(array))
def main(): files = get_corpus("C:/Users/vysha/Downloads/wav/train") # for testing if False: files[50:] = [] # truncate test for speed print("%d files"%(len(files))) adv_ms = 10 len_ms = 20 # We want to retain offset_s about the center offset_s = 0.25 timer = Timer() pca = pca_analysis_of_spectra(files, adv_ms, len_ms, offset_s) print("PCA feature generation and analysis time {}, feature extraction..." .format(timer.elapsed())) timer.reset() # Read features - each row is a feature vector components = 40 examples = extract_features_from_corpus( files, adv_ms, len_ms, offset_s, pca, components) print("Time to generate features {}".format(timer.elapsed())) timer.reset() labels = get_class(files) outputN = len(set(labels)) # Specify model architectures models = [ # 3 layer 20x20xoutput baseline (problem set 3) [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [20], {'activation':'relu', 'input_dim':20}), (Dense, [outputN], {'activation':'softmax', 'input_dim':20}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [40], {'activation':'relu', 'input_dim':20}), (Dense, [outputN], {'activation':'softmax', 'input_dim':40}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [20], {'activation':'relu', 'input_dim':20}), (Dense, [40], {'activation':'relu', 'input_dim':20}), (Dense, [outputN], {'activation':'softmax', 'input_dim':40}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dropout, [0.3], {}), (Dense, [20], {'activation':'relu', 'input_dim':20}), (Dropout, [0.3], {}), (Dense, [40], {'activation':'relu', 'input_dim':20}), (Dense, [outputN], {'activation':'softmax', 'input_dim':40}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dropout, [0.2], {}), (Dense, [20], {'activation':'relu', 'input_dim':20}), (Dropout, [0.2], {}), (Dense, [40], {'activation':'relu', 'input_dim':20}), (Dense, [outputN], {'activation':'softmax', 'input_dim':40}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [20], {'activation':'relu', 'input_dim':20,'kernel_regularizer': keras.regularizers.l1(0.01)}), (Dense, [40], {'activation':'relu', 'input_dim':20, 'kernel_regularizer': keras.regularizers.l1(0.01)} ), (Dense, [outputN], {'activation':'softmax', 'input_dim':40}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [20], {'activation':'relu', 'input_dim':20,'kernel_regularizer': keras.regularizers.l2(0.01)}), (Dense, [40], {'activation':'relu', 'input_dim':20, 'kernel_regularizer': keras.regularizers.l2(0.01)}), (Dense, [outputN], {'activation':'softmax', 'input_dim':40}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [20], {'activation':'relu', 'input_dim':20,'kernel_regularizer': keras.regularizers.l1(0.01)}), (Dense, [40], {'activation':'relu', 'input_dim':20, 'kernel_regularizer': keras.regularizers.l1(0.01)}), (Dense, [outputN], {'activation':'softmax', 'input_dim':40}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [20], {'activation':'relu', 'input_dim':10,'kernel_regularizer': keras.regularizers.l1(0.01)}), (Dense, [outputN], {'activation':'softmax', 'input_dim':20}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [20], {'activation':'relu', 'input_dim':10,'kernel_regularizer': keras.regularizers.l1(0.01)}), (Dense, [outputN], {'activation':'softmax', 'input_dim':20}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [20], {'activation':'relu', 'input_dim':10,'kernel_regularizer': keras.regularizers.l2(0.01)}), (Dense, [outputN], {'activation':'softmax', 'input_dim':20,}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [20], {'activation':'relu', 'input_dim':10,'kernel_regularizer': keras.regularizers.l2(0.01)}), (Dense, [outputN], {'activation':'softmax', 'input_dim':20}) ], [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [20], {'activation':'relu', 'input_dim':20}), (Dense, [outputN], {'activation':'softmax', 'input_dim':20}) ], [(Dense, [10], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [10], {'activation':'relu', 'input_dim':10}), (Dense, [10], {'activation':'relu', 'input_dim':10}), (Dense, [10], {'activation':'relu', 'input_dim':10}), (Dense, [outputN], {'activation':'softmax', 'input_dim':10}) ], [(Dense, [5], {'activation':'relu', 'input_dim':examples.shape[1]}), (Dense, [5], {'activation':'relu', 'input_dim':5}), (Dense, [5], {'activation':'relu', 'input_dim':5}), (Dense, [5], {'activation':'relu', 'input_dim':5}), (Dense, [10], {'activation':'relu', 'input_dim':5}), (Dense, [10], {'activation':'relu', 'input_dim':10}), (Dense, [outputN], {'activation':'softmax', 'input_dim':10}) ] # Add more models here... [(...), (...), ...], [(...), ...], .... ] print("Time to build matrix {}, starting cross validation".format( timer.elapsed())) # ============================================================================= # compare L1 and L2 # ============================================================================= # ============================================================================= # models.clear() # models = [ # [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1]}), # (Dense, [20], {'activation':'relu', 'input_dim':20}), # (Dense, [outputN], {'activation':'softmax', 'input_dim':20}) # ], # [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1], # 'kernel_regularizer': keras.regularizers.l1(0.01)}), # (Dense, [20], {'activation':'relu', 'input_dim':10,'kernel_regularizer': keras.regularizers.l1(0.01)}), # (Dense, [outputN], {'activation':'softmax', 'input_dim':20}) # ], # [(Dense, [20], {'activation':'relu', 'input_dim':examples.shape[1], # 'kernel_regularizer': keras.regularizers.l2(0.01)}), # (Dense, [20], {'activation':'relu', 'input_dim':10,'kernel_regularizer': keras.regularizers.l2(0.01)}), # (Dense, [outputN], {'activation':'softmax', 'input_dim':20,}) # ] # ] # ============================================================================= debug = False c = [] if debug: c.append(CrossValidator(examples, labels, models[2], epochs=50)) else: for architecture in models: c.append(CrossValidator(examples, labels, architecture, epochs=100)) # do something useful with c... e.g. generate tables/graphs, etc. avg_Err = [] std_Err = [] count = 0 for a in c: avg_Err.append(np.average(a.get_errors())) count = count + 1 layers = np.arange(count) print(avg_Err) # ============================================================================= # files_test = get_corpus("C:/Users/vysha/Downloads/wav/test") # print("%d test files"%(len(files_test))) # # examples_test = extract_features_from_corpus( # files_test, adv_ms, len_ms, offset_s, pca, components) # # print("Time to generate test features {}".format(timer.elapsed())) # timer.reset() # # labels_test = np_utils.to_categorical(get_class(files_test)) # # accu = [] # avgAccu = [] # for m in c: # model = m.models # for n in model: # y = n.evaluate(np.array(examples_test),np.array(labels_test),verbose = 0) # accu.append(1-y[1]) # avgAccu.append(np.average(accu)) # ============================================================================= mp.figure() # p1 = mp.bar(layers,avgAccu) p2 = mp.bar(layers,avg_Err) mp.title('L1 and L2 analysis') mp.xlabel('Expt number') mp.ylabel('Error')