%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import pickle
import numpy as np
import librosa
import librosa.display
import IPython.display
import sklearn
from keras.models import Sequential
from keras.layers import Dense, CuDNNLSTM, Permute, Dropout
from keras.utils import to_categorical
from keras.models import Sequential
def show_history(history):
print(history.history.keys())
fig = plt.figure(figsize=(20,5))
plt.subplot(121)
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.subplot(122)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='lower left')
plt.show()
n_frame = 173
n_mfcc = 40
train_x = pickle.load(open('./train_x.dat', 'rb'))
train_y = pickle.load(open('./train_y.dat', 'rb'))
val_x = pickle.load(open('./val_x.dat', 'rb'))
val_y = pickle.load(open('./val_y.dat', 'rb'))
test_x = pickle.load(open('./test_x.dat', 'rb'))
test_y = pickle.load(open('./test_y.dat', 'rb'))
train_x = train_x.transpose((0,2,1))
val_x = val_x.transpose((0,2,1))
test_x = test_x.transpose((0,2,1))
train_y = to_categorical(train_y)
val_y = to_categorical(val_y)
test_y = to_categorical(test_y)
train_x.shape
model = Sequential()
model.add(CuDNNLSTM(units=256, input_shape = (n_frame, n_mfcc), return_sequences=True))
model.add(Dropout(0.25))
model.add(CuDNNLSTM(units=256, return_sequences=False))
model.add(Dropout(0.25))
# model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='Adam',
metrics=['accuracy'])
model.summary()
history = model.fit(train_x, train_y,
epochs=20, batch_size=32,
validation_data=(test_x, test_y))
show_history(history)