{"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-09-28T06:39:30.519121Z","iopub.execute_input":"2023-09-28T06:39:30.519538Z","iopub.status.idle":"2023-09-28T06:40:01.100911Z","shell.execute_reply.started":"2023-09-28T06:39:30.519503Z","shell.execute_reply":"2023-09-28T06:40:01.099618Z"},"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-09-28T06:40:01.106713Z","iopub.execute_input":"2023-09-28T06:40:01.109409Z","iopub.status.idle":"2023-09-28T06:40:01.334192Z","shell.execute_reply.started":"2023-09-28T06:40:01.109346Z","shell.execute_reply":"2023-09-28T06:40:01.333215Z"},"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-09-28T06:40:01.335692Z","iopub.execute_input":"2023-09-28T06:40:01.336400Z","iopub.status.idle":"2023-09-28T06:41:21.957734Z","shell.execute_reply.started":"2023-09-28T06:40:01.336366Z","shell.execute_reply":"2023-09-28T06:41:21.956722Z"},"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-09-28T06:41:21.960620Z","iopub.execute_input":"2023-09-28T06:41:21.961307Z","iopub.status.idle":"2023-09-28T06:41:22.061606Z","shell.execute_reply.started":"2023-09-28T06:41:21.961273Z","shell.execute_reply":"2023-09-28T06:41:22.060669Z"},"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-09-28T06:41:22.063260Z","iopub.execute_input":"2023-09-28T06:41:22.063918Z","iopub.status.idle":"2023-09-28T06:41:31.075093Z","shell.execute_reply.started":"2023-09-28T06:41:22.063874Z","shell.execute_reply":"2023-09-28T06:41:31.073928Z"},"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-09-28T06:41:31.076841Z","iopub.execute_input":"2023-09-28T06:41:31.077551Z","iopub.status.idle":"2023-09-28T06:41:43.140388Z","shell.execute_reply.started":"2023-09-28T06:41:31.077518Z","shell.execute_reply":"2023-09-28T06:41:43.139352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-09-28T06:41:43.145471Z","iopub.execute_input":"2023-09-28T06:41:43.147810Z","iopub.status.idle":"2023-09-28T06:41:43.163597Z","shell.execute_reply.started":"2023-09-28T06:41:43.147774Z","shell.execute_reply":"2023-09-28T06:41:43.161822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-09-28T06:41:43.169331Z","iopub.execute_input":"2023-09-28T06:41:43.172148Z","iopub.status.idle":"2023-09-28T06:41:45.015894Z","shell.execute_reply.started":"2023-09-28T06:41:43.172113Z","shell.execute_reply":"2023-09-28T06:41:45.014930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet50, metrics=accuracy)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T06:41:45.016955Z","iopub.execute_input":"2023-09-28T06:41:45.017320Z","iopub.status.idle":"2023-09-28T06:41:46.889086Z","shell.execute_reply.started":"2023-09-28T06:41:45.017287Z","shell.execute_reply":"2023-09-28T06:41:46.888095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T06:41:46.897238Z","iopub.execute_input":"2023-09-28T06:41:46.899542Z","iopub.status.idle":"2023-09-28T06:42:18.109192Z","shell.execute_reply.started":"2023-09-28T06:41:46.899506Z","shell.execute_reply":"2023-09-28T06:42:18.108156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3, 3e-3)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T06:42:18.113796Z","iopub.execute_input":"2023-09-28T06:42:18.116058Z","iopub.status.idle":"2023-09-28T06:52:00.957453Z","shell.execute_reply.started":"2023-09-28T06:42:18.116008Z","shell.execute_reply":"2023-09-28T06:52:00.956318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T06:52:00.962417Z","iopub.execute_input":"2023-09-28T06:52:00.964709Z","iopub.status.idle":"2023-09-28T06:52:00.973924Z","shell.execute_reply.started":"2023-09-28T06:52:00.964668Z","shell.execute_reply":"2023-09-28T06:52:00.973080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T06:52:00.978798Z","iopub.execute_input":"2023-09-28T06:52:00.981458Z","iopub.status.idle":"2023-09-28T06:52:22.612239Z","shell.execute_reply.started":"2023-09-28T06:52:00.981424Z","shell.execute_reply":"2023-09-28T06:52:22.611074Z"},"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-09-28T06:52:22.616803Z","iopub.execute_input":"2023-09-28T06:52:22.619105Z","iopub.status.idle":"2023-09-28T07:12:58.911405Z","shell.execute_reply.started":"2023-09-28T06:52:22.619068Z","shell.execute_reply":"2023-09-28T07:12:58.910330Z"},"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-09-28T07:12:58.913351Z","iopub.execute_input":"2023-09-28T07:12:58.913751Z","iopub.status.idle":"2023-09-28T07:12:58.920027Z","shell.execute_reply.started":"2023-09-28T07:12:58.913713Z","shell.execute_reply":"2023-09-28T07:12:58.918936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T07:12:58.921569Z","iopub.execute_input":"2023-09-28T07:12:58.921986Z","iopub.status.idle":"2023-09-28T07:12:59.363857Z","shell.execute_reply.started":"2023-09-28T07:12:58.921953Z","shell.execute_reply":"2023-09-28T07:12:59.362926Z"},"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-09-28T07:12:59.365372Z","iopub.execute_input":"2023-09-28T07:12:59.365718Z","iopub.status.idle":"2023-09-28T07:33:51.984994Z","shell.execute_reply.started":"2023-09-28T07:12:59.365685Z","shell.execute_reply":"2023-09-28T07:33:51.983877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-09-28T07:33:51.986708Z","iopub.execute_input":"2023-09-28T07:33:51.987092Z","iopub.status.idle":"2023-09-28T07:33:52.677157Z","shell.execute_reply.started":"2023-09-28T07:33:51.987057Z","shell.execute_reply":"2023-09-28T07:33:52.676240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-09-28T07:33:52.742981Z","iopub.execute_input":"2023-09-28T07:33:52.743450Z","iopub.status.idle":"2023-09-28T07:33:52.920144Z","shell.execute_reply.started":"2023-09-28T07:33:52.743415Z","shell.execute_reply":"2023-09-28T07:33:52.919086Z"},"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-09-28T07:33:52.921663Z","iopub.execute_input":"2023-09-28T07:33:52.922035Z","iopub.status.idle":"2023-09-28T07:33:53.180328Z","shell.execute_reply.started":"2023-09-28T07:33:52.921989Z","shell.execute_reply":"2023-09-28T07:33:53.179314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl=learn.dls.test_dl(new)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T07:33:53.181934Z","iopub.execute_input":"2023-09-28T07:33:53.182304Z","iopub.status.idle":"2023-09-28T07:33:53.193524Z","shell.execute_reply.started":"2023-09-28T07:33:53.182270Z","shell.execute_reply":"2023-09-28T07:33:53.192353Z"},"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-09-28T07:33:53.195004Z","iopub.execute_input":"2023-09-28T07:33:53.195885Z","iopub.status.idle":"2023-09-28T07:47:26.693848Z","shell.execute_reply.started":"2023-09-28T07:33:53.195847Z","shell.execute_reply":"2023-09-28T07:47:26.692634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-09-28T07:47:26.699103Z","iopub.execute_input":"2023-09-28T07:47:26.700390Z","iopub.status.idle":"2023-09-28T07:47:26.727064Z","shell.execute_reply.started":"2023-09-28T07:47:26.700323Z","shell.execute_reply":"2023-09-28T07:47:26.726212Z"},"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-09-28T07:47:26.731531Z","iopub.execute_input":"2023-09-28T07:47:26.733751Z","iopub.status.idle":"2023-09-28T07:47:26.768600Z","shell.execute_reply.started":"2023-09-28T07:47:26.733717Z","shell.execute_reply":"2023-09-28T07:47:26.767727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T07:47:26.772738Z","iopub.execute_input":"2023-09-28T07:47:26.775136Z","iopub.status.idle":"2023-09-28T07:47:26.784976Z","shell.execute_reply.started":"2023-09-28T07:47:26.775099Z","shell.execute_reply":"2023-09-28T07:47:26.784070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-09-28T07:47:26.789554Z","iopub.execute_input":"2023-09-28T07:47:26.791809Z","iopub.status.idle":"2023-09-28T07:47:26.801795Z","shell.execute_reply.started":"2023-09-28T07:47:26.791776Z","shell.execute_reply":"2023-09-28T07:47:26.800893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-09-28T07:47:26.811915Z","iopub.execute_input":"2023-09-28T07:47:26.814179Z","iopub.status.idle":"2023-09-28T07:47:26.828939Z","shell.execute_reply.started":"2023-09-28T07:47:26.814145Z","shell.execute_reply":"2023-09-28T07:47:26.828040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T07:47:26.833590Z","iopub.execute_input":"2023-09-28T07:47:26.836081Z","iopub.status.idle":"2023-09-28T07:47:26.851100Z","shell.execute_reply.started":"2023-09-28T07:47:26.836047Z","shell.execute_reply":"2023-09-28T07:47:26.849914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-09-28T07:47:26.855509Z","iopub.execute_input":"2023-09-28T07:47:26.857891Z","iopub.status.idle":"2023-09-28T07:47:26.867570Z","shell.execute_reply.started":"2023-09-28T07:47:26.857857Z","shell.execute_reply":"2023-09-28T07:47:26.866649Z"},"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-09-28T07:47:26.869573Z","iopub.execute_input":"2023-09-28T07:47:26.871851Z","iopub.status.idle":"2023-09-28T07:47:26.880302Z","shell.execute_reply.started":"2023-09-28T07:47:26.871818Z","shell.execute_reply":"2023-09-28T07:47:26.879332Z"},"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-09-28T07:47:26.881788Z","iopub.execute_input":"2023-09-28T07:47:26.882477Z","iopub.status.idle":"2023-09-28T07:47:26.896493Z","shell.execute_reply.started":"2023-09-28T07:47:26.882444Z","shell.execute_reply":"2023-09-28T07:47:26.895435Z"},"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-09-28T07:47:26.898449Z","iopub.execute_input":"2023-09-28T07:47:26.899936Z","iopub.status.idle":"2023-09-28T07:47:27.158856Z","shell.execute_reply.started":"2023-09-28T07:47:26.899902Z","shell.execute_reply":"2023-09-28T07:47:27.157900Z"},"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-09-28T07:47:27.180148Z","iopub.execute_input":"2023-09-28T07:47:27.182569Z","iopub.status.idle":"2023-09-28T07:47:27.867738Z","shell.execute_reply.started":"2023-09-28T07:47:27.182533Z","shell.execute_reply":"2023-09-28T07:47:27.866710Z"},"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-09-28T07:47:27.872511Z","iopub.execute_input":"2023-09-28T07:47:27.874800Z","iopub.status.idle":"2023-09-28T07:47:28.939426Z","shell.execute_reply.started":"2023-09-28T07:47:27.874765Z","shell.execute_reply":"2023-09-28T07:47:28.938476Z"},"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-09-28T07:47:28.943574Z","iopub.execute_input":"2023-09-28T07:47:28.946043Z","iopub.status.idle":"2023-09-28T07:47:29.021361Z","shell.execute_reply.started":"2023-09-28T07:47:28.945994Z","shell.execute_reply":"2023-09-28T07:47:29.020396Z"},"trusted":true},"execution_count":null,"outputs":[]}]}