{"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-04T19:01:07.716134Z","iopub.execute_input":"2023-10-04T19:01:07.716589Z","iopub.status.idle":"2023-10-04T19:01:08.069952Z","shell.execute_reply.started":"2023-10-04T19:01:07.716561Z","shell.execute_reply":"2023-10-04T19:01:08.069002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:01:08.071987Z","iopub.execute_input":"2023-10-04T19:01:08.073055Z","iopub.status.idle":"2023-10-04T19:01:15.114375Z","shell.execute_reply.started":"2023-10-04T19:01:08.073020Z","shell.execute_reply":"2023-10-04T19:01:15.113375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:01:15.115695Z","iopub.execute_input":"2023-10-04T19:01:15.116564Z","iopub.status.idle":"2023-10-04T19:01:28.422977Z","shell.execute_reply.started":"2023-10-04T19:01:15.116526Z","shell.execute_reply":"2023-10-04T19:01:28.421486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from py7zr import unpack_7zarchive\nimport shutil\nshutil.register_unpack_format('7zip',['.7z'],unpack_7zarchive)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:01:28.426764Z","iopub.execute_input":"2023-10-04T19:01:28.427181Z","iopub.status.idle":"2023-10-04T19:01:28.568352Z","shell.execute_reply.started":"2023-10-04T19:01:28.427141Z","shell.execute_reply":"2023-10-04T19:01:28.567377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('../input/cifar-10/train.7z', '/kaggle/temp/')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:01:28.569713Z","iopub.execute_input":"2023-10-04T19:01:28.570061Z","iopub.status.idle":"2023-10-04T19:02:15.212376Z","shell.execute_reply.started":"2023-10-04T19:01:28.570032Z","shell.execute_reply":"2023-10-04T19:02:15.211459Z"},"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-04T19:02:15.213917Z","iopub.execute_input":"2023-10-04T19:02:15.214272Z","iopub.status.idle":"2023-10-04T19:02:15.290344Z","shell.execute_reply.started":"2023-10-04T19:02:15.214240Z","shell.execute_reply":"2023-10-04T19:02:15.289209Z"},"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-04T19:02:15.293013Z","iopub.execute_input":"2023-10-04T19:02:15.293378Z","iopub.status.idle":"2023-10-04T19:02:21.116627Z","shell.execute_reply.started":"2023-10-04T19:02:15.293345Z","shell.execute_reply":"2023-10-04T19:02:21.115662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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=64)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:02:21.117957Z","iopub.execute_input":"2023-10-04T19:02:21.118283Z","iopub.status.idle":"2023-10-04T19:02:29.283188Z","shell.execute_reply.started":"2023-10-04T19:02:21.118255Z","shell.execute_reply":"2023-10-04T19:02:29.282178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:02:29.284461Z","iopub.execute_input":"2023-10-04T19:02:29.284814Z","iopub.status.idle":"2023-10-04T19:02:29.294284Z","shell.execute_reply.started":"2023-10-04T19:02:29.284783Z","shell.execute_reply":"2023-10-04T19:02:29.293279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:02:29.298820Z","iopub.execute_input":"2023-10-04T19:02:29.299852Z","iopub.status.idle":"2023-10-04T19:02:30.592463Z","shell.execute_reply.started":"2023-10-04T19:02:29.299820Z","shell.execute_reply":"2023-10-04T19:02:30.591503Z"},"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-04T19:02:30.593418Z","iopub.execute_input":"2023-10-04T19:02:30.593738Z","iopub.status.idle":"2023-10-04T19:02:31.963115Z","shell.execute_reply.started":"2023-10-04T19:02:30.593704Z","shell.execute_reply":"2023-10-04T19:02:31.962097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:02:31.964376Z","iopub.execute_input":"2023-10-04T19:02:31.964767Z","iopub.status.idle":"2023-10-04T19:03:01.865436Z","shell.execute_reply.started":"2023-10-04T19:02:31.964735Z","shell.execute_reply":"2023-10-04T19:03:01.864265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3, 3e-3)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:03:01.867027Z","iopub.execute_input":"2023-10-04T19:03:01.868166Z","iopub.status.idle":"2023-10-04T19:12:48.180059Z","shell.execute_reply.started":"2023-10-04T19:03:01.868119Z","shell.execute_reply":"2023-10-04T19:12:48.178753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:12:48.185307Z","iopub.execute_input":"2023-10-04T19:12:48.187771Z","iopub.status.idle":"2023-10-04T19:12:48.198121Z","shell.execute_reply.started":"2023-10-04T19:12:48.187729Z","shell.execute_reply":"2023-10-04T19:12:48.196955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:12:48.203862Z","iopub.execute_input":"2023-10-04T19:12:48.206599Z","iopub.status.idle":"2023-10-04T19:13:10.497924Z","shell.execute_reply.started":"2023-10-04T19:12:48.206560Z","shell.execute_reply":"2023-10-04T19:13:10.496601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5, lr_max=slice(1e-6,3e-5))\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:13:10.503355Z","iopub.execute_input":"2023-10-04T19:13:10.506162Z","iopub.status.idle":"2023-10-04T19:34:18.047680Z","shell.execute_reply.started":"2023-10-04T19:13:10.506088Z","shell.execute_reply":"2023-10-04T19:34:18.046478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from fastai.callback.fp16 import *\n# learn = vision_learner(dls, resnet18, metrics=accuracy)\n# learn.fine_tune(10, freeze_epochs=3)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:34:18.049952Z","iopub.execute_input":"2023-10-04T19:34:18.050782Z","iopub.status.idle":"2023-10-04T19:34:18.055890Z","shell.execute_reply.started":"2023-10-04T19:34:18.050740Z","shell.execute_reply":"2023-10-04T19:34:18.054484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:34:18.057839Z","iopub.execute_input":"2023-10-04T19:34:18.058214Z","iopub.status.idle":"2023-10-04T19:34:18.452838Z","shell.execute_reply.started":"2023-10-04T19:34:18.058171Z","shell.execute_reply":"2023-10-04T19:34:18.451779Z"},"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-04T19:34:18.454260Z","iopub.execute_input":"2023-10-04T19:34:18.455160Z","iopub.status.idle":"2023-10-04T19:49:22.846931Z","shell.execute_reply.started":"2023-10-04T19:34:18.455124Z","shell.execute_reply":"2023-10-04T19:49:22.845833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:49:22.848378Z","iopub.execute_input":"2023-10-04T19:49:22.849411Z","iopub.status.idle":"2023-10-04T19:49:23.635759Z","shell.execute_reply.started":"2023-10-04T19:49:22.849371Z","shell.execute_reply":"2023-10-04T19:49:23.634650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir('/kaggle/temp/test/test')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:49:23.637396Z","iopub.execute_input":"2023-10-04T19:49:23.638183Z","iopub.status.idle":"2023-10-04T19:49:23.643169Z","shell.execute_reply.started":"2023-10-04T19:49:23.638145Z","shell.execute_reply":"2023-10-04T19:49:23.642097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:49:23.644917Z","iopub.execute_input":"2023-10-04T19:49:23.645376Z","iopub.status.idle":"2023-10-04T19:49:23.951551Z","shell.execute_reply.started":"2023-10-04T19:49:23.645336Z","shell.execute_reply":"2023-10-04T19:49:23.950209Z"},"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-04T19:49:23.958362Z","iopub.execute_input":"2023-10-04T19:49:23.958824Z","iopub.status.idle":"2023-10-04T19:49:24.440080Z","shell.execute_reply.started":"2023-10-04T19:49:23.958782Z","shell.execute_reply":"2023-10-04T19:49:24.439003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl=learn.dls.test_dl(new)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:49:24.441467Z","iopub.execute_input":"2023-10-04T19:49:24.441841Z","iopub.status.idle":"2023-10-04T19:49:24.454797Z","shell.execute_reply.started":"2023-10-04T19:49:24.441810Z","shell.execute_reply":"2023-10-04T19:49:24.453716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score,y=learn.get_preds(dl=test_dl)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T19:49:24.456531Z","iopub.execute_input":"2023-10-04T19:49:24.457226Z","iopub.status.idle":"2023-10-04T20:02:19.443799Z","shell.execute_reply.started":"2023-10-04T19:49:24.457190Z","shell.execute_reply":"2023-10-04T20:02:19.442591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:19.449029Z","iopub.execute_input":"2023-10-04T20:02:19.451920Z","iopub.status.idle":"2023-10-04T20:02:19.485167Z","shell.execute_reply.started":"2023-10-04T20:02:19.451879Z","shell.execute_reply":"2023-10-04T20:02:19.483900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score = np.argmax(class_score, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:19.488597Z","iopub.execute_input":"2023-10-04T20:02:19.492921Z","iopub.status.idle":"2023-10-04T20:02:19.535742Z","shell.execute_reply.started":"2023-10-04T20:02:19.492878Z","shell.execute_reply":"2023-10-04T20:02:19.534505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:19.547112Z","iopub.execute_input":"2023-10-04T20:02:19.550125Z","iopub.status.idle":"2023-10-04T20:02:19.561433Z","shell.execute_reply.started":"2023-10-04T20:02:19.550085Z","shell.execute_reply":"2023-10-04T20:02:19.560194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:19.566154Z","iopub.execute_input":"2023-10-04T20:02:19.567264Z","iopub.status.idle":"2023-10-04T20:02:19.576075Z","shell.execute_reply.started":"2023-10-04T20:02:19.567202Z","shell.execute_reply":"2023-10-04T20:02:19.574793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:19.577846Z","iopub.execute_input":"2023-10-04T20:02:19.578637Z","iopub.status.idle":"2023-10-04T20:02:19.590584Z","shell.execute_reply.started":"2023-10-04T20:02:19.578599Z","shell.execute_reply":"2023-10-04T20:02:19.589298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:19.592303Z","iopub.execute_input":"2023-10-04T20:02:19.593130Z","iopub.status.idle":"2023-10-04T20:02:19.603950Z","shell.execute_reply.started":"2023-10-04T20:02:19.593094Z","shell.execute_reply":"2023-10-04T20:02:19.602866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:19.605884Z","iopub.execute_input":"2023-10-04T20:02:19.606544Z","iopub.status.idle":"2023-10-04T20:02:19.618873Z","shell.execute_reply.started":"2023-10-04T20:02:19.606507Z","shell.execute_reply":"2023-10-04T20:02:19.617782Z"},"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-04T20:02:19.620493Z","iopub.execute_input":"2023-10-04T20:02:19.621175Z","iopub.status.idle":"2023-10-04T20:02:19.630261Z","shell.execute_reply.started":"2023-10-04T20:02:19.621140Z","shell.execute_reply":"2023-10-04T20:02:19.629122Z"},"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-04T20:02:19.632110Z","iopub.execute_input":"2023-10-04T20:02:19.632876Z","iopub.status.idle":"2023-10-04T20:02:19.645516Z","shell.execute_reply.started":"2023-10-04T20:02:19.632838Z","shell.execute_reply":"2023-10-04T20:02:19.644129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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-04T20:02:19.647326Z","iopub.execute_input":"2023-10-04T20:02:19.648018Z","iopub.status.idle":"2023-10-04T20:02:19.845924Z","shell.execute_reply.started":"2023-10-04T20:02:19.647979Z","shell.execute_reply":"2023-10-04T20:02:19.844772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:19.847483Z","iopub.execute_input":"2023-10-04T20:02:19.848079Z","iopub.status.idle":"2023-10-04T20:02:19.859006Z","shell.execute_reply.started":"2023-10-04T20:02:19.848043Z","shell.execute_reply":"2023-10-04T20:02:19.857973Z"},"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-04T20:02:19.860746Z","iopub.execute_input":"2023-10-04T20:02:19.862233Z","iopub.status.idle":"2023-10-04T20:02:20.517652Z","shell.execute_reply.started":"2023-10-04T20:02:19.862185Z","shell.execute_reply":"2023-10-04T20:02:20.516516Z"},"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-04T20:02:20.522454Z","iopub.execute_input":"2023-10-04T20:02:20.525140Z","iopub.status.idle":"2023-10-04T20:02:21.338058Z","shell.execute_reply.started":"2023-10-04T20:02:20.525100Z","shell.execute_reply":"2023-10-04T20:02:21.336838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:21.343177Z","iopub.execute_input":"2023-10-04T20:02:21.346360Z","iopub.status.idle":"2023-10-04T20:02:21.426185Z","shell.execute_reply.started":"2023-10-04T20:02:21.346316Z","shell.execute_reply":"2023-10-04T20:02:21.424669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:21.432043Z","iopub.execute_input":"2023-10-04T20:02:21.435117Z","iopub.status.idle":"2023-10-04T20:02:22.211493Z","shell.execute_reply.started":"2023-10-04T20:02:21.435077Z","shell.execute_reply":"2023-10-04T20:02:22.210341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = Image.open('/kaggle/temp/test/test/134190.png')\nim.to_thumb(254,254)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T20:02:22.216494Z","iopub.execute_input":"2023-10-04T20:02:22.219427Z","iopub.status.idle":"2023-10-04T20:02:22.233446Z","shell.execute_reply.started":"2023-10-04T20:02:22.219371Z","shell.execute_reply":"2023-10-04T20:02:22.232238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}