{"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":"# Name: Mohammed Bilal Shakeel\n# ID: 210051726\n\n!pip install py7zr\nimport numpy as np\nimport pandas as pd\nfrom py7zr import unpack_7zarchive\nimport shutil\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nfrom fastai.vision.all import *\nshutil.register_unpack_format('7zip',['.7z'],unpack_7zarchive)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T10:44:39.083648Z","iopub.execute_input":"2023-10-06T10:44:39.084097Z","iopub.status.idle":"2023-10-06T10:44:59.965625Z","shell.execute_reply.started":"2023-10-06T10:44:39.084062Z","shell.execute_reply":"2023-10-06T10:44:59.964519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('../input/cifar-10/train.7z', '/kaggle/temp/')\ntrain_labels = pd.read_csv(\"../input/cifar-10/trainLabels.csv\", header=\"infer\")\n\nclasses = train_labels['label'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T10:44:59.970915Z","iopub.execute_input":"2023-10-06T10:44:59.973132Z","iopub.status.idle":"2023-10-06T10:46:12.230956Z","shell.execute_reply.started":"2023-10-06T10:44:59.973095Z","shell.execute_reply":"2023-10-06T10:46:12.229918Z"},"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-06T10:46:12.232604Z","iopub.execute_input":"2023-10-06T10:46:12.233231Z","iopub.status.idle":"2023-10-06T10:46:22.923057Z","shell.execute_reply.started":"2023-10-06T10:46:12.233199Z","shell.execute_reply":"2023-10-06T10:46:22.921393Z"},"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)\nos.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-06T10:46:22.927941Z","iopub.execute_input":"2023-10-06T10:46:22.928651Z","iopub.status.idle":"2023-10-06T10:46:33.841117Z","shell.execute_reply.started":"2023-10-06T10:46:22.928617Z","shell.execute_reply":"2023-10-06T10:46:33.840171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet34, metrics=accuracy, wd=0.01)\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T10:46:33.845183Z","iopub.execute_input":"2023-10-06T10:46:33.847300Z","iopub.status.idle":"2023-10-06T10:46:57.801116Z","shell.execute_reply.started":"2023-10-06T10:46:33.847265Z","shell.execute_reply":"2023-10-06T10:46:57.795534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T10:46:57.806624Z","iopub.execute_input":"2023-10-06T10:46:57.808980Z","iopub.status.idle":"2023-10-06T10:57:04.868060Z","shell.execute_reply.started":"2023-10-06T10:46:57.808940Z","shell.execute_reply":"2023-10-06T10:57:04.866860Z"},"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')\n\npath='/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":"2023-10-06T10:57:04.872692Z","iopub.execute_input":"2023-10-06T10:57:04.874830Z","iopub.status.idle":"2023-10-06T11:24:59.101356Z","shell.execute_reply.started":"2023-10-06T10:57:04.874794Z","shell.execute_reply":"2023-10-06T11:24:59.100125Z"},"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'))","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:24:59.105936Z","iopub.execute_input":"2023-10-06T11:24:59.108483Z","iopub.status.idle":"2023-10-06T11:24:59.147245Z","shell.execute_reply.started":"2023-10-06T11:24:59.108447Z","shell.execute_reply":"2023-10-06T11:24:59.146256Z"},"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\n\ndirectory = '/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":"2023-10-06T11:26:34.504390Z","iopub.execute_input":"2023-10-06T11:26:34.504897Z","iopub.status.idle":"2023-10-06T11:26:35.966939Z","shell.execute_reply.started":"2023-10-06T11:26:34.504860Z","shell.execute_reply":"2023-10-06T11:26:35.965892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}