-
Notifications
You must be signed in to change notification settings - Fork 2
/
audio_file_nn.py
136 lines (99 loc) · 4.2 KB
/
audio_file_nn.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
import glob, re
import soundfile as sf
import matplotlib.image as mpimg
import numpy as np
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers.core import Dense
from keras.layers import Flatten, Convolution3D, MaxPooling3D, Convolution1D, MaxPooling1D
from keras.callbacks import EarlyStopping
from keras.applications.music_tagger_crnn import MusicTaggerCRNN
from keras.applications.music_tagger_crnn import preprocess_input, decode_predictions
AUDIO_SIZE = 44000
d = {"Excuse":0, "Goodby": 1, "Hello": 2, "How": 3, "Nice": 4, "Seeyou": 5, "Sorry": 6, "Thank": 7, "Thanks":7, "Time" : 8, "Welcome": 9}
def get_class(filename):
"""Given a filename return a hot encoded vector"""
pattern = re.compile(".*/([A-z]*).*")
phrase = pattern.match(filename).groups()[0]
if "_smooth" in phrase:
phrase = phrase[:-7]
cl = [0]*10
cl[d[phrase]] = 1
return cl
def pad_sound(data, length):
if len(data) > length:
return data[:length]
aux = [0]*(length - len(data))
data = np.append(data, np.asarray(aux))
return data
def load_audio_features():
"""Load the audio files and preprocess them to obtain their MusicTaggerCRNN features
and class into two numpy arrays"""
X = []
y = []
audio_files = glob.glob("videos/*/*/*.wav")
model = MusicTaggerCRNN(weights='msd', include_top=False)
for i, audio in enumerate(audio_files):
print(" {0:.2f}".format(float(i)*100/len(audio_files)), end="\r")
melgram = preprocess_input(audio)
melgrams = np.expand_dims(melgram, axis=0)
feats = model.predict(melgrams)
X.append(feats)
y.append(get_class(audio))
return np.asarray(X), np.asarray(y)
def load_sound_data():
"""Load the audio files and their class into two numpy arrays"""
X = []
y = []
audio_files = glob.glob("videos/*/*/*.wav")
for i, audio in enumerate(audio_files):
print(" {0:.2f}".format(float(i)*100/len(audio_files)), end="\r")
data, _ = sf.read(audio)
data = pad_sound(data, AUDIO_SIZE)
data = data.reshape(-1, 1)
X.append(data)
y.append(get_class(audio))
return np.asarray(X), np.asarray(y)
def create_model_mtc(trainable=False):
"""Create a model that receives as input the audio features by the MusicTaggerCRNN.
If trainable, the base structure is trained."""
mtc = MusicTaggerCRNN(weights='msd', include_top=False)
mtc.trainable = trainable
model = Sequential()
model.add(mtc)
model.add(Dense(10, activation='softmax', input_shape=(1, 32)))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["categorical_accuracy"])
return model
def create_sound_conv():
"""Create a 1D convolution model that receives as input the audio files"""
model = Sequential()
model.add(Convolution1D(32, 3, activation='relu', input_shape=(AUDIO_SIZE,1)))
model.add(MaxPooling1D(8))
model.add(Convolution1D(16, 3, activation='relu'))
model.add(MaxPooling1D(4))
model.add(Flatten())
model.add(Dense(10, activation="softmax"))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["categorical_accuracy"])
return model
def train_and_evaluate(model, X_train, y_train, X_test, y_test):
early_stopping = EarlyStopping(monitor='val_loss', patience=2)
model.fit(X_train, y_train, nb_epoch=25, batch_size=10, verbose=1, validation_split=0.1, callbacks=[early_stopping])
results = model.evaluate(X_test, y_test, batch_size=20, verbose=1)
print(results)
if __name__ == '__main__':
# print("Loading data...")
# X,y = load_sound_data()
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
#
# print("Creating Model...")
# model = create_sound_conv()
# model.summary()
#
# train_and_evaluate(model, X_train, y_train, X_test, y_test)
print("Loading data...")
X,y = load_audio_features()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
print("Creating Model...")
model = create_model_mtc()
model.summary()
train_and_evaluate(model, X_train, y_train, X_test, y_test)