File size: 29,166 Bytes
e5d1ec4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VFa5VyVNPCJY"
      },
      "source": [
        "# Project: Model Usage - Image Classification and Transfer Learning"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-9Z7RJrNvF8Q"
      },
      "source": [
        "**Instructions for Students:**\n",
        "\n",
        "Please carefully follow these steps to complete and submit your project:\n",
        "\n",
        "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",
        "\n",
        "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",
        "   \n",
        "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",
        "   \n",
        "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",
        "   \n",
        "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",
        "   \n",
        "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",
        "\n",
        "Adhering to these procedures will facilitate a smooth project evaluation process for you and the reviewers."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0Qg7V4zoPCJi"
      },
      "source": [
        "## Project Description:\n",
        "\n",
        "Welcome to your new project! You will have the opportunity to apply the knowledge and skills you've learned in class.\n",
        "\n",
        "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",
        "\n",
        "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",
        "\n",
        "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",
        "\n",
        "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!"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6MDaD2U_vF8S"
      },
      "source": [
        "## Grading Criteria\n",
        "\n",
        "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",
        "\n",
        "* 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",
        "\n",
        "* 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",
        "\n",
        "* 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",
        "\n",
        "* 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",
        "\n",
        "* 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"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZysTKHbGioh8"
      },
      "source": [
        "## Student Identity"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "i8BlcSWzioi3"
      },
      "outputs": [],
      "source": [
        "# @title #### Student Identity\n",
        "student_id = \"\" # @param {type:\"string\"}\n",
        "name = \"\" # @param {type:\"string\"}\n",
        "drive_link = \"\"  # @param {type:\"string\"}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vJWjH2kGV49k"
      },
      "source": [
        "## Installation and Import Package"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "8wWESOr0PCJk"
      },
      "outputs": [],
      "source": [
        "# Install necessary packages\n",
        "!pip install rggrader\n",
        "from rggrader import submit, submit_image\n",
        "\n",
        "# Put your code here:\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4_mbLFq9Vvcg"
      },
      "source": [
        "## Task 1 Image Classification using Pipeline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YwFqrph-vF8W"
      },
      "source": [
        "### Step 1: Image Classification using Hugging Face's Model\n",
        "\n",
        "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",
        "\n",
        "Here are the key steps that you might be able to follow:\n",
        "\n",
        "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",
        "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",
        "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",
        "4. **Output Display:** Display the output from the model in a clear and understandable manner.\n",
        "\n",
        "#### Submission\n",
        "\n",
        "- 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",
        "\n",
        "Submit in the numeric format up to 5 digits behind the decimal point. For example in below output:\n",
        "\n",
        "```\n",
        "{'0-2': '0.00152',\n",
        " '3-9': '0.00105',\n",
        " '10-19': '0.02567',\n",
        " '20-29': '3.32545',\n",
        " '30-39': '51.75200',\n",
        " '40-49': '40.24234',\n",
        " '50-59': '4.47803',\n",
        " '60-69': '0.17092',\n",
        " 'more than 70': '0.00304'}\n",
        "```\n",
        "\n",
        "The answer would be `0.00105`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "H5LA1LcdPCJm"
      },
      "outputs": [],
      "source": [
        "# @title #### 01. Image Classification using Hugging Face's Model\n",
        "\n",
        "# Put your code here:\n",
        "\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "M2EBNKYYvF8Y"
      },
      "outputs": [],
      "source": [
        "# Submit Method\n",
        "assignment_id = \"00_pipeline_and_gradio\"\n",
        "question_id = \"01_image_classification_using_hugging_faces_model\"\n",
        "answer = \"\" # Put your answer here\n",
        "submit(student_id, name, assignment_id, answer, question_id, drive_link)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "B2wOiPqDiojo"
      },
      "source": [
        "### Step 2: Image Classification using Hugging Face's Model and Gradio\n",
        "\n",
        "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",
        "\n",
        "Here are the key steps that you might be able to follow:\n",
        "\n",
        "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",
        "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",
        "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",
        "4. **Interface Launch:** Launch the Gradio interface. Make sure that the interface is accessible and easy to use.\n",
        "\n",
        "#### Submisssion\n",
        "\n",
        "![Upload colab](https://storage.googleapis.com/rg-ai-bootcamp/project-3-pipeline-and-gradio/upload-colab.png)\n",
        "\n",
        "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",
        "\n",
        "- You should upload a person's image to that app\n",
        "- The score should be included at the screenshot\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "fsMSIbrwTKuB"
      },
      "outputs": [],
      "source": [
        "# @title #### 02. Image Classification using Hugging Face's Model and Gradio\n",
        "\n",
        "# Put your code here:\n",
        "\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SHFWYR7qvF8Z"
      },
      "source": [
        "Example of Expected Output:\n",
        "\n",
        "![gradio-result](https://storage.googleapis.com/rg-ai-bootcamp/project-3-pipeline-and-gradio/gradio-result.png)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "vhJMv03IvF8Z"
      },
      "outputs": [],
      "source": [
        "# Submit Method\n",
        "question_id = \"02_image_classification_using_hugging_faces_model_and_gradio\"\n",
        "submit_image(student_id, question_id, './submission.jpg')\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "t8KSCR8OvF8Z"
      },
      "source": [
        "> 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."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SC8oMewavF8Z"
      },
      "source": [
        "# Task 2: Transfer Learning for Emotion Recognition"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RYbbazOuvF8Z"
      },
      "source": [
        "### Step 1: Environment Setup\n",
        "\n",
        "In this section, we start by installing the necessary packages and logging into Hugging Face's platform:\n",
        "- `transformers`\n",
        "- `datasets`\n",
        "- `evaluate`\n",
        "- `huggingface_hub`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "L8tVuUfnvF8a"
      },
      "outputs": [],
      "source": [
        "# Install necessary packages\n",
        "\n",
        "# Put your code here:\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pp8aOoWDvF8a"
      },
      "source": [
        "After installing, use the Hugging Face's notebook login function to log into Hugging Face's platform. Execute the following commands in your cell:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "z98RbfLwvF8a"
      },
      "outputs": [],
      "source": [
        "# Log into Hugging Face's platform\n",
        "\n",
        "# Put your code here:\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-4abRjUVvF8a"
      },
      "source": [
        "### Step 2: Load the Dataset\n",
        "\n",
        "Load the \"FastJobs/Visual_Emotional_Analysis\" dataset and split it into training and test sets with a test size of 0.2.\n",
        "\n",
        "> **Note**: please assign to variable `emotion`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "zygwYIo3vF8a"
      },
      "outputs": [],
      "source": [
        "# Load the dataset and split it\n",
        "\n",
        "# Update your code here:\n",
        "emotion = \"\"\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "F-LwIylpvF8b"
      },
      "source": [
        "**Label Mapping**\n",
        "\n",
        "> **Note**: no need to change the code below! Just run it to map labels from the dataset."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "524AEqr1vF8b"
      },
      "outputs": [],
      "source": [
        "labels = emotion[\"train\"].features[\"label\"].names\n",
        "label2id, id2label = dict(), dict()\n",
        "for i, label in enumerate(labels):\n",
        "    label2id[label] = str(i)\n",
        "    id2label[str(i)] = label"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7ZUaw3twvF8b"
      },
      "source": [
        "### Step 3: Explore and Visualize the Dataset\n",
        "\n",
        "In this step, you are required to visualize the first instance in the training dataset.\n",
        "\n",
        "> **Note**: no need to change the code below! Just run it to visualize the dataset based on index."
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import matplotlib.pyplot as plt\n",
        "\n",
        "# Define the function to convert label index to label name\n",
        "id2label_view = {str(i): label for i, label in enumerate(labels)}\n",
        "\n",
        "# Use first training example\n",
        "image = emotion['train'][0]['image'] # Explore image by index\n",
        "label_id = str(emotion['train'][0]['label'])\n",
        "label_name = id2label_view[label_id]\n",
        "\n",
        "# Display the image and its corresponding label\n",
        "plt.imshow(image)\n",
        "plt.title(f'Label: {label_name} (ID: {label_id})')\n",
        "plt.axis('off')\n",
        "plt.show()"
      ],
      "metadata": {
        "id": "F1qKjbgcvWJj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MDm9XEzbvF8b"
      },
      "source": [
        "### Step 4: Preprocess the Data\n",
        "\n",
        "You need to define the transformation function for image preprocessing and apply it to the dataset."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "vNa7A9A4vF8b"
      },
      "outputs": [],
      "source": [
        "from transformers import AutoImageProcessor\n",
        "from torchvision.transforms import RandomResizedCrop, Compose, Normalize, ToTensor\n",
        "\n",
        "# Load the image processor, Define the transforms, Define the transformation function and Apply the transformation function\n",
        "\n",
        "# Put your code here:\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "p1v9tSkvvF8c"
      },
      "source": [
        "### Step 5: Model Setup\n",
        "\n",
        "In this step, define the model architecture with the pre-trained ViT model and load it."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "INJfQ16OvF8c"
      },
      "outputs": [],
      "source": [
        "from transformers import AutoModelForImageClassification\n",
        "\n",
        "# Define the model\n",
        "\n",
        "# Put your code here:\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9pppaVt5vF8c"
      },
      "source": [
        "### Step 6: Training Setup\n",
        "\n",
        "Define the training arguments and instantiate the trainer."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "38g8OjfBvF8c"
      },
      "outputs": [],
      "source": [
        "import evaluate\n",
        "\n",
        "# Load accuracy metric\n",
        "\n",
        "# Put your code here:\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "GccP5F4svF8c"
      },
      "outputs": [],
      "source": [
        "# Define compute metric function\n",
        "\n",
        "# Put your code here:\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "sNC6OFLzvF8c"
      },
      "outputs": [],
      "source": [
        "from transformers import TrainingArguments, Trainer\n",
        "\n",
        "# Define training arguments\n",
        "\n",
        "# Update your code here:\n",
        "training_args = TrainingArguments()\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ABFxOF9wvF8d"
      },
      "source": [
        "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."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "NGeDe5JOvF8d"
      },
      "outputs": [],
      "source": [
        "%pip install accelerate -U"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "h95mzDGEvF8d"
      },
      "outputs": [],
      "source": [
        "from transformers import DefaultDataCollator\n",
        "\n",
        "# Instantiate the trainer\n",
        "\n",
        "# Update your code here:\n",
        "data_collator = DefaultDataCollator()\n",
        "trainer = Trainer()\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RUYlq1oMvF8j"
      },
      "source": [
        "If there are problems when using the Trainer after installing `accelerate` you can restart the Kernel"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-G3c0F5ZvF8j"
      },
      "source": [
        "### Step 7: Train and Evaluate the Model\n",
        "\n",
        "Now, you are ready to train the model and evaluate it on the test set."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2GvoiY4mvF8k"
      },
      "outputs": [],
      "source": [
        "# Train the model\n",
        "\n",
        "# Put your code here:\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "uVPRQNIRvF8k"
      },
      "outputs": [],
      "source": [
        "# Evaluate the model\n",
        "\n",
        "# Update your code here:\n",
        "eval_result = \"\"\n",
        "# ---- End of your code ----\n",
        "\n",
        "# Save the formatted accuracy in a variable\n",
        "accuracy_str = \"{:.4f}\".format(eval_result[\"eval_accuracy\"])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OIv95upxvF8k"
      },
      "source": [
        "### Step 8: Publishing the Trained Model\n",
        "\n",
        "Finally, make sure to push your trained model to the Hugging Face Model Hub.\n",
        "\n",
        "> **Note**: No need to change the code below! Just run to publish your model."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "aZe2lXMNvF8k"
      },
      "outputs": [],
      "source": [
        "trainer.push_to_hub()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3PRRzcPkvF8l"
      },
      "source": [
        "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",
        "\n",
        "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",
        "\n",
        "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."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "f72zoZKcvF8l"
      },
      "outputs": [],
      "source": [
        "# Submit Method\n",
        "huggingface_model_link = \"\" # Put your model link\n",
        "\n",
        "assignment_id = \"00_transfer_learning\"\n",
        "question_id = \"00_emotion_recognition_huggingface\"\n",
        "submit(student_id, name, assignment_id, huggingface_model_link, question_id, drive_link)\n",
        "\n",
        "question_id = \"01_emotion_recognition_accuracy\"\n",
        "submit(student_id, name, assignment_id, accuracy_str, question_id, drive_link)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YNRc-bowvF8l"
      },
      "source": [
        "### Step 9:  Build an Interactive Application with Gradio"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Dt61YtQYvF8l"
      },
      "source": [
        "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",
        "\n",
        "Please make sure to:\n",
        "- Install the necessary package (`gradio`) for creating the web-based interface.\n",
        "- Load your fine-tuned model as well as the pretrained model '`nateraw/vit-age-classifier`'.\n",
        "- Define a function that will take an image as input and return the predicted emotion and age.\n",
        "- Utilize Gradio to create an Interface (UI) for your function, allowing users to upload images and see the predicted emotion and age."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "VtyLNza9vF8l"
      },
      "outputs": [],
      "source": [
        "# Install Gradio\n",
        "!pip install gradio"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tD9pRSs2vF8m"
      },
      "source": [
        "#### Submisssion\n",
        "\n",
        "![Upload colab](https://storage.googleapis.com/rg-ai-bootcamp/project-3-pipeline-and-gradio/upload-colab.png)\n",
        "\n",
        "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",
        "\n",
        "- Image name screenshot is `submission.jpg`\n",
        "- You should upload a person's image to that app\n",
        "- The score should be included at the screenshot"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "7nNft9g0vF8m"
      },
      "outputs": [],
      "source": [
        "# Put your code here:\n",
        "\n",
        "# ---- End of your code ----"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7xHv6RycvF8m"
      },
      "source": [
        "Example of Expected Output:\n",
        "\n",
        "![gradio-result](https://storage.googleapis.com/rg-ai-bootcamp/project-4-transfer-learning/gradio_emotion_age_app.png)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ZhKCVEWjvF8m"
      },
      "outputs": [],
      "source": [
        "# Submit Method\n",
        "question_id = \"01_interactive_application_with_gradio\"\n",
        "submit_image(student_id, question_id, './submission.jpg')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YYUSn0TgvF8m"
      },
      "source": [
        "> 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."
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}