# ---
# 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
# ---

# + id="AVtESjUsuCXE"
# Import the required packages
import os
import numpy as np 
import tensorflow as tf
import matplotlib.pyplot as plt
import pathlib
import librosa.display
from tqdm import tqdm
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
import pandas as pd

import matplotlib.pyplot as plt
from keras.utils import np_utils

import tensorflow as tf
from tensorflow.keras.preprocessing.image import *
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, Activation, MaxPool2D, Dropout
# Deep Learning Libraries
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from keras.optimizers import Adam,SGD,Adagrad,Adadelta,RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau, LearningRateScheduler
from keras.utils import to_categorical

# + [markdown] id="1h-L3l6puCXZ"
# we will be using [spela](https://github.com/kongkip/spela) a package used to compute speech features 
# e.g spectrograms and melspectrograms implemented using tf.keras to take advantage of GPU during computations

# + colab={"base_uri": "https://localhost:8080/"} id="fEa3l8hmuCXb" outputId="c56d7b92-3c42-4b85-b226-cb9f48126386"
# a package to compute speech features, implemented using tf.keras
try:
    from spela.spectrogram import Spectrogram 
    from spela.melspectrogram import Melspectrogram
except:
    # !pip install spela
    from spela.spectrogram import Spectrogram 
    from spela.melspectrogram import Melspectrogram

# + id="h8TxcQySuCXd"
# disable eager execution, my model couldn't train well while on eager mode
tf.compat.v1.disable_eager_execution()

# + id="lKsdWvoBuCXe"
# Get the data directories
data_dir = "/content/drive/MyDrive/16000_pcm_speeches"

# + colab={"base_uri": "https://localhost:8080/"} id="A0D4kzRvu6P6" outputId="744abed7-ab83-41c3-d12b-bd4a31d3c90f"
from google.colab import drive
drive.mount('/content/drive')

# + id="BpRDfQfmzk2V"
# import shutil
# shutil.unpack_archive("/content/drive/MyDrive/archive.zip","/content/drive/MyDrive/")

# + id="9Wm9AErkWomW"
classes = ['Nelson_Mandela', 'Magaret_Tarcher','Benjamin_Netanyau','Jens_Stoltenberg', 'Julia_Gillard'] 

# + colab={"base_uri": "https://localhost:8080/"} id="eLXfH60juCXf" outputId="6803032b-00db-4205-8430-68803c495e5d"
# inspect the folders inside the dataset
os.listdir(data_dir)

# + colab={"base_uri": "https://localhost:8080/"} id="l4mDKRY5uCXg" outputId="7bc7268b-93d9-4ed7-9e36-fc7bb990479d"
# for now we are concerned with the four speakers
# lets get as a sample data from one of the speakers
nelson_madela = [item for item in os.listdir('/content/drive/MyDrive/16000_pcm_speeches/Nelson_Mandela')]
nelson_madela[:10]


# + id="2Rqr1WjfuCXh"
# lets create a function that takes in a raw wavfile and computes a spectrogram then plots it
def compute_spectrogram_melspectrogram_and_plot(wav_dir, compute_type):
    with tf.compat.v1.Session(graph=tf.compat.v1.Graph()) as sess:
        wav_filename_placeholder = tf.compat.v1.placeholder(tf.compat.v1.string, [])
        wav_loader = tf.io.read_file(wav_filename_placeholder)
        wav_decoder = tf.audio.decode_wav(wav_loader, desired_channels=1)
        wav_data = sess.run(
        wav_decoder, feed_dict={
            wav_filename_placeholder: wav_dir
        }).audio.flatten()
        sess.close()
    # audio has a sample rate of 16000 and the produced wav has a shape of (16000, 1)
    # reshape to (1, 1600)
    wav = wav_data.reshape(1, 16000)
    wav_new = wav[np.newaxis, :] # introduce a new axis to have a shape of (1, 1, 16000)
    height = wav_new.shape[1]
    width = wav_new.shape[2]
    # create a model to compute spectrogram
    model = tf.keras.Sequential() 
    if compute_type == "spectrogram":
        model.add(Spectrogram(n_dft=512, n_hop=256, input_shape=(height, width),
                            return_decibel_spectrogram=True, power_spectrogram=2.0,
                            trainable_kernel=False, name='static_stft'))
    elif compute_type == "melspectrogram":
        model.add(Melspectrogram(sr=16000, n_mels=128,n_dft=512, n_hop=256,
                            input_shape=(height, width), return_decibel_melgram=True,
                            trainable_kernel=False, name='melgram'))
   
    # producing a spectrogram/melspectrogram from the model
    pred = model.predict(x=wav_new)

    if tf.keras.backend.image_data_format() == "channel_first":
        result = pred[0, 0]
    else:
        result = pred[0, :, :, 0]

    # show the spectrogram/melspectrogram
    librosa.display.specshow(result, y_axis='linear', sr=16000)


# + colab={"base_uri": "https://localhost:8080/", "height": 258} id="r23FqBACuCXj" outputId="8e18e763-de8f-4e44-d19a-3aa5d8d78d9b"
compute_spectrogram_melspectrogram_and_plot('/content/drive/MyDrive/16000_pcm_speeches/Nelson_Mandela/0.wav', "spectrogram")

# + colab={"base_uri": "https://localhost:8080/", "height": 258} id="8WwQGMrxuCXk" outputId="db10cdb5-9e67-4000-f7d5-4b95c4e4d2a9"
compute_spectrogram_melspectrogram_and_plot('/content/drive/MyDrive/16000_pcm_speeches/Nelson_Mandela/0.wav', "melspectrogram")


# + [markdown] id="X6tka-n2uCXl"
# # Process training dataset

# + id="j40Uxj0iuCXm"
# get wav paths
def get_wav_paths(speaker):
    speaker_path = data_dir + speaker
    all_paths = [item for item in os.listdir(speaker_path)]
    return all_paths


# + id="gjnVCtyhuCXm"
nelson_mandela_paths = get_wav_paths("/Nelson_Mandela")
margaret_thatcher_paths = get_wav_paths("/Magaret_Tarcher")
benjamin_netanyau_paths = get_wav_paths("/Benjamin_Netanyau")
jens_stoltenberg_paths = get_wav_paths( '/Jens_Stoltenberg')
julia_gillard_paths = get_wav_paths("/Julia_Gillard")


# + id="83uVqnWkuCXn"
# load the data
def load_wav(wav_path, speaker):
    with tf.compat.v1.Session(graph=tf.compat.v1.Graph()) as sess:
        wav_path = data_dir +speaker + "/"+ wav_path
        wav_filename_placeholder = tf.compat.v1.placeholder(tf.compat.v1.string, [])
        wav_loader = tf.io.read_file(wav_filename_placeholder)
        wav_decoder = tf.audio.decode_wav(wav_loader, desired_channels=1)
        wav_data = sess.run(
            wav_decoder, feed_dict={
                wav_filename_placeholder: wav_path
            }).audio.flatten().reshape((1, 16000))
        sess.close()
    return wav_data
    


# + id="SzXVdYjWuCXo"
# create training data
def generate_training_data(speaker_paths, speaker, label):
    wavs, labels = [], []
    for i in tqdm(speaker_paths):
        wav = load_wav(i, speaker)
        wavs.append(wav)
        labels.append(label)
    return wavs, labels



# + colab={"base_uri": "https://localhost:8080/"} id="pcDDKy_MuCXq" outputId="0b83327b-21e8-40c2-ab8d-3c3d74d16007"
nelson_mandela_wavs, nelson_mandela_labels = generate_training_data(nelson_mandela_paths, "/Nelson_Mandela", 0) 
margaret_thatcher_wavs, margaret_thatcher_labels = generate_training_data(margaret_thatcher_paths, "/Magaret_Tarcher", 1) 
benjamin_netanyau_wavs, benjamin_netanyau_labels = generate_training_data(benjamin_netanyau_paths, "/Benjamin_Netanyau", 2) 
jens_stoltenberg_wavs, jens_stoltenberg_labels = generate_training_data(jens_stoltenberg_paths, "/Jens_Stoltenberg", 3) 
julia_gillard_wavs, julia_gillard_labels = generate_training_data(julia_gillard_paths, "/Julia_Gillard", 4) 

# + id="FqwEtUuRuCXr"
# remove the extra wav for Julia Gillard
julia_gillard_labels = julia_gillard_labels[1:]
julia_gillard_wavs = julia_gillard_wavs[1:]

# + id="z8i1qhp0uCXs"
all_wavs = nelson_mandela_wavs + margaret_thatcher_wavs + benjamin_netanyau_wavs + jens_stoltenberg_wavs + julia_gillard_wavs
all_labels = nelson_mandela_labels + margaret_thatcher_labels + benjamin_netanyau_labels + jens_stoltenberg_labels + julia_gillard_labels

# + id="0XdazeDOuCXt"
# split the dataset into trainin and testing set\
train_wavs, test_wavs, train_labels, test_labels = train_test_split(all_wavs, all_labels, test_size=0.2)

# + id="Pz6GPtWxuCXt"
train_x, train_y = np.array(train_wavs), np.array(train_labels)
test_x, test_y = np.array(test_wavs), np.array(test_labels)

# + id="NYC-rbc9uCXu"
train_y = tf.keras.utils.to_categorical(train_y)
test_y = tf.keras.utils.to_categorical(test_y)

# + id="Tuow4IE_iljq"
np.save('/content/drive/MyDrive/16000_pcm_speeches/train_x.npy',train_x)
np.save('/content/drive/MyDrive/16000_pcm_speeches/train_y.npy',train_y)
np.save('/content/drive/MyDrive/16000_pcm_speeches/test_x.npy',test_x)
np.save('/content/drive/MyDrive/16000_pcm_speeches/test_y.npy',test_y)

# + id="SAjUluJp8TS9"
train_x, train_y = np.load('/content/drive/MyDrive/16000_pcm_speeches/train_x.npy'), np.load('/content/drive/MyDrive/16000_pcm_speeches/train_y.npy')
test_x, test_y = np.load('/content/drive/MyDrive/16000_pcm_speeches/test_x.npy'), np.load('/content/drive/MyDrive/16000_pcm_speeches/test_y.npy')

# + [markdown] id="EdV1Ul4puCXu"
# # Train with 5 first labels Fashion Mnist trained model

# + id="hdhZkdQrDF5u"
model_trained = tf.keras.models.load_model("/content/drive/MyDrive/u5")
for layer in model_trained.layers[:20]:
   layer.trainable =False

# + id="IltFjmo1VdMN"
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
from keras.optimizers import Adam ,RMSprop
import os 

Learning_rate = 2.0679516672208156e-30

optimizer = Adam(lr=Learning_rate)
 
model_trained.compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])


learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', 
                                            patience=3, 
                                            verbose=1, 
                                            factor=0.5, 
                                            min_lr=1e-100)

batch_size = 45
checkpoint_path = "/content/drive/MyDrive/16000_pcm_speeches"
checkpoint_dir = os.path.dirname(checkpoint_path)
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,monitor='val_accuracy',mode='max',
                                                               save_best_only=True)

# + id="IDxEfx-9kKfp"
Y = np.array([np.where(r==1)[0][0] for r in train_y])


# + colab={"base_uri": "https://localhost:8080/"} id="O06oYYZopPLH" outputId="8aaa26f6-fdeb-4ac9-ebde-d92361265d4b"
train_x.shape

# + id="xXLgjgKS4uO3"
import os
import shutil
import  joblib
import numpy as np
import pandas as pd
import librosa as lb
import librosa.display
import matplotlib.pyplot as plt

from pathlib import Path
from tqdm.notebook import tqdm


# + id="kvEcHp6-63jo"
class params:
    """
    Parameters used for the audio data
    """
    sr = 16000

    # Melspectrogram
    n_mels = 28
    fmin = 15
    fmax = sr // 2  # Shannon theorem


# + id="1r7ULJ9_7CHB"
def compute_melspec(y, params):
    """
    Computes a mel-spectrogram and puts it at decibel scale
    Arguments:
        y {np array} -- signal
        params {AudioParams} -- Parameters to use for the spectrogram. Expected to have the attributes sr, n_mels, f_min, f_max
    Returns:
    """
    melspec = lb.feature.melspectrogram( 
        y, sr=params.sr, hop_length=590, n_mels=params.n_mels, fmin=params.fmin, fmax=params.fmax,n_fft=2000,
    )

    melspec = lb.power_to_db(melspec).astype(np.float32)
    return melspec


# + [markdown] id="Dko-Oh119lt-"
# Convert wav signal to mel_spectrograms

# + id="Fwh4_eHt3GHL"
mel_train_x = []
for i in range(len(train_x)):
  mel_train_x.append(compute_melspec(train_x[i].reshape(16000), params))
mel_test_x = []
for i in range(len(test_x)):
  mel_test_x.append(compute_melspec(test_x[i].reshape(16000), params))
  

# + colab={"base_uri": "https://localhost:8080/"} id="ND_biPTkCo3h" outputId="ce0ec1c5-62ad-4e5e-eed3-abab0c3c7418"
mel_train_x = np.array(mel_train_x)
mel_train_x.shape
mel_test_x = np.array(mel_test_x)
mel_test_x.shape

# + colab={"base_uri": "https://localhost:8080/"} id="FaTWIwcGcvYT" outputId="0920f72c-c921-4c0f-dc00-acc41da7aedd"
history3 = model_trained.fit(x=mel_train_x, y= Y, epochs = 100, validation_split = 0.10, shuffle = True,
                     batch_size = batch_size, verbose = 1, steps_per_epoch= train_x.shape[0] // batch_size,callbacks=[checkpoint,learning_rate_reduction])
model_trained.save("/content/drive/MyDrive/16000_pcm_speeches/") # saving the model

np.save('/content/drive/MyDrive/16000_pcm_speeches/history3.npy',history3.history)


# + [markdown] id="z39KclHEPXRk"
# History 1+2

# + id="UIXzvj07SzkZ"
def appendHist(h1, h2):
    if h1 == {}:
        return h2
    else:
        dest = {}
        for key, value in h1.items():
            dest[key] = value + h2[key]
        return dest


# + id="NFLsXCdJSXGF"
history1 = np.load("/content/drive/MyDrive/16000_pcm_speeches/history1.npy",allow_pickle=True).item()
history2 = np.load("/content/drive/MyDrive/16000_pcm_speeches/history2.npy",allow_pickle=True).item()
history1_2 =  appendHist (history1, history2)
np.save("/content/drive/MyDrive/16000_pcm_speeches/history1_2.npy",history1_2)
history1_2 = np.load("/content/drive/MyDrive/16000_pcm_speeches/history1_2.npy",allow_pickle=True).item()

# + id="E1ze0c7QUDuV"
history1_2 = np.load("/content/drive/MyDrive/16000_pcm_speeches/history1_2.npy",allow_pickle=True).item()
history3 = np.load("/content/drive/MyDrive/16000_pcm_speeches/history3.npy",allow_pickle=True).item()
history1_3 =  appendHist (history1_2, history3)
np.save("/content/drive/MyDrive/16000_pcm_speeches/history1_3.npy",history1_3)
history1_3 = np.load("/content/drive/MyDrive/16000_pcm_speeches/history1_3.npy",allow_pickle=True).item()

# + [markdown] id="Y44EcONhTIxI"
# Visualizations

# + colab={"base_uri": "https://localhost:8080/", "height": 573} id="P9ocMym3TIBH" outputId="20a4ccf4-d705-4cb1-bf91-e6450a17b173"

plt.figure(1)
plt.plot(history1_3['loss'])
plt.plot(history1_3['val_loss'])
plt.title('Model Complexity Graph:  Training vs. Validation Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validate'], loc='upper right')
plt.show()
plt.figure(2)
plt.plot(history1_3['accuracy'])
plt.plot(history1_3['val_accuracy'])
plt.title('Model Accuracy Graph:  Training vs. Validation accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validate'], loc='upper right')
plt.show()

# + [markdown] id="hnpb0GBNVGCj"
# Evaluate with test data 
#

# + id="NVFMvyk-VyKn"
Ytest = np.array([np.where(r==1)[0][0] for r in test_y])

# + colab={"base_uri": "https://localhost:8080/"} id="wvETwbrdU9n0" outputId="6228c563-eaa0-4b61-c44f-4828e0282a68"
model = tf.keras.models.load_model("/content/drive/MyDrive/16000_pcm_speeches")
score = model.evaluate(mel_test_x, Ytest, verbose = 1) 

print('Test loss:', score[0]) 
print('Test accuracy:', score[1])

# + [markdown] id="7xmOwCRU8PC7"
# Add trained fashion Mnist layers 

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="XYZqELP39Brb" outputId="911ca31f-33c3-4722-a72e-81201fc7a192"
from sklearn.metrics import confusion_matrix

y_vecs = model_trained.predict(mel_test_x)
y_pred = np.argmax(y_vecs, axis=1)
y_true = Ytest
cm = confusion_matrix(y_true, y_pred)
# print(cm)

# plt.imshow(cm, cmap = 'ocean')
# plt.colorbar

min_val, max_val = 0, 15

# intersection_matrix = np.random.randint(0, 10, size=(max_val, max_val))
fig, ax = plt.subplots(figsize=(20,20))
ax.matshow(cm, cmap=plt.cm.Blues)
# ax.matshow(cm, cmap=plt.cm.magma_r)
ax.xaxis.set_ticklabels(classes); ax.yaxis.set_ticklabels(classes);

for i in range(5):
    for j in range(5):
        c = cm[j,i]
        ax.text(i, j, str(c), va='center', ha='center')


plt.xticks(range(5))
plt.yticks(range(5))
plt.suptitle('Confusion matrix',size = 32)
plt.xlabel('True labeling',size = 32)
plt.ylabel('Predicted labeling',size = 32)
plt.rcParams.update({'font.size': 28})

# + [markdown] id="ogDGH_MaYGt7"
# Train with the second 5 Fashion Mnist trained model

# + id="YfQf4k1GWeQT"
model_trained = tf.keras.models.load_model("/content/drive/MyDrive/o5")
for layer in model_trained.layers[:20]:
   layer.trainable =False

# + id="egXE3HBZYtBi"
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
from keras.optimizers import Adam ,RMSprop, Adagrad
import os 

Learning_rate = 7.006492321624085e-46

optimizer = Adagrad(lr=Learning_rate)
 
model_trained.compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])


learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', 
                                            patience=2, 
                                            verbose=1, 
                                            factor=0.3, 
                                            min_lr=1e-100)

batch_size = 64
checkpoint_path = "/content/drive/MyDrive/16000_pcm_speeches/o5model/"
checkpoint_dir = os.path.dirname(checkpoint_path)
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,monitor='val_accuracy',mode='max',
                                                               save_best_only=True)

# + colab={"base_uri": "https://localhost:8080/"} id="B4N_lNgLpI6G" outputId="16f19ed5-6d37-46a4-b713-970271c80dd2"
history5 = model_trained.fit(x=mel_train_x, y= Y, epochs = 100, validation_split = 0.10, shuffle = True,
                     batch_size = batch_size, verbose = 1, steps_per_epoch= train_x.shape[0] // batch_size,callbacks=[checkpoint,learning_rate_reduction])
model_trained.save("/content/drive/MyDrive/16000_pcm_speeches/o5model") # saving the model

np.save('/content/drive/MyDrive/16000_pcm_speeches/o5model/history5.npy',history5.history)


# + id="kXYYkSzNpgtM"
history1 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1.npy",allow_pickle=True).item()
history2 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history2.npy",allow_pickle=True).item()
history1_2 =  appendHist (history1, history2)
np.save("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_2.npy",history1_2)
history1_2 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_2.npy",allow_pickle=True).item()

# + id="MVnvzFQ5rPor"
history1_2 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_2.npy",allow_pickle=True).item()
history3 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history3.npy",allow_pickle=True).item()
history1_3 =  appendHist (history1_2, history3)
np.save("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_3.npy",history1_3)
history1_3 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_3.npy",allow_pickle=True).item()

# + id="iDxSDVfzsEOJ"
history1_3 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_3.npy",allow_pickle=True).item()
history4 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history4.npy",allow_pickle=True).item()
history1_4 =  appendHist (history1_3, history4)
np.save("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_4.npy",history1_4)
history1_4 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_4.npy",allow_pickle=True).item()

# + id="k0CEiqBssz5Q"
history1_4 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_4.npy",allow_pickle=True).item()
history5 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history5.npy",allow_pickle=True).item()
history1_5 =  appendHist (history1_4, history5)
np.save("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_5.npy",history1_5)
history1_5 = np.load("/content/drive/MyDrive/16000_pcm_speeches/o5model/history1_5.npy",allow_pickle=True).item()

# + colab={"base_uri": "https://localhost:8080/", "height": 675} id="gUpA7wiIqpAT" outputId="f728a06b-e600-4b11-f6b6-f5363bfc1e54"
plt.figure(1)
plt.plot(history1_5['loss'])
plt.plot(history1_5['val_loss'])
plt.title('Model Complexity Graph:  Training vs. Validation Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validate'], loc='upper right')
plt.show()
plt.figure(2)
plt.plot(history1_5['accuracy'])
plt.plot(history1_5['val_accuracy'])
plt.title('Model Accuracy Graph:  Training vs. Validation accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validate'], loc='lower right')
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="d04gq1Ldqv_c" outputId="5a073b80-8961-475e-dd8a-f60ed087d2c1"
score = model_trained.evaluate(mel_test_x, Ytest, verbose = 1) 

print('Test loss:', score[0]) 
print('Test accuracy:', score[1])

# + colab={"base_uri": "https://localhost:8080/", "height": 865} id="H46ySnieuNMr" outputId="aa2b991a-dd46-4a95-800f-8d74faa71dda"
from sklearn.metrics import confusion_matrix

y_vecs = model_trained.predict(mel_test_x)
y_pred = np.argmax(y_vecs, axis=1)
y_true = Ytest
cm = confusion_matrix(y_true, y_pred)
# print(cm)

# plt.imshow(cm, cmap = 'ocean')
# plt.colorbar

min_val, max_val = 0, 15

# intersection_matrix = np.random.randint(0, 10, size=(max_val, max_val))
fig, ax = plt.subplots(figsize=(20,20))
ax.matshow(cm, cmap=plt.cm.Blues)
# ax.matshow(cm, cmap=plt.cm.magma_r)
ax.xaxis.set_ticklabels(classes); ax.yaxis.set_ticklabels(classes);

for i in range(5):
    for j in range(5):
        c = cm[j,i]
        ax.text(i, j, str(c), va='center', ha='center')


plt.xticks(range(5))
plt.yticks(range(5))
plt.suptitle('Confusion matrix',size = 32)
plt.xlabel('True labeling',size = 32)
plt.ylabel('Predicted labeling',size = 32)
plt.rcParams.update({'font.size': 28})

# + [markdown] id="dVr1tO3Pvnz4"
# Train with whole Fashion Mnist trained model

# + colab={"base_uri": "https://localhost:8080/"} id="rFElVpmJvB_B" outputId="bf30b95e-2641-468e-81f5-8d6a01492453"
model = tf.keras.models.load_model("/content/drive/MyDrive/all/saved_model.pb/")
for layer in model.layers[:20]:
   layer.trainable =False

model._layers.pop()
model._layers.pop()
model.summary()

# layer2 = layers.Dense(3, activation="softmax")

# + colab={"base_uri": "https://localhost:8080/"} id="nBQfJFhHwI2u" outputId="3e8c7760-1cc5-4a30-d1ff-60e584024d69"
x=model.output
x = Flatten()(x)
x = Dense(256)(x)
x = Activation('relu')(x)
x = Dense(5)(x)
x = Activation("softmax")(x)
inp = model.input
modelall2 =  Model(inputs = inp, outputs =x)
modelall2.summary()

# + id="L4HlCH505XpW"
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
from keras.optimizers import Adam ,RMSprop, Adagrad
import os 

Learning_rate = 7.006492321624085e-47

optimizer = Adam(lr=Learning_rate)
 
modelall2.compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])


learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', 
                                            patience=3, 
                                            verbose=1, 
                                            factor=0.5, 
                                            min_lr=1e-100)

batch_size = 2400
checkpoint_path = "/content/drive/MyDrive/16000_pcm_speeches/allmodel/"
checkpoint_dir = os.path.dirname(checkpoint_path)
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,monitor='val_accuracy',mode='max',
                                                               save_best_only=True)

# + colab={"base_uri": "https://localhost:8080/"} id="GJye1VEG544O" outputId="0fc899f6-9e79-48fd-9977-7f5b034a2212"
history5 = modelall2.fit(x=mel_train_x, y= Y, epochs = 20, validation_split = 0.10, shuffle = True,
                     batch_size = batch_size, verbose = 1, steps_per_epoch= train_x.shape[0] // batch_size,callbacks=[checkpoint,learning_rate_reduction])
model_trained.save("/content/drive/MyDrive/16000_pcm_speeches/allmodel/") # saving the model

np.save('/content/drive/MyDrive/16000_pcm_speeches/allmodel/history5.npy',history5.history)


# + id="wMSjOF5p9zLA"
history3 = np.load("/content/drive/MyDrive/16000_pcm_speeches/allmodel/history3.npy",allow_pickle=True).item()
history4 = np.load("/content/drive/MyDrive/16000_pcm_speeches/allmodel/history4.npy",allow_pickle=True).item()
history3_4 =  appendHist (history3, history4)
np.save("/content/drive/MyDrive/16000_pcm_speeches/allmodel/history3_4.npy",history3_4)
history3_4 = np.load("/content/drive/MyDrive/16000_pcm_speeches/allmodel/history3_4.npy",allow_pickle=True).item()

# + colab={"base_uri": "https://localhost:8080/", "height": 675} id="Hj1TjD-VQDuU" outputId="20229393-681d-4a4c-8ddc-fd6707d13fb2"
plt.figure(1)
plt.plot(history3_4['loss'])
plt.plot(history3_4['val_loss'])
plt.title('Model Complexity Graph:  Training vs. Validation Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validate'], loc='upper right')
plt.show()
plt.figure(2)
plt.plot(history3_4['accuracy'])
plt.plot(history3_4['val_accuracy'])
plt.title('Model Accuracy Graph:  Training vs. Validation accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validate'], loc='lower right')
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="6CLj6rVSV_v5" outputId="22f8ef30-a633-4da4-b40a-ec20e8d2840e"
score = modelall2.evaluate(mel_test_x, Ytest, verbose = 1) 

print('Test loss:', score[0]) 
print('Test accuracy:', score[1])

# + colab={"base_uri": "https://localhost:8080/", "height": 865} id="hwZdvyrRWIWu" outputId="60c52c57-e1cd-429b-de74-9b949d29fbcb"
from sklearn.metrics import confusion_matrix

y_vecs = modelall2.predict(mel_test_x)
y_pred = np.argmax(y_vecs, axis=1)
y_true = Ytest
cm = confusion_matrix(y_true, y_pred)
# print(cm)

# plt.imshow(cm, cmap = 'ocean')
# plt.colorbar

min_val, max_val = 0, 15

# intersection_matrix = np.random.randint(0, 10, size=(max_val, max_val))
fig, ax = plt.subplots(figsize=(20,20))
ax.matshow(cm, cmap=plt.cm.Blues)
# ax.matshow(cm, cmap=plt.cm.magma_r)
ax.xaxis.set_ticklabels(classes); ax.yaxis.set_ticklabels(classes);

for i in range(5):
    for j in range(5):
        c = cm[j,i]
        ax.text(i, j, str(c), va='center', ha='center')


plt.xticks(range(5))
plt.yticks(range(5))
plt.suptitle('Confusion matrix',size = 32)
plt.xlabel('True labeling',size = 32)
plt.ylabel('Predicted labeling',size = 32)
plt.rcParams.update({'font.size': 28})

# + [markdown] id="5p7h61FDYYbp"
# Train with a non trained model

# + id="35lfA5E7YfVx"
from tensorflow.python.keras.layers import Input , Dense , Conv2D , Activation , Add,ReLU,MaxPool2D,Flatten,Dropout,BatchNormalization
from tensorflow.python.keras.models import Model
input = Input(shape=[28, 28, 1])

x = Conv2D(32, (5, 5), strides=1, padding='same')(input)
x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform')(x)
x = Activation('relu')(x)

x = Conv2D(32, (5, 5), strides=1, padding='same')(x)
x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform')(x)
x = Activation('relu')(x)

x = MaxPool2D(pool_size=2, strides=2, padding='same')(x)
x = Dropout (0.35)(x)

x = Conv2D(64, (3, 3), strides=1, padding='same')(x)
x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform')(x)
x = Activation('relu')(x)

x = Conv2D(64, (3, 3), strides=1, padding='same')(x)
x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform')(x)
x = Activation('relu')(x)

x = Conv2D(64, (3, 3), strides=1, padding='same')(x)
x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform')(x)
x = Activation('relu')(x)

x = MaxPool2D(pool_size=2, strides=2, padding='same')(x)
x = Dropout (0.35)(x)

x = Flatten()(x)
x = Dense(256)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dense(5)(x)
x = Activation('softmax')(x)

model = Model(inputs = input, outputs =x)

# + id="2cOZXIcpYq_E"
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
from keras.optimizers import Adam ,RMSprop, Adagrad
import os 

Learning_rate = 0.01

optimizer = Adam(lr=Learning_rate)
 
model.compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])


learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', 
                                            patience=3, 
                                            verbose=1, 
                                            factor=0.5, 
                                            min_lr=1e-100)

batch_size = 64
checkpoint_path = "/content/drive/MyDrive/nontrained"
checkpoint_dir = os.path.dirname(checkpoint_path)
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,monitor='val_accuracy',mode='max',
                                                               save_best_only=True)

# + colab={"base_uri": "https://localhost:8080/"} id="hrkNts5NZCRS" outputId="d4638580-b1f9-4571-8c7e-367e58712100"
history1 = model.fit(x=mel_train_x, y= Y, epochs = 100, validation_split = 0.10, shuffle = True,
                     batch_size = batch_size, verbose = 1, steps_per_epoch= mel_train_x.shape[0] // batch_size,callbacks=[checkpoint,learning_rate_reduction])
model.save("/content/drive/MyDrive/nontrained") # saving the model

np.save('/content/drive/MyDrive/nontrained/history1.npy',history1.history)

# + colab={"base_uri": "https://localhost:8080/", "height": 675} id="AZDXgY0FZ4v1" outputId="5373770d-bb8e-4c4f-adf7-e8bc7da60200"

plt.figure(1)
plt.plot(history1['loss'])
plt.plot(history1['val_loss'])
plt.title('Model Complexity Graph:  Training vs. Validation Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validate'], loc='upper right')
plt.show()
plt.figure(2)
plt.plot(history1['accuracy'])
plt.plot(history1['val_accuracy'])
plt.title('Model Accuracy Graph:  Training vs. Validation accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validate'], loc='lower right')
plt.show()

# + id="k52RK3FaZ7f8"
history1 = np.load('/content/drive/MyDrive/nontrained/history1.npy',allow_pickle=True).item()

# + colab={"base_uri": "https://localhost:8080/"} id="P7y_9R84aPs0" outputId="03658975-47b4-4585-87ca-2b242c0b383d"
score = model.evaluate(mel_test_x, Ytest, verbose = 1) 

print('Test loss:', score[0]) 
print('Test accuracy:', score[1])

# + colab={"base_uri": "https://localhost:8080/", "height": 865} id="bxhpEP_naZlb" outputId="e8f68921-86cc-471f-edd1-2b74168aa674"
from sklearn.metrics import confusion_matrix

y_vecs = model.predict(mel_test_x)
y_pred = np.argmax(y_vecs, axis=1)
y_true = Ytest
cm = confusion_matrix(y_true, y_pred)
# print(cm)

# plt.imshow(cm, cmap = 'ocean')
# plt.colorbar

min_val, max_val = 0, 15

# intersection_matrix = np.random.randint(0, 10, size=(max_val, max_val))
fig, ax = plt.subplots(figsize=(20,20))
ax.matshow(cm, cmap=plt.cm.Blues)
# ax.matshow(cm, cmap=plt.cm.magma_r)
ax.xaxis.set_ticklabels(classes); ax.yaxis.set_ticklabels(classes);

for i in range(5):
    for j in range(5):
        c = cm[j,i]
        ax.text(i, j, str(c), va='center', ha='center')


plt.xticks(range(5))
plt.yticks(range(5))
plt.suptitle('Confusion matrix',size = 32)
plt.xlabel('True labeling',size = 32)
plt.ylabel('Predicted labeling',size = 32)
plt.rcParams.update({'font.size': 28})

StarcoderdataJupyterScriptsDedupFiltered