{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:13:32.607370Z","iopub.execute_input":"2025-11-04T05:13:32.607617Z","iopub.status.idle":"2025-11-04T05:13:32.871750Z","shell.execute_reply.started":"2025-11-04T05:13:32.607591Z","shell.execute_reply":"2025-11-04T05:13:32.870858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from fastai.vision.all import *\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:13:32.872579Z","iopub.execute_input":"2025-11-04T05:13:32.872958Z","iopub.status.idle":"2025-11-04T05:13:44.903250Z","shell.execute_reply.started":"2025-11-04T05:13:32.872933Z","shell.execute_reply":"2025-11-04T05:13:44.902437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:13:44.904936Z","iopub.execute_input":"2025-11-04T05:13:44.905162Z","iopub.status.idle":"2025-11-04T05:13:50.398075Z","shell.execute_reply.started":"2025-11-04T05:13:44.905143Z","shell.execute_reply":"2025-11-04T05:13:50.397312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from py7zr import unpack_7zarchive\nimport shutil\nshutil.register_unpack_format('7zip',['.7z'],unpack_7zarchive)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:13:50.401700Z","iopub.execute_input":"2025-11-04T05:13:50.401930Z","iopub.status.idle":"2025-11-04T05:13:50.625187Z","shell.execute_reply.started":"2025-11-04T05:13:50.401904Z","shell.execute_reply":"2025-11-04T05:13:50.624625Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"shutil.unpack_archive('../input/cifar-10/train.7z', '/kaggle/temp/')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:13:50.625949Z","iopub.execute_input":"2025-11-04T05:13:50.626171Z","iopub.status.idle":"2025-11-04T05:14:13.205411Z","shell.execute_reply.started":"2025-11-04T05:13:50.626154Z","shell.execute_reply":"2025-11-04T05:14:13.204849Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:14:13.206217Z","iopub.execute_input":"2025-11-04T05:14:13.206477Z","iopub.status.idle":"2025-11-04T05:14:13.253120Z","shell.execute_reply.started":"2025-11-04T05:14:13.206452Z","shell.execute_reply":"2025-11-04T05:14:13.252323Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:14:13.253801Z","iopub.execute_input":"2025-11-04T05:14:13.254091Z","iopub.status.idle":"2025-11-04T05:14:17.469095Z","shell.execute_reply.started":"2025-11-04T05:14:13.254067Z","shell.execute_reply":"2025-11-04T05:14:17.468519Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dls=ImageDataLoaders.from_folder(path='/kaggle/temp',\n                            train='train', valid='valid',item_tfms=Resize(224),bs=64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:14:17.471412Z","iopub.execute_input":"2025-11-04T05:14:17.471687Z","iopub.status.idle":"2025-11-04T05:14:20.036175Z","shell.execute_reply.started":"2025-11-04T05:14:17.471668Z","shell.execute_reply":"2025-11-04T05:14:20.035637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dls=ImageDataLoaders.from_folder(path='/kaggle/temp',\n                            train='train', valid='valid',item_tfms=Resize(224),bs=64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:14:20.036839Z","iopub.execute_input":"2025-11-04T05:14:20.037038Z","iopub.status.idle":"2025-11-04T05:14:22.564673Z","shell.execute_reply.started":"2025-11-04T05:14:20.037024Z","shell.execute_reply":"2025-11-04T05:14:22.564045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dls.show_batch()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:14:22.566285Z","iopub.execute_input":"2025-11-04T05:14:22.566585Z","iopub.status.idle":"2025-11-04T05:14:23.587311Z","shell.execute_reply.started":"2025-11-04T05:14:22.566565Z","shell.execute_reply":"2025-11-04T05:14:23.586605Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn = vision_learner(dls, resnet50, metrics=accuracy)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:14:23.588216Z","iopub.execute_input":"2025-11-04T05:14:23.588424Z","iopub.status.idle":"2025-11-04T05:14:24.804379Z","shell.execute_reply.started":"2025-11-04T05:14:23.588409Z","shell.execute_reply":"2025-11-04T05:14:24.803575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:14:24.805612Z","iopub.execute_input":"2025-11-04T05:14:24.805954Z","iopub.status.idle":"2025-11-04T05:14:48.330347Z","shell.execute_reply.started":"2025-11-04T05:14:24.805924Z","shell.execute_reply":"2025-11-04T05:14:48.329597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.fit_one_cycle(10, 3e-3)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T05:14:48.331362Z","iopub.execute_input":"2025-11-04T05:14:48.331654Z","execution_failed":"2025-11-04T06:49:11.501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.unfreeze()\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.fit_one_cycle(5, lr_max=slice(1e-6,3e-5))\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.recorder.plot_loss()\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"shutil.unpack_archive('/kaggle/input/cifar-10/test.7z','/kaggle/temp/test')\nshutil.unregister_unpack_format('7zip')","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path='/kaggle/temp/test/test'\nf=os.listdir(path)\nnew=[str(path)+'/'+s for s in f]","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dl=learn.dls.test_dl(new)\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_score,y=learn.get_preds(dl=test_dl)\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_score","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_score = np.argmax(class_score, axis=1)\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_score[1].item()\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_score","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(classScore)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.503Z"}},"outputs":[],"execution_count":null},{"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":{"execution_failed":"2025-11-04T06:49:11.503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_classes=np.empty(shape=300000,dtype=np.dtype('U20'))","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ind=0\nfor i in (classScore):\n    predicted_classes[ind]=classes[i]\n    ind=ind+1","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_classes","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.503Z"}},"outputs":[],"execution_count":null},{"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":{"execution_failed":"2025-11-04T06:49:11.503Z"}},"outputs":[],"execution_count":null},{"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":{"execution_failed":"2025-11-04T06:49:11.503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im = Image.open('/kaggle/temp/test/test/134190.png')\nim.to_thumb(254,254)\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-11-04T06:49:11.504Z"}},"outputs":[],"execution_count":null}]}