{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "RollNumber_partB_PA4.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "WdFUrY9aLUhI"
},
"source": [
"## CS535/EE514 - Spring 2022 - Assignment 4 - Part B\n",
"\n",
"\n",
"\n",
"#### Important Instructions and Submission Guidelines:\n",
"- ## Important Instructions and Submission Guidelines:\n",
"- Submit your code both as notebook file (.ipynb) and python script (.py) on LMS. Naming convention for submission of this notebook is `RollNumber_partB_PA4.ipynb` where. For example: `23100042_partB_PA4.ipynb`\n",
"- All the cells must be run once before submission. If your submission's cells are not showing the results (plots etc.), marks wil be deducted\n",
"- Only the code written within this notebook's marked areas will be considered while grading. No other files will be entertained\n",
"- You are advised to follow good programming practies including approriate variable naming and making use of logical comments.\n",
"\n",
"\n",
"The university honor code should be maintained. Any violation, if found, will result in disciplinary action. \n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dClE8ipfLWKI"
},
"source": [
"Double click here to enter your name and roll number: \n",
"Name: \n",
"\n",
"Roll Number: \n",
""
]
},
{
"cell_type": "markdown",
"source": [
"* Mount your Drive"
],
"metadata": {
"id": "pvItjIt2CxmQ"
}
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/gdrive', force_remount=True)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jCgAAEaqdX4P",
"outputId": "64dfa475-4283-4fdd-e950-9b71a7412648"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /content/gdrive\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"* Import Libraries"
],
"metadata": {
"id": "sNPRCZPBCzYp"
}
},
{
"cell_type": "code",
"source": [
"from numpy import array\n",
"from numpy import hstack\n",
"import tensorflow\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense\n",
"from keras.layers import Flatten\n",
"from keras.layers.convolutional import Conv1D\n",
"from keras.layers.convolutional import MaxPooling1D\n",
"from keras.layers import ReLU\n",
"from keras.layers import LeakyReLU\n",
"from keras.layers import Softmax\n",
"from tensorflow import keras\n",
"from tensorflow.keras.optimizers import Adam\n",
"import csv\n",
"import matplotlib.pyplot as plt\n",
"from keras.models import load_model\n",
"from keras.models import load_model\n",
"import numpy as np\n",
"from tensorflow import keras\n",
"import keras.optimizers\n",
"#from utils.openmax import image_show, compute_activation, compute_openmax\n",
"import librosa\n",
"import soundfile\n",
"import os, glob, pickle\n",
"import numpy as np\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.neural_network import MLPClassifier\n",
"from sklearn.metrics import accuracy_score\n",
"from sklearn.metrics import f1_score\n",
"from sklearn.metrics import precision_score\n",
"from IPython.display import Audio \n",
"from sklearn.metrics import confusion_matrix\n",
"from pydub import AudioSegment"
],
"metadata": {
"id": "Di6ZBPMnC6az"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Part 1: Load Dataset**\n",
"Use the given link to unzip the folder of wav files:\n",
"[Audio Dataset](https://drive.google.com/file/d/1C3JXfb-LwPWD8WlhWYnNt5J02BKdHRg3/view?usp=sharing) \n",
"* 2671.wav - 3213.wav are male audio files \n",
"* 1308.wav - 1811.wav are female audio files \n",
"\n",
"Perform a train-test split on your dataset. \n",
"Generate labels for the audio files as well."
],
"metadata": {
"id": "zr-zVrqyECO1"
}
},
{
"cell_type": "code",
"source": [
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "GQJM1wpYHkad"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Part 2: Feature Extraction**\n",
"\n",
"Define a function **extract_feature** to extract the mfcc, chroma, spec_cen and mel features from a sound file. This function takes 5 parameters- the file name and four Boolean parameters for the four features:\n",
"\n",
" \n",
"\n",
"1. **mfcc:** Mel Frequency Cepstral Coefficient, represents the short-term power spectrum of the sound\n",
"2. **chroma:** Pertains to the 12 different pitch classes\n",
"3. **spec_cen:** It indicates where the ”centre of mass” for a sound is located and is calculated as the weighted mean of the frequencies present in the sound.\n",
"4. **mel:** Mel Spectrogram Frequency"
],
"metadata": {
"id": "bpedhNWgDKZH"
}
},
{
"cell_type": "code",
"source": [
"def extract_feature(file_name, mfcc, chroma, mel, spec_cen):\n",
" # Code Here\n",
"\n",
" # ---------\n",
" return result"
],
"metadata": {
"id": "XBBk5GbWDJaS"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Call extract_feature function here\n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "cftPT2CrJw-a"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"If you use all the extracted features, your model might take a lot of time to converge. Apply PCA to reduce the dimensionality of vector x. \n",
"You may use an in-built library function for this."
],
"metadata": {
"id": "ee31TJRhJfhT"
}
},
{
"cell_type": "code",
"source": [
"# Apply PCA on your features to reduce dimension\n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "ogeeH2xvJ49V"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Part 3: Implementation of Logistic Regression From\n",
"Scratch (30 Marks)**"
],
"metadata": {
"id": "ITIgOIBYHw7i"
}
},
{
"cell_type": "markdown",
"source": [
"You are required to implement **Binary Logistic Regression Model** from scratch keeping in view all the discussions from the class lectures. "
],
"metadata": {
"id": "GVD15mHnHxcH"
}
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"* Sigmoid Function \n",
"\n",
"The sigmoid function is used on the classifier to compute the probability of the feature vector x belonging to that class. Then the label of the classifier which returns the highest probability is assigned to x. \n",
"**If Scikit-Learn is used in this part, you will NOT get any credit.** \n",
"It is HIGHLY recommended that you use matrix multiplication in your implementation instead of for loops to avoid unnecessary runtime. \n",
"\n",
"\n"
],
"metadata": {
"id": "eeFYeB2F7KUu"
}
},
{
"cell_type": "code",
"source": [
"# Implement sigmoid function here\n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "rRTOw1UP55dE"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"* Cross Entropy Loss \n",
"\n",
"\n",
"\n"
],
"metadata": {
"id": "4jmMIA-g7Di-"
}
},
{
"cell_type": "code",
"source": [
"# Implement Cross Entropy Loss here\n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "1FJJvyWy7c5Z"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"* Batch Gradient Descent \n",
"\n",
"We require you to incorporate **L2 regularization** in the loss function, that is: \n",
"\n",
"\n",
"where L(θ) is cross-entropy loss and λ is the regularization parameter. \n",
"\n",
"You will compute the derivative of the new loss expression and update the weights of the model using the derivative during the implementation of batch gradient descent. \n",
"\n",
"Run your batch gradient descent function for large number of epochs, e.g. around 1000, to get good F1-scores."
],
"metadata": {
"id": "fjlIqVj685lD"
}
},
{
"cell_type": "code",
"source": [
"# Implement Batch Gradient Descent here\n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "5ROtL0Oa8zVD"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"* Prediction Function \n",
"\n",
"It predicts the labels of test data.\n",
"\n"
],
"metadata": {
"id": "mHjWqFs19sat"
}
},
{
"cell_type": "code",
"source": [
"# Implement Prediction Function here\n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "TuSLLDBG92X8"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Implementation: \n",
"Using the above functions, implement your Logistic Regression Classifier. \n",
"Remember to store the training loss for result visualization. \n"
],
"metadata": {
"id": "XZ1bLQpo-PKK"
}
},
{
"cell_type": "code",
"source": [
"# Implement Logistic Regression \n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "WfIwJdLX-jEL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"Visualize Results on Training Data: \n",
"\n",
"Plot graphs for different values of learning rate and regularization parameter with no of epochs on the x-axis and training loss on the y-axis. \n",
"The overall loss is computed by adding the loss of all classifiers. "
],
"metadata": {
"id": "3wTWNwgk99Xz"
}
},
{
"cell_type": "code",
"source": [
"# Plot results for training loss here\n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "HqtUE_sY98n3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"* Evaluation function \n",
"\n",
"It calculates classification accuracy, F1 score and confusion\n",
"matrix. Pass the labels of test data in this function and report your results."
],
"metadata": {
"id": "9Bwhr_Mw_IAl"
}
},
{
"cell_type": "code",
"source": [
"# Implement Evaluation Function here\n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "s7BQDfSr_ERd"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Call Evaluation Function here \n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "7ZssuCa8_hKB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Part-4: Logistic Regression using Scikit-Learn (10 Marks)**"
],
"metadata": {
"id": "FYt259pU_oJ4"
}
},
{
"cell_type": "markdown",
"source": [
"Use Scikit-Learn’s Logistic Regression implementation to train and test the dataset. \n",
"The model should be similar to the one you made from scratch. \n",
"Remember to implement the in-built classifier in binary classification mode. \n",
"You are not required to plot graphs in this part. \n",
"Report the accuracy, F1 score, and confusion matrix of test data using Scikit-Learn. "
],
"metadata": {
"id": "qtHmUNvW_rNV"
}
},
{
"cell_type": "code",
"source": [
"# Implement Logistic Regression using Scikit-Learn library here \n",
"# Code Here\n",
"\n",
"# ---------"
],
"metadata": {
"id": "xnA5fiVn__Bd"
},
"execution_count": null,
"outputs": []
}
]
}