Created
January 6, 2018 04:27
-
-
Save ssapcsea/6db4a9ebfab93c017de3bba9ec517bae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import librosa | |
import librosa.display | |
import os | |
import pandas as pd | |
import glob | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn.preprocessing import LabelEncoder | |
from keras.utils import np_utils | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Activation, Flatten | |
precedeString_Train = '.../Documents/UrbanSoundClassifier/train' | |
precedeString_Val = '.../Documents/UrbanSoundClassifier/validate' | |
#setup train | |
data, sampling_rate = librosa.load(precedeString_Train + '/Train/6999.wav') | |
plt.figure(figsize=(12,4)) | |
librosa.display.waveplot(data,sr=sampling_rate) | |
train = pd.read_csv(precedeString_Train + '/train.csv') | |
print(train.Class.value_counts(normalize=True)) #distribution of data | |
def parser_Train(row): #load files and extract features | |
file_name = precedeString_Train + '/Train/' + str(row.ID) + '.wav' | |
try: | |
X, sample_rate = librosa.load(file_name,res_type='kaiser_fast') | |
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0) #MFCCS feature used to analyze data | |
except Exception as e: | |
print("Error encountered while parsing file: ", file_name) | |
return None, None | |
feature = mfccs | |
label = row.Class | |
print(file_name) | |
return [feature,label] | |
temp = train.apply(parser_Train, axis=1) | |
temp.columns = ['feature', 'label'] | |
X = np.array(temp.feature.tolist()) | |
y = np.array(temp.label.tolist()) | |
lb = LabelEncoder() | |
y = np_utils.to_categorical(lb.fit_transform(y)) | |
#setup validation set | |
validate = pd.read_csv(precedeString_Val + '/validate.csv') | |
def parser_Val(row): #load files and extract features | |
file_name = precedeString_Val + '/Validate/' + str(row.ID) + '.wav' | |
try: | |
# here kaiser_fast is a technique used for faster extraction | |
val_X, sample_rate = librosa.load(file_name,res_type='kaiser_fast') | |
# we extract mfcc feature from data | |
mfccs = np.mean(librosa.feature.mfcc(y=val_X, sr=sample_rate, n_mfcc=40).T,axis=0) | |
except Exception as e: | |
print("Error encountered while parsing file: ", file_name) | |
return None, None | |
feature = mfccs | |
label = row.Class | |
print(file_name) | |
return [feature,label] | |
temp_Val = validate.apply(parser_Val, axis=1) | |
temp_Val.columns = ['feature', 'label'] | |
val_X = np.array(temp_Val.feature.tolist()) | |
val_Y = np.array(temp_Val.label.tolist()) | |
lb2 = LabelEncoder() | |
val_Y = np_utils.to_categorical(lb2.fit_transform(val_Y)) | |
#training | |
num_labels = y.shape[1] | |
filter_size = 2 | |
model = Sequential() | |
model.add(Dense(256, input_shape = (40,))) | |
model.add(Activation('relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(num_labels)) | |
model.add(Activation('softmax')) | |
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam') | |
model.fit(X, y, batch_size=32, epochs=40, validation_data=(val_X, val_Y)) | |
#testing | |
file_name = ".../Documents/UrbanSoundClassifier/test/Test/5.wav" | |
test_X, sample_rate = librosa.load(file_name,res_type='kaiser_fast') | |
mfccs = np.mean(librosa.feature.mfcc(y=test_X, sr=sample_rate, n_mfcc=40).T,axis=0) | |
test_X = np.array(mfccs) | |
print(model.predict(test_X)) #########ERROR########## | |
File test.csv that contained in your link doesn't include class column, where can I find validate dataset?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Data can be found here: https://drive.google.com/drive/u/0/folders/0By0bAi7hOBAFUHVXd1JCN3MwTEU