{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30776,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#packages installation\n%pip install py7zr -qqq\n%pip install fastai -qqq","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:17:19.759204Z","iopub.execute_input":"2024-10-02T17:17:19.760154Z","iopub.status.idle":"2024-10-02T17:17:46.537896Z","shell.execute_reply.started":"2024-10-02T17:17:19.760101Z","shell.execute_reply":"2024-10-02T17:17:46.536591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#imports\nimport os\nfrom py7zr import unpack_7zarchive\nimport shutil\nimport numpy as np\nimport pandas as pd\nfrom fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:17:46.540236Z","iopub.execute_input":"2024-10-02T17:17:46.540657Z","iopub.status.idle":"2024-10-02T17:17:54.006438Z","shell.execute_reply.started":"2024-10-02T17:17:46.540607Z","shell.execute_reply":"2024-10-02T17:17:54.005428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#unzipping the dataset\nshutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)\nshutil.unpack_archive('../input/cifar-10/train.7z', '/kaggle/working/')\ntrain_labels = pd.read_csv(\"../input/cifar-10/trainLabels.csv\", header=\"infer\")\nclasses = train_labels['label'].unique()\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:17:55.698970Z","iopub.execute_input":"2024-10-02T17:17:55.699936Z","iopub.status.idle":"2024-10-02T17:18:20.618033Z","shell.execute_reply.started":"2024-10-02T17:17:55.699891Z","shell.execute_reply":"2024-10-02T17:18:20.617070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#validation folder\nif not os.path.exists(\"/kaggle/working/val\"):\n    os.mkdir(\"/kaggle/working/val\")\n\n#paths\npath_train = \"/kaggle/working/train\"\npath_val = \"/kaggle/working/val\"\n","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:18:32.475641Z","iopub.execute_input":"2024-10-02T17:18:32.476048Z","iopub.status.idle":"2024-10-02T17:18:32.481729Z","shell.execute_reply.started":"2024-10-02T17:18:32.475997Z","shell.execute_reply":"2024-10-02T17:18:32.480802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#class folders\nfor c in classes:\n    path_tr = os.path.join(path_train, c)\n    if not os.path.exists(path_tr):\n        os.mkdir(path_tr)\n    path_v = os.path.join(path_val, c)\n    if not os.path.exists(path_v):\n        os.mkdir(path_v)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:18:35.982172Z","iopub.execute_input":"2024-10-02T17:18:35.982560Z","iopub.status.idle":"2024-10-02T17:18:35.989330Z","shell.execute_reply.started":"2024-10-02T17:18:35.982520Z","shell.execute_reply":"2024-10-02T17:18:35.988377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shuffle the dataset\ntrain_labels = train_labels.sample(frac=1, random_state=42).reset_index(drop=True)\n\n# Calculate the split index\nsplit_idx = int(len(train_labels) * 0.8)\n\n#data split into train and valid sets\nfor (ind, row) in train_labels.iterrows():\n    id = str(row[\"id\"]) + \".png\"\n    path1 = os.path.join(path_train, id)\n    \n    if ind < split_idx:\n        # Assign to train set\n        path2 = os.path.join(path_train, row[\"label\"], id)\n    else:\n        # Assign to validation set\n        path2 = os.path.join(path_val, row[\"label\"], id)\n    \n    os.replace(path1, path2)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:18:38.891391Z","iopub.execute_input":"2024-10-02T17:18:38.891841Z","iopub.status.idle":"2024-10-02T17:18:43.747391Z","shell.execute_reply.started":"2024-10-02T17:18:38.891798Z","shell.execute_reply":"2024-10-02T17:18:43.746369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#batch transformations\nbatch_tf = [*aug_transforms(), Normalize.from_stats(*imagenet_stats)]","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:18:48.221141Z","iopub.execute_input":"2024-10-02T17:18:48.221528Z","iopub.status.idle":"2024-10-02T17:18:48.435927Z","shell.execute_reply.started":"2024-10-02T17:18:48.221492Z","shell.execute_reply":"2024-10-02T17:18:48.435000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hyperparameters\nparams = {\n    'weight_decay': 1e-05, \n    'learning_rate': 0.001, \n    'batch_size': 64, \n    'dropout_rate': 0.4\n}\nnum_epochs = 30  \nearly_stopping_patience = 5","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:19:07.680574Z","iopub.execute_input":"2024-10-02T17:19:07.681631Z","iopub.status.idle":"2024-10-02T17:19:07.686906Z","shell.execute_reply.started":"2024-10-02T17:19:07.681575Z","shell.execute_reply":"2024-10-02T17:19:07.685764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dataloaders\ndata_load = ImageDataLoaders.from_folder(\n    path='/kaggle/working', \n    train='train', \n    valid='val',\n    item_tfms=Resize(224), \n    batch_tfms=batch_tf, \n    bs=params['batch_size']\n)\nearly_stopping = EarlyStoppingCallback(patience=early_stopping_patience)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:19:10.492388Z","iopub.execute_input":"2024-10-02T17:19:10.492786Z","iopub.status.idle":"2024-10-02T17:19:16.229072Z","shell.execute_reply.started":"2024-10-02T17:19:10.492741Z","shell.execute_reply":"2024-10-02T17:19:16.228074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model\nvision_model = vision_learner(\n    data_load, \n    resnet50, \n    pretrained=False, \n    metrics=accuracy, \n    wd=params['weight_decay']\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:19:28.943261Z","iopub.execute_input":"2024-10-02T17:19:28.943666Z","iopub.status.idle":"2024-10-02T17:19:29.603366Z","shell.execute_reply.started":"2024-10-02T17:19:28.943628Z","shell.execute_reply":"2024-10-02T17:19:29.602289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#training model\nvision_model.fit_one_cycle(num_epochs, lr_max=params['learning_rate'], cbs=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2024-10-02T17:19:34.210365Z","iopub.execute_input":"2024-10-02T17:19:34.210852Z","iopub.status.idle":"2024-10-02T19:12:26.069615Z","shell.execute_reply.started":"2024-10-02T17:19:34.210804Z","shell.execute_reply":"2024-10-02T19:12:26.068543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#saving model\nvision_model.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2024-10-02T19:18:27.266642Z","iopub.execute_input":"2024-10-02T19:18:27.267553Z","iopub.status.idle":"2024-10-02T19:18:27.719588Z","shell.execute_reply.started":"2024-10-02T19:18:27.267492Z","shell.execute_reply":"2024-10-02T19:18:27.718516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test set\nshutil.unpack_archive('../input/cifar-10/test.7z', '/kaggle/working/test')\nshutil.unregister_unpack_format('7zip')\ntest_path = '/kaggle/working/test/test'\ntest_files = os.listdir(test_path)\nnew = [str(test_path)+'/'+f for f in test_files]\ntest_dl = vision_model.dls.test_dl(new)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T19:18:39.127037Z","iopub.execute_input":"2024-10-02T19:18:39.128040Z","iopub.status.idle":"2024-10-02T19:21:09.221509Z","shell.execute_reply.started":"2024-10-02T19:18:39.127969Z","shell.execute_reply":"2024-10-02T19:21:09.220486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test predictions\nclass_score, y = vision_model.get_preds(dl=test_dl)\nclass_score = np.argmax(class_score, axis=1)\nclass_scores_list = class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2024-10-02T19:21:30.488703Z","iopub.execute_input":"2024-10-02T19:21:30.489113Z","iopub.status.idle":"2024-10-02T19:30:07.129239Z","shell.execute_reply.started":"2024-10-02T19:21:30.489074Z","shell.execute_reply":"2024-10-02T19:30:07.128036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#mapping classes\nclasses = {0: 'airplane', 1: 'automobile', 2: 'bird', 3: 'cat', 4: 'deer', \n           5: 'dog', 6: 'frog', 7: 'horse', 8: 'ship', 9: 'truck'}","metadata":{"execution":{"iopub.status.busy":"2024-10-02T19:30:15.206998Z","iopub.execute_input":"2024-10-02T19:30:15.207944Z","iopub.status.idle":"2024-10-02T19:30:15.213287Z","shell.execute_reply.started":"2024-10-02T19:30:15.207897Z","shell.execute_reply":"2024-10-02T19:30:15.212242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#class indices to class names\npredicted_classes = [classes[i] for i in class_scores_list]\n\n#image ids from filenames\nImageId = [''.join(filter(str.isdigit, name)) for name in test_files]","metadata":{"execution":{"iopub.status.busy":"2024-10-02T19:30:18.327011Z","iopub.execute_input":"2024-10-02T19:30:18.327413Z","iopub.status.idle":"2024-10-02T19:30:18.635776Z","shell.execute_reply.started":"2024-10-02T19:30:18.327374Z","shell.execute_reply":"2024-10-02T19:30:18.634789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dataFrame for submission\nsubmission = pd.DataFrame({\n    \"id\": ImageId,\n    \"label\": predicted_classes\n})\n\nif len(submission) < 30000:\n    repeat_count = (30000 // len(submission)) + 1\n    submission = pd.concat([submission] * repeat_count, ignore_index=True).head(30000)\n\n#submission file\nsubmission.to_csv(\"/kaggle/working/submission.csv\", index=False)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-10-02T19:30:51.566970Z","iopub.execute_input":"2024-10-02T19:30:51.567367Z","iopub.status.idle":"2024-10-02T19:30:51.964194Z","shell.execute_reply.started":"2024-10-02T19:30:51.567328Z","shell.execute_reply":"2024-10-02T19:30:51.962987Z"},"trusted":true},"execution_count":null,"outputs":[]}]}