{"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-14T08:54:24.647953Z","iopub.execute_input":"2023-10-14T08:54:24.648313Z","iopub.status.idle":"2023-10-14T08:54:24.948212Z","shell.execute_reply.started":"2023-10-14T08:54:24.648292Z","shell.execute_reply":"2023-10-14T08:54:24.947241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2023-10-14T08:54:24.950025Z","iopub.execute_input":"2023-10-14T08:54:24.950688Z","iopub.status.idle":"2023-10-14T08:54:31.341256Z","shell.execute_reply.started":"2023-10-14T08:54:24.950654Z","shell.execute_reply":"2023-10-14T08:54:31.340391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-10-14T08:54:31.342523Z","iopub.execute_input":"2023-10-14T08:54:31.342848Z","iopub.status.idle":"2023-10-14T08:54:43.255451Z","shell.execute_reply.started":"2023-10-14T08:54:31.342820Z","shell.execute_reply":"2023-10-14T08:54:43.254282Z"},"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-14T08:54:43.258600Z","iopub.execute_input":"2023-10-14T08:54:43.258953Z","iopub.status.idle":"2023-10-14T08:54:43.380524Z","shell.execute_reply.started":"2023-10-14T08:54:43.258921Z","shell.execute_reply":"2023-10-14T08:54:43.379677Z"},"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-14T08:54:43.381862Z","iopub.execute_input":"2023-10-14T08:54:43.382431Z","iopub.status.idle":"2023-10-14T08:55:25.181881Z","shell.execute_reply.started":"2023-10-14T08:54:43.382400Z","shell.execute_reply":"2023-10-14T08:55:25.180914Z"},"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-14T08:55:25.183051Z","iopub.execute_input":"2023-10-14T08:55:25.183975Z","iopub.status.idle":"2023-10-14T08:55:25.247475Z","shell.execute_reply.started":"2023-10-14T08:55:25.183944Z","shell.execute_reply":"2023-10-14T08:55:25.246404Z"},"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-14T08:55:25.252304Z","iopub.execute_input":"2023-10-14T08:55:25.254888Z","iopub.status.idle":"2023-10-14T08:55:30.170397Z","shell.execute_reply.started":"2023-10-14T08:55:25.254848Z","shell.execute_reply":"2023-10-14T08:55:30.169482Z"},"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-14T08:55:30.171639Z","iopub.execute_input":"2023-10-14T08:55:30.172586Z","iopub.status.idle":"2023-10-14T08:55:40.043095Z","shell.execute_reply.started":"2023-10-14T08:55:30.172554Z","shell.execute_reply":"2023-10-14T08:55:40.042136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-14T08:55:40.044334Z","iopub.execute_input":"2023-10-14T08:55:40.044873Z","iopub.status.idle":"2023-10-14T08:55:40.055029Z","shell.execute_reply.started":"2023-10-14T08:55:40.044840Z","shell.execute_reply":"2023-10-14T08:55:40.054151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T08:55:40.059700Z","iopub.execute_input":"2023-10-14T08:55:40.060226Z","iopub.status.idle":"2023-10-14T08:55:41.807784Z","shell.execute_reply.started":"2023-10-14T08:55:40.060196Z","shell.execute_reply":"2023-10-14T08:55:41.806975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet101, metrics=accuracy, cbs=MixUp(0.4))","metadata":{"execution":{"iopub.status.busy":"2023-10-14T08:55:41.808775Z","iopub.execute_input":"2023-10-14T08:55:41.809060Z","iopub.status.idle":"2023-10-14T08:55:44.943051Z","shell.execute_reply.started":"2023-10-14T08:55:41.809035Z","shell.execute_reply":"2023-10-14T08:55:44.942123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initial fine-tuning with frozen pre-trained weights\nlearn.fit_one_cycle(3, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T08:55:44.944222Z","iopub.execute_input":"2023-10-14T08:55:44.944761Z","iopub.status.idle":"2023-10-14T09:09:54.588252Z","shell.execute_reply.started":"2023-10-14T08:55:44.944731Z","shell.execute_reply":"2023-10-14T09:09:54.587224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Unfreeze all layers\nlearn.unfreeze()\n\n# Find an optimal learning rate again if you wish\nlearn.lr_find() # This is optional, but useful to see the optimal range again","metadata":{"execution":{"iopub.status.busy":"2023-10-14T09:09:54.590076Z","iopub.execute_input":"2023-10-14T09:09:54.590414Z","iopub.status.idle":"2023-10-14T09:10:44.301276Z","shell.execute_reply.started":"2023-10-14T09:09:54.590381Z","shell.execute_reply":"2023-10-14T09:10:44.300293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply differential learning rates\nlearn.fit_one_cycle(5, lr_max=slice(1e-6,3e-5))","metadata":{"execution":{"iopub.status.busy":"2023-10-14T09:10:44.302940Z","iopub.execute_input":"2023-10-14T09:10:44.303948Z","iopub.status.idle":"2023-10-14T09:41:30.826943Z","shell.execute_reply.started":"2023-10-14T09:10:44.303917Z","shell.execute_reply":"2023-10-14T09:41:30.825427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T09:41:30.828696Z","iopub.execute_input":"2023-10-14T09:41:30.829306Z","iopub.status.idle":"2023-10-14T09:41:31.431231Z","shell.execute_reply.started":"2023-10-14T09:41:30.829269Z","shell.execute_reply":"2023-10-14T09:41:31.430321Z"},"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-14T09:41:31.432423Z","iopub.execute_input":"2023-10-14T09:41:31.433222Z","iopub.status.idle":"2023-10-14T09:54:39.967738Z","shell.execute_reply.started":"2023-10-14T09:41:31.433180Z","shell.execute_reply":"2023-10-14T09:54:39.966789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-14T09:54:39.969212Z","iopub.execute_input":"2023-10-14T09:54:39.969880Z","iopub.status.idle":"2023-10-14T09:54:40.159708Z","shell.execute_reply.started":"2023-10-14T09:54:39.969848Z","shell.execute_reply":"2023-10-14T09:54:40.158718Z"},"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-14T09:54:40.161181Z","iopub.execute_input":"2023-10-14T09:54:40.161515Z","iopub.status.idle":"2023-10-14T09:54:40.505880Z","shell.execute_reply.started":"2023-10-14T09:54:40.161471Z","shell.execute_reply":"2023-10-14T09:54:40.502571Z"},"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-14T09:54:40.511459Z","iopub.execute_input":"2023-10-14T09:54:40.512262Z","iopub.status.idle":"2023-10-14T10:08:23.820842Z","shell.execute_reply.started":"2023-10-14T09:54:40.512224Z","shell.execute_reply":"2023-10-14T10:08:23.819709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-14T10:08:23.822475Z","iopub.execute_input":"2023-10-14T10:08:23.823079Z","iopub.status.idle":"2023-10-14T10:08:23.839439Z","shell.execute_reply.started":"2023-10-14T10:08:23.823044Z","shell.execute_reply":"2023-10-14T10:08:23.838494Z"},"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-14T10:08:23.840964Z","iopub.execute_input":"2023-10-14T10:08:23.841291Z","iopub.status.idle":"2023-10-14T10:08:23.876082Z","shell.execute_reply.started":"2023-10-14T10:08:23.841262Z","shell.execute_reply":"2023-10-14T10:08:23.875192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T10:08:23.877256Z","iopub.execute_input":"2023-10-14T10:08:23.877821Z","iopub.status.idle":"2023-10-14T10:08:23.884411Z","shell.execute_reply.started":"2023-10-14T10:08:23.877790Z","shell.execute_reply":"2023-10-14T10:08:23.883231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-14T10:08:23.885888Z","iopub.execute_input":"2023-10-14T10:08:23.886612Z","iopub.status.idle":"2023-10-14T10:08:23.894519Z","shell.execute_reply.started":"2023-10-14T10:08:23.886581Z","shell.execute_reply":"2023-10-14T10:08:23.893562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)\nlearn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-10-14T10:09:06.490294Z","iopub.execute_input":"2023-10-14T10:09:06.490761Z","iopub.status.idle":"2023-10-14T10:09:06.501404Z","shell.execute_reply.started":"2023-10-14T10:09:06.490727Z","shell.execute_reply":"2023-10-14T10:09:06.500250Z"},"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-14T10:09:11.115978Z","iopub.execute_input":"2023-10-14T10:09:11.116401Z","iopub.status.idle":"2023-10-14T10:09:11.122172Z","shell.execute_reply.started":"2023-10-14T10:09:11.116369Z","shell.execute_reply":"2023-10-14T10:09:11.121260Z"},"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-14T10:09:16.702587Z","iopub.execute_input":"2023-10-14T10:09:16.702977Z","iopub.status.idle":"2023-10-14T10:09:16.717246Z","shell.execute_reply.started":"2023-10-14T10:09:16.702948Z","shell.execute_reply":"2023-10-14T10:09:16.716295Z"},"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-14T10:09:28.993390Z","iopub.execute_input":"2023-10-14T10:09:28.993861Z","iopub.status.idle":"2023-10-14T10:09:29.963026Z","shell.execute_reply.started":"2023-10-14T10:09:28.993826Z","shell.execute_reply":"2023-10-14T10:09:29.960755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"execution":{"iopub.status.busy":"2023-10-14T10:08:24.180859Z","iopub.status.idle":"2023-10-14T10:08:24.181811Z","shell.execute_reply.started":"2023-10-14T10:08:24.181593Z","shell.execute_reply":"2023-10-14T10:08:24.181615Z"},"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-14T10:08:24.182934Z","iopub.status.idle":"2023-10-14T10:08:24.183882Z","shell.execute_reply.started":"2023-10-14T10:08:24.183665Z","shell.execute_reply":"2023-10-14T10:08:24.183686Z"},"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-14T10:08:24.184991Z","iopub.status.idle":"2023-10-14T10:08:24.185954Z","shell.execute_reply.started":"2023-10-14T10:08:24.185738Z","shell.execute_reply":"2023-10-14T10:08:24.185759Z"},"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-14T10:08:24.187052Z","iopub.status.idle":"2023-10-14T10:08:24.187991Z","shell.execute_reply.started":"2023-10-14T10:08:24.187776Z","shell.execute_reply":"2023-10-14T10:08:24.187797Z"},"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":"2023-10-14T10:08:24.189174Z","iopub.status.idle":"2023-10-14T10:08:24.190145Z","shell.execute_reply.started":"2023-10-14T10:08:24.189931Z","shell.execute_reply":"2023-10-14T10:08:24.189951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy = learn.validate()\nprint(f'Final Validation Loss: {loss}')\nprint(f'Final Validation Accuracy: {accuracy}')","metadata":{"execution":{"iopub.status.busy":"2023-10-14T10:08:24.191409Z","iopub.status.idle":"2023-10-14T10:08:24.192428Z","shell.execute_reply.started":"2023-10-14T10:08:24.192125Z","shell.execute_reply":"2023-10-14T10:08:24.192146Z"},"trusted":true},"execution_count":null,"outputs":[]}]}