Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USE THIS MODEL TO ANSWER THE UPCOMING QUESTIONS MODEL: import numpy as np import pandas as pd import tensorflow as tf from tensorflow.keras import layers,

USE THIS MODEL TO ANSWER THE UPCOMING QUESTIONS
MODEL:
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers, models
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
data:
x_test, x_train, x_val, y_test, y_test_num, y_train, y_train_num, y_val, y_val_num
tf.keras.utils.set_random_seed(42)
tf.config.experimental.enable_op_determinism()
num_classes =4
model = models.Sequential([
layers.Conv2D(4,(5,5), activation='relu', padding='same', input_shape=(80,80,1)),
layers.MaxPooling2D((2,2), strides=(2,2)),
layers.Conv2D(8,(5,5), activation='relu', padding='same'),
layers.MaxPooling2D((2,2), strides=(2,2)),
layers.Conv2D(16,(5,5), activation='relu', padding='same'),
layers.MaxPooling2D((2,2)),
layers.Flatten(),
layers.Dense(50, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01)),
layers.Dense(num_classes, activation='softmax')
])
model.compile(loss='ca
tegorical_crossentropy', optimizer= 'adam', metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=20, batch_size=32, validation_data=(x_val, y_val), verbose=1)
# Plotting training & validation accuracy values/loss values
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
# Evaluating the model on the test set
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test Loss:', test_loss)
print('Test Accuracy:', test_acc)
# Predictions
train_predictions = model.predict(x_train)
train_pred_labels = np.argmax(train_predictions, axis=1)
train_true_labels = np.argmax(y_train, axis=1)
train_conf_matrix = confusion_matrix(train_true_labels, train_pred_labels)
sns.heatmap(train_conf_matrix, annot=True, fmt='d')
plt.title('Confusion Matrix - Training Set')
plt.show()
test_predictions = model.predict(x_test)
test_pred_labels = np.argmax(test_predictions, axis=1)
test_true_labels = np.argmax(y_test, axis=1)
test_conf_matrix = confusion_matrix(test_true_labels, test_pred_labels)
sns.heatmap(test_conf_matrix, annot=True, fmt='d')
plt.title('Confusion Matrix - Test Set')
plt.show()
Answer to the following questions:
How many parameters does the model train? Before performing the training, do you expect this model to overfit? Which aspects would influence the overfitting (or not) of this model?
Plot the loss function and the accuracy per epoch for the train and validation sets.
Which accuracy do you obtain on the test set?
Using the function plot_confusion_matrix plot the confusion matrices of the classification task on the train set and test set. What do you observe from this metric? Which classes display more correct predictions? And wrong?
Using the function ind_correct_uncorrect extract the indexes of the training data that were predicted correctly and incorrectly, per each class. For each music genre, perform the following steps:
Using the function plot_spectrograms plot the 12 mel spectrograms of the first 6 data points which were predicted correctly and the first 6 which were predicted wrongly. Do you observe some differences among music genres?
Using the function print_wrong_prediction print the predicted classes of the first 6 data points which were predicted wrongly.
Using the Grad-CAM method, implemented in the function plot_gradcam_spectrogram, print the heatmaps of the last pooling layer for the same 12 extracts (6 correct +6 wrong). Comment on the heatmaps obtained. Do you observe differences among the heatmaps of different music genres? Can you understand why the model got some predictions wrong?
Comment on the previous question: what are your thoughts about the applicability of the Grad-CAM tool on these data?
P2- Disentangling time and frequency
The images we are using in this assignment are different from a usual picture: the x and y axes carry different meanings. With the tools we are exploring during lectures and seminars, can you propose a CNN architecture that takes into account differently the time and frequency components of the spectrograms?
Present and describe the architecture you have chosen and justify the rationale behind it. Plot training and validation loss and accuracy over 20 epochs (this time you can use the GPU runtime if the model is slow to train). Print the accuracy on the test set and the confusion matrices on the training and test sets.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions