def predict(self, signal, fs): # remove silence signal = remove_silence(fs, signal) # signal = StandardScaler().fit_transform(signal) # Extract feature mfcc_feat = mfcc(signal, fs) if self.is_delta: mfcc_feat = delta(mfcc_feat, 2) return self.NN.predict(mfcc_feat)
def train(self): self.message = "Training result:" # File path wavpath = self.filepath + "/wav" if self.verbose: print("Wav files from: " + wavpath) # List all the filenames of wav directory wav_files = [f for f in os.listdir(wavpath) if f.endswith('.wav')] n = len(wav_files) if (n < 1): assert ("No wav files found at " + wavpath + ". Cancelling operation") return features = None target = None is_firstrun = True row = 0 col = 0 userList = open(self.filepath + "/metadata.txt", "w") wav_dic = {} # Loop until all files are converted to csv for audio in wav_files: fs, signal = wavfile.read(wavpath + "/" + audio) # remove silence signal = remove_silence(fs, signal) # Extract features from audio signal mfcc_feat = mfcc(signal, fs) if self.is_delta: mfcc_feat = delta(mfcc_feat, 2) # save feature as csv files for debugging np.savetxt(self.filepath + "/csv/" + audio[7:-4] + ".csv", mfcc_feat, fmt='%.8f', delimiter=',') # If user is not already in the list/dictionary add them try: wav_dic[audio[0:-6]] except: userList.write(audio[0:-6] + "\n") wav_dic[audio[0:-6]] = col col += 1 if self.verbose: print("\nFile:", audio) print("Feature: ", mfcc_feat.shape) if is_firstrun: features = mfcc_feat target = [0] * mfcc_feat.shape[0] for i in range(mfcc_feat.shape[0]): target[row] = wav_dic[audio[0:-6]] row += 1 is_firstrun = False else: features = np.vstack((features, mfcc_feat)) for i in range(mfcc_feat.shape[0]): target = np.append(target, [wav_dic[audio[0:-6]]]) row += 1 # userList.write("Anonymous") userList.close() # Load user names userList = open(self.filepath + "/metadata.txt", "r") self.users = userList.read().split("\n")[:-1] userList.close() features = features.astype(np.float) if self.verbose: print_label("Data sets", character="-") print("Features:", features.shape) print(features) print("\nTarget:", target.shape) print(target) print_label("Neural Network modelling", character="-") print("Modelling started, this may take a while") # Features and target are made now train them from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.33, random_state=42) self.mlp.fit(X_train, y_train) self.message += "\nTotal iteration run: %d" % self.mlp.n_iter_ predictions = self.mlp.predict(X_test) from sklearn.metrics import classification_report, confusion_matrix self.message += "\n" self.message += classification_report(y_test, predictions, target_names=self.users) self.message += "\nconfusion_matrix\n" self.message += np.array_str(confusion_matrix(y_test, predictions)) # Training is now complete, save the model pickle.dump(self.mlp, open(self.filepath + '/model.pkl', 'wb')) self.message += "\nModel is saved to %s/model.pkl" % self.filepath if self.verbose: print(self.message) # Load files self.NN = pickle.load(open(self.filepath + '/model.pkl', 'rb')) # Load user names userList = open(self.filepath + "/metadata.txt", "r") self.users = userList.read().split('\n') userList.close() return self.message