Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- 03A_deep_learning_project.ipynb +586 -0
- 03B_pytorch_project.ipynb +0 -0
- 04_model_usage_project.ipynb +824 -0
- README.md +3 -9
- app.py +576 -64
- emotion-classifier/runs/Feb23_03-09-15_LAPTOP-VKSJJT34/events.out.tfevents.1740255001.LAPTOP-VKSJJT34.10956.0 +3 -0
- emotion-classifier/runs/Feb23_03-21-00_LAPTOP-VKSJJT34/events.out.tfevents.1740255792.LAPTOP-VKSJJT34.10956.1 +3 -0
- emotion-classifier/runs/Feb23_03-27-01_LAPTOP-VKSJJT34/events.out.tfevents.1740256033.LAPTOP-VKSJJT34.21776.0 +3 -0
- nyoba-03A.ipynb +485 -0
- nyoba-03B.ipynb +536 -0
- nyoba-04.ipynb +0 -0
- spam.csv +0 -0
03A_deep_learning_project.ipynb
ADDED
@@ -0,0 +1,586 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 |
+
"**Instructions for Students:**\n",
|
19 |
+
"\n",
|
20 |
+
"Please carefully follow these steps to complete and submit your project:\n",
|
21 |
+
"\n",
|
22 |
+
"1. **Make a copy of the Project**: Please make a copy of this project either to your own Google Drive or download locally. Work on the copy of the project. The master project is **Read-Only**, meaning you can edit, but it will not be saved when you close the master project. To avoid total loss of your work, remember to make a copy.\n",
|
23 |
+
"\n",
|
24 |
+
"2. **Completing the Project**: You are required to work on and complete all tasks in the provided project. Be disciplined and ensure that you thoroughly engage with each task.\n",
|
25 |
+
" \n",
|
26 |
+
"3. **Creating a Google Drive Folder**: Each of you must create a new folder on your Google Drive. This will be the repository for all your completed project files, aiding you in keeping your work organized and accessible.\n",
|
27 |
+
" \n",
|
28 |
+
"4. **Uploading Completed Project**: Upon completion of your project, make sure to upload all necessary files, involving codes, reports, and related documents into the created Google Drive folder. Save this link in the 'Student Identity' section and also provide it as the last parameter in the `submit` function that has been provided.\n",
|
29 |
+
" \n",
|
30 |
+
"5. **Sharing Folder Link**: You're required to share the link to your project Google Drive folder. This is crucial for the submission and evaluation of your project.\n",
|
31 |
+
" \n",
|
32 |
+
"6. **Setting Permission to Public**: Please make sure your Google Drive folder is set to public. This allows your instructor to access your solutions and assess your work correctly.\n",
|
33 |
+
"\n",
|
34 |
+
"Adhering to these procedures will facilitate a smooth project evaluation process for you and the reviewers."
|
35 |
+
]
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"cell_type": "markdown",
|
39 |
+
"metadata": {
|
40 |
+
"id": "XeK2fKDrZ0-s"
|
41 |
+
},
|
42 |
+
"source": [
|
43 |
+
"## Project Description\n",
|
44 |
+
"\n",
|
45 |
+
"The Deep Learning Projects are divided into two parts, the first is the Calculations worth 30% in this notebook and the second one is Pytorch Project worth 70%.\n",
|
46 |
+
"\n",
|
47 |
+
"The two projects will help you gain experience to learn about Deep Learning in detail.\n",
|
48 |
+
"\n",
|
49 |
+
"Happy coding!"
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "markdown",
|
54 |
+
"metadata": {
|
55 |
+
"id": "Lgd8szRCZ0-t"
|
56 |
+
},
|
57 |
+
"source": [
|
58 |
+
"## Grading Criteria\n",
|
59 |
+
"\n",
|
60 |
+
"There are 4 primary tasks in this project, all have the same weight. Each task will give you either 100 point if you are correct and 0 if you are wrong. The final score for the project will the the average of all 4 tasks.\n",
|
61 |
+
"\n",
|
62 |
+
"There is also an optional task (Task 5) that you can do to challenge your understanding.\n",
|
63 |
+
"\n",
|
64 |
+
"* Task 1: This task will assess your ability to do basic matrix multiplication which is an important part in machine and deep learning.\n",
|
65 |
+
"\n",
|
66 |
+
"* Task 2: This task will assess your ability to understand how a neural network work through weight and biases.\n",
|
67 |
+
"\n",
|
68 |
+
"* Task 3: The task will assess your ability to understand how a neural network layer works.\n",
|
69 |
+
"\n",
|
70 |
+
"* Task 4: This task will assess your ability to understand how backpropagation works in a neural network.\n",
|
71 |
+
"\n",
|
72 |
+
"* Task 5 (optional): This task will assess your ability to understand how to calculate cost and why it's important by using backpropagation in a neural network.\n"
|
73 |
+
]
|
74 |
+
},
|
75 |
+
{
|
76 |
+
"cell_type": "markdown",
|
77 |
+
"metadata": {
|
78 |
+
"id": "W1dT1yHmZ0-t"
|
79 |
+
},
|
80 |
+
"source": [
|
81 |
+
"## Student Identity"
|
82 |
+
]
|
83 |
+
},
|
84 |
+
{
|
85 |
+
"cell_type": "code",
|
86 |
+
"execution_count": null,
|
87 |
+
"metadata": {
|
88 |
+
"id": "-Vysq0mbZ0-t"
|
89 |
+
},
|
90 |
+
"outputs": [],
|
91 |
+
"source": [
|
92 |
+
"# @title #### Student Identity\n",
|
93 |
+
"student_id = \"\" # @param {type:\"string\"}\n",
|
94 |
+
"name = \"\" # @param {type:\"string\"}\n",
|
95 |
+
"drive_link = \"\" # @param {type:\"string\"}"
|
96 |
+
]
|
97 |
+
},
|
98 |
+
{
|
99 |
+
"cell_type": "markdown",
|
100 |
+
"metadata": {
|
101 |
+
"id": "TuVolX9FZ0-u"
|
102 |
+
},
|
103 |
+
"source": [
|
104 |
+
"## Import package"
|
105 |
+
]
|
106 |
+
},
|
107 |
+
{
|
108 |
+
"cell_type": "code",
|
109 |
+
"execution_count": null,
|
110 |
+
"metadata": {
|
111 |
+
"id": "Pl3QEX2HZ0-u"
|
112 |
+
},
|
113 |
+
"outputs": [],
|
114 |
+
"source": [
|
115 |
+
"!pip install fastbook\n",
|
116 |
+
"\n",
|
117 |
+
"!pip install rggrader\n",
|
118 |
+
"from rggrader import submit"
|
119 |
+
]
|
120 |
+
},
|
121 |
+
{
|
122 |
+
"cell_type": "markdown",
|
123 |
+
"metadata": {
|
124 |
+
"id": "4gtGSA8SZ0-v"
|
125 |
+
},
|
126 |
+
"source": [
|
127 |
+
"\n",
|
128 |
+
"## Task 1\n",
|
129 |
+
"\n",
|
130 |
+
"Given\n",
|
131 |
+
"\n",
|
132 |
+
"$$\n",
|
133 |
+
"X = \\begin{bmatrix}\n",
|
134 |
+
"a \\\\\n",
|
135 |
+
"b \\\\\n",
|
136 |
+
"c \\\\\n",
|
137 |
+
"d \\\\\n",
|
138 |
+
"e\n",
|
139 |
+
"\\end{bmatrix}\n",
|
140 |
+
"$$\n",
|
141 |
+
"\n",
|
142 |
+
"and\n",
|
143 |
+
"\n",
|
144 |
+
"$$\n",
|
145 |
+
"M =\n",
|
146 |
+
"\\begin{bmatrix}\n",
|
147 |
+
"-2 & 3 & 3 & 3 & -4 \\\\\n",
|
148 |
+
"0 & -4 & -1 & 1 & 2 \\\\\n",
|
149 |
+
"1 & 5 & 4 & 2 & 0 \\\\\n",
|
150 |
+
"-2 & 5 & -5 & 3 & 1 \\\\\n",
|
151 |
+
"-3 & 2 & 4 & 3 & 4 \\\\\n",
|
152 |
+
"\\end{bmatrix}\n",
|
153 |
+
"$$\n",
|
154 |
+
"\n",
|
155 |
+
"and\n",
|
156 |
+
"\n",
|
157 |
+
"$$\n",
|
158 |
+
"M \\cdot X =\n",
|
159 |
+
" =\n",
|
160 |
+
"\\begin{bmatrix}\n",
|
161 |
+
"-33 \\\\\n",
|
162 |
+
"9 \\\\\n",
|
163 |
+
"-34 \\\\\n",
|
164 |
+
"-38 \\\\\n",
|
165 |
+
" -40\n",
|
166 |
+
"\\end{bmatrix}\n",
|
167 |
+
"$$\n",
|
168 |
+
"\n",
|
169 |
+
"What is the value of $a + b + c + d + e$?"
|
170 |
+
]
|
171 |
+
},
|
172 |
+
{
|
173 |
+
"cell_type": "code",
|
174 |
+
"execution_count": null,
|
175 |
+
"metadata": {
|
176 |
+
"id": "JCwFuKpEZ0-v"
|
177 |
+
},
|
178 |
+
"outputs": [],
|
179 |
+
"source": [
|
180 |
+
"# You may add any code here to derive your variables\n",
|
181 |
+
"# Please change this\n",
|
182 |
+
"a = 0\n",
|
183 |
+
"b = 0\n",
|
184 |
+
"c = 0\n",
|
185 |
+
"d = 0\n",
|
186 |
+
"e = 0\n",
|
187 |
+
"total = a + b + c + d + e\n",
|
188 |
+
"\n",
|
189 |
+
"print(f\"The total is {total}\")\n",
|
190 |
+
"\n",
|
191 |
+
"assignment_id = \"09-deep-learning-project\"\n",
|
192 |
+
"question_id = \"q1_linear_algebra\"\n",
|
193 |
+
"submit(student_id, name, assignment_id, str(total), question_id, drive_link)\n"
|
194 |
+
]
|
195 |
+
},
|
196 |
+
{
|
197 |
+
"cell_type": "markdown",
|
198 |
+
"metadata": {
|
199 |
+
"id": "AnzwjHI5Z0-v"
|
200 |
+
},
|
201 |
+
"source": [
|
202 |
+
"## Task 2\n",
|
203 |
+
"\n",
|
204 |
+
"What is the output of the following Neural Network?"
|
205 |
+
]
|
206 |
+
},
|
207 |
+
{
|
208 |
+
"cell_type": "code",
|
209 |
+
"execution_count": null,
|
210 |
+
"metadata": {
|
211 |
+
"id": "5Dhvs1l-Z0-v",
|
212 |
+
"outputId": "8fec2c6f-eb16-4791-bcee-d3414a8b7fab"
|
213 |
+
},
|
214 |
+
"outputs": [
|
215 |
+
{
|
216 |
+
"data": {
|
217 |
+
"image/svg+xml": [
|
218 |
+
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
|
219 |
+
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
|
220 |
+
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
|
221 |
+
"<!-- Generated by graphviz version 8.1.0 (20230707.0739)\n",
|
222 |
+
" -->\n",
|
223 |
+
"<!-- Title: G Pages: 1 -->\n",
|
224 |
+
"<svg width=\"452pt\" height=\"132pt\"\n",
|
225 |
+
" viewBox=\"0.00 0.00 451.92 132.40\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
|
226 |
+
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 128.4)\">\n",
|
227 |
+
"<title>G</title>\n",
|
228 |
+
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-128.4 447.92,-128.4 447.92,4 -4,4\"/>\n",
|
229 |
+
"<!-- z_1 -->\n",
|
230 |
+
"<g id=\"node1\" class=\"node\">\n",
|
231 |
+
"<title>z_1</title>\n",
|
232 |
+
"<polygon fill=\"none\" stroke=\"black\" points=\"209.5,-121.4 123.25,-121.4 119.25,-117.4 119.25,-71 205.5,-71 209.5,-75 209.5,-121.4\"/>\n",
|
233 |
+
"<polyline fill=\"none\" stroke=\"black\" points=\"205.5,-117.4 119.25,-117.4\"/>\n",
|
234 |
+
"<polyline fill=\"none\" stroke=\"black\" points=\"205.5,-117.4 205.5,-71\"/>\n",
|
235 |
+
"<polyline fill=\"none\" stroke=\"black\" points=\"205.5,-117.4 209.5,-121.4\"/>\n",
|
236 |
+
"<text text-anchor=\"middle\" x=\"164.38\" y=\"-91.15\" font-family=\"Times,serif\" font-size=\"14.00\">b = -2, ReLU</text>\n",
|
237 |
+
"</g>\n",
|
238 |
+
"<!-- z_3 -->\n",
|
239 |
+
"<g id=\"node3\" class=\"node\">\n",
|
240 |
+
"<title>z_3</title>\n",
|
241 |
+
"<polygon fill=\"none\" stroke=\"black\" points=\"338,-87.4 256.25,-87.4 252.25,-83.4 252.25,-37 334,-37 338,-41 338,-87.4\"/>\n",
|
242 |
+
"<polyline fill=\"none\" stroke=\"black\" points=\"334,-83.4 252.25,-83.4\"/>\n",
|
243 |
+
"<polyline fill=\"none\" stroke=\"black\" points=\"334,-83.4 334,-37\"/>\n",
|
244 |
+
"<polyline fill=\"none\" stroke=\"black\" points=\"334,-83.4 338,-87.4\"/>\n",
|
245 |
+
"<text text-anchor=\"middle\" x=\"295.12\" y=\"-57.15\" font-family=\"Times,serif\" font-size=\"14.00\">b = 4, ReLU</text>\n",
|
246 |
+
"</g>\n",
|
247 |
+
"<!-- z_1->z_3 -->\n",
|
248 |
+
"<g id=\"edge5\" class=\"edge\">\n",
|
249 |
+
"<title>z_1->z_3</title>\n",
|
250 |
+
"<path fill=\"none\" stroke=\"black\" d=\"M209.68,-84.5C219.94,-81.79 230.95,-78.88 241.52,-76.09\"/>\n",
|
251 |
+
"<polygon fill=\"black\" stroke=\"black\" points=\"242.13,-79.29 250.91,-73.35 240.34,-72.52 242.13,-79.29\"/>\n",
|
252 |
+
"<text text-anchor=\"middle\" x=\"230.88\" y=\"-82.4\" font-family=\"Times,serif\" font-size=\"14.00\">2</text>\n",
|
253 |
+
"</g>\n",
|
254 |
+
"<!-- z_2 -->\n",
|
255 |
+
"<g id=\"node2\" class=\"node\">\n",
|
256 |
+
"<title>z_2</title>\n",
|
257 |
+
"<polygon fill=\"none\" stroke=\"black\" points=\"209.5,-53.4 123.25,-53.4 119.25,-49.4 119.25,-3 205.5,-3 209.5,-7 209.5,-53.4\"/>\n",
|
258 |
+
"<polyline fill=\"none\" stroke=\"black\" points=\"205.5,-49.4 119.25,-49.4\"/>\n",
|
259 |
+
"<polyline fill=\"none\" stroke=\"black\" points=\"205.5,-49.4 205.5,-3\"/>\n",
|
260 |
+
"<polyline fill=\"none\" stroke=\"black\" points=\"205.5,-49.4 209.5,-53.4\"/>\n",
|
261 |
+
"<text text-anchor=\"middle\" x=\"164.38\" y=\"-23.15\" font-family=\"Times,serif\" font-size=\"14.00\">b = -6, ReLU</text>\n",
|
262 |
+
"</g>\n",
|
263 |
+
"<!-- z_2->z_3 -->\n",
|
264 |
+
"<g id=\"edge6\" class=\"edge\">\n",
|
265 |
+
"<title>z_2->z_3</title>\n",
|
266 |
+
"<path fill=\"none\" stroke=\"black\" d=\"M209.68,-39.9C219.94,-42.61 230.95,-45.52 241.52,-48.31\"/>\n",
|
267 |
+
"<polygon fill=\"black\" stroke=\"black\" points=\"240.34,-51.88 250.91,-51.05 242.13,-45.11 240.34,-51.88\"/>\n",
|
268 |
+
"<text text-anchor=\"middle\" x=\"230.88\" y=\"-49.4\" font-family=\"Times,serif\" font-size=\"14.00\">3</text>\n",
|
269 |
+
"</g>\n",
|
270 |
+
"<!-- output -->\n",
|
271 |
+
"<g id=\"node6\" class=\"node\">\n",
|
272 |
+
"<title>output</title>\n",
|
273 |
+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"409.46\" cy=\"-62.2\" rx=\"34.46\" ry=\"18\"/>\n",
|
274 |
+
"<text text-anchor=\"middle\" x=\"409.46\" y=\"-57.15\" font-family=\"Times,serif\" font-size=\"14.00\">output</text>\n",
|
275 |
+
"</g>\n",
|
276 |
+
"<!-- z_3->output -->\n",
|
277 |
+
"<g id=\"edge7\" class=\"edge\">\n",
|
278 |
+
"<title>z_3->output</title>\n",
|
279 |
+
"<path fill=\"none\" stroke=\"black\" d=\"M338.27,-62.2C346.67,-62.2 355.52,-62.2 363.99,-62.2\"/>\n",
|
280 |
+
"<polygon fill=\"black\" stroke=\"black\" points=\"363.85,-65.7 373.85,-62.2 363.85,-58.7 363.85,-65.7\"/>\n",
|
281 |
+
"</g>\n",
|
282 |
+
"<!-- input_1 -->\n",
|
283 |
+
"<g id=\"node4\" class=\"node\">\n",
|
284 |
+
"<title>input_1</title>\n",
|
285 |
+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"36\" cy=\"-99.2\" rx=\"36\" ry=\"25.2\"/>\n",
|
286 |
+
"<text text-anchor=\"middle\" x=\"36\" y=\"-94.15\" font-family=\"Times,serif\" font-size=\"14.00\">4</text>\n",
|
287 |
+
"</g>\n",
|
288 |
+
"<!-- input_1->z_1 -->\n",
|
289 |
+
"<g id=\"edge1\" class=\"edge\">\n",
|
290 |
+
"<title>input_1->z_1</title>\n",
|
291 |
+
"<path fill=\"none\" stroke=\"black\" d=\"M72.35,-99.96C78.24,-100.06 84.29,-100.15 90,-100.2 95,-100.25 96.25,-100.36 101.25,-100.2 103.49,-100.13 105.77,-100.04 108.07,-99.94\"/>\n",
|
292 |
+
"<polygon fill=\"black\" stroke=\"black\" points=\"108.12,-103.4 117.94,-99.41 107.78,-96.4 108.12,-103.4\"/>\n",
|
293 |
+
"<text text-anchor=\"middle\" x=\"95.62\" y=\"-103.4\" font-family=\"Times,serif\" font-size=\"14.00\">4</text>\n",
|
294 |
+
"</g>\n",
|
295 |
+
"<!-- input_1->z_2 -->\n",
|
296 |
+
"<g id=\"edge3\" class=\"edge\">\n",
|
297 |
+
"<title>input_1->z_2</title>\n",
|
298 |
+
"<path fill=\"none\" stroke=\"black\" d=\"M64.73,-83.62C77.85,-76.25 93.98,-67.19 109.37,-58.54\"/>\n",
|
299 |
+
"<polygon fill=\"black\" stroke=\"black\" points=\"110.82,-61.18 117.82,-53.23 107.39,-55.08 110.82,-61.18\"/>\n",
|
300 |
+
"<text text-anchor=\"middle\" x=\"95.62\" y=\"-70.4\" font-family=\"Times,serif\" font-size=\"14.00\">0</text>\n",
|
301 |
+
"</g>\n",
|
302 |
+
"<!-- input_2 -->\n",
|
303 |
+
"<g id=\"node5\" class=\"node\">\n",
|
304 |
+
"<title>input_2</title>\n",
|
305 |
+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"36\" cy=\"-25.2\" rx=\"36\" ry=\"25.2\"/>\n",
|
306 |
+
"<text text-anchor=\"middle\" x=\"36\" y=\"-20.15\" font-family=\"Times,serif\" font-size=\"14.00\">-3</text>\n",
|
307 |
+
"</g>\n",
|
308 |
+
"<!-- input_2->z_1 -->\n",
|
309 |
+
"<g id=\"edge2\" class=\"edge\">\n",
|
310 |
+
"<title>input_2->z_1</title>\n",
|
311 |
+
"<path fill=\"none\" stroke=\"black\" d=\"M71.04,-31.49C81.21,-34.28 92.09,-38.21 101.25,-43.7 111.09,-49.6 110.68,-54.57 119.25,-62.2 119.85,-62.73 120.45,-63.26 121.06,-63.79\"/>\n",
|
312 |
+
"<polygon fill=\"black\" stroke=\"black\" points=\"118.35,-66.93 128.25,-70.69 122.85,-61.57 118.35,-66.93\"/>\n",
|
313 |
+
"<text text-anchor=\"middle\" x=\"95.62\" y=\"-46.4\" font-family=\"Times,serif\" font-size=\"14.00\">2</text>\n",
|
314 |
+
"</g>\n",
|
315 |
+
"<!-- input_2->z_2 -->\n",
|
316 |
+
"<g id=\"edge4\" class=\"edge\">\n",
|
317 |
+
"<title>input_2->z_2</title>\n",
|
318 |
+
"<path fill=\"none\" stroke=\"black\" d=\"M66.37,-10.95C73.9,-7.96 82.11,-5.24 90,-3.7 95.93,-2.54 102.05,-2.55 108.09,-3.35\"/>\n",
|
319 |
+
"<polygon fill=\"black\" stroke=\"black\" points=\"107.36,-6.97 117.86,-5.55 108.76,-0.12 107.36,-6.97\"/>\n",
|
320 |
+
"<text text-anchor=\"middle\" x=\"95.62\" y=\"-6.4\" font-family=\"Times,serif\" font-size=\"14.00\">-1</text>\n",
|
321 |
+
"</g>\n",
|
322 |
+
"</g>\n",
|
323 |
+
"</svg>\n"
|
324 |
+
],
|
325 |
+
"text/plain": [
|
326 |
+
"<graphviz.sources.Source at 0x28f274710>"
|
327 |
+
]
|
328 |
+
},
|
329 |
+
"execution_count": 22,
|
330 |
+
"metadata": {},
|
331 |
+
"output_type": "execute_result"
|
332 |
+
}
|
333 |
+
],
|
334 |
+
"source": [
|
335 |
+
"from fastbook import *\n",
|
336 |
+
"\n",
|
337 |
+
"\n",
|
338 |
+
"# Draw neurons with multiple inputs and weights\n",
|
339 |
+
"gv('''\n",
|
340 |
+
"z_1[shape=box3d width=1 height=0.7 label=\"b = -2, ReLU\"];\n",
|
341 |
+
"z_2[shape=box3d width=1 height=0.7 label=\"b = -6, ReLU\"];\n",
|
342 |
+
"z_3[shape=box3d width=1 height=0.7 label=\"b = 4, ReLU\"];\n",
|
343 |
+
"input_1[width=1 height=0.7 label=\"4\"];\n",
|
344 |
+
"input_2[width=1 height=0.7 label=\"-3\"];\n",
|
345 |
+
"input_1 -> z_1 [label=\"4\"]\n",
|
346 |
+
"input_2 -> z_1 [label=\"2\"]\n",
|
347 |
+
"input_1 -> z_2 [label=\"0\"]\n",
|
348 |
+
"input_2 -> z_2 [label=\"-1\"]\n",
|
349 |
+
"\n",
|
350 |
+
"z_1 -> z_3 [label=\"2\"]\n",
|
351 |
+
"z_2 -> z_3 [label=\"3\"]\n",
|
352 |
+
"z_3 ->output\n",
|
353 |
+
"\n",
|
354 |
+
"\n",
|
355 |
+
"''')\n"
|
356 |
+
]
|
357 |
+
},
|
358 |
+
{
|
359 |
+
"cell_type": "code",
|
360 |
+
"execution_count": null,
|
361 |
+
"metadata": {
|
362 |
+
"id": "jtzFBQeIZ0-w"
|
363 |
+
},
|
364 |
+
"outputs": [],
|
365 |
+
"source": [
|
366 |
+
"# You may add any code here to derive your variables\n",
|
367 |
+
"# Please change this\n",
|
368 |
+
"output = 0\n",
|
369 |
+
"\n",
|
370 |
+
"print(f\"The output is {output}\")\n",
|
371 |
+
"\n",
|
372 |
+
"assignment_id = \"09-deep-learning-project\"\n",
|
373 |
+
"question_id = \"q2_simple_neural_network\"\n",
|
374 |
+
"submit(student_id, name, assignment_id, str(output), question_id, drive_link)\n"
|
375 |
+
]
|
376 |
+
},
|
377 |
+
{
|
378 |
+
"cell_type": "markdown",
|
379 |
+
"metadata": {
|
380 |
+
"id": "nw5Rc4_QZ0-w"
|
381 |
+
},
|
382 |
+
"source": [
|
383 |
+
"## Task 3\n",
|
384 |
+
"\n",
|
385 |
+
"Given the following Neural Networks:\n",
|
386 |
+
"\n",
|
387 |
+
"First layer:\n",
|
388 |
+
"\n",
|
389 |
+
"$$\n",
|
390 |
+
"W = \\begin{bmatrix}\n",
|
391 |
+
"0.23 & 0.67 & 0.12 \\\\\n",
|
392 |
+
"-0.89 & -0.45 & 0.78 \\\\\n",
|
393 |
+
"0.34 & 0.56 & -0.90 \\\\\n",
|
394 |
+
"-0.12 & 0.34 & 0.56 \\\\\n",
|
395 |
+
"0.78 & -0.90 & 0.23 \\\\\n",
|
396 |
+
"\\end{bmatrix}\n",
|
397 |
+
"$$\n",
|
398 |
+
"\n",
|
399 |
+
"$$\n",
|
400 |
+
"bias = \\begin{bmatrix}\n",
|
401 |
+
"0.23 \\\\\n",
|
402 |
+
"-0.89 \\\\\n",
|
403 |
+
"0.34 \\\\\n",
|
404 |
+
"0.12 \\\\\n",
|
405 |
+
"-0.78 \\\\\n",
|
406 |
+
"\\end{bmatrix}\n",
|
407 |
+
"$$\n",
|
408 |
+
"\n",
|
409 |
+
"Second layer:\n",
|
410 |
+
"\n",
|
411 |
+
"$$\n",
|
412 |
+
"W = \\begin{bmatrix}\n",
|
413 |
+
"0.23 & 0.67 & 0.12 & 0.45 & 0.89 \\\\\n",
|
414 |
+
"0.12 & 0.34 & 0.56 & 0.78 & 0.90 \\\\\n",
|
415 |
+
"\\end{bmatrix}\n",
|
416 |
+
"$$\n",
|
417 |
+
"\n",
|
418 |
+
"$$\n",
|
419 |
+
"bias = \\begin{bmatrix}\n",
|
420 |
+
"1.96 \\\\\n",
|
421 |
+
"-1.08 \\\\\n",
|
422 |
+
"\\end{bmatrix}\n",
|
423 |
+
"$$\n",
|
424 |
+
"\n",
|
425 |
+
"Third layer:\n",
|
426 |
+
"$$\n",
|
427 |
+
"W = \\begin{bmatrix}\n",
|
428 |
+
"1.08 & -0.16\n",
|
429 |
+
"\\end{bmatrix}\n",
|
430 |
+
"$$\n",
|
431 |
+
"\n",
|
432 |
+
"$$\n",
|
433 |
+
"bias = \\begin{bmatrix}\n",
|
434 |
+
"-2.8\n",
|
435 |
+
"\\end{bmatrix}\n",
|
436 |
+
"$$\n",
|
437 |
+
"\n",
|
438 |
+
"All layers use the ReLU activation function.\n",
|
439 |
+
"\n",
|
440 |
+
"What is the output given the following inputs?\n",
|
441 |
+
"\n",
|
442 |
+
"$$\n",
|
443 |
+
"X = \\begin{bmatrix}\n",
|
444 |
+
"1 \\\\\n",
|
445 |
+
"2 \\\\\n",
|
446 |
+
"4 \\\\\n",
|
447 |
+
"\\end{bmatrix}\n",
|
448 |
+
"\n"
|
449 |
+
]
|
450 |
+
},
|
451 |
+
{
|
452 |
+
"cell_type": "code",
|
453 |
+
"execution_count": null,
|
454 |
+
"metadata": {
|
455 |
+
"id": "4d8Oibi1Z0-x"
|
456 |
+
},
|
457 |
+
"outputs": [],
|
458 |
+
"source": [
|
459 |
+
"# You may add any code here to derive your variables\n",
|
460 |
+
"\n",
|
461 |
+
"# Please change this\n",
|
462 |
+
"output = 0\n",
|
463 |
+
"\n",
|
464 |
+
"print(f\"The output is {output}\")\n",
|
465 |
+
"\n",
|
466 |
+
"assignment_id = \"09-deep-learning-project\"\n",
|
467 |
+
"question_id = \"q3_complex_neural_network\"\n",
|
468 |
+
"submit(student_id, name, assignment_id, str(output), question_id, drive_link)\n"
|
469 |
+
]
|
470 |
+
},
|
471 |
+
{
|
472 |
+
"cell_type": "markdown",
|
473 |
+
"metadata": {
|
474 |
+
"id": "Vn5o5XmfZ0-x"
|
475 |
+
},
|
476 |
+
"source": [
|
477 |
+
"## Task 4\n",
|
478 |
+
"\n",
|
479 |
+
"Given [the following sheet (sheet name = Project A)](https://docs.google.com/spreadsheets/d/15JWbRFB4k5CNcfD-2hduHHssXIc1SGhNVSpu_30wVAw/edit#gid=180755192)\n",
|
480 |
+
"\n",
|
481 |
+
"Make a backpropagation algorithm to train the network. You can refer to AND and NOT sheet. Use sigmoid activation function for all\n",
|
482 |
+
"\n",
|
483 |
+
"Hint: it needs 1 layer with 1 neuron\n",
|
484 |
+
"\n",
|
485 |
+
"What is the cost at 10th iteration (the iteration start from 1)?"
|
486 |
+
]
|
487 |
+
},
|
488 |
+
{
|
489 |
+
"cell_type": "code",
|
490 |
+
"execution_count": null,
|
491 |
+
"metadata": {
|
492 |
+
"id": "d2aSUbaaZ0-x"
|
493 |
+
},
|
494 |
+
"outputs": [],
|
495 |
+
"source": [
|
496 |
+
"# Please clone the Google Sheet and do your calculation there\n",
|
497 |
+
"\n",
|
498 |
+
"# Please change this\n",
|
499 |
+
"cost = 0\n",
|
500 |
+
"\n",
|
501 |
+
"# Don't forget to fill in the link to your Google Sheet\n",
|
502 |
+
"link_to_gsheet = \"\"\n",
|
503 |
+
"\n",
|
504 |
+
"print(f\"The cost is {cost}\")\n",
|
505 |
+
"\n",
|
506 |
+
"assignment_id = \"09-deep-learning-project\"\n",
|
507 |
+
"\n",
|
508 |
+
"question_id = \"q4_cost_function_cost\"\n",
|
509 |
+
"submit(student_id, name, assignment_id, str(cost), question_id)\n",
|
510 |
+
"\n",
|
511 |
+
"question_id = \"q4_cost_function_gsheet\"\n",
|
512 |
+
"submit(student_id, name, assignment_id, str(link_to_gsheet), question_id, drive_link)\n"
|
513 |
+
]
|
514 |
+
},
|
515 |
+
{
|
516 |
+
"cell_type": "markdown",
|
517 |
+
"metadata": {
|
518 |
+
"id": "Kh7uzjtDZ0-x"
|
519 |
+
},
|
520 |
+
"source": [
|
521 |
+
"## Task 5 (Bonus - Optional)\n",
|
522 |
+
"\n",
|
523 |
+
"_This task is optional, it will earn you a distinction, but will not give you additional points._\n",
|
524 |
+
"\n",
|
525 |
+
"Given [the following sheet (sheet name = Project B)](https://docs.google.com/spreadsheets/d/15JWbRFB4k5CNcfD-2hduHHssXIc1SGhNVSpu_30wVAw/edit#gid=1029811725)\n",
|
526 |
+
"\n",
|
527 |
+
"Make a backpropagation algorithm to train the network. You can refer to AND and NOT sheet. Use sigmoid activation function for all\n",
|
528 |
+
"\n",
|
529 |
+
"Hint: it needs 2 layer (1 hidden layer + 1 output layer). The hidden layer has 2 neurons\n",
|
530 |
+
"\n",
|
531 |
+
"What is the cost at 10th iteration (the iteration start from 1)?"
|
532 |
+
]
|
533 |
+
},
|
534 |
+
{
|
535 |
+
"cell_type": "code",
|
536 |
+
"execution_count": null,
|
537 |
+
"metadata": {
|
538 |
+
"id": "KJJOi3V1Z0-x"
|
539 |
+
},
|
540 |
+
"outputs": [],
|
541 |
+
"source": [
|
542 |
+
"# Please clone the Google Sheet and do your calculation there\n",
|
543 |
+
"\n",
|
544 |
+
"# Please change this\n",
|
545 |
+
"cost = 0\n",
|
546 |
+
"\n",
|
547 |
+
"# Don't forget to fill in the link to your Google Sheet\n",
|
548 |
+
"link_to_gsheet = \"\"\n",
|
549 |
+
"\n",
|
550 |
+
"print(f\"The cost is {cost}\")\n",
|
551 |
+
"\n",
|
552 |
+
"assignment_id = \"09-deep-learning-project\"\n",
|
553 |
+
"\n",
|
554 |
+
"question_id = \"q5_cost_function_cost\"\n",
|
555 |
+
"submit(student_id, name, assignment_id, str(cost), question_id)\n",
|
556 |
+
"\n",
|
557 |
+
"question_id = \"q5_cost_function_gsheet\"\n",
|
558 |
+
"submit(student_id, name, assignment_id, str(link_to_gsheet), question_id, drive_link)\n"
|
559 |
+
]
|
560 |
+
}
|
561 |
+
],
|
562 |
+
"metadata": {
|
563 |
+
"kernelspec": {
|
564 |
+
"display_name": "Python 3 (ipykernel)",
|
565 |
+
"language": "python",
|
566 |
+
"name": "python3"
|
567 |
+
},
|
568 |
+
"language_info": {
|
569 |
+
"codemirror_mode": {
|
570 |
+
"name": "ipython",
|
571 |
+
"version": 3
|
572 |
+
},
|
573 |
+
"file_extension": ".py",
|
574 |
+
"mimetype": "text/x-python",
|
575 |
+
"name": "python",
|
576 |
+
"nbconvert_exporter": "python",
|
577 |
+
"pygments_lexer": "ipython3",
|
578 |
+
"version": "3.11.3"
|
579 |
+
},
|
580 |
+
"colab": {
|
581 |
+
"provenance": []
|
582 |
+
}
|
583 |
+
},
|
584 |
+
"nbformat": 4,
|
585 |
+
"nbformat_minor": 0
|
586 |
+
}
|
03B_pytorch_project.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
04_model_usage_project.ipynb
ADDED
@@ -0,0 +1,824 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "VFa5VyVNPCJY"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"# Project: Model Usage - Image Classification and Transfer Learning"
|
10 |
+
]
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"cell_type": "markdown",
|
14 |
+
"metadata": {
|
15 |
+
"id": "-9Z7RJrNvF8Q"
|
16 |
+
},
|
17 |
+
"source": [
|
18 |
+
"**Instructions for Students:**\n",
|
19 |
+
"\n",
|
20 |
+
"Please carefully follow these steps to complete and submit your project:\n",
|
21 |
+
"\n",
|
22 |
+
"1. **Make a copy of the Project**: Please make a copy of this project either to your own Google Drive or download locally. Work on the copy of the project. The master project is **Read-Only**, meaning you can edit, but it will not be saved when you close the master project. To avoid total loss of your work, remember to make a copy.\n",
|
23 |
+
"\n",
|
24 |
+
"2. **Completing the Project**: You are required to work on and complete all tasks in the provided project. Be disciplined and ensure that you thoroughly engage with each task.\n",
|
25 |
+
" \n",
|
26 |
+
"3. **Creating a Google Drive Folder**: Each of you must create a new folder on your Google Drive. This will be the repository for all your completed project files, aiding you in keeping your work organized and accessible.\n",
|
27 |
+
" \n",
|
28 |
+
"4. **Uploading Completed Project**: Upon completion of your project, make sure to upload all necessary files, involving codes, reports, and related documents into the created Google Drive folder. Save this link in the 'Student Identity' section and also provide it as the last parameter in the `submit` function that has been provided.\n",
|
29 |
+
" \n",
|
30 |
+
"5. **Sharing Folder Link**: You're required to share the link to your project Google Drive folder. This is crucial for the submission and evaluation of your project.\n",
|
31 |
+
" \n",
|
32 |
+
"6. **Setting Permission to Public**: Please make sure your Google Drive folder is set to public. This allows your instructor to access your solutions and assess your work correctly.\n",
|
33 |
+
"\n",
|
34 |
+
"Adhering to these procedures will facilitate a smooth project evaluation process for you and the reviewers."
|
35 |
+
]
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"cell_type": "markdown",
|
39 |
+
"metadata": {
|
40 |
+
"id": "0Qg7V4zoPCJi"
|
41 |
+
},
|
42 |
+
"source": [
|
43 |
+
"## Project Description:\n",
|
44 |
+
"\n",
|
45 |
+
"Welcome to your new project! You will have the opportunity to apply the knowledge and skills you've learned in class.\n",
|
46 |
+
"\n",
|
47 |
+
"The tasks are divided into two parts, the first part is to create an image classification project that predicts a person's age based on their photograph. You will be utilizing the power of machine learning pipelines to streamline your workflow and effectively manage the different stages of this project, from data preprocessing to model training and evaluation.\n",
|
48 |
+
"\n",
|
49 |
+
"In the second part is transfer learning where you'll use a [Vision Transformer (ViT)](https://huggingface.co/google/vit-base-patch16-224-in21k) model pre-trained on ImageNet-21k and fine-tune it on the [FastJobs/Visual_Emotional_Analysis](https://huggingface.co/datasets/FastJobs/Visual_Emotional_Analysis) dataset for emotion recognition, with the final step being the publication of your trained model to the Hugging Face Model Hub.\n",
|
50 |
+
"\n",
|
51 |
+
"Remember, the goal of this assignment is not just to build a model that makes accurate predictions, but also to understand the process of developing a machine-learning pipeline and the role each component plays in this process.\n",
|
52 |
+
"\n",
|
53 |
+
"We encourage you to be creative, explore different strategies, and most importantly, have fun while learning. We can't wait to see the innovative solutions you come up with! Best of luck!"
|
54 |
+
]
|
55 |
+
},
|
56 |
+
{
|
57 |
+
"cell_type": "markdown",
|
58 |
+
"metadata": {
|
59 |
+
"id": "6MDaD2U_vF8S"
|
60 |
+
},
|
61 |
+
"source": [
|
62 |
+
"## Grading Criteria\n",
|
63 |
+
"\n",
|
64 |
+
"There are 2 tasks in this project with 5 criterias for scoring, all except Criteria 4 have the same weight. Each criteria except Criteria 4 will give you either 100 point if you are correct and 0 if you are wrong. The final score for the project will the the average of all 5 criterias from both projects.\n",
|
65 |
+
"\n",
|
66 |
+
"* Task-1 Criteria 1: This task will assess your ability to understand how a model is likely to be used, in this use a model from Huggingface (HF) preferably using HF Pipeline, pass the input and get the correct answer form the model's output.\n",
|
67 |
+
"\n",
|
68 |
+
"* Task-1 Criteria 2: This task will assess your ability to use Gradio as a UI (User Interface) and interact with the model, in this case, the model used in Task-1 Criteria 1.\n",
|
69 |
+
"\n",
|
70 |
+
"* Task-2 Criteria 3: The task will assess your ability to perform transfer learning using a model from Huggingface and publish the new model to Huggingface platform.\n",
|
71 |
+
"\n",
|
72 |
+
"* Task-2 Criteria 4: This task will assess your ability to perform transfer learning and perform an evaluation. The accuracy submitted will be used in a Bell Curve Distribution where the average accuracy score will be mapped to a score of 70. This ensures fairness since the accuracy of all students who submit their accuracy score are taken into account and distributed evenly. For example, if the average students score is 56, those who submit their accuracy as 56 will get a score of 70; student with accuracy of 43 will get a score of 60; student with accuracy of 70 will get 80; naturally there is a gradation, meaning the accuracy between 43-56 will get a score between 60 to 70 and so on.\n",
|
73 |
+
"\n",
|
74 |
+
"* Task-2 Criteria 5: This task will assess your ability to use Gradio as a UI and interact with more than one models, in this case the model from Task-1 Criteria 1 and Task-2 Criteria 3.\n"
|
75 |
+
]
|
76 |
+
},
|
77 |
+
{
|
78 |
+
"cell_type": "markdown",
|
79 |
+
"metadata": {
|
80 |
+
"id": "ZysTKHbGioh8"
|
81 |
+
},
|
82 |
+
"source": [
|
83 |
+
"## Student Identity"
|
84 |
+
]
|
85 |
+
},
|
86 |
+
{
|
87 |
+
"cell_type": "code",
|
88 |
+
"execution_count": null,
|
89 |
+
"metadata": {
|
90 |
+
"id": "i8BlcSWzioi3"
|
91 |
+
},
|
92 |
+
"outputs": [],
|
93 |
+
"source": [
|
94 |
+
"# @title #### Student Identity\n",
|
95 |
+
"student_id = \"\" # @param {type:\"string\"}\n",
|
96 |
+
"name = \"\" # @param {type:\"string\"}\n",
|
97 |
+
"drive_link = \"\" # @param {type:\"string\"}"
|
98 |
+
]
|
99 |
+
},
|
100 |
+
{
|
101 |
+
"cell_type": "markdown",
|
102 |
+
"metadata": {
|
103 |
+
"id": "vJWjH2kGV49k"
|
104 |
+
},
|
105 |
+
"source": [
|
106 |
+
"## Installation and Import Package"
|
107 |
+
]
|
108 |
+
},
|
109 |
+
{
|
110 |
+
"cell_type": "code",
|
111 |
+
"execution_count": null,
|
112 |
+
"metadata": {
|
113 |
+
"id": "8wWESOr0PCJk"
|
114 |
+
},
|
115 |
+
"outputs": [],
|
116 |
+
"source": [
|
117 |
+
"# Install necessary packages\n",
|
118 |
+
"!pip install rggrader\n",
|
119 |
+
"from rggrader import submit, submit_image\n",
|
120 |
+
"\n",
|
121 |
+
"# Put your code here:\n",
|
122 |
+
"\n",
|
123 |
+
"# ---- End of your code ----"
|
124 |
+
]
|
125 |
+
},
|
126 |
+
{
|
127 |
+
"cell_type": "markdown",
|
128 |
+
"metadata": {
|
129 |
+
"id": "4_mbLFq9Vvcg"
|
130 |
+
},
|
131 |
+
"source": [
|
132 |
+
"## Task 1 Image Classification using Pipeline"
|
133 |
+
]
|
134 |
+
},
|
135 |
+
{
|
136 |
+
"cell_type": "markdown",
|
137 |
+
"metadata": {
|
138 |
+
"id": "YwFqrph-vF8W"
|
139 |
+
},
|
140 |
+
"source": [
|
141 |
+
"### Step 1: Image Classification using Hugging Face's Model\n",
|
142 |
+
"\n",
|
143 |
+
"In this first task, your task is to develop an image classification pipeline that takes **an image URL as input**, displays the image, and uses the Hugging Face's model to predict the age of the person in the image. You can get the model [here](https://huggingface.co/nateraw/vit-age-classifier).\n",
|
144 |
+
"\n",
|
145 |
+
"Here are the key steps that you might be able to follow:\n",
|
146 |
+
"\n",
|
147 |
+
"1. **Image URL Input:** Your program should accept an image URL as input. Make sure to handle potential issues with invalid URLs or inaccessible images.\n",
|
148 |
+
"2. **Image Display:** Display the image from the URL in your notebook. This will provide a visual confirmation that the correct image is being processed.\n",
|
149 |
+
"3. **Model Loading and Prediction:** Load the 'nateraw/vit-age-classifier' model from Hugging Face's model hub and pass the image URL to the model to obtain the prediction. The model should predict the age of the person in the image.\n",
|
150 |
+
"4. **Output Display:** Display the output from the model in a clear and understandable manner.\n",
|
151 |
+
"\n",
|
152 |
+
"#### Submission\n",
|
153 |
+
"\n",
|
154 |
+
"- What percentage is the person in this picture (https://images.unsplash.com/photo-1596392927852-2a18c336fb78?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1280&q=80) is between age of \"3-9\"?\n",
|
155 |
+
"\n",
|
156 |
+
"Submit in the numeric format up to 5 digits behind the decimal point. For example in below output:\n",
|
157 |
+
"\n",
|
158 |
+
"```\n",
|
159 |
+
"{'0-2': '0.00152',\n",
|
160 |
+
" '3-9': '0.00105',\n",
|
161 |
+
" '10-19': '0.02567',\n",
|
162 |
+
" '20-29': '3.32545',\n",
|
163 |
+
" '30-39': '51.75200',\n",
|
164 |
+
" '40-49': '40.24234',\n",
|
165 |
+
" '50-59': '4.47803',\n",
|
166 |
+
" '60-69': '0.17092',\n",
|
167 |
+
" 'more than 70': '0.00304'}\n",
|
168 |
+
"```\n",
|
169 |
+
"\n",
|
170 |
+
"The answer would be `0.00105`."
|
171 |
+
]
|
172 |
+
},
|
173 |
+
{
|
174 |
+
"cell_type": "code",
|
175 |
+
"execution_count": null,
|
176 |
+
"metadata": {
|
177 |
+
"id": "H5LA1LcdPCJm"
|
178 |
+
},
|
179 |
+
"outputs": [],
|
180 |
+
"source": [
|
181 |
+
"# @title #### 01. Image Classification using Hugging Face's Model\n",
|
182 |
+
"\n",
|
183 |
+
"# Put your code here:\n",
|
184 |
+
"\n",
|
185 |
+
"\n",
|
186 |
+
"# ---- End of your code ----"
|
187 |
+
]
|
188 |
+
},
|
189 |
+
{
|
190 |
+
"cell_type": "code",
|
191 |
+
"execution_count": null,
|
192 |
+
"metadata": {
|
193 |
+
"id": "M2EBNKYYvF8Y"
|
194 |
+
},
|
195 |
+
"outputs": [],
|
196 |
+
"source": [
|
197 |
+
"# Submit Method\n",
|
198 |
+
"assignment_id = \"00_pipeline_and_gradio\"\n",
|
199 |
+
"question_id = \"01_image_classification_using_hugging_faces_model\"\n",
|
200 |
+
"answer = \"\" # Put your answer here\n",
|
201 |
+
"submit(student_id, name, assignment_id, answer, question_id, drive_link)"
|
202 |
+
]
|
203 |
+
},
|
204 |
+
{
|
205 |
+
"cell_type": "markdown",
|
206 |
+
"metadata": {
|
207 |
+
"id": "B2wOiPqDiojo"
|
208 |
+
},
|
209 |
+
"source": [
|
210 |
+
"### Step 2: Image Classification using Hugging Face's Model and Gradio\n",
|
211 |
+
"\n",
|
212 |
+
"In this second task, you will create a user-friendly interface using Gradio for your image classification pipeline that you created in Task 1. The difference with task 1 is, that in this task, you use **image files as input**, process them through the Hugging Face model, and display predictions output. The output displayed is **only the results with the highest `score`**.\n",
|
213 |
+
"\n",
|
214 |
+
"Here are the key steps that you might be able to follow:\n",
|
215 |
+
"\n",
|
216 |
+
"1. **Image Input:** Create a function to accept an image file as input. The image should be in a format that can be processed by the model.\n",
|
217 |
+
"2. **Model Loading and Prediction:** Load the model from Hugging Face's model hub and pass the image to the model to obtain the prediction. The model predicts the age of the person in the image.\n",
|
218 |
+
"3. **Gradio Interface:** Use Gradio to create a user-friendly interface for your application. The interface should allow users to upload an image file, and it should display the model's output in a clear and understandable manner.\n",
|
219 |
+
"4. **Interface Launch:** Launch the Gradio interface. Make sure that the interface is accessible and easy to use.\n",
|
220 |
+
"\n",
|
221 |
+
"#### Submisssion\n",
|
222 |
+
"\n",
|
223 |
+
"\n",
|
224 |
+
"\n",
|
225 |
+
"You need to submit screenshot of your Gradio's app. In Google Colab you can just use the \"Folder\" sidebar and click the upload button. Make sure your screenshot match below requirements:\n",
|
226 |
+
"\n",
|
227 |
+
"- You should upload a person's image to that app\n",
|
228 |
+
"- The score should be included at the screenshot\n"
|
229 |
+
]
|
230 |
+
},
|
231 |
+
{
|
232 |
+
"cell_type": "code",
|
233 |
+
"execution_count": null,
|
234 |
+
"metadata": {
|
235 |
+
"id": "fsMSIbrwTKuB"
|
236 |
+
},
|
237 |
+
"outputs": [],
|
238 |
+
"source": [
|
239 |
+
"# @title #### 02. Image Classification using Hugging Face's Model and Gradio\n",
|
240 |
+
"\n",
|
241 |
+
"# Put your code here:\n",
|
242 |
+
"\n",
|
243 |
+
"\n",
|
244 |
+
"# ---- End of your code ----"
|
245 |
+
]
|
246 |
+
},
|
247 |
+
{
|
248 |
+
"cell_type": "markdown",
|
249 |
+
"metadata": {
|
250 |
+
"id": "SHFWYR7qvF8Z"
|
251 |
+
},
|
252 |
+
"source": [
|
253 |
+
"Example of Expected Output:\n",
|
254 |
+
"\n",
|
255 |
+
""
|
256 |
+
]
|
257 |
+
},
|
258 |
+
{
|
259 |
+
"cell_type": "code",
|
260 |
+
"execution_count": null,
|
261 |
+
"metadata": {
|
262 |
+
"id": "vhJMv03IvF8Z"
|
263 |
+
},
|
264 |
+
"outputs": [],
|
265 |
+
"source": [
|
266 |
+
"# Submit Method\n",
|
267 |
+
"question_id = \"02_image_classification_using_hugging_faces_model_and_gradio\"\n",
|
268 |
+
"submit_image(student_id, question_id, './submission.jpg')\n"
|
269 |
+
]
|
270 |
+
},
|
271 |
+
{
|
272 |
+
"cell_type": "markdown",
|
273 |
+
"metadata": {
|
274 |
+
"id": "t8KSCR8OvF8Z"
|
275 |
+
},
|
276 |
+
"source": [
|
277 |
+
"> Note: If your submission for Task-2 did not run (After you run it never changes from \"*\" to a number), stop the Code block that's running the Gradio app, then the submission will run. To stop the Code block, you can click on the Code block and then click the stop button."
|
278 |
+
]
|
279 |
+
},
|
280 |
+
{
|
281 |
+
"cell_type": "markdown",
|
282 |
+
"metadata": {
|
283 |
+
"id": "SC8oMewavF8Z"
|
284 |
+
},
|
285 |
+
"source": [
|
286 |
+
"# Task 2: Transfer Learning for Emotion Recognition"
|
287 |
+
]
|
288 |
+
},
|
289 |
+
{
|
290 |
+
"cell_type": "markdown",
|
291 |
+
"metadata": {
|
292 |
+
"id": "RYbbazOuvF8Z"
|
293 |
+
},
|
294 |
+
"source": [
|
295 |
+
"### Step 1: Environment Setup\n",
|
296 |
+
"\n",
|
297 |
+
"In this section, we start by installing the necessary packages and logging into Hugging Face's platform:\n",
|
298 |
+
"- `transformers`\n",
|
299 |
+
"- `datasets`\n",
|
300 |
+
"- `evaluate`\n",
|
301 |
+
"- `huggingface_hub`"
|
302 |
+
]
|
303 |
+
},
|
304 |
+
{
|
305 |
+
"cell_type": "code",
|
306 |
+
"execution_count": null,
|
307 |
+
"metadata": {
|
308 |
+
"id": "L8tVuUfnvF8a"
|
309 |
+
},
|
310 |
+
"outputs": [],
|
311 |
+
"source": [
|
312 |
+
"# Install necessary packages\n",
|
313 |
+
"\n",
|
314 |
+
"# Put your code here:\n",
|
315 |
+
"\n",
|
316 |
+
"# ---- End of your code ----"
|
317 |
+
]
|
318 |
+
},
|
319 |
+
{
|
320 |
+
"cell_type": "markdown",
|
321 |
+
"metadata": {
|
322 |
+
"id": "pp8aOoWDvF8a"
|
323 |
+
},
|
324 |
+
"source": [
|
325 |
+
"After installing, use the Hugging Face's notebook login function to log into Hugging Face's platform. Execute the following commands in your cell:"
|
326 |
+
]
|
327 |
+
},
|
328 |
+
{
|
329 |
+
"cell_type": "code",
|
330 |
+
"execution_count": null,
|
331 |
+
"metadata": {
|
332 |
+
"id": "z98RbfLwvF8a"
|
333 |
+
},
|
334 |
+
"outputs": [],
|
335 |
+
"source": [
|
336 |
+
"# Log into Hugging Face's platform\n",
|
337 |
+
"\n",
|
338 |
+
"# Put your code here:\n",
|
339 |
+
"\n",
|
340 |
+
"# ---- End of your code ----"
|
341 |
+
]
|
342 |
+
},
|
343 |
+
{
|
344 |
+
"cell_type": "markdown",
|
345 |
+
"metadata": {
|
346 |
+
"id": "-4abRjUVvF8a"
|
347 |
+
},
|
348 |
+
"source": [
|
349 |
+
"### Step 2: Load the Dataset\n",
|
350 |
+
"\n",
|
351 |
+
"Load the \"FastJobs/Visual_Emotional_Analysis\" dataset and split it into training and test sets with a test size of 0.2.\n",
|
352 |
+
"\n",
|
353 |
+
"> **Note**: please assign to variable `emotion`"
|
354 |
+
]
|
355 |
+
},
|
356 |
+
{
|
357 |
+
"cell_type": "code",
|
358 |
+
"execution_count": null,
|
359 |
+
"metadata": {
|
360 |
+
"id": "zygwYIo3vF8a"
|
361 |
+
},
|
362 |
+
"outputs": [],
|
363 |
+
"source": [
|
364 |
+
"# Load the dataset and split it\n",
|
365 |
+
"\n",
|
366 |
+
"# Update your code here:\n",
|
367 |
+
"emotion = \"\"\n",
|
368 |
+
"# ---- End of your code ----"
|
369 |
+
]
|
370 |
+
},
|
371 |
+
{
|
372 |
+
"cell_type": "markdown",
|
373 |
+
"metadata": {
|
374 |
+
"id": "F-LwIylpvF8b"
|
375 |
+
},
|
376 |
+
"source": [
|
377 |
+
"**Label Mapping**\n",
|
378 |
+
"\n",
|
379 |
+
"> **Note**: no need to change the code below! Just run it to map labels from the dataset."
|
380 |
+
]
|
381 |
+
},
|
382 |
+
{
|
383 |
+
"cell_type": "code",
|
384 |
+
"execution_count": null,
|
385 |
+
"metadata": {
|
386 |
+
"id": "524AEqr1vF8b"
|
387 |
+
},
|
388 |
+
"outputs": [],
|
389 |
+
"source": [
|
390 |
+
"labels = emotion[\"train\"].features[\"label\"].names\n",
|
391 |
+
"label2id, id2label = dict(), dict()\n",
|
392 |
+
"for i, label in enumerate(labels):\n",
|
393 |
+
" label2id[label] = str(i)\n",
|
394 |
+
" id2label[str(i)] = label"
|
395 |
+
]
|
396 |
+
},
|
397 |
+
{
|
398 |
+
"cell_type": "markdown",
|
399 |
+
"metadata": {
|
400 |
+
"id": "7ZUaw3twvF8b"
|
401 |
+
},
|
402 |
+
"source": [
|
403 |
+
"### Step 3: Explore and Visualize the Dataset\n",
|
404 |
+
"\n",
|
405 |
+
"In this step, you are required to visualize the first instance in the training dataset.\n",
|
406 |
+
"\n",
|
407 |
+
"> **Note**: no need to change the code below! Just run it to visualize the dataset based on index."
|
408 |
+
]
|
409 |
+
},
|
410 |
+
{
|
411 |
+
"cell_type": "code",
|
412 |
+
"source": [
|
413 |
+
"import matplotlib.pyplot as plt\n",
|
414 |
+
"\n",
|
415 |
+
"# Define the function to convert label index to label name\n",
|
416 |
+
"id2label_view = {str(i): label for i, label in enumerate(labels)}\n",
|
417 |
+
"\n",
|
418 |
+
"# Use first training example\n",
|
419 |
+
"image = emotion['train'][0]['image'] # Explore image by index\n",
|
420 |
+
"label_id = str(emotion['train'][0]['label'])\n",
|
421 |
+
"label_name = id2label_view[label_id]\n",
|
422 |
+
"\n",
|
423 |
+
"# Display the image and its corresponding label\n",
|
424 |
+
"plt.imshow(image)\n",
|
425 |
+
"plt.title(f'Label: {label_name} (ID: {label_id})')\n",
|
426 |
+
"plt.axis('off')\n",
|
427 |
+
"plt.show()"
|
428 |
+
],
|
429 |
+
"metadata": {
|
430 |
+
"id": "F1qKjbgcvWJj"
|
431 |
+
},
|
432 |
+
"execution_count": null,
|
433 |
+
"outputs": []
|
434 |
+
},
|
435 |
+
{
|
436 |
+
"cell_type": "markdown",
|
437 |
+
"metadata": {
|
438 |
+
"id": "MDm9XEzbvF8b"
|
439 |
+
},
|
440 |
+
"source": [
|
441 |
+
"### Step 4: Preprocess the Data\n",
|
442 |
+
"\n",
|
443 |
+
"You need to define the transformation function for image preprocessing and apply it to the dataset."
|
444 |
+
]
|
445 |
+
},
|
446 |
+
{
|
447 |
+
"cell_type": "code",
|
448 |
+
"execution_count": null,
|
449 |
+
"metadata": {
|
450 |
+
"id": "vNa7A9A4vF8b"
|
451 |
+
},
|
452 |
+
"outputs": [],
|
453 |
+
"source": [
|
454 |
+
"from transformers import AutoImageProcessor\n",
|
455 |
+
"from torchvision.transforms import RandomResizedCrop, Compose, Normalize, ToTensor\n",
|
456 |
+
"\n",
|
457 |
+
"# Load the image processor, Define the transforms, Define the transformation function and Apply the transformation function\n",
|
458 |
+
"\n",
|
459 |
+
"# Put your code here:\n",
|
460 |
+
"\n",
|
461 |
+
"# ---- End of your code ----"
|
462 |
+
]
|
463 |
+
},
|
464 |
+
{
|
465 |
+
"cell_type": "markdown",
|
466 |
+
"metadata": {
|
467 |
+
"id": "p1v9tSkvvF8c"
|
468 |
+
},
|
469 |
+
"source": [
|
470 |
+
"### Step 5: Model Setup\n",
|
471 |
+
"\n",
|
472 |
+
"In this step, define the model architecture with the pre-trained ViT model and load it."
|
473 |
+
]
|
474 |
+
},
|
475 |
+
{
|
476 |
+
"cell_type": "code",
|
477 |
+
"execution_count": null,
|
478 |
+
"metadata": {
|
479 |
+
"id": "INJfQ16OvF8c"
|
480 |
+
},
|
481 |
+
"outputs": [],
|
482 |
+
"source": [
|
483 |
+
"from transformers import AutoModelForImageClassification\n",
|
484 |
+
"\n",
|
485 |
+
"# Define the model\n",
|
486 |
+
"\n",
|
487 |
+
"# Put your code here:\n",
|
488 |
+
"\n",
|
489 |
+
"# ---- End of your code ----"
|
490 |
+
]
|
491 |
+
},
|
492 |
+
{
|
493 |
+
"cell_type": "markdown",
|
494 |
+
"metadata": {
|
495 |
+
"id": "9pppaVt5vF8c"
|
496 |
+
},
|
497 |
+
"source": [
|
498 |
+
"### Step 6: Training Setup\n",
|
499 |
+
"\n",
|
500 |
+
"Define the training arguments and instantiate the trainer."
|
501 |
+
]
|
502 |
+
},
|
503 |
+
{
|
504 |
+
"cell_type": "code",
|
505 |
+
"execution_count": null,
|
506 |
+
"metadata": {
|
507 |
+
"id": "38g8OjfBvF8c"
|
508 |
+
},
|
509 |
+
"outputs": [],
|
510 |
+
"source": [
|
511 |
+
"import evaluate\n",
|
512 |
+
"\n",
|
513 |
+
"# Load accuracy metric\n",
|
514 |
+
"\n",
|
515 |
+
"# Put your code here:\n",
|
516 |
+
"\n",
|
517 |
+
"# ---- End of your code ----"
|
518 |
+
]
|
519 |
+
},
|
520 |
+
{
|
521 |
+
"cell_type": "code",
|
522 |
+
"execution_count": null,
|
523 |
+
"metadata": {
|
524 |
+
"id": "GccP5F4svF8c"
|
525 |
+
},
|
526 |
+
"outputs": [],
|
527 |
+
"source": [
|
528 |
+
"# Define compute metric function\n",
|
529 |
+
"\n",
|
530 |
+
"# Put your code here:\n",
|
531 |
+
"\n",
|
532 |
+
"# ---- End of your code ----"
|
533 |
+
]
|
534 |
+
},
|
535 |
+
{
|
536 |
+
"cell_type": "code",
|
537 |
+
"execution_count": null,
|
538 |
+
"metadata": {
|
539 |
+
"id": "sNC6OFLzvF8c"
|
540 |
+
},
|
541 |
+
"outputs": [],
|
542 |
+
"source": [
|
543 |
+
"from transformers import TrainingArguments, Trainer\n",
|
544 |
+
"\n",
|
545 |
+
"# Define training arguments\n",
|
546 |
+
"\n",
|
547 |
+
"# Update your code here:\n",
|
548 |
+
"training_args = TrainingArguments()\n",
|
549 |
+
"# ---- End of your code ----"
|
550 |
+
]
|
551 |
+
},
|
552 |
+
{
|
553 |
+
"cell_type": "markdown",
|
554 |
+
"metadata": {
|
555 |
+
"id": "ABFxOF9wvF8d"
|
556 |
+
},
|
557 |
+
"source": [
|
558 |
+
"To use Hugging Face `Trainer` you need to install the `accelerate` library version `0.20.1` or later. It is used for performance enhancement on PyTorch."
|
559 |
+
]
|
560 |
+
},
|
561 |
+
{
|
562 |
+
"cell_type": "code",
|
563 |
+
"execution_count": null,
|
564 |
+
"metadata": {
|
565 |
+
"id": "NGeDe5JOvF8d"
|
566 |
+
},
|
567 |
+
"outputs": [],
|
568 |
+
"source": [
|
569 |
+
"%pip install accelerate -U"
|
570 |
+
]
|
571 |
+
},
|
572 |
+
{
|
573 |
+
"cell_type": "code",
|
574 |
+
"execution_count": null,
|
575 |
+
"metadata": {
|
576 |
+
"id": "h95mzDGEvF8d"
|
577 |
+
},
|
578 |
+
"outputs": [],
|
579 |
+
"source": [
|
580 |
+
"from transformers import DefaultDataCollator\n",
|
581 |
+
"\n",
|
582 |
+
"# Instantiate the trainer\n",
|
583 |
+
"\n",
|
584 |
+
"# Update your code here:\n",
|
585 |
+
"data_collator = DefaultDataCollator()\n",
|
586 |
+
"trainer = Trainer()\n",
|
587 |
+
"# ---- End of your code ----"
|
588 |
+
]
|
589 |
+
},
|
590 |
+
{
|
591 |
+
"cell_type": "markdown",
|
592 |
+
"metadata": {
|
593 |
+
"id": "RUYlq1oMvF8j"
|
594 |
+
},
|
595 |
+
"source": [
|
596 |
+
"If there are problems when using the Trainer after installing `accelerate` you can restart the Kernel"
|
597 |
+
]
|
598 |
+
},
|
599 |
+
{
|
600 |
+
"cell_type": "markdown",
|
601 |
+
"metadata": {
|
602 |
+
"id": "-G3c0F5ZvF8j"
|
603 |
+
},
|
604 |
+
"source": [
|
605 |
+
"### Step 7: Train and Evaluate the Model\n",
|
606 |
+
"\n",
|
607 |
+
"Now, you are ready to train the model and evaluate it on the test set."
|
608 |
+
]
|
609 |
+
},
|
610 |
+
{
|
611 |
+
"cell_type": "code",
|
612 |
+
"execution_count": null,
|
613 |
+
"metadata": {
|
614 |
+
"id": "2GvoiY4mvF8k"
|
615 |
+
},
|
616 |
+
"outputs": [],
|
617 |
+
"source": [
|
618 |
+
"# Train the model\n",
|
619 |
+
"\n",
|
620 |
+
"# Put your code here:\n",
|
621 |
+
"\n",
|
622 |
+
"# ---- End of your code ----"
|
623 |
+
]
|
624 |
+
},
|
625 |
+
{
|
626 |
+
"cell_type": "code",
|
627 |
+
"execution_count": null,
|
628 |
+
"metadata": {
|
629 |
+
"id": "uVPRQNIRvF8k"
|
630 |
+
},
|
631 |
+
"outputs": [],
|
632 |
+
"source": [
|
633 |
+
"# Evaluate the model\n",
|
634 |
+
"\n",
|
635 |
+
"# Update your code here:\n",
|
636 |
+
"eval_result = \"\"\n",
|
637 |
+
"# ---- End of your code ----\n",
|
638 |
+
"\n",
|
639 |
+
"# Save the formatted accuracy in a variable\n",
|
640 |
+
"accuracy_str = \"{:.4f}\".format(eval_result[\"eval_accuracy\"])"
|
641 |
+
]
|
642 |
+
},
|
643 |
+
{
|
644 |
+
"cell_type": "markdown",
|
645 |
+
"metadata": {
|
646 |
+
"id": "OIv95upxvF8k"
|
647 |
+
},
|
648 |
+
"source": [
|
649 |
+
"### Step 8: Publishing the Trained Model\n",
|
650 |
+
"\n",
|
651 |
+
"Finally, make sure to push your trained model to the Hugging Face Model Hub.\n",
|
652 |
+
"\n",
|
653 |
+
"> **Note**: No need to change the code below! Just run to publish your model."
|
654 |
+
]
|
655 |
+
},
|
656 |
+
{
|
657 |
+
"cell_type": "code",
|
658 |
+
"execution_count": null,
|
659 |
+
"metadata": {
|
660 |
+
"id": "aZe2lXMNvF8k"
|
661 |
+
},
|
662 |
+
"outputs": [],
|
663 |
+
"source": [
|
664 |
+
"trainer.push_to_hub()"
|
665 |
+
]
|
666 |
+
},
|
667 |
+
{
|
668 |
+
"cell_type": "markdown",
|
669 |
+
"metadata": {
|
670 |
+
"id": "3PRRzcPkvF8l"
|
671 |
+
},
|
672 |
+
"source": [
|
673 |
+
"Once you've trained your model and pushed it to the Hugging Face Model Hub, you'll have a link that points directly to your model's page. You can share this link with others, and they can use it to directly load your model for their own uses.\n",
|
674 |
+
"\n",
|
675 |
+
"The following link is an example of what a trained model's page looks like: https://huggingface.co/aditira/emotion_classification. This is not your model, but rather an example of what your final result might resemble.\n",
|
676 |
+
"\n",
|
677 |
+
"Remember, for this project you should push your output model to your own Hugging Face account. The link for your model will be different and should reflect your own username and model name."
|
678 |
+
]
|
679 |
+
},
|
680 |
+
{
|
681 |
+
"cell_type": "code",
|
682 |
+
"execution_count": null,
|
683 |
+
"metadata": {
|
684 |
+
"id": "f72zoZKcvF8l"
|
685 |
+
},
|
686 |
+
"outputs": [],
|
687 |
+
"source": [
|
688 |
+
"# Submit Method\n",
|
689 |
+
"huggingface_model_link = \"\" # Put your model link\n",
|
690 |
+
"\n",
|
691 |
+
"assignment_id = \"00_transfer_learning\"\n",
|
692 |
+
"question_id = \"00_emotion_recognition_huggingface\"\n",
|
693 |
+
"submit(student_id, name, assignment_id, huggingface_model_link, question_id, drive_link)\n",
|
694 |
+
"\n",
|
695 |
+
"question_id = \"01_emotion_recognition_accuracy\"\n",
|
696 |
+
"submit(student_id, name, assignment_id, accuracy_str, question_id, drive_link)"
|
697 |
+
]
|
698 |
+
},
|
699 |
+
{
|
700 |
+
"cell_type": "markdown",
|
701 |
+
"metadata": {
|
702 |
+
"id": "YNRc-bowvF8l"
|
703 |
+
},
|
704 |
+
"source": [
|
705 |
+
"### Step 9: Build an Interactive Application with Gradio"
|
706 |
+
]
|
707 |
+
},
|
708 |
+
{
|
709 |
+
"cell_type": "markdown",
|
710 |
+
"metadata": {
|
711 |
+
"id": "Dt61YtQYvF8l"
|
712 |
+
},
|
713 |
+
"source": [
|
714 |
+
"In this task, you will be building an interactive application using Gradio that will use your fine-tuned emotion recognition model along with another pretrained model ('`nateraw/vit-age-classifier`') to guess the emotion and age from an input image.\n",
|
715 |
+
"\n",
|
716 |
+
"Please make sure to:\n",
|
717 |
+
"- Install the necessary package (`gradio`) for creating the web-based interface.\n",
|
718 |
+
"- Load your fine-tuned model as well as the pretrained model '`nateraw/vit-age-classifier`'.\n",
|
719 |
+
"- Define a function that will take an image as input and return the predicted emotion and age.\n",
|
720 |
+
"- Utilize Gradio to create an Interface (UI) for your function, allowing users to upload images and see the predicted emotion and age."
|
721 |
+
]
|
722 |
+
},
|
723 |
+
{
|
724 |
+
"cell_type": "code",
|
725 |
+
"execution_count": null,
|
726 |
+
"metadata": {
|
727 |
+
"id": "VtyLNza9vF8l"
|
728 |
+
},
|
729 |
+
"outputs": [],
|
730 |
+
"source": [
|
731 |
+
"# Install Gradio\n",
|
732 |
+
"!pip install gradio"
|
733 |
+
]
|
734 |
+
},
|
735 |
+
{
|
736 |
+
"cell_type": "markdown",
|
737 |
+
"metadata": {
|
738 |
+
"id": "tD9pRSs2vF8m"
|
739 |
+
},
|
740 |
+
"source": [
|
741 |
+
"#### Submisssion\n",
|
742 |
+
"\n",
|
743 |
+
"\n",
|
744 |
+
"\n",
|
745 |
+
"You need to submit screenshot of your Gradio's app. In Google Colab you can just use the \"Folder\" sidebar and click the upload button. Make sure your screenshot match below requirements:\n",
|
746 |
+
"\n",
|
747 |
+
"- Image name screenshot is `submission.jpg`\n",
|
748 |
+
"- You should upload a person's image to that app\n",
|
749 |
+
"- The score should be included at the screenshot"
|
750 |
+
]
|
751 |
+
},
|
752 |
+
{
|
753 |
+
"cell_type": "code",
|
754 |
+
"execution_count": null,
|
755 |
+
"metadata": {
|
756 |
+
"id": "7nNft9g0vF8m"
|
757 |
+
},
|
758 |
+
"outputs": [],
|
759 |
+
"source": [
|
760 |
+
"# Put your code here:\n",
|
761 |
+
"\n",
|
762 |
+
"# ---- End of your code ----"
|
763 |
+
]
|
764 |
+
},
|
765 |
+
{
|
766 |
+
"cell_type": "markdown",
|
767 |
+
"metadata": {
|
768 |
+
"id": "7xHv6RycvF8m"
|
769 |
+
},
|
770 |
+
"source": [
|
771 |
+
"Example of Expected Output:\n",
|
772 |
+
"\n",
|
773 |
+
""
|
774 |
+
]
|
775 |
+
},
|
776 |
+
{
|
777 |
+
"cell_type": "code",
|
778 |
+
"execution_count": null,
|
779 |
+
"metadata": {
|
780 |
+
"id": "ZhKCVEWjvF8m"
|
781 |
+
},
|
782 |
+
"outputs": [],
|
783 |
+
"source": [
|
784 |
+
"# Submit Method\n",
|
785 |
+
"question_id = \"01_interactive_application_with_gradio\"\n",
|
786 |
+
"submit_image(student_id, question_id, './submission.jpg')"
|
787 |
+
]
|
788 |
+
},
|
789 |
+
{
|
790 |
+
"cell_type": "markdown",
|
791 |
+
"metadata": {
|
792 |
+
"id": "YYUSn0TgvF8m"
|
793 |
+
},
|
794 |
+
"source": [
|
795 |
+
"> Note: If your submission for Task-2 did not run (After you run it never changes from \"*\" to a number), stop the Code block that's running the Gradio app, then the submission will run. To stop the Code block, you can click on the Code block and then click the stop button."
|
796 |
+
]
|
797 |
+
}
|
798 |
+
],
|
799 |
+
"metadata": {
|
800 |
+
"accelerator": "GPU",
|
801 |
+
"colab": {
|
802 |
+
"provenance": []
|
803 |
+
},
|
804 |
+
"kernelspec": {
|
805 |
+
"display_name": "Python 3 (ipykernel)",
|
806 |
+
"language": "python",
|
807 |
+
"name": "python3"
|
808 |
+
},
|
809 |
+
"language_info": {
|
810 |
+
"codemirror_mode": {
|
811 |
+
"name": "ipython",
|
812 |
+
"version": 3
|
813 |
+
},
|
814 |
+
"file_extension": ".py",
|
815 |
+
"mimetype": "text/x-python",
|
816 |
+
"name": "python",
|
817 |
+
"nbconvert_exporter": "python",
|
818 |
+
"pygments_lexer": "ipython3",
|
819 |
+
"version": "3.11.3"
|
820 |
+
}
|
821 |
+
},
|
822 |
+
"nbformat": 4,
|
823 |
+
"nbformat_minor": 0
|
824 |
+
}
|
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji: 💬
|
4 |
-
colorFrom: yellow
|
5 |
-
colorTo: purple
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 5.0.1
|
8 |
app_file: app.py
|
9 |
-
|
|
|
10 |
---
|
11 |
-
|
12 |
-
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
|
|
1 |
---
|
2 |
+
title: Age_Classification
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
s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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# # Project: Model Usage - Image Classification and Transfer Learning
|
5 |
+
|
6 |
+
# **Instructions for Students:**
|
7 |
+
#
|
8 |
+
# Please carefully follow these steps to complete and submit your project:
|
9 |
+
#
|
10 |
+
# 1. **Make a copy of the Project**: Please make a copy of this project either to your own Google Drive or download locally. Work on the copy of the project. The master project is **Read-Only**, meaning you can edit, but it will not be saved when you close the master project. To avoid total loss of your work, remember to make a copy.
|
11 |
+
#
|
12 |
+
# 2. **Completing the Project**: You are required to work on and complete all tasks in the provided project. Be disciplined and ensure that you thoroughly engage with each task.
|
13 |
+
#
|
14 |
+
# 3. **Creating a Google Drive Folder**: Each of you must create a new folder on your Google Drive. This will be the repository for all your completed project files, aiding you in keeping your work organized and accessible.
|
15 |
+
#
|
16 |
+
# 4. **Uploading Completed Project**: Upon completion of your project, make sure to upload all necessary files, involving codes, reports, and related documents into the created Google Drive folder. Save this link in the 'Student Identity' section and also provide it as the last parameter in the `submit` function that has been provided.
|
17 |
+
#
|
18 |
+
# 5. **Sharing Folder Link**: You're required to share the link to your project Google Drive folder. This is crucial for the submission and evaluation of your project.
|
19 |
+
#
|
20 |
+
# 6. **Setting Permission to Public**: Please make sure your Google Drive folder is set to public. This allows your instructor to access your solutions and assess your work correctly.
|
21 |
+
#
|
22 |
+
# Adhering to these procedures will facilitate a smooth project evaluation process for you and the reviewers.
|
23 |
+
|
24 |
+
# ## Project Description:
|
25 |
+
#
|
26 |
+
# Welcome to your new project! You will have the opportunity to apply the knowledge and skills you've learned in class.
|
27 |
+
#
|
28 |
+
# The tasks are divided into two parts, the first part is to create an image classification project that predicts a person's age based on their photograph. You will be utilizing the power of machine learning pipelines to streamline your workflow and effectively manage the different stages of this project, from data preprocessing to model training and evaluation.
|
29 |
+
#
|
30 |
+
# In the second part is transfer learning where you'll use a [Vision Transformer (ViT)](https://huggingface.co/google/vit-base-patch16-224-in21k) model pre-trained on ImageNet-21k and fine-tune it on the [FastJobs/Visual_Emotional_Analysis](https://huggingface.co/datasets/FastJobs/Visual_Emotional_Analysis) dataset for emotion recognition, with the final step being the publication of your trained model to the Hugging Face Model Hub.
|
31 |
+
#
|
32 |
+
# Remember, the goal of this assignment is not just to build a model that makes accurate predictions, but also to understand the process of developing a machine-learning pipeline and the role each component plays in this process.
|
33 |
+
#
|
34 |
+
# We encourage you to be creative, explore different strategies, and most importantly, have fun while learning. We can't wait to see the innovative solutions you come up with! Best of luck!
|
35 |
+
|
36 |
+
# ## Grading Criteria
|
37 |
+
#
|
38 |
+
# There are 2 tasks in this project with 5 criterias for scoring, all except Criteria 4 have the same weight. Each criteria except Criteria 4 will give you either 100 point if you are correct and 0 if you are wrong. The final score for the project will the the average of all 5 criterias from both projects.
|
39 |
+
#
|
40 |
+
# * Task-1 Criteria 1: This task will assess your ability to understand how a model is likely to be used, in this use a model from Huggingface (HF) preferably using HF Pipeline, pass the input and get the correct answer form the model's output.
|
41 |
+
#
|
42 |
+
# * Task-1 Criteria 2: This task will assess your ability to use Gradio as a UI (User Interface) and interact with the model, in this case, the model used in Task-1 Criteria 1.
|
43 |
+
#
|
44 |
+
# * Task-2 Criteria 3: The task will assess your ability to perform transfer learning using a model from Huggingface and publish the new model to Huggingface platform.
|
45 |
+
#
|
46 |
+
# * Task-2 Criteria 4: This task will assess your ability to perform transfer learning and perform an evaluation. The accuracy submitted will be used in a Bell Curve Distribution where the average accuracy score will be mapped to a score of 70. This ensures fairness since the accuracy of all students who submit their accuracy score are taken into account and distributed evenly. For example, if the average students score is 56, those who submit their accuracy as 56 will get a score of 70; student with accuracy of 43 will get a score of 60; student with accuracy of 70 will get 80; naturally there is a gradation, meaning the accuracy between 43-56 will get a score between 60 to 70 and so on.
|
47 |
+
#
|
48 |
+
# * Task-2 Criteria 5: This task will assess your ability to use Gradio as a UI and interact with more than one models, in this case the model from Task-1 Criteria 1 and Task-2 Criteria 3.
|
49 |
+
#
|
50 |
+
|
51 |
+
# ## Student Identity
|
52 |
+
|
53 |
+
# ## Installation and Import Package
|
54 |
+
|
55 |
+
# In[2]:
|
56 |
+
|
57 |
+
|
58 |
+
# Install necessary packages
|
59 |
+
#!pip install rggrader
|
60 |
+
#from rggrader import submit, submit_image
|
61 |
+
#!pip install transformers datasets evaluate huggingface_hub gradio rggrader accelerate -U
|
62 |
+
# Put your code here:
|
63 |
+
import torch
|
64 |
+
import numpy as np
|
65 |
+
from transformers import pipeline, AutoImageProcessor, AutoModelForImageClassification, TrainingArguments, Trainer, DefaultDataCollator
|
66 |
+
from datasets import load_dataset
|
67 |
+
from evaluate import load
|
68 |
+
import gradio as gr
|
69 |
+
from PIL import Image
|
70 |
+
import requests
|
71 |
+
from io import BytesIO
|
72 |
+
import matplotlib.pyplot as plt
|
73 |
+
from torchvision.transforms import RandomResizedCrop, Compose, Normalize, ToTensor
|
74 |
+
from huggingface_hub import notebook_login
|
75 |
+
# ---- End of your code ----
|
76 |
+
|
77 |
+
|
78 |
+
# ## Task 1 Image Classification using Pipeline
|
79 |
+
|
80 |
+
# ### Step 1: Image Classification using Hugging Face's Model
|
81 |
+
#
|
82 |
+
# In this first task, your task is to develop an image classification pipeline that takes **an image URL as input**, displays the image, and uses the Hugging Face's model to predict the age of the person in the image. You can get the model [here](https://huggingface.co/nateraw/vit-age-classifier).
|
83 |
+
#
|
84 |
+
# Here are the key steps that you might be able to follow:
|
85 |
+
#
|
86 |
+
# 1. **Image URL Input:** Your program should accept an image URL as input. Make sure to handle potential issues with invalid URLs or inaccessible images.
|
87 |
+
# 2. **Image Display:** Display the image from the URL in your notebook. This will provide a visual confirmation that the correct image is being processed.
|
88 |
+
# 3. **Model Loading and Prediction:** Load the 'nateraw/vit-age-classifier' model from Hugging Face's model hub and pass the image URL to the model to obtain the prediction. The model should predict the age of the person in the image.
|
89 |
+
# 4. **Output Display:** Display the output from the model in a clear and understandable manner.
|
90 |
+
#
|
91 |
+
# #### Submission
|
92 |
+
#
|
93 |
+
# - What percentage is the person in this picture (https://images.unsplash.com/photo-1596392927852-2a18c336fb78?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1280&q=80) is between age of "3-9"?
|
94 |
+
#
|
95 |
+
# Submit in the numeric format up to 5 digits behind the decimal point. For example in below output:
|
96 |
+
#
|
97 |
+
# ```
|
98 |
+
# {'0-2': '0.00152',
|
99 |
+
# '3-9': '0.00105',
|
100 |
+
# '10-19': '0.02567',
|
101 |
+
# '20-29': '3.32545',
|
102 |
+
# '30-39': '51.75200',
|
103 |
+
# '40-49': '40.24234',
|
104 |
+
# '50-59': '4.47803',
|
105 |
+
# '60-69': '0.17092',
|
106 |
+
# 'more than 70': '0.00304'}
|
107 |
+
# ```
|
108 |
+
#
|
109 |
+
# The answer would be `0.00105`.
|
110 |
+
|
111 |
+
# In[3]:
|
112 |
+
|
113 |
+
|
114 |
+
# @title #### 01. Image Classification using Hugging Face's Model
|
115 |
+
|
116 |
+
# Put your code here:
|
117 |
+
def predict_age_from_url(image_url):
|
118 |
+
# Load the model pipeline
|
119 |
+
classifier = pipeline("image-classification", model="nateraw/vit-age-classifier")
|
120 |
+
|
121 |
+
# Get the image from URL
|
122 |
+
response = requests.get(image_url)
|
123 |
+
image = Image.open(BytesIO(response.content))
|
124 |
+
|
125 |
+
# Display the image
|
126 |
+
plt.imshow(image)
|
127 |
+
plt.axis('off')
|
128 |
+
plt.show()
|
129 |
+
|
130 |
+
# Make prediction
|
131 |
+
predictions = classifier(image)
|
132 |
+
|
133 |
+
# Convert to dictionary format
|
134 |
+
results = {pred['label']: f"{pred['score']:.5f}" for pred in predictions}
|
135 |
+
return results
|
136 |
+
|
137 |
+
# Test the function with the given URL
|
138 |
+
url = "https://images.unsplash.com/photo-1596392927852-2a18c336fb78?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1280&q=80"
|
139 |
+
results = predict_age_from_url(url)
|
140 |
+
answer = results["3-9"]
|
141 |
+
|
142 |
+
print(results)
|
143 |
+
print(answer)
|
144 |
+
|
145 |
+
# ---- End of your code ----
|
146 |
+
|
147 |
+
|
148 |
+
# # Submit Method
|
149 |
+
# assignment_id = "00_pipeline_and_gradio"
|
150 |
+
# question_id = "01_image_classification_using_hugging_faces_model"
|
151 |
+
# answer = "" # Put your answer here
|
152 |
+
# submit(student_id, name, assignment_id, answer, question_id, drive_link)
|
153 |
+
|
154 |
+
# ### Step 2: Image Classification using Hugging Face's Model and Gradio
|
155 |
+
#
|
156 |
+
# In this second task, you will create a user-friendly interface using Gradio for your image classification pipeline that you created in Task 1. The difference with task 1 is, that in this task, you use **image files as input**, process them through the Hugging Face model, and display predictions output. The output displayed is **only the results with the highest `score`**.
|
157 |
+
#
|
158 |
+
# Here are the key steps that you might be able to follow:
|
159 |
+
#
|
160 |
+
# 1. **Image Input:** Create a function to accept an image file as input. The image should be in a format that can be processed by the model.
|
161 |
+
# 2. **Model Loading and Prediction:** Load the model from Hugging Face's model hub and pass the image to the model to obtain the prediction. The model predicts the age of the person in the image.
|
162 |
+
# 3. **Gradio Interface:** Use Gradio to create a user-friendly interface for your application. The interface should allow users to upload an image file, and it should display the model's output in a clear and understandable manner.
|
163 |
+
# 4. **Interface Launch:** Launch the Gradio interface. Make sure that the interface is accessible and easy to use.
|
164 |
+
#
|
165 |
+
# #### Submisssion
|
166 |
+
#
|
167 |
+
# 
|
168 |
+
#
|
169 |
+
# You need to submit screenshot of your Gradio's app. In Google Colab you can just use the "Folder" sidebar and click the upload button. Make sure your screenshot match below requirements:
|
170 |
+
#
|
171 |
+
# - You should upload a person's image to that app
|
172 |
+
# - The score should be included at the screenshot
|
173 |
+
#
|
174 |
+
|
175 |
+
# In[4]:
|
176 |
+
|
177 |
+
|
178 |
+
# @title #### 02. Image Classification using Hugging Face's Model and Gradio
|
179 |
+
|
180 |
+
# Put your code here:
|
181 |
+
|
182 |
+
def predict_age(image):
|
183 |
+
if isinstance(image, np.ndarray): # If image is a NumPy array, convert it
|
184 |
+
image = Image.fromarray(image)
|
185 |
+
|
186 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
187 |
+
classifier = pipeline("image-classification", model="nateraw/vit-age-classifier")
|
188 |
+
predictions = classifier(image)
|
189 |
+
# Get the prediction with highest score
|
190 |
+
max_pred = max(predictions, key=lambda x: x['score'])
|
191 |
+
return {"score": round(max_pred["score"], 10), "label": max_pred["label"]}
|
192 |
+
|
193 |
+
demo = gr.Interface(
|
194 |
+
fn=predict_age,
|
195 |
+
inputs=gr.Image(),
|
196 |
+
outputs="json",
|
197 |
+
title="Age Classification"
|
198 |
+
)
|
199 |
+
|
200 |
+
demo.launch(share=True)
|
201 |
+
# ---- End of your code ----
|
202 |
+
|
203 |
+
|
204 |
+
# Example of Expected Output:
|
205 |
+
#
|
206 |
+
# 
|
207 |
+
|
208 |
+
# # Submit Method
|
209 |
+
# question_id = "02_image_classification_using_hugging_faces_model_and_gradio"
|
210 |
+
# submit_image(student_id, question_id, './submission.jpg')
|
211 |
+
#
|
212 |
+
|
213 |
+
# > Note: If your submission for Task-2 did not run (After you run it never changes from "*" to a number), stop the Code block that's running the Gradio app, then the submission will run. To stop the Code block, you can click on the Code block and then click the stop button.
|
214 |
+
|
215 |
+
# # Task 2: Transfer Learning for Emotion Recognition
|
216 |
+
|
217 |
+
# ### Step 1: Environment Setup
|
218 |
+
#
|
219 |
+
# In this section, we start by installing the necessary packages and logging into Hugging Face's platform:
|
220 |
+
# - `transformers`
|
221 |
+
# - `datasets`
|
222 |
+
# - `evaluate`
|
223 |
+
# - `huggingface_hub`
|
224 |
+
|
225 |
+
# In[5]:
|
226 |
+
|
227 |
+
|
228 |
+
# Install necessary packages
|
229 |
+
#!pip install datasets evaluate huggingface_hub accelerate -U
|
230 |
+
# Put your code here:
|
231 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
232 |
+
from transformers import TrainingArguments, Trainer, DefaultDataCollator
|
233 |
+
from datasets import load_dataset
|
234 |
+
from evaluate import load
|
235 |
+
from huggingface_hub import notebook_login
|
236 |
+
|
237 |
+
# ---- End of your code ----
|
238 |
+
|
239 |
+
|
240 |
+
# After installing, use the Hugging Face's notebook login function to log into Hugging Face's platform. Execute the following commands in your cell:
|
241 |
+
|
242 |
+
# In[6]:
|
243 |
+
|
244 |
+
|
245 |
+
# Log into Hugging Face's platform
|
246 |
+
|
247 |
+
# Put your code here:
|
248 |
+
# Login to Hugging Face
|
249 |
+
notebook_login()
|
250 |
+
# ---- End of your code ----
|
251 |
+
|
252 |
+
|
253 |
+
# ### Step 2: Load the Dataset
|
254 |
+
#
|
255 |
+
# Load the "FastJobs/Visual_Emotional_Analysis" dataset and split it into training and test sets with a test size of 0.2.
|
256 |
+
#
|
257 |
+
# > **Note**: please assign to variable `emotion`
|
258 |
+
|
259 |
+
# In[7]:
|
260 |
+
|
261 |
+
|
262 |
+
# Load the dataset and split it
|
263 |
+
|
264 |
+
# Update your code here:
|
265 |
+
emotion = load_dataset("FastJobs/Visual_Emotional_Analysis")
|
266 |
+
emotion = emotion["train"].train_test_split(test_size=0.2)
|
267 |
+
|
268 |
+
# Create label mappings
|
269 |
+
labels = emotion["train"].features["label"].names
|
270 |
+
label2id = {label: str(i) for i, label in enumerate(labels)}
|
271 |
+
id2label = {str(i): label for i, label in enumerate(labels)}
|
272 |
+
# ---- End of your code ----
|
273 |
+
|
274 |
+
|
275 |
+
# **Label Mapping**
|
276 |
+
#
|
277 |
+
# > **Note**: no need to change the code below! Just run it to map labels from the dataset.
|
278 |
+
|
279 |
+
# In[8]:
|
280 |
+
|
281 |
+
|
282 |
+
labels = emotion["train"].features["label"].names
|
283 |
+
label2id, id2label = dict(), dict()
|
284 |
+
for i, label in enumerate(labels):
|
285 |
+
label2id[label] = str(i)
|
286 |
+
id2label[str(i)] = label
|
287 |
+
|
288 |
+
|
289 |
+
# ### Step 3: Explore and Visualize the Dataset
|
290 |
+
#
|
291 |
+
# In this step, you are required to visualize the first instance in the training dataset.
|
292 |
+
#
|
293 |
+
# > **Note**: no need to change the code below! Just run it to visualize the dataset based on index.
|
294 |
+
|
295 |
+
# In[9]:
|
296 |
+
|
297 |
+
|
298 |
+
import matplotlib.pyplot as plt
|
299 |
+
|
300 |
+
# Define the function to convert label index to label name
|
301 |
+
id2label_view = {str(i): label for i, label in enumerate(labels)}
|
302 |
+
|
303 |
+
# Use first training example
|
304 |
+
image = emotion['train'][0]['image'] # Explore image by index
|
305 |
+
label_id = str(emotion['train'][0]['label'])
|
306 |
+
label_name = id2label_view[label_id]
|
307 |
+
|
308 |
+
# Display the image and its corresponding label
|
309 |
+
plt.imshow(image)
|
310 |
+
plt.title(f'Label: {label_name} (ID: {label_id})')
|
311 |
+
plt.axis('off')
|
312 |
+
plt.show()
|
313 |
+
|
314 |
+
|
315 |
+
# ### Step 4: Preprocess the Data
|
316 |
+
#
|
317 |
+
# You need to define the transformation function for image preprocessing and apply it to the dataset.
|
318 |
+
|
319 |
+
# In[10]:
|
320 |
+
|
321 |
+
|
322 |
+
from transformers import AutoImageProcessor
|
323 |
+
from torchvision.transforms import RandomResizedCrop, Compose, Normalize, ToTensor
|
324 |
+
|
325 |
+
# Load the image processor, Define the transforms, Define the transformation function and Apply the transformation function
|
326 |
+
|
327 |
+
# Put your code here:
|
328 |
+
image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")
|
329 |
+
normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std)
|
330 |
+
size = (
|
331 |
+
image_processor.size["height"],
|
332 |
+
image_processor.size["width"],
|
333 |
+
)
|
334 |
+
|
335 |
+
transform = Compose([RandomResizedCrop(size), ToTensor(), Normalize(mean=image_processor.image_mean, std=image_processor.image_std)])
|
336 |
+
|
337 |
+
def transform_example(example):
|
338 |
+
image = example["image"] # Extract image
|
339 |
+
image = transform(image) # Apply transformation
|
340 |
+
example["pixel_values"] = image # Store transformed image
|
341 |
+
del example["image"] # Remove original image key to avoid errors
|
342 |
+
return example
|
343 |
+
|
344 |
+
emotion_transformed = emotion.map(transform_example)
|
345 |
+
|
346 |
+
# ---- End of your code ----
|
347 |
+
|
348 |
+
|
349 |
+
# ### Step 5: Model Setup
|
350 |
+
#
|
351 |
+
# In this step, define the model architecture with the pre-trained ViT model and load it.
|
352 |
+
|
353 |
+
# In[11]:
|
354 |
+
|
355 |
+
|
356 |
+
from transformers import AutoModelForImageClassification
|
357 |
+
|
358 |
+
# Define the model
|
359 |
+
|
360 |
+
# Put your code here:
|
361 |
+
model = AutoModelForImageClassification.from_pretrained(
|
362 |
+
"google/vit-base-patch16-224-in21k",
|
363 |
+
num_labels=len(labels),
|
364 |
+
id2label=id2label,
|
365 |
+
label2id=label2id,
|
366 |
+
)
|
367 |
+
# ---- End of your code ----
|
368 |
+
|
369 |
+
|
370 |
+
# ### Step 6: Training Setup
|
371 |
+
#
|
372 |
+
# Define the training arguments and instantiate the trainer.
|
373 |
+
|
374 |
+
# In[12]:
|
375 |
+
|
376 |
+
|
377 |
+
import evaluate
|
378 |
+
|
379 |
+
# Load accuracy metric
|
380 |
+
|
381 |
+
# Put your code here:
|
382 |
+
metric = load("accuracy")
|
383 |
+
|
384 |
+
|
385 |
+
# ---- End of your code ----
|
386 |
+
|
387 |
+
|
388 |
+
# In[13]:
|
389 |
+
|
390 |
+
|
391 |
+
# Define compute metric function
|
392 |
+
|
393 |
+
# Put your code here:
|
394 |
+
def compute_metrics(eval_pred):
|
395 |
+
predictions, labels = eval_pred
|
396 |
+
predictions = predictions.argmax(axis=1)
|
397 |
+
return metric.compute(predictions=predictions, references=labels)
|
398 |
+
|
399 |
+
# ---- End of your code ----
|
400 |
+
|
401 |
+
|
402 |
+
# In[14]:
|
403 |
+
|
404 |
+
|
405 |
+
from transformers import TrainingArguments, Trainer
|
406 |
+
|
407 |
+
# Define training arguments
|
408 |
+
|
409 |
+
# Update your code here:
|
410 |
+
# Define training arguments
|
411 |
+
training_args = TrainingArguments(
|
412 |
+
output_dir="emotion-classifier",
|
413 |
+
learning_rate=5e-5,
|
414 |
+
per_device_train_batch_size=16,
|
415 |
+
per_device_eval_batch_size=16,
|
416 |
+
num_train_epochs=3,
|
417 |
+
weight_decay=0.01,
|
418 |
+
evaluation_strategy="epoch",
|
419 |
+
save_strategy="epoch",
|
420 |
+
load_best_model_at_end=True,
|
421 |
+
push_to_hub=True,
|
422 |
+
)
|
423 |
+
|
424 |
+
# ---- End of your code ----
|
425 |
+
|
426 |
+
|
427 |
+
# To use Hugging Face `Trainer` you need to install the `accelerate` library version `0.20.1` or later. It is used for performance enhancement on PyTorch.
|
428 |
+
|
429 |
+
# In[15]:
|
430 |
+
|
431 |
+
|
432 |
+
get_ipython().run_line_magic('pip', 'install accelerate -U')
|
433 |
+
|
434 |
+
|
435 |
+
# In[ ]:
|
436 |
+
|
437 |
+
|
438 |
+
from transformers import DefaultDataCollator
|
439 |
+
|
440 |
+
# Instantiate the trainer
|
441 |
+
|
442 |
+
# Update your code here:
|
443 |
+
trainer = Trainer(
|
444 |
+
model=model,
|
445 |
+
args=training_args,
|
446 |
+
train_dataset=emotion_transformed["train"],
|
447 |
+
eval_dataset=emotion_transformed["test"],
|
448 |
+
compute_metrics=compute_metrics,
|
449 |
+
data_collator=DefaultDataCollator(),
|
450 |
+
)
|
451 |
+
# ---- End of your code ----
|
452 |
+
|
453 |
+
|
454 |
+
# If there are problems when using the Trainer after installing `accelerate` you can restart the Kernel
|
455 |
+
|
456 |
+
# ### Step 7: Train and Evaluate the Model
|
457 |
+
#
|
458 |
+
# Now, you are ready to train the model and evaluate it on the test set.
|
459 |
+
|
460 |
+
# In[ ]:
|
461 |
+
|
462 |
+
|
463 |
+
# Train the model
|
464 |
+
|
465 |
+
# Put your code here:
|
466 |
+
trainer.train()
|
467 |
+
# ---- End of your code ----
|
468 |
+
|
469 |
+
|
470 |
+
# In[ ]:
|
471 |
+
|
472 |
+
|
473 |
+
# Evaluate the model
|
474 |
+
|
475 |
+
# Update your code here:
|
476 |
+
eval_result = trainer.evaluate()
|
477 |
+
|
478 |
+
# ---- End of your code ----
|
479 |
+
|
480 |
+
# Save the formatted accuracy in a variable
|
481 |
+
accuracy_str = "{:.4f}".format(eval_result["eval_accuracy"])
|
482 |
+
print(f"Accuracy: {accuracy_str}")
|
483 |
+
|
484 |
+
|
485 |
+
# ### Step 8: Publishing the Trained Model
|
486 |
+
#
|
487 |
+
# Finally, make sure to push your trained model to the Hugging Face Model Hub.
|
488 |
+
#
|
489 |
+
# > **Note**: No need to change the code below! Just run to publish your model.
|
490 |
+
|
491 |
+
# In[ ]:
|
492 |
+
|
493 |
+
|
494 |
+
trainer.push_to_hub()
|
495 |
+
|
496 |
+
|
497 |
+
# Once you've trained your model and pushed it to the Hugging Face Model Hub, you'll have a link that points directly to your model's page. You can share this link with others, and they can use it to directly load your model for their own uses.
|
498 |
+
#
|
499 |
+
# The following link is an example of what a trained model's page looks like: https://huggingface.co/aditira/emotion_classification. This is not your model, but rather an example of what your final result might resemble.
|
500 |
+
#
|
501 |
+
# Remember, for this project you should push your output model to your own Hugging Face account. The link for your model will be different and should reflect your own username and model name.
|
502 |
+
|
503 |
+
# # Submit Method
|
504 |
+
# huggingface_model_link = "" # Put your model link
|
505 |
+
#
|
506 |
+
# assignment_id = "00_transfer_learning"
|
507 |
+
# question_id = "00_emotion_recognition_huggingface"
|
508 |
+
# submit(student_id, name, assignment_id, huggingface_model_link, question_id, drive_link)
|
509 |
+
#
|
510 |
+
# question_id = "01_emotion_recognition_accuracy"
|
511 |
+
# submit(student_id, name, assignment_id, accuracy_str, question_id, drive_link)
|
512 |
+
|
513 |
+
# ### Step 9: Build an Interactive Application with Gradio
|
514 |
+
|
515 |
+
# In this task, you will be building an interactive application using Gradio that will use your fine-tuned emotion recognition model along with another pretrained model ('`nateraw/vit-age-classifier`') to guess the emotion and age from an input image.
|
516 |
+
#
|
517 |
+
# Please make sure to:
|
518 |
+
# - Install the necessary package (`gradio`) for creating the web-based interface.
|
519 |
+
# - Load your fine-tuned model as well as the pretrained model '`nateraw/vit-age-classifier`'.
|
520 |
+
# - Define a function that will take an image as input and return the predicted emotion and age.
|
521 |
+
# - Utilize Gradio to create an Interface (UI) for your function, allowing users to upload images and see the predicted emotion and age.
|
522 |
+
|
523 |
+
# In[ ]:
|
524 |
+
|
525 |
+
|
526 |
+
# Install Gradio
|
527 |
+
get_ipython().system('pip install gradio')
|
528 |
+
|
529 |
+
|
530 |
+
# #### Submisssion
|
531 |
+
#
|
532 |
+
# 
|
533 |
+
#
|
534 |
+
# You need to submit screenshot of your Gradio's app. In Google Colab you can just use the "Folder" sidebar and click the upload button. Make sure your screenshot match below requirements:
|
535 |
+
#
|
536 |
+
# - Image name screenshot is `submission.jpg`
|
537 |
+
# - You should upload a person's image to that app
|
538 |
+
# - The score should be included at the screenshot
|
539 |
+
|
540 |
+
# In[ ]:
|
541 |
+
|
542 |
+
|
543 |
+
# Put your code here:
|
544 |
+
def predict_age_and_emotion(image):
|
545 |
+
# Age prediction
|
546 |
+
age_classifier = pipeline("image-classification", model="nateraw/vit-age-classifier")
|
547 |
+
age_pred = age_classifier(image)
|
548 |
+
max_age_pred = max(age_pred, key=lambda x: x['score'])
|
549 |
+
|
550 |
+
# Emotion prediction
|
551 |
+
# Replace YOUR_USERNAME with your actual Hugging Face username
|
552 |
+
emotion_classifier = pipeline("image-classification", model="YOUR_USERNAME/emotion-classifier")
|
553 |
+
emotion_pred = emotion_classifier(image)
|
554 |
+
max_emotion_pred = max(emotion_pred, key=lambda x: x['score'])
|
555 |
+
|
556 |
+
return (
|
557 |
+
f"Age: {max_age_pred['label']} (confidence: {max_age_pred['score']:.5f})",
|
558 |
+
f"Emotion: {max_emotion_pred['label']} (confidence: {max_emotion_pred['score']:.5f})"
|
559 |
+
)
|
560 |
+
|
561 |
+
demo_combined = gr.Interface(
|
562 |
+
fn=predict_age_and_emotion,
|
563 |
+
inputs=gr.Image(),
|
564 |
+
outputs=["text", "text"],
|
565 |
+
title="Age and Emotion Classification"
|
566 |
+
)
|
567 |
+
|
568 |
+
demo_combined.launch()
|
569 |
+
# ---- End of your code ----
|
570 |
+
|
571 |
+
|
572 |
+
# Example of Expected Output:
|
573 |
+
#
|
574 |
+
# 
|
575 |
+
|
576 |
+
# > Note: If your submission for Task-2 did not run (After you run it never changes from "*" to a number), stop the Code block that's running the Gradio app, then the submission will run. To stop the Code block, you can click on the Code block and then click the stop button.
|
emotion-classifier/runs/Feb23_03-09-15_LAPTOP-VKSJJT34/events.out.tfevents.1740255001.LAPTOP-VKSJJT34.10956.0
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:df5c9e86fb96834d47e966966b2d771fe78f96f083b0a72d43f0f801f77eab3e
|
3 |
+
size 5256
|
emotion-classifier/runs/Feb23_03-21-00_LAPTOP-VKSJJT34/events.out.tfevents.1740255792.LAPTOP-VKSJJT34.10956.1
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:41089a6c3f2ddc3f7883da97570aa90d19546a73557e8f2b042d134c02c029c3
|
3 |
+
size 5256
|
emotion-classifier/runs/Feb23_03-27-01_LAPTOP-VKSJJT34/events.out.tfevents.1740256033.LAPTOP-VKSJJT34.21776.0
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e70406af94dea2575ec8d1aabb623a2c87facf19b73863d2e7767f7c3cbbc891
|
3 |
+
size 5256
|
nyoba-03A.ipynb
ADDED
@@ -0,0 +1,485 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "_8ZwOksxZ0-q"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"# Project: Deep Learning - Calculations"
|
10 |
+
]
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"cell_type": "markdown",
|
14 |
+
"metadata": {
|
15 |
+
"id": "_R3K9DR2Z0-r"
|
16 |
+
},
|
17 |
+
"source": [
|
18 |
+
"**Instructions for Students:**\n",
|
19 |
+
"\n",
|
20 |
+
"Please carefully follow these steps to complete and submit your project:\n",
|
21 |
+
"\n",
|
22 |
+
"1. **Make a copy of the Project**: Please make a copy of this project either to your own Google Drive or download locally. Work on the copy of the project. The master project is **Read-Only**, meaning you can edit, but it will not be saved when you close the master project. To avoid total loss of your work, remember to make a copy.\n",
|
23 |
+
"\n",
|
24 |
+
"2. **Completing the Project**: You are required to work on and complete all tasks in the provided project. Be disciplined and ensure that you thoroughly engage with each task.\n",
|
25 |
+
" \n",
|
26 |
+
"3. **Creating a Google Drive Folder**: Each of you must create a new folder on your Google Drive. This will be the repository for all your completed project files, aiding you in keeping your work organized and accessible.\n",
|
27 |
+
" \n",
|
28 |
+
"4. **Uploading Completed Project**: Upon completion of your project, make sure to upload all necessary files, involving codes, reports, and related documents into the created Google Drive folder. Save this link in the 'Student Identity' section and also provide it as the last parameter in the `submit` function that has been provided.\n",
|
29 |
+
" \n",
|
30 |
+
"5. **Sharing Folder Link**: You're required to share the link to your project Google Drive folder. This is crucial for the submission and evaluation of your project.\n",
|
31 |
+
" \n",
|
32 |
+
"6. **Setting Permission to Public**: Please make sure your Google Drive folder is set to public. This allows your instructor to access your solutions and assess your work correctly.\n",
|
33 |
+
"\n",
|
34 |
+
"Adhering to these procedures will facilitate a smooth project evaluation process for you and the reviewers."
|
35 |
+
]
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"cell_type": "markdown",
|
39 |
+
"metadata": {
|
40 |
+
"id": "XeK2fKDrZ0-s"
|
41 |
+
},
|
42 |
+
"source": [
|
43 |
+
"## Project Description\n",
|
44 |
+
"\n",
|
45 |
+
"The Deep Learning Projects are divided into two parts, the first is the Calculations worth 30% in this notebook and the second one is Pytorch Project worth 70%.\n",
|
46 |
+
"\n",
|
47 |
+
"The two projects will help you gain experience to learn about Deep Learning in detail.\n",
|
48 |
+
"\n",
|
49 |
+
"Happy coding!"
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "markdown",
|
54 |
+
"metadata": {
|
55 |
+
"id": "Lgd8szRCZ0-t"
|
56 |
+
},
|
57 |
+
"source": [
|
58 |
+
"## Grading Criteria\n",
|
59 |
+
"\n",
|
60 |
+
"There are 4 primary tasks in this project, all have the same weight. Each task will give you either 100 point if you are correct and 0 if you are wrong. The final score for the project will the the average of all 4 tasks.\n",
|
61 |
+
"\n",
|
62 |
+
"There is also an optional task (Task 5) that you can do to challenge your understanding.\n",
|
63 |
+
"\n",
|
64 |
+
"* Task 1: This task will assess your ability to do basic matrix multiplication which is an important part in machine and deep learning.\n",
|
65 |
+
"\n",
|
66 |
+
"* Task 2: This task will assess your ability to understand how a neural network work through weight and biases.\n",
|
67 |
+
"\n",
|
68 |
+
"* Task 3: The task will assess your ability to understand how a neural network layer works.\n",
|
69 |
+
"\n",
|
70 |
+
"* Task 4: This task will assess your ability to understand how backpropagation works in a neural network.\n",
|
71 |
+
"\n",
|
72 |
+
"* Task 5 (optional): This task will assess your ability to understand how to calculate cost and why it's important by using backpropagation in a neural network.\n"
|
73 |
+
]
|
74 |
+
},
|
75 |
+
{
|
76 |
+
"cell_type": "markdown",
|
77 |
+
"metadata": {
|
78 |
+
"id": "W1dT1yHmZ0-t"
|
79 |
+
},
|
80 |
+
"source": [
|
81 |
+
"## Student Identity"
|
82 |
+
]
|
83 |
+
},
|
84 |
+
{
|
85 |
+
"cell_type": "markdown",
|
86 |
+
"metadata": {
|
87 |
+
"id": "TuVolX9FZ0-u"
|
88 |
+
},
|
89 |
+
"source": [
|
90 |
+
"## Import package"
|
91 |
+
]
|
92 |
+
},
|
93 |
+
{
|
94 |
+
"cell_type": "code",
|
95 |
+
"execution_count": null,
|
96 |
+
"metadata": {
|
97 |
+
"id": "Pl3QEX2HZ0-u"
|
98 |
+
},
|
99 |
+
"outputs": [],
|
100 |
+
"source": [
|
101 |
+
"import numpy as np\n",
|
102 |
+
"import matplotlib.pyplot as plt \n",
|
103 |
+
"import graphviz"
|
104 |
+
]
|
105 |
+
},
|
106 |
+
{
|
107 |
+
"cell_type": "markdown",
|
108 |
+
"metadata": {
|
109 |
+
"id": "4gtGSA8SZ0-v"
|
110 |
+
},
|
111 |
+
"source": [
|
112 |
+
"\n",
|
113 |
+
"## Task 1\n",
|
114 |
+
"\n",
|
115 |
+
"Given\n",
|
116 |
+
"\n",
|
117 |
+
"$$\n",
|
118 |
+
"X = \\begin{bmatrix}\n",
|
119 |
+
"a \\\\\n",
|
120 |
+
"b \\\\\n",
|
121 |
+
"c \\\\\n",
|
122 |
+
"d \\\\\n",
|
123 |
+
"e\n",
|
124 |
+
"\\end{bmatrix}\n",
|
125 |
+
"$$\n",
|
126 |
+
"\n",
|
127 |
+
"and\n",
|
128 |
+
"\n",
|
129 |
+
"$$\n",
|
130 |
+
"M =\n",
|
131 |
+
"\\begin{bmatrix}\n",
|
132 |
+
"-2 & 3 & 3 & 3 & -4 \\\\\n",
|
133 |
+
"0 & -4 & -1 & 1 & 2 \\\\\n",
|
134 |
+
"1 & 5 & 4 & 2 & 0 \\\\\n",
|
135 |
+
"-2 & 5 & -5 & 3 & 1 \\\\\n",
|
136 |
+
"-3 & 2 & 4 & 3 & 4 \\\\\n",
|
137 |
+
"\\end{bmatrix}\n",
|
138 |
+
"$$\n",
|
139 |
+
"\n",
|
140 |
+
"and\n",
|
141 |
+
"\n",
|
142 |
+
"$$\n",
|
143 |
+
"M \\cdot X =\n",
|
144 |
+
" =\n",
|
145 |
+
"\\begin{bmatrix}\n",
|
146 |
+
"-33 \\\\\n",
|
147 |
+
"9 \\\\\n",
|
148 |
+
"-34 \\\\\n",
|
149 |
+
"-38 \\\\\n",
|
150 |
+
" -40\n",
|
151 |
+
"\\end{bmatrix}\n",
|
152 |
+
"$$\n",
|
153 |
+
"\n",
|
154 |
+
"What is the value of $a + b + c + d + e$?"
|
155 |
+
]
|
156 |
+
},
|
157 |
+
{
|
158 |
+
"cell_type": "code",
|
159 |
+
"execution_count": null,
|
160 |
+
"metadata": {
|
161 |
+
"id": "JCwFuKpEZ0-v"
|
162 |
+
},
|
163 |
+
"outputs": [],
|
164 |
+
"source": [
|
165 |
+
"# You may add any code here to derive your variables\n",
|
166 |
+
"# Please change this\n",
|
167 |
+
"M = np.array([\n",
|
168 |
+
" [-2, 3, 3, 3, -4],\n",
|
169 |
+
" [0, -4, -1, 1, 2],\n",
|
170 |
+
" [1, 5, 4, 2, 0],\n",
|
171 |
+
" [-2, 5, -5, 3, 1],\n",
|
172 |
+
" [-3, 2, 4, 3, 4]\n",
|
173 |
+
"])\n",
|
174 |
+
"\n",
|
175 |
+
"MX = np.array([-33, 9, -34, -38, -40])\n",
|
176 |
+
"\n",
|
177 |
+
"# Solve for X\n",
|
178 |
+
"X = np.linalg.solve(M, MX)\n",
|
179 |
+
"\n",
|
180 |
+
"# Calculate the sum of a, b, c, d, e\n",
|
181 |
+
"total = np.sum(X)\n",
|
182 |
+
"\n",
|
183 |
+
"\n",
|
184 |
+
"print(f\"The total is {total}\")"
|
185 |
+
]
|
186 |
+
},
|
187 |
+
{
|
188 |
+
"cell_type": "markdown",
|
189 |
+
"metadata": {
|
190 |
+
"id": "AnzwjHI5Z0-v"
|
191 |
+
},
|
192 |
+
"source": [
|
193 |
+
"## Task 2\n",
|
194 |
+
"\n",
|
195 |
+
"What is the output of the following Neural Network?"
|
196 |
+
]
|
197 |
+
},
|
198 |
+
{
|
199 |
+
"cell_type": "code",
|
200 |
+
"execution_count": null,
|
201 |
+
"metadata": {
|
202 |
+
"id": "5Dhvs1l-Z0-v",
|
203 |
+
"outputId": "8fec2c6f-eb16-4791-bcee-d3414a8b7fab"
|
204 |
+
},
|
205 |
+
"outputs": [],
|
206 |
+
"source": [
|
207 |
+
"from fastbook import *\n",
|
208 |
+
"\n",
|
209 |
+
"\n",
|
210 |
+
"# Draw neurons with multiple inputs and weights\n",
|
211 |
+
"gv('''\n",
|
212 |
+
"z_1[shape=box3d width=1 height=0.7 label=\"b = -2, ReLU\"];\n",
|
213 |
+
"z_2[shape=box3d width=1 height=0.7 label=\"b = -6, ReLU\"];\n",
|
214 |
+
"z_3[shape=box3d width=1 height=0.7 label=\"b = 4, ReLU\"];\n",
|
215 |
+
"input_1[width=1 height=0.7 label=\"4\"];\n",
|
216 |
+
"input_2[width=1 height=0.7 label=\"-3\"];\n",
|
217 |
+
"input_1 -> z_1 [label=\"4\"]\n",
|
218 |
+
"input_2 -> z_1 [label=\"2\"]\n",
|
219 |
+
"input_1 -> z_2 [label=\"0\"]\n",
|
220 |
+
"input_2 -> z_2 [label=\"-1\"]\n",
|
221 |
+
"\n",
|
222 |
+
"z_1 -> z_3 [label=\"2\"]\n",
|
223 |
+
"z_2 -> z_3 [label=\"3\"]\n",
|
224 |
+
"z_3 ->output\n",
|
225 |
+
"\n",
|
226 |
+
"\n",
|
227 |
+
"''')\n"
|
228 |
+
]
|
229 |
+
},
|
230 |
+
{
|
231 |
+
"cell_type": "code",
|
232 |
+
"execution_count": null,
|
233 |
+
"metadata": {
|
234 |
+
"id": "jtzFBQeIZ0-w"
|
235 |
+
},
|
236 |
+
"outputs": [],
|
237 |
+
"source": [
|
238 |
+
"# You may add any code here to derive your variables\n",
|
239 |
+
"\n",
|
240 |
+
"# Inputs\n",
|
241 |
+
"input_1 = 4 # This is the first input\n",
|
242 |
+
"input_2 = -3 # This is the second input\n",
|
243 |
+
"\n",
|
244 |
+
"# Weights and biases\n",
|
245 |
+
"w1 = np.array([4, 2]) # Weights for the first neuron (z1)\n",
|
246 |
+
"w2 = np.array([0, -1]) # Weights for the second neuron (z2)\n",
|
247 |
+
"b1 = -2 # Bias for the first neuron (z1)\n",
|
248 |
+
"b2 = -6 # Bias for the second neuron (z2)\n",
|
249 |
+
"\n",
|
250 |
+
"# Calculate z1 and z2\n",
|
251 |
+
"z1 = max(0, input_1 * w1[0] + input_2 * w1[1] + b1) # ReLU activation for z1\n",
|
252 |
+
"z2 = max(0, input_1 * w2[0] + input_2 * w2[1] + b2) # ReLU activation for z2\n",
|
253 |
+
"\n",
|
254 |
+
"# Weights and bias for the next layer\n",
|
255 |
+
"w3 = np.array([2, 3]) # Weights for the output neuron\n",
|
256 |
+
"b3 = 4 # Bias for the output neuron\n",
|
257 |
+
"\n",
|
258 |
+
"# Calculate the output\n",
|
259 |
+
"output = max(0, z1 * w3[0] + z2 * w3[1] + b3)\n",
|
260 |
+
"\n",
|
261 |
+
"\n",
|
262 |
+
"print(f\"The output is {output}\")\n"
|
263 |
+
]
|
264 |
+
},
|
265 |
+
{
|
266 |
+
"cell_type": "markdown",
|
267 |
+
"metadata": {
|
268 |
+
"id": "nw5Rc4_QZ0-w"
|
269 |
+
},
|
270 |
+
"source": [
|
271 |
+
"## Task 3\n",
|
272 |
+
"\n",
|
273 |
+
"Given the following Neural Networks:\n",
|
274 |
+
"\n",
|
275 |
+
"First layer:\n",
|
276 |
+
"\n",
|
277 |
+
"$$\n",
|
278 |
+
"W = \\begin{bmatrix}\n",
|
279 |
+
"0.23 & 0.67 & 0.12 \\\\\n",
|
280 |
+
"-0.89 & -0.45 & 0.78 \\\\\n",
|
281 |
+
"0.34 & 0.56 & -0.90 \\\\\n",
|
282 |
+
"-0.12 & 0.34 & 0.56 \\\\\n",
|
283 |
+
"0.78 & -0.90 & 0.23 \\\\\n",
|
284 |
+
"\\end{bmatrix}\n",
|
285 |
+
"$$\n",
|
286 |
+
"\n",
|
287 |
+
"$$\n",
|
288 |
+
"bias = \\begin{bmatrix}\n",
|
289 |
+
"0.23 \\\\\n",
|
290 |
+
"-0.89 \\\\\n",
|
291 |
+
"0.34 \\\\\n",
|
292 |
+
"0.12 \\\\\n",
|
293 |
+
"-0.78 \\\\\n",
|
294 |
+
"\\end{bmatrix}\n",
|
295 |
+
"$$\n",
|
296 |
+
"\n",
|
297 |
+
"Second layer:\n",
|
298 |
+
"\n",
|
299 |
+
"$$\n",
|
300 |
+
"W = \\begin{bmatrix}\n",
|
301 |
+
"0.23 & 0.67 & 0.12 & 0.45 & 0.89 \\\\\n",
|
302 |
+
"0.12 & 0.34 & 0.56 & 0.78 & 0.90 \\\\\n",
|
303 |
+
"\\end{bmatrix}\n",
|
304 |
+
"$$\n",
|
305 |
+
"\n",
|
306 |
+
"$$\n",
|
307 |
+
"bias = \\begin{bmatrix}\n",
|
308 |
+
"1.96 \\\\\n",
|
309 |
+
"-1.08 \\\\\n",
|
310 |
+
"\\end{bmatrix}\n",
|
311 |
+
"$$\n",
|
312 |
+
"\n",
|
313 |
+
"Third layer:\n",
|
314 |
+
"$$\n",
|
315 |
+
"W = \\begin{bmatrix}\n",
|
316 |
+
"1.08 & -0.16\n",
|
317 |
+
"\\end{bmatrix}\n",
|
318 |
+
"$$\n",
|
319 |
+
"\n",
|
320 |
+
"$$\n",
|
321 |
+
"bias = \\begin{bmatrix}\n",
|
322 |
+
"-2.8\n",
|
323 |
+
"\\end{bmatrix}\n",
|
324 |
+
"$$\n",
|
325 |
+
"\n",
|
326 |
+
"All layers use the ReLU activation function.\n",
|
327 |
+
"\n",
|
328 |
+
"What is the output given the following inputs?\n",
|
329 |
+
"\n",
|
330 |
+
"$$\n",
|
331 |
+
"X = \\begin{bmatrix}\n",
|
332 |
+
"1 \\\\\n",
|
333 |
+
"2 \\\\\n",
|
334 |
+
"4 \\\\\n",
|
335 |
+
"\\end{bmatrix}\n",
|
336 |
+
"\n"
|
337 |
+
]
|
338 |
+
},
|
339 |
+
{
|
340 |
+
"cell_type": "code",
|
341 |
+
"execution_count": null,
|
342 |
+
"metadata": {
|
343 |
+
"id": "4d8Oibi1Z0-x"
|
344 |
+
},
|
345 |
+
"outputs": [],
|
346 |
+
"source": [
|
347 |
+
"# You may add any code here to derive your variables\n",
|
348 |
+
"# Input\n",
|
349 |
+
"X = np.array([1, 2, 4])\n",
|
350 |
+
"\n",
|
351 |
+
"# First layer weights and biases\n",
|
352 |
+
"W1 = np.array([\n",
|
353 |
+
" [0.23, 0.67, 0.12],\n",
|
354 |
+
" [-0.89, -0.45, 0.78],\n",
|
355 |
+
" [0.34, 0.56, -0.90],\n",
|
356 |
+
" [-0.12, 0.34, 0.56],\n",
|
357 |
+
" [0.78, -0.90, 0.23]\n",
|
358 |
+
"])\n",
|
359 |
+
"b1 = np.array([0.23, -0.89, 0.34, 0.12, -0.78])\n",
|
360 |
+
"\n",
|
361 |
+
"# Second layer weights and biases\n",
|
362 |
+
"W2 = np.array([\n",
|
363 |
+
" [0.23, 0.67, 0.12, 0.45, 0.89],\n",
|
364 |
+
" [0.12, 0.34, 0.56, 0.78, 0.90]\n",
|
365 |
+
"])\n",
|
366 |
+
"b2 = np.array([1.96, -1.08])\n",
|
367 |
+
"\n",
|
368 |
+
"# Third layer weights and biases\n",
|
369 |
+
"W3 = np.array([1.08, -0.16])\n",
|
370 |
+
"b3 = np.array([-2.8])\n",
|
371 |
+
"\n",
|
372 |
+
"# First layer calculation\n",
|
373 |
+
"z1 = np.maximum(0, np.dot(W1, X) + b1)\n",
|
374 |
+
"\n",
|
375 |
+
"# Second layer calculation\n",
|
376 |
+
"z2 = np.maximum(0, np.dot(W2, z1) + b2)\n",
|
377 |
+
"\n",
|
378 |
+
"# Third layer calculation\n",
|
379 |
+
"output = np.maximum(0, np.dot(W3, z2) + b3)\n",
|
380 |
+
"\n",
|
381 |
+
"print(f\"The output is {output}\")\n",
|
382 |
+
"\n"
|
383 |
+
]
|
384 |
+
},
|
385 |
+
{
|
386 |
+
"cell_type": "markdown",
|
387 |
+
"metadata": {
|
388 |
+
"id": "Vn5o5XmfZ0-x"
|
389 |
+
},
|
390 |
+
"source": [
|
391 |
+
"## Task 4\n",
|
392 |
+
"\n",
|
393 |
+
"Given [the following sheet (sheet name = Project A)](https://docs.google.com/spreadsheets/d/15JWbRFB4k5CNcfD-2hduHHssXIc1SGhNVSpu_30wVAw/edit#gid=180755192)\n",
|
394 |
+
"\n",
|
395 |
+
"Make a backpropagation algorithm to train the network. You can refer to AND and NOT sheet. Use sigmoid activation function for all\n",
|
396 |
+
"\n",
|
397 |
+
"Hint: it needs 1 layer with 1 neuron\n",
|
398 |
+
"\n",
|
399 |
+
"What is the cost at 10th iteration (the iteration start from 1)?"
|
400 |
+
]
|
401 |
+
},
|
402 |
+
{
|
403 |
+
"cell_type": "code",
|
404 |
+
"execution_count": null,
|
405 |
+
"metadata": {
|
406 |
+
"id": "d2aSUbaaZ0-x"
|
407 |
+
},
|
408 |
+
"outputs": [],
|
409 |
+
"source": [
|
410 |
+
"# Please clone the Google Sheet and do your calculation there\n",
|
411 |
+
"\n",
|
412 |
+
"# Please change this\n",
|
413 |
+
"cost = 0\n",
|
414 |
+
"\n",
|
415 |
+
"# Don't forget to fill in the link to your Google Sheet\n",
|
416 |
+
"link_to_gsheet = \"\"\n",
|
417 |
+
"\n",
|
418 |
+
"print(f\"The cost is {cost}\")\n",
|
419 |
+
"\n"
|
420 |
+
]
|
421 |
+
},
|
422 |
+
{
|
423 |
+
"cell_type": "markdown",
|
424 |
+
"metadata": {
|
425 |
+
"id": "Kh7uzjtDZ0-x"
|
426 |
+
},
|
427 |
+
"source": [
|
428 |
+
"## Task 5 (Bonus - Optional)\n",
|
429 |
+
"\n",
|
430 |
+
"_This task is optional, it will earn you a distinction, but will not give you additional points._\n",
|
431 |
+
"\n",
|
432 |
+
"Given [the following sheet (sheet name = Project B)](https://docs.google.com/spreadsheets/d/15JWbRFB4k5CNcfD-2hduHHssXIc1SGhNVSpu_30wVAw/edit#gid=1029811725)\n",
|
433 |
+
"\n",
|
434 |
+
"Make a backpropagation algorithm to train the network. You can refer to AND and NOT sheet. Use sigmoid activation function for all\n",
|
435 |
+
"\n",
|
436 |
+
"Hint: it needs 2 layer (1 hidden layer + 1 output layer). The hidden layer has 2 neurons\n",
|
437 |
+
"\n",
|
438 |
+
"What is the cost at 10th iteration (the iteration start from 1)?"
|
439 |
+
]
|
440 |
+
},
|
441 |
+
{
|
442 |
+
"cell_type": "code",
|
443 |
+
"execution_count": null,
|
444 |
+
"metadata": {
|
445 |
+
"id": "KJJOi3V1Z0-x"
|
446 |
+
},
|
447 |
+
"outputs": [],
|
448 |
+
"source": [
|
449 |
+
"# Please clone the Google Sheet and do your calculation there\n",
|
450 |
+
"\n",
|
451 |
+
"# Please change this\n",
|
452 |
+
"cost = 0\n",
|
453 |
+
"\n",
|
454 |
+
"# Don't forget to fill in the link to your Google Sheet\n",
|
455 |
+
"link_to_gsheet = \"\"\n",
|
456 |
+
"\n",
|
457 |
+
"print(f\"The cost is {cost}\")\n"
|
458 |
+
]
|
459 |
+
}
|
460 |
+
],
|
461 |
+
"metadata": {
|
462 |
+
"colab": {
|
463 |
+
"provenance": []
|
464 |
+
},
|
465 |
+
"kernelspec": {
|
466 |
+
"display_name": "Python 3",
|
467 |
+
"language": "python",
|
468 |
+
"name": "python3"
|
469 |
+
},
|
470 |
+
"language_info": {
|
471 |
+
"codemirror_mode": {
|
472 |
+
"name": "ipython",
|
473 |
+
"version": 3
|
474 |
+
},
|
475 |
+
"file_extension": ".py",
|
476 |
+
"mimetype": "text/x-python",
|
477 |
+
"name": "python",
|
478 |
+
"nbconvert_exporter": "python",
|
479 |
+
"pygments_lexer": "ipython3",
|
480 |
+
"version": "3.9.6"
|
481 |
+
}
|
482 |
+
},
|
483 |
+
"nbformat": 4,
|
484 |
+
"nbformat_minor": 0
|
485 |
+
}
|
nyoba-03B.ipynb
ADDED
@@ -0,0 +1,536 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "def8e46c-7606-4991-9b29-9223ccacd54b",
|
6 |
+
"metadata": {
|
7 |
+
"id": "def8e46c-7606-4991-9b29-9223ccacd54b"
|
8 |
+
},
|
9 |
+
"source": [
|
10 |
+
"# Project: Deep Learning - Pytorch"
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "markdown",
|
15 |
+
"id": "911a145c-7d01-4210-9699-0e1fef80ff19",
|
16 |
+
"metadata": {
|
17 |
+
"id": "911a145c-7d01-4210-9699-0e1fef80ff19"
|
18 |
+
},
|
19 |
+
"source": [
|
20 |
+
"**Instructions for Students:**\n",
|
21 |
+
"\n",
|
22 |
+
"Please carefully follow these steps to complete and submit your project:\n",
|
23 |
+
"\n",
|
24 |
+
"1. **Make a copy of the Project**: Please make a copy of this project either to your own Google Drive or download locally. Work on the copy of the project. The master project is **Read-Only**, meaning you can edit, but it will not be saved when you close the master project. To avoid total loss of your work, remember to make a copy.\n",
|
25 |
+
"\n",
|
26 |
+
"2. **Completing the Project**: You are required to work on and complete all tasks in the provided project. Be disciplined and ensure that you thoroughly engage with each task.\n",
|
27 |
+
" \n",
|
28 |
+
"3. **Creating a Google Drive Folder**: Each of you must create a new folder on your Google Drive. This will be the repository for all your completed project files, aiding you in keeping your work organized and accessible.\n",
|
29 |
+
" \n",
|
30 |
+
"4. **Uploading Completed Project**: Upon completion of your project, make sure to upload all necessary files, involving codes, reports, and related documents into the created Google Drive folder. Save this link in the 'Student Identity' section and also provide it as the last parameter in the `submit` function that has been provided.\n",
|
31 |
+
" \n",
|
32 |
+
"5. **Sharing Folder Link**: You're required to share the link to your project Google Drive folder. This is crucial for the submission and evaluation of your project.\n",
|
33 |
+
" \n",
|
34 |
+
"6. **Setting Permission to Public**: Please make sure your Google Drive folder is set to public. This allows your instructor to access your solutions and assess your work correctly.\n",
|
35 |
+
"\n",
|
36 |
+
"Adhering to these procedures will facilitate a smooth project evaluation process for you and the reviewers."
|
37 |
+
]
|
38 |
+
},
|
39 |
+
{
|
40 |
+
"cell_type": "markdown",
|
41 |
+
"id": "d3b9e0f7-75d2-476a-9b29-07c1a3800925",
|
42 |
+
"metadata": {
|
43 |
+
"id": "d3b9e0f7-75d2-476a-9b29-07c1a3800925"
|
44 |
+
},
|
45 |
+
"source": [
|
46 |
+
"## Project Description\n",
|
47 |
+
"\n",
|
48 |
+
"The Deep Learning Projects are divided into two parts, the first is the Calculations worth 30% and the second one is Pytorch Project worth 70% in this notebook.\n",
|
49 |
+
"\n",
|
50 |
+
"The two projects will help you gain experience to learn about Deep Learning in detail.\n",
|
51 |
+
"\n",
|
52 |
+
"In this project, you will use what you learn to create your own Deep Learning model. We'll use a variety of datasets, each with different data types such as images, text, and numerical/categorical data. Your task is to choose one of these datasets and build a deep learning model upon it.\n"
|
53 |
+
]
|
54 |
+
},
|
55 |
+
{
|
56 |
+
"cell_type": "markdown",
|
57 |
+
"id": "d333f8fe-3ae6-4060-b597-e9f7d9e37582",
|
58 |
+
"metadata": {
|
59 |
+
"id": "d333f8fe-3ae6-4060-b597-e9f7d9e37582"
|
60 |
+
},
|
61 |
+
"source": [
|
62 |
+
"## Datasets\n",
|
63 |
+
"\n",
|
64 |
+
"Choose one from the following datasets for your project:\n",
|
65 |
+
"\n",
|
66 |
+
"1. **Digits Dataset**: A simplified version of MNIST containing 8x8 images of hand-written digits. [Dataset Link](https://scikit-learn.org/stable/auto_examples/datasets/plot_digits_last_image.html)\n",
|
67 |
+
"2. **Dogs vs. Cats Dataset from Kaggle**: A sizable dataset of 25,000 images with equal representation of dogs and cats. [Dataset Link](https://www.kaggle.com/c/dogs-vs-cats)\n",
|
68 |
+
"3. **Breast Cancer Wisconsin (Diagnostic) Dataset**: Computed features from a digitized image of a fine needle aspirate (FNA) of a breast mass. [Dataset Link](https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Diagnostic%29)\n",
|
69 |
+
"4. **Spam Text Message Classification Dataset**: A collection of labeled SMS messages, categorized as \"spam\" or \"ham\". [Dataset Link](https://www.kaggle.com/uciml/sms-spam-collection-dataset)\n",
|
70 |
+
"5. **German Credit Risk Dataset**: People's data categorized by various attributes to predict credit risk. [Dataset Link](https://archive.ics.uci.edu/ml/datasets/Statlog+%28German+Credit+Data%29)\n"
|
71 |
+
]
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"cell_type": "markdown",
|
75 |
+
"id": "84fea308-2c09-472c-a5ec-e4ba0900d496",
|
76 |
+
"metadata": {
|
77 |
+
"id": "84fea308-2c09-472c-a5ec-e4ba0900d496"
|
78 |
+
},
|
79 |
+
"source": [
|
80 |
+
"\n",
|
81 |
+
"## Grading Criteria\n",
|
82 |
+
"\n",
|
83 |
+
"Your work will be evaluated based on both accuracy and loss value:\n",
|
84 |
+
"\n",
|
85 |
+
"100: The model has an accuracy of more than 80% and a Loss Value of less than 0.2. This model is excellent and demonstrates a strong understanding of the task.\n",
|
86 |
+
"\n",
|
87 |
+
"90: The model has an accuracy between 70% - 79% and a Loss Value between 0.2 - 0.3. This model is very good, with some room for improvement.\n",
|
88 |
+
"\n",
|
89 |
+
"80: The model has an accuracy between 60% - 69% and a Loss Value between 0.3 - 0.4. This model is fairly good but needs improvement in balancing accuracy and loss value.\n",
|
90 |
+
"\n",
|
91 |
+
"70: The model has an accuracy between 50% - 59% and a Loss Value between 0.4 - 0.5. This model is below average and needs significant improvement.\n",
|
92 |
+
"\n",
|
93 |
+
"60 or below: The model has an accuracy of less than 50% or a Loss Value of more than 0.5, or the student did not submit the accuracy and Loss Value. This model is poor and needs considerable improvement."
|
94 |
+
]
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"cell_type": "markdown",
|
98 |
+
"id": "915df21b-3a7e-444a-999d-c830acdbe7f2",
|
99 |
+
"metadata": {
|
100 |
+
"id": "915df21b-3a7e-444a-999d-c830acdbe7f2"
|
101 |
+
},
|
102 |
+
"source": [
|
103 |
+
"Rmember to make a copy of this notebook in your Google Drive and work in your own copy.\n",
|
104 |
+
"\n",
|
105 |
+
"Let's start your deep learning journey! Choose your dataset and delve into the project! Happy modeling!"
|
106 |
+
]
|
107 |
+
},
|
108 |
+
{
|
109 |
+
"cell_type": "markdown",
|
110 |
+
"id": "8018f21d-d661-4ae4-a50d-9fb66feb291d",
|
111 |
+
"metadata": {
|
112 |
+
"id": "8018f21d-d661-4ae4-a50d-9fb66feb291d"
|
113 |
+
},
|
114 |
+
"source": [
|
115 |
+
"## Student Identity"
|
116 |
+
]
|
117 |
+
},
|
118 |
+
{
|
119 |
+
"cell_type": "markdown",
|
120 |
+
"id": "e0b68327-fc7b-43de-9f16-b058d11d775b",
|
121 |
+
"metadata": {
|
122 |
+
"id": "e0b68327-fc7b-43de-9f16-b058d11d775b"
|
123 |
+
},
|
124 |
+
"source": [
|
125 |
+
"## Project Structure\n",
|
126 |
+
"\n",
|
127 |
+
"Your project should be organized into five main sections."
|
128 |
+
]
|
129 |
+
},
|
130 |
+
{
|
131 |
+
"cell_type": "markdown",
|
132 |
+
"id": "25ecc459-5019-42d8-84a6-8644cecafef3",
|
133 |
+
"metadata": {
|
134 |
+
"id": "25ecc459-5019-42d8-84a6-8644cecafef3"
|
135 |
+
},
|
136 |
+
"source": [
|
137 |
+
"### 1. Package and Module Installation\n",
|
138 |
+
"\n",
|
139 |
+
"First, let's pool all package and module that you'll need in the installation section below."
|
140 |
+
]
|
141 |
+
},
|
142 |
+
{
|
143 |
+
"cell_type": "code",
|
144 |
+
"execution_count": null,
|
145 |
+
"id": "62606ca1-5868-4c65-93ef-1d6ee8d8d59a",
|
146 |
+
"metadata": {
|
147 |
+
"id": "62606ca1-5868-4c65-93ef-1d6ee8d8d59a"
|
148 |
+
},
|
149 |
+
"outputs": [],
|
150 |
+
"source": [
|
151 |
+
"# Write any package/module installation that you need\n",
|
152 |
+
"# pip install goes here, this helps declutter your output below\n",
|
153 |
+
"import pandas as pd\n",
|
154 |
+
"import numpy as np\n",
|
155 |
+
"from ucimlrepo import fetch_ucirepo \n",
|
156 |
+
"import plotly.graph_objects as go\n",
|
157 |
+
"from plotly.subplots import make_subplots\n",
|
158 |
+
"from sklearn.preprocessing import LabelEncoder\n",
|
159 |
+
"from sklearn.model_selection import train_test_split\n",
|
160 |
+
"import torch\n",
|
161 |
+
"import torch.nn as nn\n",
|
162 |
+
"import torch.optim as optim\n",
|
163 |
+
"from torch.utils.data import Dataset, DataLoader\n",
|
164 |
+
"from sklearn.preprocessing import StandardScaler\n"
|
165 |
+
]
|
166 |
+
},
|
167 |
+
{
|
168 |
+
"cell_type": "markdown",
|
169 |
+
"id": "b0837717-de95-4193-9898-e2f4cadfcfc6",
|
170 |
+
"metadata": {
|
171 |
+
"id": "b0837717-de95-4193-9898-e2f4cadfcfc6"
|
172 |
+
},
|
173 |
+
"source": [
|
174 |
+
"### 2. Data Loading and Preprocessing\n",
|
175 |
+
"\n",
|
176 |
+
"Load the chosen dataset and preprocess it for deep learning."
|
177 |
+
]
|
178 |
+
},
|
179 |
+
{
|
180 |
+
"cell_type": "code",
|
181 |
+
"execution_count": null,
|
182 |
+
"id": "6458c396",
|
183 |
+
"metadata": {},
|
184 |
+
"outputs": [],
|
185 |
+
"source": [
|
186 |
+
"# Fetch dataset from UCI repository based on documentation\n",
|
187 |
+
"breast_cancer_wisconsin_diagnostic = fetch_ucirepo(id=17)\n",
|
188 |
+
"\n",
|
189 |
+
"# Extract features (X) and target labels (y) based on documentation\n",
|
190 |
+
"X = breast_cancer_wisconsin_diagnostic.data.features # Features as pandas DataFrame\n",
|
191 |
+
"y = breast_cancer_wisconsin_diagnostic.data.targets # Target as pandas DataFrame\n",
|
192 |
+
"\n",
|
193 |
+
"# Combine features and target into a single DataFrame for visualization\n",
|
194 |
+
"df = pd.concat([X, y], axis=1)\n",
|
195 |
+
"\n",
|
196 |
+
"# Display first 5 rows of the full dataset\n",
|
197 |
+
"print(df.head())"
|
198 |
+
]
|
199 |
+
},
|
200 |
+
{
|
201 |
+
"cell_type": "code",
|
202 |
+
"execution_count": null,
|
203 |
+
"id": "f052beb7",
|
204 |
+
"metadata": {},
|
205 |
+
"outputs": [],
|
206 |
+
"source": [
|
207 |
+
"print(df.tail())"
|
208 |
+
]
|
209 |
+
},
|
210 |
+
{
|
211 |
+
"cell_type": "code",
|
212 |
+
"execution_count": null,
|
213 |
+
"id": "db941354",
|
214 |
+
"metadata": {},
|
215 |
+
"outputs": [],
|
216 |
+
"source": [
|
217 |
+
"df.info()"
|
218 |
+
]
|
219 |
+
},
|
220 |
+
{
|
221 |
+
"cell_type": "code",
|
222 |
+
"execution_count": null,
|
223 |
+
"id": "cd4056ec",
|
224 |
+
"metadata": {},
|
225 |
+
"outputs": [],
|
226 |
+
"source": [
|
227 |
+
"df.describe()"
|
228 |
+
]
|
229 |
+
},
|
230 |
+
{
|
231 |
+
"cell_type": "code",
|
232 |
+
"execution_count": null,
|
233 |
+
"id": "26e0715f",
|
234 |
+
"metadata": {},
|
235 |
+
"outputs": [],
|
236 |
+
"source": [
|
237 |
+
"df.isnull().sum()"
|
238 |
+
]
|
239 |
+
},
|
240 |
+
{
|
241 |
+
"cell_type": "code",
|
242 |
+
"execution_count": null,
|
243 |
+
"id": "b5512b49",
|
244 |
+
"metadata": {},
|
245 |
+
"outputs": [],
|
246 |
+
"source": [
|
247 |
+
"df.shape"
|
248 |
+
]
|
249 |
+
},
|
250 |
+
{
|
251 |
+
"cell_type": "code",
|
252 |
+
"execution_count": null,
|
253 |
+
"id": "fa185a09",
|
254 |
+
"metadata": {},
|
255 |
+
"outputs": [],
|
256 |
+
"source": [
|
257 |
+
"# Visualize feature distributions in a grid layout\n",
|
258 |
+
"features = df.columns[2:] # Select the feature columns\n",
|
259 |
+
"num_features = len(features)\n",
|
260 |
+
"num_cols = 3 # Number of columns in the grid\n",
|
261 |
+
"num_rows = (num_features + num_cols - 1) // num_cols # Calculate the number of rows\n",
|
262 |
+
"\n",
|
263 |
+
"fig = make_subplots(rows=num_rows, cols=num_cols, subplot_titles=features)\n",
|
264 |
+
"\n",
|
265 |
+
"for i, feature in enumerate(features):\n",
|
266 |
+
" row = i // num_cols + 1\n",
|
267 |
+
" col = i % num_cols + 1\n",
|
268 |
+
" \n",
|
269 |
+
" # Plot the histogram for the current feature\n",
|
270 |
+
" fig.add_trace(go.Histogram(x=df[df['Diagnosis'] == 'M'][feature], name='Malignant', opacity=0.7), row=row, col=col)\n",
|
271 |
+
" fig.add_trace(go.Histogram(x=df[df['Diagnosis'] == 'B'][feature], name='Benign', opacity=0.7), row=row, col=col)\n",
|
272 |
+
" \n",
|
273 |
+
" # Set subplot title\n",
|
274 |
+
" fig.update_xaxes(title_text=feature, row=row, col=col)\n",
|
275 |
+
"\n",
|
276 |
+
"# Update layout\n",
|
277 |
+
"fig.update_layout(showlegend=False, height=2000, width=1500, title_text=\"Feature Distributions\")\n",
|
278 |
+
"\n",
|
279 |
+
"fig.show()"
|
280 |
+
]
|
281 |
+
},
|
282 |
+
{
|
283 |
+
"cell_type": "code",
|
284 |
+
"execution_count": null,
|
285 |
+
"id": "20848bd8",
|
286 |
+
"metadata": {},
|
287 |
+
"outputs": [],
|
288 |
+
"source": [
|
289 |
+
"# Calculate the correlation matrix\n",
|
290 |
+
"cor = df.corr()\n",
|
291 |
+
"\n",
|
292 |
+
"# Create the heatmap trace\n",
|
293 |
+
"heatmap = go.Heatmap(\n",
|
294 |
+
" z=cor.values,\n",
|
295 |
+
" x=cor.columns,\n",
|
296 |
+
" y=cor.columns,\n",
|
297 |
+
" colorscale='RdBu',\n",
|
298 |
+
" colorbar=dict(title='Correlation')\n",
|
299 |
+
")\n",
|
300 |
+
"\n",
|
301 |
+
"# Create the layout\n",
|
302 |
+
"layout = go.Layout(\n",
|
303 |
+
" title='Correlation Matrix',\n",
|
304 |
+
" xaxis=dict(title='Features'),\n",
|
305 |
+
" yaxis=dict(title='Features'),\n",
|
306 |
+
")\n",
|
307 |
+
"\n",
|
308 |
+
"# Create the figure and add the trace\n",
|
309 |
+
"fig = go.Figure(data=[heatmap], layout=layout)\n",
|
310 |
+
"\n",
|
311 |
+
"# Show the figure\n",
|
312 |
+
"fig.show()"
|
313 |
+
]
|
314 |
+
},
|
315 |
+
{
|
316 |
+
"cell_type": "markdown",
|
317 |
+
"id": "30ce0c59-0e94-480a-81ae-528e68356a15",
|
318 |
+
"metadata": {
|
319 |
+
"id": "30ce0c59-0e94-480a-81ae-528e68356a15"
|
320 |
+
},
|
321 |
+
"source": [
|
322 |
+
"### 3. Model Building\n",
|
323 |
+
"\n",
|
324 |
+
"Define your deep learning model's architecture."
|
325 |
+
]
|
326 |
+
},
|
327 |
+
{
|
328 |
+
"cell_type": "code",
|
329 |
+
"execution_count": null,
|
330 |
+
"id": "1aa3c3f7-c6c5-4419-9b25-e8d7a8ec345a",
|
331 |
+
"metadata": {
|
332 |
+
"id": "1aa3c3f7-c6c5-4419-9b25-e8d7a8ec345a"
|
333 |
+
},
|
334 |
+
"outputs": [],
|
335 |
+
"source": [
|
336 |
+
"# Write your code here for Model Building here\n",
|
337 |
+
"label_encoder = LabelEncoder()\n",
|
338 |
+
"df['Diagnosis'] = label_encoder.fit_transform(df['Diagnosis'])\n",
|
339 |
+
"# define your Deep Learning Model here, training is in the next section.\n",
|
340 |
+
"\n"
|
341 |
+
]
|
342 |
+
},
|
343 |
+
{
|
344 |
+
"cell_type": "code",
|
345 |
+
"execution_count": null,
|
346 |
+
"id": "cfeaeee7",
|
347 |
+
"metadata": {},
|
348 |
+
"outputs": [],
|
349 |
+
"source": [
|
350 |
+
"features = df.drop(columns=['Diagnosis']).values\n",
|
351 |
+
"labels = df['Diagnosis'].values\n",
|
352 |
+
"\n",
|
353 |
+
"X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)"
|
354 |
+
]
|
355 |
+
},
|
356 |
+
{
|
357 |
+
"cell_type": "code",
|
358 |
+
"execution_count": null,
|
359 |
+
"id": "c94dbf5d",
|
360 |
+
"metadata": {},
|
361 |
+
"outputs": [],
|
362 |
+
"source": [
|
363 |
+
"# Convert the data to PyTorch tensors\n",
|
364 |
+
"X_train = torch.tensor(X_train, dtype=torch.float32)\n",
|
365 |
+
"X_test = torch.tensor(X_test, dtype=torch.float32)\n",
|
366 |
+
"y_train = torch.tensor(y_train, dtype=torch.long)\n",
|
367 |
+
"y_test = torch.tensor(y_test, dtype=torch.long)\n",
|
368 |
+
"\n",
|
369 |
+
"# Define the model architecture in PyTorch\n",
|
370 |
+
"class DiseaseClassifier(nn.Module):\n",
|
371 |
+
" def __init__(self, input_dim, hidden_dim, output_dim):\n",
|
372 |
+
" super(DiseaseClassifier, self).__init__()\n",
|
373 |
+
" self.fc1 = nn.Linear(input_dim, hidden_dim)\n",
|
374 |
+
" self.relu = nn.ReLU()\n",
|
375 |
+
" self.fc2 = nn.Linear(hidden_dim, output_dim)\n",
|
376 |
+
" \n",
|
377 |
+
" def forward(self, x):\n",
|
378 |
+
" out = self.fc1(x)\n",
|
379 |
+
" out = self.relu(out)\n",
|
380 |
+
" out = self.fc2(out)\n",
|
381 |
+
" return out\n",
|
382 |
+
"\n",
|
383 |
+
"# Define the hyperparameters\n",
|
384 |
+
"input_dim = X_train.shape[1]\n",
|
385 |
+
"hidden_dim = 64\n",
|
386 |
+
"output_dim = 2\n",
|
387 |
+
"learning_rate = 0.001\n",
|
388 |
+
"num_epochs = 10\n",
|
389 |
+
"batch_size = 16\n",
|
390 |
+
"\n",
|
391 |
+
"# Create an instance of the model\n",
|
392 |
+
"model = DiseaseClassifier(input_dim, hidden_dim, output_dim)\n",
|
393 |
+
"\n",
|
394 |
+
"# Define the loss function and optimizer\n",
|
395 |
+
"criterion = nn.CrossEntropyLoss()\n",
|
396 |
+
"optimizer = optim.Adam(model.parameters(), lr=learning_rate)\n",
|
397 |
+
"\n",
|
398 |
+
"# Create data loaders for batching\n",
|
399 |
+
"train_dataset = torch.utils.data.TensorDataset(X_train, y_train)\n",
|
400 |
+
"train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)"
|
401 |
+
]
|
402 |
+
},
|
403 |
+
{
|
404 |
+
"cell_type": "markdown",
|
405 |
+
"id": "585b925f-fcf0-4518-81d8-598dba65d646",
|
406 |
+
"metadata": {
|
407 |
+
"id": "585b925f-fcf0-4518-81d8-598dba65d646"
|
408 |
+
},
|
409 |
+
"source": [
|
410 |
+
"### 4. Model Training\n",
|
411 |
+
"\n",
|
412 |
+
"Train your model and evaluate its performance using validation data."
|
413 |
+
]
|
414 |
+
},
|
415 |
+
{
|
416 |
+
"cell_type": "code",
|
417 |
+
"execution_count": null,
|
418 |
+
"id": "ee33d9bc-2b35-4f78-b5d8-3eb65db1e361",
|
419 |
+
"metadata": {
|
420 |
+
"id": "ee33d9bc-2b35-4f78-b5d8-3eb65db1e361"
|
421 |
+
},
|
422 |
+
"outputs": [],
|
423 |
+
"source": [
|
424 |
+
"# Write your code here for Model Training here\n",
|
425 |
+
"#dl_loss_value = 1\n",
|
426 |
+
"\n",
|
427 |
+
"#define the iteration\n",
|
428 |
+
"\n",
|
429 |
+
"#create the training loop\n",
|
430 |
+
"# Training loop\n",
|
431 |
+
"for epoch in range(num_epochs):\n",
|
432 |
+
" running_loss = 0.0\n",
|
433 |
+
" for inputs, labels in train_loader:\n",
|
434 |
+
" # Zero the gradients\n",
|
435 |
+
" optimizer.zero_grad()\n",
|
436 |
+
" \n",
|
437 |
+
" # Forward pass\n",
|
438 |
+
" outputs = model(inputs)\n",
|
439 |
+
" \n",
|
440 |
+
" # Compute the loss\n",
|
441 |
+
" loss = criterion(outputs, labels)\n",
|
442 |
+
" \n",
|
443 |
+
" # Backward pass and optimization\n",
|
444 |
+
" loss.backward()\n",
|
445 |
+
" optimizer.step()\n",
|
446 |
+
" \n",
|
447 |
+
" # Track the loss\n",
|
448 |
+
" running_loss += loss.item()\n",
|
449 |
+
" \n",
|
450 |
+
" # Print the average loss for the epoch\n",
|
451 |
+
" print(f\"Epoch {epoch+1}/{num_epochs}, Loss: {running_loss / len(train_loader)}\")\n"
|
452 |
+
]
|
453 |
+
},
|
454 |
+
{
|
455 |
+
"cell_type": "markdown",
|
456 |
+
"id": "1d391ec9-ca95-4c56-b4f6-fccf8e0be14a",
|
457 |
+
"metadata": {
|
458 |
+
"id": "1d391ec9-ca95-4c56-b4f6-fccf8e0be14a"
|
459 |
+
},
|
460 |
+
"source": [
|
461 |
+
"### 5. Model Evaluation\n",
|
462 |
+
"Evaluate your model's performance on the test data using the grading scheme defined above."
|
463 |
+
]
|
464 |
+
},
|
465 |
+
{
|
466 |
+
"cell_type": "code",
|
467 |
+
"execution_count": null,
|
468 |
+
"id": "c8ca8f78-e33c-4552-9f4d-8bed711e661f",
|
469 |
+
"metadata": {
|
470 |
+
"id": "c8ca8f78-e33c-4552-9f4d-8bed711e661f"
|
471 |
+
},
|
472 |
+
"outputs": [],
|
473 |
+
"source": [
|
474 |
+
"# Write your code here for Model Evaluation here\n",
|
475 |
+
"# dl_accuracy = 0\n",
|
476 |
+
"\n",
|
477 |
+
"#define the iteration\n",
|
478 |
+
"\n",
|
479 |
+
"#create the training loop\n",
|
480 |
+
"with torch.no_grad():\n",
|
481 |
+
" model.eval()\n",
|
482 |
+
" outputs = model(X_test)\n",
|
483 |
+
" _, predicted = torch.max(outputs.data, 1)\n",
|
484 |
+
" accuracy = (predicted == y_test).sum().item() / y_test.size(0)\n",
|
485 |
+
"\n",
|
486 |
+
"print(f\"\\nFinal Test Accuracy: {accuracy * 100:.2f}%\")\n"
|
487 |
+
]
|
488 |
+
},
|
489 |
+
{
|
490 |
+
"cell_type": "markdown",
|
491 |
+
"id": "5d59af48-18c0-4e40-80ff-33a85030b205",
|
492 |
+
"metadata": {
|
493 |
+
"id": "5d59af48-18c0-4e40-80ff-33a85030b205"
|
494 |
+
},
|
495 |
+
"source": [
|
496 |
+
"## Submission\n",
|
497 |
+
"\n",
|
498 |
+
"Once you are satisfied with the performance of your model, then you run the code block below to submit your project.\n"
|
499 |
+
]
|
500 |
+
},
|
501 |
+
{
|
502 |
+
"cell_type": "markdown",
|
503 |
+
"id": "aa89b9ff-0d31-416a-b3c4-851c725fadf7",
|
504 |
+
"metadata": {
|
505 |
+
"id": "aa89b9ff-0d31-416a-b3c4-851c725fadf7"
|
506 |
+
},
|
507 |
+
"source": [
|
508 |
+
"## FIN"
|
509 |
+
]
|
510 |
+
}
|
511 |
+
],
|
512 |
+
"metadata": {
|
513 |
+
"colab": {
|
514 |
+
"provenance": []
|
515 |
+
},
|
516 |
+
"kernelspec": {
|
517 |
+
"display_name": "Python 3",
|
518 |
+
"language": "python",
|
519 |
+
"name": "python3"
|
520 |
+
},
|
521 |
+
"language_info": {
|
522 |
+
"codemirror_mode": {
|
523 |
+
"name": "ipython",
|
524 |
+
"version": 3
|
525 |
+
},
|
526 |
+
"file_extension": ".py",
|
527 |
+
"mimetype": "text/x-python",
|
528 |
+
"name": "python",
|
529 |
+
"nbconvert_exporter": "python",
|
530 |
+
"pygments_lexer": "ipython3",
|
531 |
+
"version": "3.9.6"
|
532 |
+
}
|
533 |
+
},
|
534 |
+
"nbformat": 4,
|
535 |
+
"nbformat_minor": 5
|
536 |
+
}
|
nyoba-04.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
spam.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|