{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-03T08:21:47.468064Z","iopub.execute_input":"2023-10-03T08:21:47.468547Z","iopub.status.idle":"2023-10-03T08:21:47.946259Z","shell.execute_reply.started":"2023-10-03T08:21:47.468508Z","shell.execute_reply":"2023-10-03T08:21:47.945427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Import Libraries","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *\n!pip install py7zr\nfrom py7zr import unpack_7zarchive\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:21:48.161004Z","iopub.execute_input":"2023-10-03T08:21:48.163352Z","iopub.status.idle":"2023-10-03T08:22:07.198307Z","shell.execute_reply.started":"2023-10-03T08:21:48.163317Z","shell.execute_reply":"2023-10-03T08:22:07.197162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Register 7zip as an unpack format","metadata":{}},{"cell_type":"code","source":"shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:22:07.203422Z","iopub.execute_input":"2023-10-03T08:22:07.204273Z","iopub.status.idle":"2023-10-03T08:22:07.209972Z","shell.execute_reply.started":"2023-10-03T08:22:07.204237Z","shell.execute_reply":"2023-10-03T08:22:07.208695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Unpack the CIFAR-10 training data & Read the CSV file","metadata":{}},{"cell_type":"code","source":"shutil.unpack_archive('../input/cifar-10/train.7z', '/kaggle/temp/')","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:22:07.211381Z","iopub.execute_input":"2023-10-03T08:22:07.212188Z","iopub.status.idle":"2023-10-03T08:22:48.665401Z","shell.execute_reply.started":"2023-10-03T08:22:07.212093Z","shell.execute_reply":"2023-10-03T08:22:48.664264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv(\"../input/cifar-10/trainLabels.csv\", header=\"infer\")","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:22:48.667881Z","iopub.execute_input":"2023-10-03T08:22:48.668144Z","iopub.status.idle":"2023-10-03T08:22:48.710682Z","shell.execute_reply.started":"2023-10-03T08:22:48.668121Z","shell.execute_reply":"2023-10-03T08:22:48.709695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Get the unique classes from the training labels","metadata":{}},{"cell_type":"code","source":"classes = train_labels['label'].unique()\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:22:48.715113Z","iopub.execute_input":"2023-10-03T08:22:48.717830Z","iopub.status.idle":"2023-10-03T08:22:48.782906Z","shell.execute_reply.started":"2023-10-03T08:22:48.717791Z","shell.execute_reply":"2023-10-03T08:22:48.781942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists(\"/kaggle/temp/valid\"):\n    os.mkdir(\"/kaggle/temp/valid\")","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:22:48.784203Z","iopub.execute_input":"2023-10-03T08:22:48.785186Z","iopub.status.idle":"2023-10-03T08:22:48.795261Z","shell.execute_reply.started":"2023-10-03T08:22:48.785150Z","shell.execute_reply":"2023-10-03T08:22:48.794408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define paths for training and validation data\n\n","metadata":{}},{"cell_type":"code","source":"parent_path_train = \"/kaggle/temp/train\"\nparent_path_valid = \"/kaggle/temp/valid\"","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:22:48.796720Z","iopub.execute_input":"2023-10-03T08:22:48.797308Z","iopub.status.idle":"2023-10-03T08:22:48.808809Z","shell.execute_reply.started":"2023-10-03T08:22:48.797277Z","shell.execute_reply":"2023-10-03T08:22:48.807877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Loop through each class and create subdirectories for training and validation data","metadata":{}},{"cell_type":"code","source":"for class1 in classes:\n    path_train = os.path.join(parent_path_train, class1)\n    if not os.path.exists(path_train):\n        os.mkdir(path_train)\n    path_valid = os.path.join(parent_path_valid, class1)\n    if not os.path.exists(path_valid):\n        os.mkdir(path_valid)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:22:48.809979Z","iopub.execute_input":"2023-10-03T08:22:48.810901Z","iopub.status.idle":"2023-10-03T08:22:48.821533Z","shell.execute_reply.started":"2023-10-03T08:22:48.810871Z","shell.execute_reply":"2023-10-03T08:22:48.820712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Split training data into training and validation sets","metadata":{}},{"cell_type":"code","source":"for (int_ind, row) in train_labels.iterrows():\n    id = str(row[\"id\"]) + \".png\"\n    source_path = os.path.join(parent_path_train, id)\n    \n    # Randomly assign data to either training (80%) or validation (20%) set\n    p = np.random.random()\n    if p <= 0.8:\n        target_path = os.path.join(parent_path_train, row[\"label\"], id)\n        os.replace(source_path, target_path)\n    else:\n        target_path = os.path.join(parent_path_valid, row[\"label\"], id)\n        os.replace(source_path, target_path)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:22:48.822889Z","iopub.execute_input":"2023-10-03T08:22:48.823467Z","iopub.status.idle":"2023-10-03T08:22:57.150566Z","shell.execute_reply.started":"2023-10-03T08:22:48.823438Z","shell.execute_reply":"2023-10-03T08:22:57.149642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create ImageDataLoaders for training and validation","metadata":{}},{"cell_type":"code","source":"dls = ImageDataLoaders.from_folder(path='/kaggle/temp', train='train', valid='valid', item_tfms=Resize(224), bs=64)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:22:57.153023Z","iopub.execute_input":"2023-10-03T08:22:57.153614Z","iopub.status.idle":"2023-10-03T08:23:02.501820Z","shell.execute_reply.started":"2023-10-03T08:22:57.153579Z","shell.execute_reply":"2023-10-03T08:23:02.500812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Show a batch of data","metadata":{}},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:01:11.195079Z","iopub.execute_input":"2023-10-03T09:01:11.195500Z","iopub.status.idle":"2023-10-03T09:01:12.317475Z","shell.execute_reply.started":"2023-10-03T09:01:11.195463Z","shell.execute_reply":"2023-10-03T09:01:12.316650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create a vision learner using a ResNet50 architecture","metadata":{}},{"cell_type":"code","source":"learn = vision_learner(dls, resnet50, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:01:14.456558Z","iopub.execute_input":"2023-10-03T09:01:14.456929Z","iopub.status.idle":"2023-10-03T09:01:15.040383Z","shell.execute_reply.started":"2023-10-03T09:01:14.456900Z","shell.execute_reply":"2023-10-03T09:01:15.039406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Find an appropriate learning rate","metadata":{}},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:01:15.041847Z","iopub.execute_input":"2023-10-03T09:01:15.042172Z","iopub.status.idle":"2023-10-03T09:01:40.579589Z","shell.execute_reply.started":"2023-10-03T09:01:15.042148Z","shell.execute_reply":"2023-10-03T09:01:40.578604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train the model","metadata":{}},{"cell_type":"code","source":"learn.fit_one_cycle(10, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:01:40.581864Z","iopub.execute_input":"2023-10-03T09:01:40.582445Z","iopub.status.idle":"2023-10-03T09:41:02.445997Z","shell.execute_reply.started":"2023-10-03T09:01:40.582406Z","shell.execute_reply":"2023-10-03T09:41:02.444742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Unfreeze the model and find a learning rate for fine-tuning","metadata":{}},{"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:41:02.448057Z","iopub.execute_input":"2023-10-03T09:41:02.448655Z","iopub.status.idle":"2023-10-03T09:41:20.158130Z","shell.execute_reply.started":"2023-10-03T09:41:02.448617Z","shell.execute_reply":"2023-10-03T09:41:20.157119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fine-tune the model","metadata":{}},{"cell_type":"code","source":"# learn.fit_one_cycle(5, lr_max=slice(1e-6, 3e-5)\n# Fine-tuning a pre-trained model with different learning rates for different layers\nlearn.fit_one_cycle(5, lr_max=slice(1e-6, 1e-4))  # Adjust the lr_max values for earlier and later layers","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:41:20.160763Z","iopub.execute_input":"2023-10-03T09:41:20.161677Z","iopub.status.idle":"2023-10-03T10:06:33.657872Z","shell.execute_reply.started":"2023-10-03T09:41:20.161639Z","shell.execute_reply":"2023-10-03T10:06:33.656834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot the training loss","metadata":{}},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:06:33.659635Z","iopub.execute_input":"2023-10-03T10:06:33.660655Z","iopub.status.idle":"2023-10-03T10:06:34.008063Z","shell.execute_reply.started":"2023-10-03T10:06:33.660616Z","shell.execute_reply":"2023-10-03T10:06:34.007170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Unpack the test data","metadata":{}},{"cell_type":"code","source":"shutil.unpack_archive('/kaggle/input/cifar-10/test.7z', '/kaggle/temp/test')\nshutil.unregister_unpack_format('7zip')","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:06:34.009187Z","iopub.execute_input":"2023-10-03T10:06:34.009995Z","iopub.status.idle":"2023-10-03T10:19:40.241912Z","shell.execute_reply.started":"2023-10-03T10:06:34.009962Z","shell.execute_reply":"2023-10-03T10:19:40.240887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Save the trained model","metadata":{}},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:19:40.243438Z","iopub.execute_input":"2023-10-03T10:19:40.243767Z","iopub.status.idle":"2023-10-03T10:19:40.824766Z","shell.execute_reply.started":"2023-10-03T10:19:40.243733Z","shell.execute_reply":"2023-10-03T10:19:40.823766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Get the list of test images","metadata":{}},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:19:40.826450Z","iopub.execute_input":"2023-10-03T10:19:40.827144Z","iopub.status.idle":"2023-10-03T10:19:40.977243Z","shell.execute_reply.started":"2023-10-03T10:19:40.827108Z","shell.execute_reply":"2023-10-03T10:19:40.976299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define paths for test data","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/temp/test/test'\nf = os.listdir(path)\nnew = [str(path) + '/' + s for s in f]","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:19:40.978654Z","iopub.execute_input":"2023-10-03T10:19:40.979262Z","iopub.status.idle":"2023-10-03T10:19:41.175037Z","shell.execute_reply.started":"2023-10-03T10:19:40.979229Z","shell.execute_reply":"2023-10-03T10:19:41.174228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create a test dataloader","metadata":{}},{"cell_type":"code","source":"test_dl = learn.dls.test_dl(new)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:19:41.178576Z","iopub.execute_input":"2023-10-03T10:19:41.179151Z","iopub.status.idle":"2023-10-03T10:19:41.192266Z","shell.execute_reply.started":"2023-10-03T10:19:41.179117Z","shell.execute_reply":"2023-10-03T10:19:41.191397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Get class scores and predictions for test data","metadata":{}},{"cell_type":"code","source":"class_score, y = learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:19:41.193638Z","iopub.execute_input":"2023-10-03T10:19:41.194527Z","iopub.status.idle":"2023-10-03T10:29:48.740839Z","shell.execute_reply.started":"2023-10-03T10:19:41.194492Z","shell.execute_reply":"2023-10-03T10:29:48.739785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Convert class scores to class labels","metadata":{}},{"cell_type":"code","source":"class_score = np.argmax(class_score, axis=1)\nclass_score[1].item()\nclass_score","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:29:48.742758Z","iopub.execute_input":"2023-10-03T10:29:48.743115Z","iopub.status.idle":"2023-10-03T10:29:48.780735Z","shell.execute_reply.started":"2023-10-03T10:29:48.743080Z","shell.execute_reply":"2023-10-03T10:29:48.779830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Convert class scores to a list of predicted classes","metadata":{}},{"cell_type":"code","source":"classScore = class_score.tolist()\nlen(classScore)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:29:48.782034Z","iopub.execute_input":"2023-10-03T10:29:48.782651Z","iopub.status.idle":"2023-10-03T10:29:48.793555Z","shell.execute_reply.started":"2023-10-03T10:29:48.782619Z","shell.execute_reply":"2023-10-03T10:29:48.792763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define class labels","metadata":{}},{"cell_type":"code","source":"learn.dls.vocab\nclasses = {0: 'airplane', 1: 'automobile', 2: 'bird', 3: 'cat', 4: 'deer', 5: 'dog', 6: 'frog', 7: 'horse', 8: 'ship', 9: 'truck'}","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:29:48.796248Z","iopub.execute_input":"2023-10-03T10:29:48.797011Z","iopub.status.idle":"2023-10-03T10:29:48.810789Z","shell.execute_reply.started":"2023-10-03T10:29:48.796981Z","shell.execute_reply":"2023-10-03T10:29:48.809643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Map predicted class indices to class labels","metadata":{}},{"cell_type":"code","source":"predicted_classes = np.empty(shape=300000, dtype=np.dtype('U20'))\nind = 0\nfor i in (classScore):\n    predicted_classes[ind] = classes[i]\n    ind = ind + 1\n\npredicted_classes","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:29:48.812172Z","iopub.execute_input":"2023-10-03T10:29:48.812983Z","iopub.status.idle":"2023-10-03T10:29:48.915266Z","shell.execute_reply.started":"2023-10-03T10:29:48.812953Z","shell.execute_reply":"2023-10-03T10:29:48.914382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Get Image IDs from file names","metadata":{}},{"cell_type":"code","source":"directory = '/kaggle/temp/test/test'\nImageId = [ (''.join(filter(str.isdigit, name))) for name in os.listdir(directory)]","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:29:48.916398Z","iopub.execute_input":"2023-10-03T10:29:48.916695Z","iopub.status.idle":"2023-10-03T10:29:49.247000Z","shell.execute_reply.started":"2023-10-03T10:29:48.916666Z","shell.execute_reply":"2023-10-03T10:29:49.246050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create a submission DataFrame","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"id\": ImageId,\n    \"label\": predicted_classes\n})","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:29:49.248382Z","iopub.execute_input":"2023-10-03T10:29:49.248932Z","iopub.status.idle":"2023-10-03T10:29:49.296986Z","shell.execute_reply.started":"2023-10-03T10:29:49.248900Z","shell.execute_reply":"2023-10-03T10:29:49.296077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Save the submission as a CSV file","metadata":{}},{"cell_type":"code","source":"submission.to_csv(\"Assignment_5_submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:29:49.986752Z","iopub.execute_input":"2023-10-03T10:29:49.987069Z","iopub.status.idle":"2023-10-03T10:29:50.308325Z","shell.execute_reply.started":"2023-10-03T10:29:49.987040Z","shell.execute_reply":"2023-10-03T10:29:50.307186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Display the first and last few rows of the submission","metadata":{}},{"cell_type":"code","source":"display(submission.head(3))\ndisplay(submission.tail(3))","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:29:50.309985Z","iopub.execute_input":"2023-10-03T10:29:50.310651Z","iopub.status.idle":"2023-10-03T10:29:50.324043Z","shell.execute_reply.started":"2023-10-03T10:29:50.310619Z","shell.execute_reply":"2023-10-03T10:29:50.322891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Convert the 'id' column to integers and save the CSV file again","metadata":{}},{"cell_type":"code","source":"submission['id'] = submission['id'].astype(int)\nsubmission.to_csv('Assignment_5_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:29:50.325194Z","iopub.execute_input":"2023-10-03T10:29:50.325946Z","iopub.status.idle":"2023-10-03T10:29:50.640611Z","shell.execute_reply.started":"2023-10-03T10:29:50.325915Z","shell.execute_reply":"2023-10-03T10:29:50.639441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Open and display an image","metadata":{}},{"cell_type":"code","source":"im = Image.open('/kaggle/temp/test/test/134190.png')\nim.to_thumb(254, 254)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T10:29:50.642013Z","iopub.execute_input":"2023-10-03T10:29:50.642869Z","iopub.status.idle":"2023-10-03T10:29:50.650816Z","shell.execute_reply.started":"2023-10-03T10:29:50.642836Z","shell.execute_reply":"2023-10-03T10:29:50.649658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}