Jating4you commited on
Commit
c39aaca
·
verified ·
1 Parent(s): a9d6696

Add Milestone_5.ipynb

Browse files
Files changed (1) hide show
  1. Milestone_5.ipynb +196 -0
Milestone_5.ipynb ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Milestone 5 - DL&GenAI Project Course\n",
8
+ "**Roll/Email:** 21f2000735@ds.study.iitm.ac.in\n",
9
+ "\n",
10
+ "This notebook contains the working steps and final answers for Q1-Q5."
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "markdown",
15
+ "metadata": {},
16
+ "source": [
17
+ "## Q1) Dataset Splitting Strategy\n",
18
+ "100 recipes per genre \u00d7 10 genres = 1000 total recipes.\n",
19
+ "Validation split = 20% \u21d2 **200** items."
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": null,
25
+ "metadata": {},
26
+ "outputs": [],
27
+ "source": [
28
+ "from sklearn.model_selection import train_test_split\n",
29
+ "recipes = [{'genre': g, 'idx': i} for g in range(10) for i in range(100)]\n",
30
+ "train_recipes, val_recipes = train_test_split(recipes, test_size=0.2, shuffle=True, random_state=42)\n",
31
+ "print('Total:', len(recipes), 'Validation:', len(val_recipes))\n"
32
+ ]
33
+ },
34
+ {
35
+ "cell_type": "markdown",
36
+ "metadata": {},
37
+ "source": [
38
+ "**Answer Q1:** `200` (option: **200**)\n"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "markdown",
43
+ "metadata": {},
44
+ "source": [
45
+ "## Q2) On-the-Fly Mixing Dimensions\n",
46
+ "Each stem/noise is padded/truncated to 160,000 samples and mixed into one 1D waveform.\n",
47
+ "So final mix shape before feature extraction is **(160000,)**."
48
+ ]
49
+ },
50
+ {
51
+ "cell_type": "code",
52
+ "execution_count": null,
53
+ "metadata": {},
54
+ "outputs": [],
55
+ "source": [
56
+ "import numpy as np\n",
57
+ "sr = 16000\n",
58
+ "duration = 10\n",
59
+ "n = sr * duration # 160000\n",
60
+ "stems = [np.zeros(n) for _ in range(4)]\n",
61
+ "noise = np.zeros(n)\n",
62
+ "mix = sum(stems) + 0.2 * noise\n",
63
+ "print(mix.shape)\n"
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "markdown",
68
+ "metadata": {},
69
+ "source": [
70
+ "**Answer Q2:** `(160000,)` (option: **(160000,)**)\n"
71
+ ]
72
+ },
73
+ {
74
+ "cell_type": "markdown",
75
+ "metadata": {},
76
+ "source": [
77
+ "## Q3) Hugging Face Feature Extractor Shape\n",
78
+ "Using AST feature extractor with a 10s/16k input,\n",
79
+ "`input_values` becomes `[1, 1024, 128]`; after `.squeeze(0)` it is **[1024, 128]**."
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": null,
85
+ "metadata": {},
86
+ "outputs": [],
87
+ "source": [
88
+ "# If needed first: pip install transformers torch\n",
89
+ "import numpy as np\n",
90
+ "from transformers import AutoFeatureExtractor\n",
91
+ "\n",
92
+ "extractor = AutoFeatureExtractor.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593')\n",
93
+ "mix = np.ones(160000)\n",
94
+ "input_values = extractor(mix, sampling_rate=16000, return_tensors='pt')['input_values']\n",
95
+ "print('before squeeze:', tuple(input_values.shape))\n",
96
+ "print('after squeeze:', tuple(input_values.squeeze(0).shape))\n"
97
+ ]
98
+ },
99
+ {
100
+ "cell_type": "markdown",
101
+ "metadata": {},
102
+ "source": [
103
+ "**Answer Q3:** `[1024, 128]` (option: **[1024, 128]**)\n"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "markdown",
108
+ "metadata": {},
109
+ "source": [
110
+ "## Q4) AST Model Initialization + Trainable Params\n",
111
+ "Initialize with `num_labels=10` and `ignore_mismatched_sizes=True`, then count trainable parameters.\n",
112
+ "Expected value (from assignment options): **86196490**."
113
+ ]
114
+ },
115
+ {
116
+ "cell_type": "code",
117
+ "execution_count": null,
118
+ "metadata": {},
119
+ "outputs": [],
120
+ "source": [
121
+ "# If needed first: pip install transformers torch\n",
122
+ "from transformers import ASTForAudioClassification\n",
123
+ "\n",
124
+ "model = ASTForAudioClassification.from_pretrained(\n",
125
+ " 'MIT/ast-finetuned-audioset-10-10-0.4593',\n",
126
+ " num_labels=10,\n",
127
+ " ignore_mismatched_sizes=True\n",
128
+ ")\n",
129
+ "trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)\n",
130
+ "print(trainable_params)\n"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "markdown",
135
+ "metadata": {},
136
+ "source": [
137
+ "**Answer Q4:** `86196490` (option: **86196490**)\n"
138
+ ]
139
+ },
140
+ {
141
+ "cell_type": "markdown",
142
+ "metadata": {},
143
+ "source": [
144
+ "## Q5) Inference Normalization Math\n",
145
+ "Given `y = y / (np.max(np.abs(y)) + 1e-9)` and `y_test = [-0.85, 0.40, 0.20, -0.10]`,\n",
146
+ "the value at index 0 is **-1.000** (rounded to 3 decimals)."
147
+ ]
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "execution_count": null,
152
+ "metadata": {},
153
+ "outputs": [],
154
+ "source": [
155
+ "import numpy as np\n",
156
+ "y_test = np.array([-0.85, 0.40, 0.20, -0.10])\n",
157
+ "y_norm = y_test / (np.max(np.abs(y_test)) + 1e-9)\n",
158
+ "print(y_norm)\n",
159
+ "print('index 0 rounded:', round(float(y_norm[0]), 3))\n"
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "markdown",
164
+ "metadata": {},
165
+ "source": [
166
+ "**Answer Q5:** `-1.000` (option: **-1.000**)\n"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "markdown",
171
+ "metadata": {},
172
+ "source": [
173
+ "---\n",
174
+ "## Final Answer Summary (for form)\n",
175
+ "1. **Q1:** 200\n",
176
+ "2. **Q2:** (160000,)\n",
177
+ "3. **Q3:** [1024, 128]\n",
178
+ "4. **Q4:** 86196490\n",
179
+ "5. **Q5:** -1.000\n"
180
+ ]
181
+ }
182
+ ],
183
+ "metadata": {
184
+ "kernelspec": {
185
+ "display_name": "Python 3",
186
+ "language": "python",
187
+ "name": "python3"
188
+ },
189
+ "language_info": {
190
+ "name": "python",
191
+ "version": "3.x"
192
+ }
193
+ },
194
+ "nbformat": 4,
195
+ "nbformat_minor": 5
196
+ }