File size: 5,787 Bytes
c39aaca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
{
  "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
}