{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-10-01T11:34:09.178568Z","iopub.execute_input":"2024-10-01T11:34:09.179781Z","iopub.status.idle":"2024-10-01T11:34:24.259567Z","shell.execute_reply.started":"2024-10-01T11:34:09.179728Z","shell.execute_reply":"2024-10-01T11:34:24.258629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport itertools\nfrom py7zr import unpack_7zarchive\nimport shutil\n\nfrom fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2024-10-01T11:34:24.261799Z","iopub.execute_input":"2024-10-01T11:34:24.262122Z","iopub.status.idle":"2024-10-01T11:34:31.723803Z","shell.execute_reply.started":"2024-10-01T11:34:24.262086Z","shell.execute_reply":"2024-10-01T11:34:31.723019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.register_unpack_format('7zip',['.7z'],unpack_7zarchive)\nshutil.unpack_archive('../input/cifar-10/train.7z', '/kaggle/temp/')","metadata":{"execution":{"iopub.status.busy":"2024-10-01T11:34:31.725114Z","iopub.execute_input":"2024-10-01T11:34:31.725423Z","iopub.status.idle":"2024-10-01T11:34:57.595299Z","shell.execute_reply.started":"2024-10-01T11:34:31.725391Z","shell.execute_reply":"2024-10-01T11:34:57.594251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv(\"../input/cifar-10/trainLabels.csv\", header=\"infer\")\nclasses = train_labels['label'].unique()\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2024-10-01T11:34:57.596476Z","iopub.execute_input":"2024-10-01T11:34:57.596801Z","iopub.status.idle":"2024-10-01T11:34:57.657152Z","shell.execute_reply.started":"2024-10-01T11:34:57.596768Z","shell.execute_reply":"2024-10-01T11:34:57.656200Z"},"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)\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":"2024-10-01T11:34:57.660436Z","iopub.execute_input":"2024-10-01T11:34:57.661093Z","iopub.status.idle":"2024-10-01T11:35:03.050559Z","shell.execute_reply.started":"2024-10-01T11:34:57.661048Z","shell.execute_reply":"2024-10-01T11:35:03.049778Z"},"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',train='train', valid='valid',item_tfms=Resize(224), batch_tfms=batch_tfms,bs=64)\nlearn = vision_learner(dls, resnet50, pretrained=False, metrics=accuracy, wd=0.01)","metadata":{"execution":{"iopub.status.busy":"2024-10-01T11:35:03.051891Z","iopub.execute_input":"2024-10-01T11:35:03.052172Z","iopub.status.idle":"2024-10-01T11:35:09.601530Z","shell.execute_reply.started":"2024-10-01T11:35:03.052141Z","shell.execute_reply":"2024-10-01T11:35:09.600502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2024-10-01T11:35:09.602912Z","iopub.execute_input":"2024-10-01T11:35:09.603275Z","iopub.status.idle":"2024-10-01T11:35:42.261257Z","shell.execute_reply.started":"2024-10-01T11:35:09.603231Z","shell.execute_reply":"2024-10-01T11:35:42.260180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(20, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2024-10-01T11:35:42.263267Z","iopub.execute_input":"2024-10-01T11:35:42.264035Z","iopub.status.idle":"2024-10-01T12:13:15.029628Z","shell.execute_reply.started":"2024-10-01T11:35:42.263983Z","shell.execute_reply":"2024-10-01T12:13:15.028416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2024-10-01T12:13:15.031707Z","iopub.execute_input":"2024-10-01T12:13:15.032614Z","iopub.status.idle":"2024-10-01T12:13:44.651960Z","shell.execute_reply.started":"2024-10-01T12:13:15.032546Z","shell.execute_reply":"2024-10-01T12:13:44.650969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(20, lr_max=slice(1e-6,3e-4))","metadata":{"execution":{"iopub.status.busy":"2024-10-01T12:13:44.653420Z","iopub.execute_input":"2024-10-01T12:13:44.653810Z","iopub.status.idle":"2024-10-01T13:28:45.847044Z","shell.execute_reply.started":"2024-10-01T12:13:44.653763Z","shell.execute_reply":"2024-10-01T13:28:45.845538Z"},"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":"2024-10-01T13:28:45.849057Z","iopub.execute_input":"2024-10-01T13:28:45.849511Z","iopub.status.idle":"2024-10-01T13:31:22.698400Z","shell.execute_reply.started":"2024-10-01T13:28:45.849461Z","shell.execute_reply":"2024-10-01T13:31:22.697525Z"},"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]\ntest_dl=learn.dls.test_dl(new)\nclass_score,y=learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2024-10-01T13:31:22.699774Z","iopub.execute_input":"2024-10-01T13:31:22.700144Z","iopub.status.idle":"2024-10-01T13:39:51.950627Z","shell.execute_reply.started":"2024-10-01T13:31:22.700097Z","shell.execute_reply":"2024-10-01T13:39:51.949534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score = np.argmax(class_score, axis=1)\nclassScore=class_score.tolist()\nlen(classScore)\nlearn.dls.vocab\nclasses={0:'airplane',1:'automobile',2:'bird',3:'cat',4:'deer',5:'dog',6:'frog',7:'horse',8:'ship',9:'truck'}\npredicted_classes=np.empty(shape=300000,dtype=np.dtype('U20'))\n\nind=0\nfor i in (classScore):\n    predicted_classes[ind]=classes[i]\n    ind=ind+1","metadata":{"execution":{"iopub.status.busy":"2024-10-01T13:39:51.952506Z","iopub.execute_input":"2024-10-01T13:39:51.952933Z","iopub.status.idle":"2024-10-01T13:39:52.172391Z","shell.execute_reply.started":"2024-10-01T13:39:51.952887Z","shell.execute_reply":"2024-10-01T13:39:52.171593Z"},"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)]\nsubmission  = pd.DataFrame({\n    \"id\": ImageId,\n    \"label\": predicted_classes\n})\n\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-01T13:39:52.177745Z","iopub.execute_input":"2024-10-01T13:39:52.178053Z","iopub.status.idle":"2024-10-01T13:39:53.030215Z","shell.execute_reply.started":"2024-10-01T13:39:52.178020Z","shell.execute_reply":"2024-10-01T13:39:53.029414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2024-10-02T10:12:00.224345Z","iopub.execute_input":"2024-10-02T10:12:00.224819Z","iopub.status.idle":"2024-10-02T10:12:00.550206Z","shell.execute_reply.started":"2024-10-02T10:12:00.224784Z","shell.execute_reply":"2024-10-02T10:12:00.549175Z"},"trusted":true},"execution_count":null,"outputs":[]}]}