-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathurbanclap_audio_classification.py
217 lines (119 loc) · 4.07 KB
/
urbanclap_audio_classification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# coding: utf-8
# In[61]:
# importing necessary libraries and dependencies
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import librosa
from sklearn.preprocessing import LabelEncoder
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import Adam
from sklearn.cross_validation import train_test_split
from sklearn import metrics
# In[2]:
train_data_dir = 'sounds/train/train_sound/'
test_data_dir = 'sounds/test/test_sound/'
# reading the labels
train = pd.read_csv('sounds/labels/train.csv')
test = pd.read_csv('sounds/labels/test.csv')
# In[3]:
# function to load files and extract features
def parser(row, data_dir):
# setting path
file_name = os.path.join(data_dir,str(row.ID)+'.wav')
print(file_name)
# check if the file is corrupted
try:
# here kaiser_fast is a technique used for faster extraction
# X-> audio_time_series_data; sample_rate-> sampling rate
X, sample_rate = librosa.load(file_name, res_type='kaiser_fast')
# extraccting Mel-Frequeny Cepstral Coeficients feature from data
# y -> accepts time-series audio data; sr -> accepts sampling rate
# n_mfccs -> no. of MFCCs to return
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis = 0)
except Exception as e:
print("Error encountered while parsing file: ", e)
return None, None
# store mfccs features
feature = mfccs
# store the respective id
data_id = row.ID
return [data_id, feature]
# ### Reading train.csv and storing into temp
# In[4]:
# parsing train
temp = train.apply(parser,axis=1,data_dir=train_data_dir)
temp.columns = ['ID','feature']
# In[5]:
# adding Class to 'temp'
temp['Class'] = train['Class']
# In[6]:
type(temp)
# ### Reading test.csv and storing into temp_test
# In[7]:
# parsing test
temp_test = test.apply(parser, axis=1,data_dir=test_data_dir)
temp_test.columns = ['ID', 'feature']
# In[16]:
temp_test = pd.DataFrame(temp_test)
type(temp_test)
# In[19]:
temp_test.columns = ['mix']
# In[23]:
temp_test.keys()
# In[24]:
temp_test[['ID','feature']] = temp_test['mix'].apply(pd.Series)
# In[28]:
temp_test.drop('mix',axis=1,inplace=True)
# In[32]:
print("\n---------------------train data---------------------")
print(type(temp))
print(temp.head())
print("\n---------------------test data---------------------")
print(type(temp_test))
print(temp_test.head())
print('---------------------Checking for NONE values---------------------')
# checking for NONE values
print(temp[temp.Class.isnull()])
# removing NONE values from temp
temp = temp[temp.Class.notnull()]
temp_test = temp_test[temp_test.notnull()]
#print(temp.ID[temp.label.isnull()])
# In[37]:
temp.Class.unique()
# In[38]:
temp.Class.nunique()
# In[35]:
# Label Encoding the audio data
lb = LabelEncoder()
# converting pd.series into np.array for faster processing
X = np.array(temp.feature.tolist())
y = np.array(temp.Class.tolist())
y = to_categorical(lb.fit_transform(y))
# In[62]:
x_train,x_test,y_train,y_test = train_test_split(X,y, test_size=0.3)
# ## Building a deep learning model
# In[73]:
num_labels = y.shape[1]
filter_size = 2
def categorical_classifier():
model = Sequential()
# input and first hidden layer
model.add(Dense(input_shape=(40,), units=256, activation='relu', kernel_initializer='uniform'))
model.add(Dropout(0.5))
# second hidden layer
model.add(Dense(units=256,activation='relu',kernel_initializer='uniform'))
model.add(Dropout(0.5))
# output layer
model.add(Dense(units=num_labels, activation='softmax'))
# compiling our model
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
# training the data
#model.fit(X,y, batch_size=32, epochs=500, validation_split=0.3)
return model
# In[77]:
# training the data
model.fit(x_train,y_train, batch_size=32, epochs=650, validation_data=(x_test, y_test))