{"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 # 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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom tensorflow import keras\nimport tensorflow as tf\nfrom keras import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPooling2D, Activation, BatchNormalization, Flatten, Dropout\nfrom keras.utils.np_utils import to_categorical\nfrom keras.preprocessing.image import ImageDataGenerator\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))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-19T13:42:00.641139Z","iopub.execute_input":"2022-09-19T13:42:00.642312Z","iopub.status.idle":"2022-09-19T13:42:08.308116Z","shell.execute_reply.started":"2022-09-19T13:42:00.642218Z","shell.execute_reply":"2022-09-19T13:42:08.306448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2022-09-19T13:42:08.310998Z","iopub.execute_input":"2022-09-19T13:42:08.312273Z","iopub.status.idle":"2022-09-19T13:42:28.250674Z","shell.execute_reply.started":"2022-09-19T13:42:08.312218Z","shell.execute_reply":"2022-09-19T13:42:28.249058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nfrom py7zr import unpack_7zarchive\nshutil.register_unpack_format('7zip',['.7z'],unpack_7zarchive)\nshutil.unpack_archive('../input/cifar-10/train.7z', '/kaggle/temp/')","metadata":{"execution":{"iopub.status.busy":"2022-09-19T13:42:28.254214Z","iopub.execute_input":"2022-09-19T13:42:28.255788Z","iopub.status.idle":"2022-09-19T13:43:30.755453Z","shell.execute_reply.started":"2022-09-19T13:42:28.255730Z","shell.execute_reply":"2022-09-19T13:43:30.754098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \ntrain_labels = pd.read_csv(\"../input/cifar-10/trainLabels.csv\", header=\"infer\")\n\nclasses = train_labels['label'].unique()\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T13:43:30.759610Z","iopub.execute_input":"2022-09-19T13:43:30.760448Z","iopub.status.idle":"2022-09-19T13:43:30.865339Z","shell.execute_reply.started":"2022-09-19T13:43:30.760400Z","shell.execute_reply":"2022-09-19T13:43:30.859149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\n\nif 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\"\nparent_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)\n        \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":"2022-09-19T13:43:30.874178Z","iopub.execute_input":"2022-09-19T13:43:30.876995Z","iopub.status.idle":"2022-09-19T13:43:40.346756Z","shell.execute_reply.started":"2022-09-19T13:43:30.876909Z","shell.execute_reply":"2022-09-19T13:43:40.345326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/temp/valid/airplane/* | wc\n!ls /kaggle/temp/train","metadata":{"execution":{"iopub.status.busy":"2022-09-19T13:43:40.349055Z","iopub.execute_input":"2022-09-19T13:43:40.349595Z","iopub.status.idle":"2022-09-19T13:43:42.970057Z","shell.execute_reply.started":"2022-09-19T13:43:40.349519Z","shell.execute_reply":"2022-09-19T13:43:42.968122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\ndls_img=ImageDataLoaders.from_folder(path='/kaggle/temp',\n                            train='train', valid='valid',item_tfms=Resize(224),bs=64)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T13:43:42.972698Z","iopub.execute_input":"2022-09-19T13:43:42.973339Z","iopub.status.idle":"2022-09-19T13:44:00.581034Z","shell.execute_reply.started":"2022-09-19T13:43:42.973286Z","shell.execute_reply":"2022-09-19T13:44:00.579705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls_img.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-09-19T13:44:00.583471Z","iopub.execute_input":"2022-09-19T13:44:00.584314Z","iopub.status.idle":"2022-09-19T13:44:01.674987Z","shell.execute_reply.started":"2022-09-19T13:44:00.584269Z","shell.execute_reply":"2022-09-19T13:44:01.673736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls_img, resnet50, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T13:44:01.676245Z","iopub.execute_input":"2022-09-19T13:44:01.676760Z","iopub.status.idle":"2022-09-19T13:44:06.265216Z","shell.execute_reply.started":"2022-09-19T13:44:01.676720Z","shell.execute_reply":"2022-09-19T13:44:06.263811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2022-09-19T13:44:06.272406Z","iopub.execute_input":"2022-09-19T13:44:06.273484Z","iopub.status.idle":"2022-09-19T13:44:41.357113Z","shell.execute_reply.started":"2022-09-19T13:44:06.273394Z","shell.execute_reply":"2022-09-19T13:44:41.355701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T13:44:41.359203Z","iopub.execute_input":"2022-09-19T13:44:41.360358Z","iopub.status.idle":"2022-09-19T13:54:58.117348Z","shell.execute_reply.started":"2022-09-19T13:44:41.360312Z","shell.execute_reply":"2022-09-19T13:54:58.115757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"execution":{"iopub.status.busy":"2022-09-19T13:54:58.119783Z","iopub.execute_input":"2022-09-19T13:54:58.120804Z","iopub.status.idle":"2022-09-19T13:54:58.459056Z","shell.execute_reply.started":"2022-09-19T13:54:58.120750Z","shell.execute_reply":"2022-09-19T13:54:58.457725Z"},"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":"2022-09-19T13:54:58.460867Z","iopub.execute_input":"2022-09-19T13:54:58.462160Z","iopub.status.idle":"2022-09-19T14:20:30.314233Z","shell.execute_reply.started":"2022-09-19T13:54:58.462114Z","shell.execute_reply":"2022-09-19T14:20:30.312686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:20:30.321175Z","iopub.execute_input":"2022-09-19T14:20:30.322582Z","iopub.status.idle":"2022-09-19T14:20:31.086865Z","shell.execute_reply.started":"2022-09-19T14:20:30.322534Z","shell.execute_reply":"2022-09-19T14:20:31.081838Z"},"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":"2022-09-19T14:20:31.108617Z","iopub.execute_input":"2022-09-19T14:20:31.115382Z","iopub.status.idle":"2022-09-19T14:20:32.053068Z","shell.execute_reply.started":"2022-09-19T14:20:31.115312Z","shell.execute_reply":"2022-09-19T14:20:32.051568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl=learn.dls.test_dl(new)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:20:32.059762Z","iopub.execute_input":"2022-09-19T14:20:32.063168Z","iopub.status.idle":"2022-09-19T14:20:32.114001Z","shell.execute_reply.started":"2022-09-19T14:20:32.063090Z","shell.execute_reply":"2022-09-19T14:20:32.112479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score,y=learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:20:32.120703Z","iopub.execute_input":"2022-09-19T14:20:32.124126Z","iopub.status.idle":"2022-09-19T14:36:01.782279Z","shell.execute_reply.started":"2022-09-19T14:20:32.124079Z","shell.execute_reply":"2022-09-19T14:36:01.780792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:36:01.784432Z","iopub.execute_input":"2022-09-19T14:36:01.785983Z","iopub.status.idle":"2022-09-19T14:36:01.804177Z","shell.execute_reply.started":"2022-09-19T14:36:01.785929Z","shell.execute_reply":"2022-09-19T14:36:01.800428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score = np.argmax(class_score, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:36:01.806759Z","iopub.execute_input":"2022-09-19T14:36:01.807677Z","iopub.status.idle":"2022-09-19T14:36:01.868370Z","shell.execute_reply.started":"2022-09-19T14:36:01.807606Z","shell.execute_reply":"2022-09-19T14:36:01.867046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:36:01.872183Z","iopub.execute_input":"2022-09-19T14:36:01.872489Z","iopub.status.idle":"2022-09-19T14:36:01.883163Z","shell.execute_reply.started":"2022-09-19T14:36:01.872452Z","shell.execute_reply":"2022-09-19T14:36:01.881669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:36:01.886169Z","iopub.execute_input":"2022-09-19T14:36:01.887149Z","iopub.status.idle":"2022-09-19T14:36:02.076804Z","shell.execute_reply.started":"2022-09-19T14:36:01.887109Z","shell.execute_reply":"2022-09-19T14:36:02.075280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:36:02.078364Z","iopub.execute_input":"2022-09-19T14:36:02.078860Z","iopub.status.idle":"2022-09-19T14:36:02.093340Z","shell.execute_reply.started":"2022-09-19T14:36:02.078816Z","shell.execute_reply":"2022-09-19T14:36:02.091722Z"},"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":"2022-09-19T14:36:02.097589Z","iopub.execute_input":"2022-09-19T14:36:02.097964Z","iopub.status.idle":"2022-09-19T14:36:02.103958Z","shell.execute_reply.started":"2022-09-19T14:36:02.097932Z","shell.execute_reply":"2022-09-19T14:36:02.102530Z"},"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":"2022-09-19T14:36:02.105959Z","iopub.execute_input":"2022-09-19T14:36:02.106554Z","iopub.status.idle":"2022-09-19T14:36:02.132335Z","shell.execute_reply.started":"2022-09-19T14:36:02.106513Z","shell.execute_reply":"2022-09-19T14:36:02.131032Z"},"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":"2022-09-19T14:36:02.134688Z","iopub.execute_input":"2022-09-19T14:36:02.135237Z","iopub.status.idle":"2022-09-19T14:36:02.251544Z","shell.execute_reply.started":"2022-09-19T14:36:02.135195Z","shell.execute_reply":"2022-09-19T14:36:02.250165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:36:02.253453Z","iopub.execute_input":"2022-09-19T14:36:02.254329Z","iopub.status.idle":"2022-09-19T14:36:02.264241Z","shell.execute_reply.started":"2022-09-19T14:36:02.254286Z","shell.execute_reply":"2022-09-19T14:36:02.262504Z"},"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":"2022-09-19T14:36:02.266811Z","iopub.execute_input":"2022-09-19T14:36:02.268025Z","iopub.status.idle":"2022-09-19T14:36:03.034027Z","shell.execute_reply.started":"2022-09-19T14:36:02.267972Z","shell.execute_reply":"2022-09-19T14:36:03.031912Z"},"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":"2022-09-19T14:36:03.052302Z","iopub.execute_input":"2022-09-19T14:36:03.055918Z","iopub.status.idle":"2022-09-19T14:36:03.508256Z","shell.execute_reply.started":"2022-09-19T14:36:03.055869Z","shell.execute_reply":"2022-09-19T14:36:03.505671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:36:03.510132Z","iopub.execute_input":"2022-09-19T14:36:03.510525Z","iopub.status.idle":"2022-09-19T14:36:03.601635Z","shell.execute_reply.started":"2022-09-19T14:36:03.510484Z","shell.execute_reply":"2022-09-19T14:36:03.600148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T14:36:03.603943Z","iopub.execute_input":"2022-09-19T14:36:03.604447Z","iopub.status.idle":"2022-09-19T14:36:03.979890Z","shell.execute_reply.started":"2022-09-19T14:36:03.604388Z","shell.execute_reply":"2022-09-19T14:36:03.977887Z"},"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":{"execution":{"iopub.status.busy":"2022-09-19T14:36:03.982042Z","iopub.execute_input":"2022-09-19T14:36:03.983203Z","iopub.status.idle":"2022-09-19T14:36:03.995427Z","shell.execute_reply.started":"2022-09-19T14:36:03.983159Z","shell.execute_reply":"2022-09-19T14:36:03.993914Z"},"trusted":true},"execution_count":null,"outputs":[]}]}