{"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-06T05:09:01.018790Z","iopub.execute_input":"2023-10-06T05:09:01.019111Z","iopub.status.idle":"2023-10-06T05:09:01.342021Z","shell.execute_reply.started":"2023-10-06T05:09:01.019086Z","shell.execute_reply":"2023-10-06T05:09:01.341067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:09:01.343933Z","iopub.execute_input":"2023-10-06T05:09:01.344662Z","iopub.status.idle":"2023-10-06T05:09:06.291817Z","shell.execute_reply.started":"2023-10-06T05:09:01.344628Z","shell.execute_reply":"2023-10-06T05:09:06.290941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:09:06.293147Z","iopub.execute_input":"2023-10-06T05:09:06.294088Z","iopub.status.idle":"2023-10-06T05:09:18.019190Z","shell.execute_reply.started":"2023-10-06T05:09:06.294056Z","shell.execute_reply":"2023-10-06T05:09:18.018047Z"},"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-06T05:09:18.025310Z","iopub.execute_input":"2023-10-06T05:09:18.027628Z","iopub.status.idle":"2023-10-06T05:09:18.219197Z","shell.execute_reply.started":"2023-10-06T05:09:18.027592Z","shell.execute_reply":"2023-10-06T05:09:18.217724Z"},"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-06T05:09:18.223410Z","iopub.execute_input":"2023-10-06T05:09:18.225440Z","iopub.status.idle":"2023-10-06T05:10:00.881283Z","shell.execute_reply.started":"2023-10-06T05:09:18.225409Z","shell.execute_reply":"2023-10-06T05:10:00.880320Z"},"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-06T05:10:00.882830Z","iopub.execute_input":"2023-10-06T05:10:00.883171Z","iopub.status.idle":"2023-10-06T05:10:00.950892Z","shell.execute_reply.started":"2023-10-06T05:10:00.883140Z","shell.execute_reply":"2023-10-06T05:10:00.949933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists(\"/kaggle/temp/valid\"):\n    os.makedirs(\"/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-06T05:10:00.955712Z","iopub.execute_input":"2023-10-06T05:10:00.958301Z","iopub.status.idle":"2023-10-06T05:10:06.220409Z","shell.execute_reply.started":"2023-10-06T05:10:00.958204Z","shell.execute_reply":"2023-10-06T05:10:06.219489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_tfms = [*aug_transforms(), Normalize.from_stats(*imagenet_stats)]\ndls = ImageDataLoaders.from_folder(path='/kaggle/temp', \n                                   train='train', \n                                   valid='valid', \n                                   item_tfms=Resize(224), \n                                   batch_tfms=batch_tfms, \n                                   bs=64)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:10:06.222157Z","iopub.execute_input":"2023-10-06T05:10:06.222801Z","iopub.status.idle":"2023-10-06T05:10:16.776072Z","shell.execute_reply.started":"2023-10-06T05:10:06.222769Z","shell.execute_reply":"2023-10-06T05:10:16.775119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:10:16.777196Z","iopub.execute_input":"2023-10-06T05:10:16.777549Z","iopub.status.idle":"2023-10-06T05:10:16.788043Z","shell.execute_reply.started":"2023-10-06T05:10:16.777513Z","shell.execute_reply":"2023-10-06T05:10:16.785672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:10:16.792562Z","iopub.execute_input":"2023-10-06T05:10:16.793408Z","iopub.status.idle":"2023-10-06T05:10:18.039077Z","shell.execute_reply.started":"2023-10-06T05:10:16.793371Z","shell.execute_reply":"2023-10-06T05:10:18.038303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet34, metrics=accuracy, wd=0.01)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:10:18.040023Z","iopub.execute_input":"2023-10-06T05:10:18.040414Z","iopub.status.idle":"2023-10-06T05:10:19.040176Z","shell.execute_reply.started":"2023-10-06T05:10:18.040361Z","shell.execute_reply":"2023-10-06T05:10:19.039274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:10:19.041772Z","iopub.execute_input":"2023-10-06T05:10:19.042408Z","iopub.status.idle":"2023-10-06T05:10:40.078684Z","shell.execute_reply.started":"2023-10-06T05:10:19.042375Z","shell.execute_reply":"2023-10-06T05:10:40.077765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:10:40.080600Z","iopub.execute_input":"2023-10-06T05:10:40.081279Z","iopub.status.idle":"2023-10-06T05:17:17.057419Z","shell.execute_reply.started":"2023-10-06T05:10:40.081229Z","shell.execute_reply":"2023-10-06T05:17:17.056408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:17:17.058992Z","iopub.execute_input":"2023-10-06T05:17:17.059883Z","iopub.status.idle":"2023-10-06T05:17:32.669092Z","shell.execute_reply.started":"2023-10-06T05:17:17.059847Z","shell.execute_reply":"2023-10-06T05:17:32.668099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5, lr_max=slice(1e-6,3e-5))","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:17:32.671065Z","iopub.execute_input":"2023-10-06T05:17:32.671738Z","iopub.status.idle":"2023-10-06T05:30:43.706238Z","shell.execute_reply.started":"2023-10-06T05:17:32.671705Z","shell.execute_reply":"2023-10-06T05:30:43.705025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:30:43.708235Z","iopub.execute_input":"2023-10-06T05:30:43.708685Z","iopub.status.idle":"2023-10-06T05:30:44.054562Z","shell.execute_reply.started":"2023-10-06T05:30:43.708634Z","shell.execute_reply":"2023-10-06T05:30:44.053629Z"},"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')\nlearn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:30:44.055956Z","iopub.execute_input":"2023-10-06T05:30:44.056854Z","iopub.status.idle":"2023-10-06T05:44:04.785501Z","shell.execute_reply.started":"2023-10-06T05:30:44.056820Z","shell.execute_reply":"2023-10-06T05:44:04.784492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:44:04.786857Z","iopub.execute_input":"2023-10-06T05:44:04.787165Z","iopub.status.idle":"2023-10-06T05:44:05.144619Z","shell.execute_reply.started":"2023-10-06T05:44:04.787142Z","shell.execute_reply":"2023-10-06T05:44:05.143618Z"},"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-06T05:44:05.146832Z","iopub.execute_input":"2023-10-06T05:44:05.148691Z","iopub.status.idle":"2023-10-06T05:44:05.748810Z","shell.execute_reply.started":"2023-10-06T05:44:05.148655Z","shell.execute_reply":"2023-10-06T05:44:05.747802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl=learn.dls.test_dl(new)\nclass_score,y=learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:44:05.753139Z","iopub.execute_input":"2023-10-06T05:44:05.755217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score = np.argmax(class_score, axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)\nlearn.dls.vocab","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes=np.empty(shape=300000,dtype=np.dtype('U20'))","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = Image.open('/kaggle/temp/test/test/134190.png')\nim.to_thumb(254,254)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}