<jupyter_start><jupyter_text>Overall, all models were poor-performing.<jupyter_code>%%time
import numpy as np
# from tempfile import TemporaryFile
import os
import pickle
import random 
import operator
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import IPython.display as ipd
import time
%%time
import librosa 
import librosa.display
import tensorflow as tf
blue_sig1, blue_sr1 = librosa.load("../data/blues/blues.00019.wav",sr=None)
blue_sig1.shape
Dm = librosa.feature.melspectrogram(y=blue_sig1, sr=blue_sr1)
plt.figure(figsize=(10, 4))
librosa.display.specshow(
    (librosa.power_to_db(Dm,ref=np.max)+80)/80, y_axis='mel', x_axis='time')
plt.colorbar(format='%.3f dB')
plt.title('Mel Spectrogram')
plt.tight_layout()
librosa.power_to_db(Dm,ref=np.max) +80
%%time
directory = "../data/"
data = []
classes = []
for folder in os.listdir(directory):
    if folder=="mf_files":
        continue
    for file in os.listdir(directory+folder):  
        sig, rate = librosa.load(directory+folder+"/"+file)
        mel = librosa.feature.melspectrogram(y=sig, sr=rate)
        # scale to between 0 and 1
        mel_db = (librosa.power_to_db(mel, ref= np.max)+80)/80
        data.append(mel_db)
        classes.append(folder)
poor_data = []
for i in range(len(data)):
#     print(data[i].shape)
    if data[i].shape != (128,1293):
        print(i, "\t", data[i].shape)
        poor_data.append(i)
print(len(poor_data))
usable_data = np.stack([element for i,element in enumerate(data) if i not in poor_data])
usable_classes = np.array([element for i,element in enumerate(classes) if i not in poor_data])
print(usable_data.shape)
print(usable_classes.shape)
label_dict = {}
for i, element in enumerate(np.unique(usable_classes)):
    label_dict[element] = i
print(label_dict)
# convert from string to int
usable_labels = np.array([label_dict[element] for element in list(usable_classes)])
inv_labels = {v: k for k, v in label_dict.items()}
from sklearn.model_selection import train_test_split
trainX, testX, trainY, testY = train_test_split(usable_data, 
                                                usable_labels,
                                                test_size = 0.25, 
                                                random_state=29,
                                                stratify = usable_labels)<jupyter_output><empty_output><jupyter_text>Overfitting:* Regularization* Batch Normalization* Dropout Layers:    * tf.keras.layers.Dropout(rate=0.2)     * 0.20-0.3 in RNN and 0.4-.5 in CNNTry with ReLU layersor Elu or SELU layers:* tf.keras.layers.Dense(64, kernel_initializer='lecun_normal', activation='selu')* tf.keras.layers.Dense(64, kernel_initializer='he_normal',                                activation= 'elu')<jupyter_code>def plot_results(fit):
    plt.figure(figsize=(20,15))
    fig, ax = plt.subplots(2)
    # accuracy subplot
    ax[0].plot(fit.history["accuracy"], label="train")
    ax[0].plot(fit.history["val_accuracy"], label="test")    
    ax[0].set_ylabel("Accuracy")
    ax[0].legend(loc="lower right")
    ax[0].set_title("Accuracy")
    
    # Error subplot
    ax[1].plot(fit.history["loss"], label="train error")
    ax[1].plot(fit.history["val_loss"], label="test error")    
    ax[1].set_ylabel("Error")
    ax[1].set_xlabel("Epoch")
    ax[1].legend(loc="upper right")
    ax[1].set_title("Error")
    plt.tight_layout()
    plt.show()
train_times = []
%%time
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(trainX.shape[1], trainX.shape[2])),
    tf.keras.layers.Dense(512, activation= "relu", 
                          kernel_regularizer = tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Dropout(rate=0.01),
    tf.keras.layers.Dense(512, activation = "relu", 
                          kernel_regularizer = tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Dropout(rate=0.03),
    tf.keras.layers.Dense(512, activation = "relu", 
                          kernel_regularizer = tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Dropout(rate=0.01),
    tf.keras.layers.Dense(128, activation = "relu", 
                          kernel_regularizer = tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Dropout(rate=0.01),
    tf.keras.layers.Dense(10, activation = "linear")
])
## get output layer as logits for each class 
##(we can use softmax to get probabilities, but doing so makes model unstable:
# https://www.tensorflow.org/tutorials/quickstart/beginner
model.summary()

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

start = time.time()
fit = model.fit(trainX, trainY,
         validation_data = (testX,testY),
                 epochs = 100)
stop = time.time()
train_times.append(stop-start)


plot_results(fit)

probability_model = tf.keras.Sequential([model, 
                                         tf.keras.layers.Softmax()])
predictions = probability_model.predict(testX)
pred_labels = [np.argmax(prediction) for i,prediction in enumerate(predictions)]

from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.metrics import accuracy_score

ConfusionMatrixDisplay.from_predictions(
    y_true = [inv_labels[e] for i,e in enumerate(testY)],
    y_pred = [inv_labels[e] for i,e in enumerate(pred_labels)], 
    xticks_rotation= "vertical")
print(accuracy_score(testY,pred_labels))<jupyter_output>Model: "sequential_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_2 (Flatten)          (None, 165504)            0         
_________________________________________________________________
dense_10 (Dense)             (None, 512)               84738560  
_________________________________________________________________
dropout_8 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_11 (Dense)             (None, 512)               262656    
_________________________________________________________________
dropout_9 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_12 (Dense)             (None, 512)               262656    
______________________________________________________[...]<jupyter_text>With Selu<jupyter_code>%%time
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(trainX.shape[1], trainX.shape[2])),
    tf.keras.layers.Dense(512, kernel_initializer='lecun_normal', 
                          activation='selu', 
                          kernel_regularizer =tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Dropout(rate=0.01),
    tf.keras.layers.Dense(256, kernel_initializer='lecun_normal', 
                          activation='selu', 
                          kernel_regularizer =tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Dropout(rate=0.02),
    tf.keras.layers.Dense(128, kernel_initializer='lecun_normal', 
                          activation='selu', 
                          kernel_regularizer =tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Dropout(rate=0.01),
    tf.keras.layers.Dense(64, kernel_initializer='lecun_normal', 
                          activation='selu', 
                          kernel_regularizer =tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Dropout(rate=0.01),
    tf.keras.layers.Dense(10, activation = "linear")
])
## get output layer as logits for each class 
##(we can use softmax to get probabilities, but doing so makes model unstable:
# https://www.tensorflow.org/tutorials/quickstart/beginner
model.summary()
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
fit = model.fit(trainX, trainY,
         validation_data = (testX,testY),
                 epochs = 300)

plot_results(fit)
probability_model = tf.keras.Sequential([model, 
                                         tf.keras.layers.Softmax()])

predictions = probability_model.predict(testX)
pred_labels = [np.argmax(prediction) for i,prediction in enumerate(predictions)]

from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.metrics import accuracy_score

ConfusionMatrixDisplay.from_predictions(
    y_true = [inv_labels[e] for i,e in enumerate(testY)],
    y_pred = [inv_labels[e] for i,e in enumerate(pred_labels)], 
    xticks_rotation= "vertical")
print(accuracy_score(testY,pred_labels))<jupyter_output>Model: "sequential_10"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_5 (Flatten)          (None, 165504)            0         
_________________________________________________________________
dense_25 (Dense)             (None, 512)               84738560  
_________________________________________________________________
dropout_20 (Dropout)         (None, 512)               0         
_________________________________________________________________
dense_26 (Dense)             (None, 256)               131328    
_________________________________________________________________
dropout_21 (Dropout)         (None, 256)               0         
_________________________________________________________________
dense_27 (Dense)             (None, 128)               32896     
_____________________________________________________[...]<jupyter_text>Selu with BatchNormalization<jupyter_code>%%time
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(trainX.shape[1], trainX.shape[2])),
    tf.keras.layers.Dense(512, kernel_initializer='lecun_normal', 
                          activation='selu', 
                          kernel_regularizer =tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dense(256, kernel_initializer='lecun_normal', 
                          activation='selu', 
                          kernel_regularizer =tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dense(128, kernel_initializer='lecun_normal', 
                          activation='selu', 
                          kernel_regularizer =tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dense(64, kernel_initializer='lecun_normal', 
                          activation='selu', 
                          kernel_regularizer =tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dense(10, activation = "linear")
])
## get output layer as logits for each class 
##(we can use softmax to get probabilities, but doing so makes model unstable:
# https://www.tensorflow.org/tutorials/quickstart/beginner
model.summary()
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
fit = model.fit(trainX, trainY,
         validation_data = (testX,testY),
                 epochs = 100)

plot_results(fit)
probability_model = tf.keras.Sequential([model, 
                                         tf.keras.layers.Softmax()])

predictions = probability_model.predict(testX)
pred_labels = [np.argmax(prediction) for i,prediction in enumerate(predictions)]

from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.metrics import accuracy_score

ConfusionMatrixDisplay.from_predictions(
    y_true = [inv_labels[e] for i,e in enumerate(testY)],
    y_pred = [inv_labels[e] for i,e in enumerate(pred_labels)], 
    xticks_rotation= "vertical")
print(accuracy_score(testY,pred_labels))<jupyter_output>Model: "sequential_14"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_7 (Flatten)          (None, 165504)            0         
_________________________________________________________________
dense_35 (Dense)             (None, 512)               84738560  
_________________________________________________________________
batch_normalization_4 (Batch (None, 512)               2048      
_________________________________________________________________
dense_36 (Dense)             (None, 256)               131328    
_________________________________________________________________
batch_normalization_5 (Batch (None, 256)               1024      
_________________________________________________________________
dense_37 (Dense)             (None, 128)               32896     
_____________________________________________________[...]
{'fraction_non_alphanumeric': 0.12180579216354344, 'fraction_numerical': 0.0291931237416757, 'mean_word_length': 3.1209317166560306, 'pattern_counts': {'":': 1, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '139647', 'license': 'MIT', 'n_tokens_mistral': 3994, 'n_tokens_neox': 3665, 'n_words': 662, 'path': 'models/ANNs-MelSpectrogram.ipynb', 'repo_name': 'yashmanne/Music_Genre_Classification', 'size': '12914'}