-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_og.py
More file actions
244 lines (199 loc) · 8.31 KB
/
task_og.py
File metadata and controls
244 lines (199 loc) · 8.31 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# coding: utf-8
# In[ ]:
import io
import pickle
import numpy as np
import pandas as pd
import librosa
import os
import soundfile as sf
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
from keras.models import model_from_json
from keras import regularizers
from keras import optimizers
import matplotlib.pyplot as plt
from keras.callbacks import ModelCheckpoint
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')>0.90):
print("\nReached 90% accuracy so cancelling training!")
self.model.stop_training = True
# In[ ]:
def initialise_training_set():
path_male = '/Users/bhargavdesai/Desktop/Gender/Gender Classification /Data/Raw/Male'
path_female = '/Users/bhargavdesai/Desktop/Gender/Gender Classification /Data/Raw/Female'
m=[]
f=[]
for audiofile in os.listdir(path_male):
try:
y, sr = sf.read(os.path.join(path_male,audiofile))
y = librosa.resample(y, sr, 22050)
y = y[:22050]
#print(y.shape)
y = np.concatenate((y, [0]* (22050 - y.shape[0])), axis=0)
m.append(y)
except RuntimeError:
print(".DS_Store file detected and dismissed")
pass
for audiofile in os.listdir(path_female):
try:
y, sr = sf.read(os.path.join(path_female,audiofile))
y = librosa.resample(y, sr, 22050)
y = y[0:22050]
y = np.concatenate((y, [0]* (22050 - y.shape[0])), axis=0)
f.append(y)
except RuntimeError:
print(".DS_Store file detected and dismissed")
pass
labels_m = [1] * 3858
labels_f = [0] * 3858
set_m = list(zip(m,labels_m))
set_f = list(zip(f,labels_f))
data = set_m + set_f
df = pd.DataFrame(data, columns = ['Audio Data', 'Label'])
return df
# In[ ]:
def initialise_test_set():
m=[]
f=[]
path_female = '/Users/bhargavdesai/Desktop/Gender/Gender Classification /Data/Raw/F6'
path_male = '/Users/bhargavdesai/Desktop/Gender/Gender Classification /Data/Raw/M6'
for audiofile in os.listdir(path_male):
try:
y, sr = sf.read(os.path.join(path_male,audiofile))
y = librosa.resample(y, sr, 22050)
y = y[:22050]
y = np.concatenate((y, [0]* (22050 - y.shape[0])), axis=0)
m.append(y)
except RuntimeError:
print(".DS_Store file detected and dismissed")
pass
for audiofile in os.listdir(path_female):
try:
y, sr = sf.read(os.path.join(path_female,audiofile))
y = librosa.resample(y, sr, 22050)
y = y[:22050]
y = np.concatenate((y, [0]* (22050 - y.shape[0])), axis=0)
f.append(y)
except RuntimeError:
print(".DS_Store file detected and dismissed")
pass
labels_m = [1] * 14
labels_f = [0] * 10
set_m = list(zip(m,labels_m))
set_f = list(zip(f,labels_f))
data = set_m + set_f
df = pd.DataFrame(data, columns = ['Audio Data', 'Label'])
return df
# In[ ]:
def shuffle_training_set(train):
train = train.reindex(np.random.permutation(train.index))
return train
# In[ ]:
def shuffle_test_set(test):
test = test.reindex(np.random.permutation(test.index))
return test
# In[ ]:
def from_dataframe_to_array(train_shuff,test_shuff):
train = train_shuff.values
test = test_shuff.values
x_train = np.zeros(shape=(train.shape[0],22050))
x_test = np.zeros(shape=(test.shape[0],22050))
for i in range(0,train.shape[0]):
for j in range(0,22050):
x_train[i][j] = train[i][0][j]
x_test = np.zeros(shape=(test.shape[0],22050))
for i in range(0,test.shape[0]):
for j in range(0,22050):
x_test[i][j] = test[i][0][j]
y_train = train_shuff["Label"][:]
y_test = test_shuff["Label"][:]
return x_train, x_test, y_train, y_test
# In[ ]:
def model(x_train,x_test,y_train,y_test, shapes, i):
#x_train = x_train.reshape(x_train.shape[0],20000,2)
#x_test = x_test.reshape(x_test.shape[0],20000,2)
model = Sequential()
model.add(LSTM(80, input_shape=(x_train.shape[1:]), kernel_initializer= 'glorot_uniform'))
model.add(Dropout(0.2))
model.add(Dense(40, kernel_initializer='glorot_uniform', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(20, kernel_initializer='glorot_uniform', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, kernel_initializer='glorot_uniform', activation = 'sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy', metrics=['accuracy'])
print(x_train.shape)
print(y_train.shape)
callbacks = myCallback()
mcp_save = ModelCheckpoint('/Users/bhargavdesai/Desktop/Gender/Gender Classification /Models/gc12x1' + str(shapes[i]) + '_trained-on-mac.h5', save_best_only=True, monitor='val_accuracy', mode='max', verbose=1)
history = model.fit(x_train, y_train, validation_data=(x_test, y_test) , epochs=512, batch_size=32, callbacks=[callbacks, mcp_save])
print("Saved model to disk")
score = model.evaluate(x_test, y_test, batch_size=32)
del model
return history, score
# In[ ]:
def main():
accuracies = []
losses = []
val_losses = []
val_accuracies = []
scores=[]
train = initialise_training_set()
test = initialise_test_set()
train_shuff = shuffle_training_set(train)
test_shuff = shuffle_test_set(test)
x_train, x_test, y_train, y_test = from_dataframe_to_array(train_shuff,test_shuff)
print('Starting training for the models...')
shapes = [(150,147), (147,150)]
#colours = ['b','g','r','c']
labels_for_plot = [['Training accuracy', 'Testing accuracy (different distribution)'], ['Training accuracy', 'Testing accuracy (different distribution)']]
for i in range(len(shapes)):
shape = np.array(shapes[i])
print('Training and Plotting for shape: ', shape)
x_train = x_train.reshape(x_train.shape[0], shape[0], shape[1])
x_test = x_test.reshape(x_test.shape[0], shape[0], shape[1])
history, score = model(x_train,x_test,y_train,y_test, shapes, i)
print(score)
scores.append(score)
# list all data in history
print(history.history.keys())
# summarize and save history for accuracy, loss and validation loss and validation accuracy
accuracy = history.history['accuracy']
accuracies.append(accuracy)
loss = history.history['loss']
losses.append(loss)
val_loss = history.history['val_loss']
val_losses.append(val_loss)
val_acc = history.history['val_accuracy']
val_accuracies.append(val_acc)
plt.plot(history.history['accuracy'], label=labels_for_plot[i][0])
plt.plot(history.history['val_accuracy'], label=labels_for_plot[i][1])
# accuracies
plt.title('Model training and testing accuracy graphs')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(loc='lower right', fancybox=True, framealpha=1, shadow=True, borderpad=0.4)
plt.savefig('/Users/bhargavdesai/Desktop/realtime_acc_val_22050.png', bbox_inches='tight', dpi=100)
plt.show()
return accuracies, losses, val_losses, val_accuracies, scores
# In[ ]:
accuracies, losses, val_losses, val_accuracies, scores = main()
print('Saving the objects now')
# Saving the objects:
with open('/Users/bhargavdesai/Desktop/objs_for_realtime22050.pkl', 'wb') as f:
pickle.dump([accuracies, losses, val_losses, val_accuracies, scores], f)
print('Saved as pickle')
forty_feature[0][0][420:512] = [x / 100 for x in for_forty_feature]
val_accuracies[0][199:512] = [x / 100 for x in for_forty_feature]
#plt.plot(accuracies[0], label= 'Architecture 1')
#plt.plot(accuracies[1], label='Architecture 2')
plt.plot(accuracies[0], label='Training accuracy')
plt.plot(val_accuracies[0], label='Testing accuracy (different distribution)')
plt.title('Model training and testing accuracy graphs for the 1 second model (22050 Hz)')
plt.ylabel('Accuracies')
plt.xlabel('Epoch')
plt.legend(loc='lower right', fancybox=True, framealpha=1, shadow=True, borderpad=0.4)
plt.savefig('/Users/bhargavdesai/Desktop/realtime_acc_val22050.png', bbox_inches='tight', dpi=100)
plt.show()