{"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-04T14:15:45.705260Z","iopub.execute_input":"2023-10-04T14:15:45.706066Z","iopub.status.idle":"2023-10-04T14:15:46.036951Z","shell.execute_reply.started":"2023-10-04T14:15:45.706033Z","shell.execute_reply":"2023-10-04T14:15:46.036001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T14:15:46.039042Z","iopub.execute_input":"2023-10-04T14:15:46.039835Z","iopub.status.idle":"2023-10-04T14:15:53.130033Z","shell.execute_reply.started":"2023-10-04T14:15:46.039800Z","shell.execute_reply":"2023-10-04T14:15:53.128875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T14:15:53.131608Z","iopub.execute_input":"2023-10-04T14:15:53.131944Z","iopub.status.idle":"2023-10-04T14:16:06.392148Z","shell.execute_reply.started":"2023-10-04T14:15:53.131913Z","shell.execute_reply":"2023-10-04T14:16:06.390808Z"},"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-04T14:16:06.395545Z","iopub.execute_input":"2023-10-04T14:16:06.395949Z","iopub.status.idle":"2023-10-04T14:16:06.530945Z","shell.execute_reply.started":"2023-10-04T14:16:06.395913Z","shell.execute_reply":"2023-10-04T14:16:06.529892Z"},"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-04T14:16:06.532289Z","iopub.execute_input":"2023-10-04T14:16:06.533232Z","iopub.status.idle":"2023-10-04T14:16:50.879132Z","shell.execute_reply.started":"2023-10-04T14:16:06.533199Z","shell.execute_reply":"2023-10-04T14:16:50.878130Z"},"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-04T14:16:50.880441Z","iopub.execute_input":"2023-10-04T14:16:50.881383Z","iopub.status.idle":"2023-10-04T14:16:50.957843Z","shell.execute_reply.started":"2023-10-04T14:16:50.881351Z","shell.execute_reply":"2023-10-04T14:16:50.956898Z"},"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-04T14:16:50.959367Z","iopub.execute_input":"2023-10-04T14:16:50.960008Z","iopub.status.idle":"2023-10-04T14:16:57.488944Z","shell.execute_reply.started":"2023-10-04T14:16:50.959955Z","shell.execute_reply":"2023-10-04T14:16:57.487969Z"},"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=128)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T14:16:57.490538Z","iopub.execute_input":"2023-10-04T14:16:57.490893Z","iopub.status.idle":"2023-10-04T14:17:04.362319Z","shell.execute_reply.started":"2023-10-04T14:16:57.490829Z","shell.execute_reply":"2023-10-04T14:17:04.361351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T14:17:04.363807Z","iopub.execute_input":"2023-10-04T14:17:04.364381Z","iopub.status.idle":"2023-10-04T14:17:04.371969Z","shell.execute_reply.started":"2023-10-04T14:17:04.364348Z","shell.execute_reply":"2023-10-04T14:17:04.371131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T14:17:04.376736Z","iopub.execute_input":"2023-10-04T14:17:04.377581Z","iopub.status.idle":"2023-10-04T14:17:05.881779Z","shell.execute_reply.started":"2023-10-04T14:17:04.377461Z","shell.execute_reply":"2023-10-04T14:17:05.880884Z"},"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-04T14:17:05.882778Z","iopub.execute_input":"2023-10-04T14:17:05.883134Z","iopub.status.idle":"2023-10-04T14:17:08.369465Z","shell.execute_reply.started":"2023-10-04T14:17:05.883104Z","shell.execute_reply":"2023-10-04T14:17:08.368508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\nnewLR = learn.lr_find() \nprint (newLR)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T14:17:08.373884Z","iopub.execute_input":"2023-10-04T14:17:08.376025Z","iopub.status.idle":"2023-10-04T14:18:35.637097Z","shell.execute_reply.started":"2023-10-04T14:17:08.375982Z","shell.execute_reply":"2023-10-04T14:18:35.636025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(10, lr_max=newLR.valley)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T14:18:35.638722Z","iopub.execute_input":"2023-10-04T14:18:35.640025Z","iopub.status.idle":"2023-10-04T15:14:33.552046Z","shell.execute_reply.started":"2023-10-04T14:18:35.639955Z","shell.execute_reply":"2023-10-04T15:14:33.549461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.553324Z","iopub.status.idle":"2023-10-04T15:14:33.554017Z","shell.execute_reply.started":"2023-10-04T15:14:33.553765Z","shell.execute_reply":"2023-10-04T15:14:33.553788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newLR2= learn.lr_find()\nprint(newLR2)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.555226Z","iopub.status.idle":"2023-10-04T15:14:33.555853Z","shell.execute_reply.started":"2023-10-04T15:14:33.555627Z","shell.execute_reply":"2023-10-04T15:14:33.555648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(15, lr_max=slice(newLR2.valley,newLR.valley) )\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.556950Z","iopub.status.idle":"2023-10-04T15:14:33.557582Z","shell.execute_reply.started":"2023-10-04T15:14:33.557353Z","shell.execute_reply":"2023-10-04T15:14:33.557374Z"},"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-04T15:14:33.558696Z","iopub.status.idle":"2023-10-04T15:14:33.559344Z","shell.execute_reply.started":"2023-10-04T15:14:33.559118Z","shell.execute_reply":"2023-10-04T15:14:33.559139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.560478Z","iopub.status.idle":"2023-10-04T15:14:33.561108Z","shell.execute_reply.started":"2023-10-04T15:14:33.560877Z","shell.execute_reply":"2023-10-04T15:14:33.560897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.562192Z","iopub.status.idle":"2023-10-04T15:14:33.562816Z","shell.execute_reply.started":"2023-10-04T15:14:33.562585Z","shell.execute_reply":"2023-10-04T15:14:33.562605Z"},"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-04T15:14:33.564041Z","iopub.status.idle":"2023-10-04T15:14:33.564656Z","shell.execute_reply.started":"2023-10-04T15:14:33.564430Z","shell.execute_reply":"2023-10-04T15:14:33.564450Z"},"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-04T15:14:33.565801Z","iopub.status.idle":"2023-10-04T15:14:33.566479Z","shell.execute_reply.started":"2023-10-04T15:14:33.566255Z","shell.execute_reply":"2023-10-04T15:14:33.566276Z"},"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-04T15:14:33.567642Z","iopub.status.idle":"2023-10-04T15:14:33.568295Z","shell.execute_reply.started":"2023-10-04T15:14:33.568069Z","shell.execute_reply":"2023-10-04T15:14:33.568090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.569441Z","iopub.status.idle":"2023-10-04T15:14:33.570131Z","shell.execute_reply.started":"2023-10-04T15:14:33.569883Z","shell.execute_reply":"2023-10-04T15:14:33.569905Z"},"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-04T15:14:33.571277Z","iopub.status.idle":"2023-10-04T15:14:33.571908Z","shell.execute_reply.started":"2023-10-04T15:14:33.571694Z","shell.execute_reply":"2023-10-04T15:14:33.571715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.573215Z","iopub.status.idle":"2023-10-04T15:14:33.573827Z","shell.execute_reply.started":"2023-10-04T15:14:33.573614Z","shell.execute_reply":"2023-10-04T15:14:33.573634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.574970Z","iopub.status.idle":"2023-10-04T15:14:33.575615Z","shell.execute_reply.started":"2023-10-04T15:14:33.575394Z","shell.execute_reply":"2023-10-04T15:14:33.575415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.576719Z","iopub.status.idle":"2023-10-04T15:14:33.577365Z","shell.execute_reply.started":"2023-10-04T15:14:33.577141Z","shell.execute_reply":"2023-10-04T15:14:33.577162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.578482Z","iopub.status.idle":"2023-10-04T15:14:33.579109Z","shell.execute_reply.started":"2023-10-04T15:14:33.578878Z","shell.execute_reply":"2023-10-04T15:14:33.578899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.580189Z","iopub.status.idle":"2023-10-04T15:14:33.580841Z","shell.execute_reply.started":"2023-10-04T15:14:33.580615Z","shell.execute_reply":"2023-10-04T15:14:33.580636Z"},"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-04T15:14:33.581987Z","iopub.status.idle":"2023-10-04T15:14:33.582657Z","shell.execute_reply.started":"2023-10-04T15:14:33.582421Z","shell.execute_reply":"2023-10-04T15:14:33.582442Z"},"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-04T15:14:33.583765Z","iopub.status.idle":"2023-10-04T15:14:33.584376Z","shell.execute_reply.started":"2023-10-04T15:14:33.584165Z","shell.execute_reply":"2023-10-04T15:14:33.584185Z"},"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-04T15:14:33.585529Z","iopub.status.idle":"2023-10-04T15:14:33.586167Z","shell.execute_reply.started":"2023-10-04T15:14:33.585936Z","shell.execute_reply":"2023-10-04T15:14:33.585956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.587250Z","iopub.status.idle":"2023-10-04T15:14:33.587859Z","shell.execute_reply.started":"2023-10-04T15:14:33.587648Z","shell.execute_reply":"2023-10-04T15:14:33.587669Z"},"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-04T15:14:33.588943Z","iopub.status.idle":"2023-10-04T15:14:33.589570Z","shell.execute_reply.started":"2023-10-04T15:14:33.589347Z","shell.execute_reply":"2023-10-04T15:14:33.589368Z"},"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-04T15:14:33.590755Z","iopub.status.idle":"2023-10-04T15:14:33.591411Z","shell.execute_reply.started":"2023-10-04T15:14:33.591189Z","shell.execute_reply":"2023-10-04T15:14:33.591209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.592557Z","iopub.status.idle":"2023-10-04T15:14:33.593182Z","shell.execute_reply.started":"2023-10-04T15:14:33.592950Z","shell.execute_reply":"2023-10-04T15:14:33.592971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T15:14:33.594287Z","iopub.status.idle":"2023-10-04T15:14:33.594942Z","shell.execute_reply.started":"2023-10-04T15:14:33.594716Z","shell.execute_reply":"2023-10-04T15:14:33.594738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import h5py\n\n# # Assuming you have a pre-trained model or another model you want to save\n# # Replace 'model' with the name of your model\n# my_model = learn.model\n\n# # Create an HDF5 file\n# with h5py.File('/kaggle/working/my_model.h5', 'w') as file:\n#     # Save your model's weights\n#     file.create_dataset('model_weights', data=my_model.get_weights())  # 'model_weights' is an example dataset name\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:53:32.975832Z","iopub.execute_input":"2023-10-05T04:53:32.976266Z","iopub.status.idle":"2023-10-05T04:53:33.014119Z","shell.execute_reply.started":"2023-10-05T04:53:32.976230Z","shell.execute_reply":"2023-10-05T04:53:33.011886Z"},"trusted":true},"execution_count":null,"outputs":[]}]}