{"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":"import numpy as np \nimport pandas as pd\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        \nfrom fastai.vision.all import *\n!pip install py7zr","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-02T17:44:22.242983Z","iopub.execute_input":"2023-10-02T17:44:22.243783Z","iopub.status.idle":"2023-10-02T17:44:41.283262Z","shell.execute_reply.started":"2023-10-02T17:44:22.243750Z","shell.execute_reply":"2023-10-02T17:44:41.282145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil \n\n  \n\ntry: \n\n    shutil.unregister_unpack_format('7zip') \n\nexcept KeyError: \n\n    pass  # Ignore if the format is not registered \n\n  \n\nfrom py7zr import unpack_7zarchive \n\nshutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive) \n\n ","metadata":{"execution":{"iopub.status.busy":"2023-10-02T17:44:53.279020Z","iopub.execute_input":"2023-10-02T17:44:53.279353Z","iopub.status.idle":"2023-10-02T17:44:53.285939Z","shell.execute_reply.started":"2023-10-02T17:44:53.279324Z","shell.execute_reply":"2023-10-02T17:44:53.284952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('../input/cifar-10/train.7z', '/kaggle/temp/')","metadata":{"execution":{"iopub.status.busy":"2023-10-02T17:45:09.870501Z","iopub.execute_input":"2023-10-02T17:45:09.870835Z","iopub.status.idle":"2023-10-02T17:45:53.842421Z","shell.execute_reply.started":"2023-10-02T17:45:09.870809Z","shell.execute_reply":"2023-10-02T17:45:53.841561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv(\"../input/cifar-10/trainLabels.csv\", header=\"infer\")\n\nclasses = train_labels['label'].unique()\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T17:46:05.428588Z","iopub.execute_input":"2023-10-02T17:46:05.429015Z","iopub.status.idle":"2023-10-02T17:46:05.547864Z","shell.execute_reply.started":"2023-10-02T17:46:05.428960Z","shell.execute_reply":"2023-10-02T17:46:05.546998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists(\"/kaggle/temp/valid\"):\n    os.mkdir(\"/kaggle/temp/valid\")\n    \nparent_path_train = \"/kaggle/temp/train\"\nparent_path_valid = \"/kaggle/temp/valid\"\n# parent_path_test = \"/kaggle/temp/test\"\n\nfor 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)\nfor (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    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-02T17:46:15.083766Z","iopub.execute_input":"2023-10-02T17:46:15.084181Z","iopub.status.idle":"2023-10-02T17:46:24.575725Z","shell.execute_reply.started":"2023-10-02T17:46:15.084140Z","shell.execute_reply":"2023-10-02T17:46:24.574733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls=ImageDataLoaders.from_folder(path='/kaggle/temp',\n                            train='train', valid='valid',item_tfms=Resize(224),bs=32)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T17:46:35.400751Z","iopub.execute_input":"2023-10-02T17:46:35.401279Z","iopub.status.idle":"2023-10-02T17:46:44.568546Z","shell.execute_reply.started":"2023-10-02T17:46:35.401238Z","shell.execute_reply":"2023-10-02T17:46:44.567548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-02T17:46:49.568073Z","iopub.execute_input":"2023-10-02T17:46:49.568492Z","iopub.status.idle":"2023-10-02T17:46:49.580929Z","shell.execute_reply.started":"2023-10-02T17:46:49.568458Z","shell.execute_reply":"2023-10-02T17:46:49.579806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-02T17:46:55.608282Z","iopub.execute_input":"2023-10-02T17:46:55.608719Z","iopub.status.idle":"2023-10-02T17:46:57.247656Z","shell.execute_reply.started":"2023-10-02T17:46:55.608676Z","shell.execute_reply":"2023-10-02T17:46:57.246719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet50, metrics=accuracy)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-02T17:47:07.036721Z","iopub.execute_input":"2023-10-02T17:47:07.037239Z","iopub.status.idle":"2023-10-02T17:47:09.002547Z","shell.execute_reply.started":"2023-10-02T17:47:07.037188Z","shell.execute_reply":"2023-10-02T17:47:09.001580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-02T17:47:14.278503Z","iopub.execute_input":"2023-10-02T17:47:14.278912Z","iopub.status.idle":"2023-10-02T17:47:34.360006Z","shell.execute_reply.started":"2023-10-02T17:47:14.278879Z","shell.execute_reply":"2023-10-02T17:47:34.358860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(2, 3e-3)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-02T17:49:05.064554Z","iopub.execute_input":"2023-10-02T17:49:05.065698Z","iopub.status.idle":"2023-10-02T17:56:10.269025Z","shell.execute_reply.started":"2023-10-02T17:49:05.065653Z","shell.execute_reply":"2023-10-02T17:56:10.267924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-02T18:11:09.305279Z","iopub.execute_input":"2023-10-02T18:11:09.305854Z","iopub.status.idle":"2023-10-02T18:11:09.314481Z","shell.execute_reply.started":"2023-10-02T18:11:09.305810Z","shell.execute_reply":"2023-10-02T18:11:09.313497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-02T18:11:14.904636Z","iopub.execute_input":"2023-10-02T18:11:14.905073Z","iopub.status.idle":"2023-10-02T18:11:28.503418Z","shell.execute_reply.started":"2023-10-02T18:11:14.905038Z","shell.execute_reply":"2023-10-02T18:11:28.502283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3, lr_max=slice(1e-4,3e-5))\n","metadata":{"execution":{"iopub.status.busy":"2023-10-02T18:11:47.318787Z","iopub.execute_input":"2023-10-02T18:11:47.319210Z","iopub.status.idle":"2023-10-02T18:25:38.802673Z","shell.execute_reply.started":"2023-10-02T18:11:47.319176Z","shell.execute_reply":"2023-10-02T18:25:38.801578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-02T18:31:31.661540Z","iopub.execute_input":"2023-10-02T18:31:31.662110Z","iopub.status.idle":"2023-10-02T18:31:32.183927Z","shell.execute_reply.started":"2023-10-02T18:31:31.662065Z","shell.execute_reply":"2023-10-02T18:31:32.182950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-02T18:31:45.848641Z","iopub.execute_input":"2023-10-02T18:31:45.849063Z","iopub.status.idle":"2023-10-02T18:49:07.289588Z","shell.execute_reply.started":"2023-10-02T18:31:45.849028Z","shell.execute_reply":"2023-10-02T18:49:07.288593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-10-02T18:51:34.188628Z","iopub.execute_input":"2023-10-02T18:51:34.188971Z","iopub.status.idle":"2023-10-02T18:51:34.714310Z","shell.execute_reply.started":"2023-10-02T18:51:34.188943Z","shell.execute_reply":"2023-10-02T18:51:34.713220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-02T18:51:42.410993Z","iopub.execute_input":"2023-10-02T18:51:42.411519Z","iopub.status.idle":"2023-10-02T18:51:42.683962Z","shell.execute_reply.started":"2023-10-02T18:51:42.411479Z","shell.execute_reply":"2023-10-02T18:51:42.682995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-02T18:51:49.425431Z","iopub.execute_input":"2023-10-02T18:51:49.425967Z","iopub.status.idle":"2023-10-02T18:51:49.752409Z","shell.execute_reply.started":"2023-10-02T18:51:49.425927Z","shell.execute_reply":"2023-10-02T18:51:49.751428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl=learn.dls.test_dl(new)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T18:52:06.998759Z","iopub.execute_input":"2023-10-02T18:52:06.999173Z","iopub.status.idle":"2023-10-02T18:52:07.019262Z","shell.execute_reply.started":"2023-10-02T18:52:06.999140Z","shell.execute_reply":"2023-10-02T18:52:07.018336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score,y=learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:12:23.008098Z","iopub.execute_input":"2023-10-02T19:12:23.008581Z","iopub.status.idle":"2023-10-02T19:25:16.229764Z","shell.execute_reply.started":"2023-10-02T19:12:23.008543Z","shell.execute_reply":"2023-10-02T19:25:16.228688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score = np.argmax(class_score, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:26:03.475634Z","iopub.execute_input":"2023-10-02T19:26:03.476045Z","iopub.status.idle":"2023-10-02T19:26:03.500533Z","shell.execute_reply.started":"2023-10-02T19:26:03.476012Z","shell.execute_reply":"2023-10-02T19:26:03.499621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:25:42.935000Z","iopub.execute_input":"2023-10-02T19:25:42.935537Z","iopub.status.idle":"2023-10-02T19:25:42.950463Z","shell.execute_reply.started":"2023-10-02T19:25:42.935489Z","shell.execute_reply":"2023-10-02T19:25:42.949493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:26:10.957293Z","iopub.execute_input":"2023-10-02T19:26:10.957799Z","iopub.status.idle":"2023-10-02T19:26:10.965887Z","shell.execute_reply.started":"2023-10-02T19:26:10.957761Z","shell.execute_reply":"2023-10-02T19:26:10.964944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:26:19.177333Z","iopub.execute_input":"2023-10-02T19:26:19.177778Z","iopub.status.idle":"2023-10-02T19:26:19.188073Z","shell.execute_reply.started":"2023-10-02T19:26:19.177744Z","shell.execute_reply":"2023-10-02T19:26:19.186867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:26:55.387214Z","iopub.execute_input":"2023-10-02T19:26:55.387689Z","iopub.status.idle":"2023-10-02T19:26:55.402634Z","shell.execute_reply.started":"2023-10-02T19:26:55.387651Z","shell.execute_reply":"2023-10-02T19:26:55.401417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:26:59.456058Z","iopub.execute_input":"2023-10-02T19:26:59.456523Z","iopub.status.idle":"2023-10-02T19:26:59.463675Z","shell.execute_reply.started":"2023-10-02T19:26:59.456484Z","shell.execute_reply":"2023-10-02T19:26:59.462609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:27:07.080847Z","iopub.execute_input":"2023-10-02T19:27:07.081296Z","iopub.status.idle":"2023-10-02T19:27:07.092787Z","shell.execute_reply.started":"2023-10-02T19:27:07.081243Z","shell.execute_reply":"2023-10-02T19:27:07.091801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes={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-02T19:27:22.302622Z","iopub.execute_input":"2023-10-02T19:27:22.303043Z","iopub.status.idle":"2023-10-02T19:27:22.309091Z","shell.execute_reply.started":"2023-10-02T19:27:22.303010Z","shell.execute_reply":"2023-10-02T19:27:22.307951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes=np.empty(shape=300000,dtype=np.dtype('U20'))","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:28:06.182817Z","iopub.execute_input":"2023-10-02T19:28:06.183225Z","iopub.status.idle":"2023-10-02T19:28:06.210347Z","shell.execute_reply.started":"2023-10-02T19:28:06.183193Z","shell.execute_reply":"2023-10-02T19:28:06.209446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind=0\nfor i in (classScore):\n    predicted_classes[ind]=classes[i]\n    ind=ind+1","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:28:18.312167Z","iopub.execute_input":"2023-10-02T19:28:18.312637Z","iopub.status.idle":"2023-10-02T19:28:18.535811Z","shell.execute_reply.started":"2023-10-02T19:28:18.312599Z","shell.execute_reply":"2023-10-02T19:28:18.534806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-02T19:28:22.173090Z","iopub.execute_input":"2023-10-02T19:28:22.173559Z","iopub.status.idle":"2023-10-02T19:28:22.887655Z","shell.execute_reply.started":"2023-10-02T19:28:22.173522Z","shell.execute_reply":"2023-10-02T19:28:22.886642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission  = pd.DataFrame({\n    \"id\": ImageId,\n    \"label\": predicted_classes\n})\n# submission.sort_values(by=[\"ImageId\"], inplace = True)\nsubmission.to_csv(\"submission.csv\", index=False)\ndisplay(submission.head(3))\ndisplay(submission.tail(3))","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:28:31.913114Z","iopub.execute_input":"2023-10-02T19:28:31.913584Z","iopub.status.idle":"2023-10-02T19:28:32.659645Z","shell.execute_reply.started":"2023-10-02T19:28:31.913544Z","shell.execute_reply":"2023-10-02T19:28:32.658486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T19:28:45.086627Z","iopub.execute_input":"2023-10-02T19:28:45.087067Z","iopub.status.idle":"2023-10-02T19:28:45.744411Z","shell.execute_reply.started":"2023-10-02T19:28:45.087033Z","shell.execute_reply":"2023-10-02T19:28:45.743420Z"},"trusted":true},"execution_count":null,"outputs":[]}]}