{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Milestone 5 - DL&GenAI Project Course\n", "**Roll/Email:** 21f2000735@ds.study.iitm.ac.in\n", "\n", "This notebook contains the working steps and final answers for Q1-Q5." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q1) Dataset Splitting Strategy\n", "100 recipes per genre \u00d7 10 genres = 1000 total recipes.\n", "Validation split = 20% \u21d2 **200** items." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "recipes = [{'genre': g, 'idx': i} for g in range(10) for i in range(100)]\n", "train_recipes, val_recipes = train_test_split(recipes, test_size=0.2, shuffle=True, random_state=42)\n", "print('Total:', len(recipes), 'Validation:', len(val_recipes))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer Q1:** `200` (option: **200**)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q2) On-the-Fly Mixing Dimensions\n", "Each stem/noise is padded/truncated to 160,000 samples and mixed into one 1D waveform.\n", "So final mix shape before feature extraction is **(160000,)**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "sr = 16000\n", "duration = 10\n", "n = sr * duration # 160000\n", "stems = [np.zeros(n) for _ in range(4)]\n", "noise = np.zeros(n)\n", "mix = sum(stems) + 0.2 * noise\n", "print(mix.shape)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer Q2:** `(160000,)` (option: **(160000,)**)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q3) Hugging Face Feature Extractor Shape\n", "Using AST feature extractor with a 10s/16k input,\n", "`input_values` becomes `[1, 1024, 128]`; after `.squeeze(0)` it is **[1024, 128]**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If needed first: pip install transformers torch\n", "import numpy as np\n", "from transformers import AutoFeatureExtractor\n", "\n", "extractor = AutoFeatureExtractor.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593')\n", "mix = np.ones(160000)\n", "input_values = extractor(mix, sampling_rate=16000, return_tensors='pt')['input_values']\n", "print('before squeeze:', tuple(input_values.shape))\n", "print('after squeeze:', tuple(input_values.squeeze(0).shape))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer Q3:** `[1024, 128]` (option: **[1024, 128]**)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q4) AST Model Initialization + Trainable Params\n", "Initialize with `num_labels=10` and `ignore_mismatched_sizes=True`, then count trainable parameters.\n", "Expected value (from assignment options): **86196490**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If needed first: pip install transformers torch\n", "from transformers import ASTForAudioClassification\n", "\n", "model = ASTForAudioClassification.from_pretrained(\n", " 'MIT/ast-finetuned-audioset-10-10-0.4593',\n", " num_labels=10,\n", " ignore_mismatched_sizes=True\n", ")\n", "trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)\n", "print(trainable_params)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer Q4:** `86196490` (option: **86196490**)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q5) Inference Normalization Math\n", "Given `y = y / (np.max(np.abs(y)) + 1e-9)` and `y_test = [-0.85, 0.40, 0.20, -0.10]`,\n", "the value at index 0 is **-1.000** (rounded to 3 decimals)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "y_test = np.array([-0.85, 0.40, 0.20, -0.10])\n", "y_norm = y_test / (np.max(np.abs(y_test)) + 1e-9)\n", "print(y_norm)\n", "print('index 0 rounded:', round(float(y_norm[0]), 3))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer Q5:** `-1.000` (option: **-1.000**)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## Final Answer Summary (for form)\n", "1. **Q1:** 200\n", "2. **Q2:** (160000,)\n", "3. **Q3:** [1024, 128]\n", "4. **Q4:** 86196490\n", "5. **Q5:** -1.000\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.x" } }, "nbformat": 4, "nbformat_minor": 5 }