# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + colab={} colab_type="code" id="1Nhau6hODx_H"
from __future__ import print_function

import numpy as np
import pickle
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.preprocessing import LabelEncoder

from keras.preprocessing import sequence
from keras.layers.noise import GaussianNoise
from keras.models import Sequential
from keras.layers.core import Dense,Dropout,Activation,Flatten
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv1D,MaxPooling1D
from keras.utils import np_utils
from tensorflow.keras import regularizers

# + colab={} colab_type="code" id="cplzftzzROem"
#set parameters

nb_filter = 64
filter_length_1 = 10
filter_length_2 = 7
filter_length_3 = 3
hidden_dims = 10
batch_size = 64
nb_epoch = 100
nb_classes = 16

# + colab={} colab_type="code" id="YosiMd03RqAE"
# #load the sequential MFCC data
# with open(os.path.join(proj_dir, 'seq_mfcc.pickle'), 'rb') as jar:
#   mfcc_data = pickle.load(jar)

mfcc_filename = "mfccs_dev_22050.csv"
dataset = pd.read_csv(mfcc_filename)
df = pd.DataFrame(dataset)
mfcc_data = df[["mfcc_mean1","mfcc_mean2","mfcc_mean3","mfcc_mean4","mfcc_mean5","mfcc_mean6","mfcc_mean7","mfcc_mean8","mfcc_mean9","mfcc_mean10","mfcc_mean11","mfcc_mean12","mfcc_mean13"]]
test_dim = mfcc_data.shape[0]

#set up targets (labels)
accent_data = pd.read_csv("cv-valid-dev-acc-mp3.csv")
targets_raw = np.array(accent_data['accent'])

label_encoder = LabelEncoder()
mfcc_targets = label_encoder.fit_transform(targets_raw)

X_train,X_test,y_train,y_test = train_test_split(
    mfcc_data,mfcc_targets,test_size=0.20,random_state=42
)

Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

X_train = X_train.values.reshape(X_train.shape[0], X_train.shape[1], 1)
X_test = X_test.values.reshape(X_test.shape[0], X_test.shape[1],1)

print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
print('y_train shape:', y_train.shape)
print('y_test shape:', y_test.shape)

# + colab={} colab_type="code" id="6DbKSHznSMzW"
#build model
model = Sequential()

#layer 1
#add a Convolution1D which will learn nb_filter mfcc groups:
model.add(Conv1D(filters=nb_filter,
                 kernel_size=filter_length_1,
                 input_shape=(X_train.shape[1], 1),
                 padding='valid', activation='relu'
                ))

#batch normalization to keep weights in [0,1] range:
model.add(BatchNormalization())

#layer2
model.add(Conv1D(filters=nb_filter,
                 kernel_size=filter_length_2,
                 padding='same', activation='relu'
                ))

# #standard maxpooling to halve the output of previous layer
# model.add(MaxPooling1D(pool_size=2))

#layer3
model.add(BatchNormalization())
model.add(Conv1D(filters=nb_filter,
                 kernel_size=filter_length_2,
                 padding='same', activation='relu'
                ))
model.add(MaxPooling1D(pool_size=2))

# #layer4
# model.add(BatchNormalization())
# model.add(Conv1D(filters=nb_filter,
#                  kernel_size=filter_length_3,
#                  padding='same', activation='relu'
#                 ))
# # model.add(MaxPooling1D(pool_size=2))

# #layer5
# model.add(BatchNormalization())
# model.add(Conv1D(filters=nb_filter,
#                  kernel_size=filter_length_1,
#                  padding='same', activation='relu'
#                 ))
# model.add(MaxPooling1D(pool_size=2))


#flatten the output of the convolutional layers
#so that we can add a vanilla dense layer
model.add(Flatten())

# Dropout reduces overfitting
model.add(Dropout(.25))

#project onto a single unit output layer
#and squash with a softmax into 0-1 probability space
model.add(Dense(nb_classes,activation='softmax',
#                 kernel_regularizer=regularizers.l2(1e-5)
               kernel_regularizer=regularizers.l1_l2(l1=1e-5, l2=1e-4)
               ))

#fit model
model.compile(loss='categorical_crossentropy',
              optimizer='adam',metrics=["accuracy"])
model.summary()
history = model.fit(X_train,Y_train, batch_size=batch_size, epochs=nb_epoch, 
                    validation_data=(X_test, Y_test), verbose=1)

#print report of recall, precision, f1 score
y_pred = model.predict_classes(X_test)
print(classification_report(y_test,y_pred))

# +
#increased epoch from 10 to 30
#decreased nb_filters from 512 to 64
#increased dropout rate from 0.1 to 0.25 (accuracy converges to 0.85)

# +
from keras.callbacks import History 
import matplotlib.pyplot as plt

print(history.history.keys())
#  "Accuracy"
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model acuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# "Loss"
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# -

print(confusion_matrix(y_test,y_pred))



StarcoderdataJupyterScriptsDedupFiltered