{"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":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\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-16T12:28:19.637522Z","iopub.execute_input":"2023-10-16T12:28:19.637839Z","iopub.status.idle":"2023-10-16T12:28:19.955014Z","shell.execute_reply.started":"2023-10-16T12:28:19.637812Z","shell.execute_reply":"2023-10-16T12:28:19.954136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:28:19.956675Z","iopub.execute_input":"2023-10-16T12:28:19.957570Z","iopub.status.idle":"2023-10-16T12:28:25.745918Z","shell.execute_reply.started":"2023-10-16T12:28:19.957537Z","shell.execute_reply":"2023-10-16T12:28:25.744989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:28:25.747143Z","iopub.execute_input":"2023-10-16T12:28:25.747474Z","iopub.status.idle":"2023-10-16T12:28:37.344117Z","shell.execute_reply.started":"2023-10-16T12:28:25.747445Z","shell.execute_reply":"2023-10-16T12:28:37.343002Z"},"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-16T12:28:37.347102Z","iopub.execute_input":"2023-10-16T12:28:37.348237Z","iopub.status.idle":"2023-10-16T12:28:37.468062Z","shell.execute_reply.started":"2023-10-16T12:28:37.348203Z","shell.execute_reply":"2023-10-16T12:28:37.467095Z"},"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-16T12:28:37.469239Z","iopub.execute_input":"2023-10-16T12:28:37.469772Z","iopub.status.idle":"2023-10-16T12:29:21.956486Z","shell.execute_reply.started":"2023-10-16T12:28:37.469741Z","shell.execute_reply":"2023-10-16T12:29:21.955526Z"},"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-16T12:29:21.957863Z","iopub.execute_input":"2023-10-16T12:29:21.958212Z","iopub.status.idle":"2023-10-16T12:29:22.048466Z","shell.execute_reply.started":"2023-10-16T12:29:21.958182Z","shell.execute_reply":"2023-10-16T12:29:22.047462Z"},"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-16T12:29:22.050177Z","iopub.execute_input":"2023-10-16T12:29:22.050556Z","iopub.status.idle":"2023-10-16T12:29:27.524387Z","shell.execute_reply.started":"2023-10-16T12:29:22.050525Z","shell.execute_reply":"2023-10-16T12:29:27.523306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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=64)","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:29:27.525912Z","iopub.execute_input":"2023-10-16T12:29:27.526626Z","iopub.status.idle":"2023-10-16T12:29:33.615043Z","shell.execute_reply.started":"2023-10-16T12:29:27.526594Z","shell.execute_reply":"2023-10-16T12:29:33.614073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:29:33.616313Z","iopub.execute_input":"2023-10-16T12:29:33.616639Z","iopub.status.idle":"2023-10-16T12:29:33.624085Z","shell.execute_reply.started":"2023-10-16T12:29:33.616608Z","shell.execute_reply":"2023-10-16T12:29:33.623075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:29:33.628476Z","iopub.execute_input":"2023-10-16T12:29:33.628754Z","iopub.status.idle":"2023-10-16T12:29:34.805853Z","shell.execute_reply.started":"2023-10-16T12:29:33.628734Z","shell.execute_reply":"2023-10-16T12:29:34.805010Z"},"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-16T12:29:34.806795Z","iopub.execute_input":"2023-10-16T12:29:34.807124Z","iopub.status.idle":"2023-10-16T12:29:35.946925Z","shell.execute_reply.started":"2023-10-16T12:29:34.807096Z","shell.execute_reply":"2023-10-16T12:29:35.945993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:29:35.948228Z","iopub.execute_input":"2023-10-16T12:29:35.949138Z","iopub.status.idle":"2023-10-16T12:30:04.884475Z","shell.execute_reply.started":"2023-10-16T12:29:35.949106Z","shell.execute_reply":"2023-10-16T12:30:04.883514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3, 3e-3)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:30:04.885961Z","iopub.execute_input":"2023-10-16T12:30:04.886980Z","iopub.status.idle":"2023-10-16T12:38:58.382627Z","shell.execute_reply.started":"2023-10-16T12:30:04.886885Z","shell.execute_reply":"2023-10-16T12:38:58.381538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:38:58.384286Z","iopub.execute_input":"2023-10-16T12:38:58.385267Z","iopub.status.idle":"2023-10-16T12:38:58.391762Z","shell.execute_reply.started":"2023-10-16T12:38:58.385233Z","shell.execute_reply":"2023-10-16T12:38:58.390799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:38:58.393209Z","iopub.execute_input":"2023-10-16T12:38:58.393821Z","iopub.status.idle":"2023-10-16T12:39:16.824075Z","shell.execute_reply.started":"2023-10-16T12:38:58.393791Z","shell.execute_reply":"2023-10-16T12:39:16.823113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5, lr_max=slice(1e-6,3e-5))\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:39:16.825651Z","iopub.execute_input":"2023-10-16T12:39:16.826655Z","iopub.status.idle":"2023-10-16T12:57:58.056566Z","shell.execute_reply.started":"2023-10-16T12:39:16.826624Z","shell.execute_reply":"2023-10-16T12:57:58.055231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T12:57:58.059308Z","iopub.execute_input":"2023-10-16T12:57:58.059676Z","iopub.status.idle":"2023-10-16T12:57:58.405399Z","shell.execute_reply.started":"2023-10-16T12:57:58.059640Z","shell.execute_reply":"2023-10-16T12:57:58.404552Z"},"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-16T12:57:58.406876Z","iopub.execute_input":"2023-10-16T12:57:58.407474Z","iopub.status.idle":"2023-10-16T13:11:53.033886Z","shell.execute_reply.started":"2023-10-16T12:57:58.407441Z","shell.execute_reply":"2023-10-16T13:11:53.032985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:11:53.035154Z","iopub.execute_input":"2023-10-16T13:11:53.035466Z","iopub.status.idle":"2023-10-16T13:11:53.950437Z","shell.execute_reply.started":"2023-10-16T13:11:53.035437Z","shell.execute_reply":"2023-10-16T13:11:53.947816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir('/kaggle/temp/test/test')","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:11:53.970683Z","iopub.execute_input":"2023-10-16T13:11:53.971454Z","iopub.status.idle":"2023-10-16T13:11:54.001600Z","shell.execute_reply.started":"2023-10-16T13:11:53.971406Z","shell.execute_reply":"2023-10-16T13:11:54.000454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:11:54.011394Z","iopub.execute_input":"2023-10-16T13:11:54.019215Z","iopub.status.idle":"2023-10-16T13:11:54.607626Z","shell.execute_reply.started":"2023-10-16T13:11:54.019180Z","shell.execute_reply":"2023-10-16T13:11:54.606788Z"},"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-16T13:11:54.611532Z","iopub.execute_input":"2023-10-16T13:11:54.613568Z","iopub.status.idle":"2023-10-16T13:11:54.979704Z","shell.execute_reply.started":"2023-10-16T13:11:54.613536Z","shell.execute_reply":"2023-10-16T13:11:54.978781Z"},"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-16T13:11:54.984285Z","iopub.execute_input":"2023-10-16T13:11:54.986357Z","iopub.status.idle":"2023-10-16T13:11:55.004284Z","shell.execute_reply.started":"2023-10-16T13:11:54.986324Z","shell.execute_reply":"2023-10-16T13:11:55.003146Z"},"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-16T13:11:55.008064Z","iopub.execute_input":"2023-10-16T13:11:55.010082Z","iopub.status.idle":"2023-10-16T13:22:15.037681Z","shell.execute_reply.started":"2023-10-16T13:11:55.010051Z","shell.execute_reply":"2023-10-16T13:22:15.036603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:22:15.039139Z","iopub.execute_input":"2023-10-16T13:22:15.039470Z","iopub.status.idle":"2023-10-16T13:22:15.064074Z","shell.execute_reply.started":"2023-10-16T13:22:15.039436Z","shell.execute_reply":"2023-10-16T13:22:15.063189Z"},"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-16T13:22:15.065308Z","iopub.execute_input":"2023-10-16T13:22:15.065885Z","iopub.status.idle":"2023-10-16T13:22:15.113709Z","shell.execute_reply.started":"2023-10-16T13:22:15.065847Z","shell.execute_reply":"2023-10-16T13:22:15.112864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:22:15.114880Z","iopub.execute_input":"2023-10-16T13:22:15.115219Z","iopub.status.idle":"2023-10-16T13:22:15.122301Z","shell.execute_reply.started":"2023-10-16T13:22:15.115188Z","shell.execute_reply":"2023-10-16T13:22:15.121334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:22:15.128759Z","iopub.execute_input":"2023-10-16T13:22:15.129380Z","iopub.status.idle":"2023-10-16T13:22:15.135957Z","shell.execute_reply.started":"2023-10-16T13:22:15.129349Z","shell.execute_reply":"2023-10-16T13:22:15.134833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:22:15.137571Z","iopub.execute_input":"2023-10-16T13:22:15.138229Z","iopub.status.idle":"2023-10-16T13:22:15.147305Z","shell.execute_reply.started":"2023-10-16T13:22:15.138194Z","shell.execute_reply":"2023-10-16T13:22:15.146421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:22:15.148541Z","iopub.execute_input":"2023-10-16T13:22:15.149065Z","iopub.status.idle":"2023-10-16T13:22:15.159480Z","shell.execute_reply.started":"2023-10-16T13:22:15.149037Z","shell.execute_reply":"2023-10-16T13:22:15.158445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:22:15.160886Z","iopub.execute_input":"2023-10-16T13:22:15.161611Z","iopub.status.idle":"2023-10-16T13:22:15.170262Z","shell.execute_reply.started":"2023-10-16T13:22:15.161580Z","shell.execute_reply":"2023-10-16T13:22:15.169329Z"},"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-16T13:22:15.171648Z","iopub.execute_input":"2023-10-16T13:22:15.172213Z","iopub.status.idle":"2023-10-16T13:22:15.179086Z","shell.execute_reply.started":"2023-10-16T13:22:15.172181Z","shell.execute_reply":"2023-10-16T13:22:15.178329Z"},"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-16T13:22:15.180255Z","iopub.execute_input":"2023-10-16T13:22:15.180748Z","iopub.status.idle":"2023-10-16T13:22:15.193695Z","shell.execute_reply.started":"2023-10-16T13:22:15.180716Z","shell.execute_reply":"2023-10-16T13:22:15.192794Z"},"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-16T13:22:15.194886Z","iopub.execute_input":"2023-10-16T13:22:15.195734Z","iopub.status.idle":"2023-10-16T13:22:15.288156Z","shell.execute_reply.started":"2023-10-16T13:22:15.195705Z","shell.execute_reply":"2023-10-16T13:22:15.287291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:22:15.289421Z","iopub.execute_input":"2023-10-16T13:22:15.289959Z","iopub.status.idle":"2023-10-16T13:22:15.296152Z","shell.execute_reply.started":"2023-10-16T13:22:15.289905Z","shell.execute_reply":"2023-10-16T13:22:15.295148Z"},"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-16T13:22:15.297508Z","iopub.execute_input":"2023-10-16T13:22:15.298122Z","iopub.status.idle":"2023-10-16T13:22:15.634282Z","shell.execute_reply.started":"2023-10-16T13:22:15.298093Z","shell.execute_reply":"2023-10-16T13:22:15.633355Z"},"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-16T13:22:15.635703Z","iopub.execute_input":"2023-10-16T13:22:15.636052Z","iopub.status.idle":"2023-10-16T13:22:16.032565Z","shell.execute_reply.started":"2023-10-16T13:22:15.636022Z","shell.execute_reply":"2023-10-16T13:22:16.031497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:22:16.033879Z","iopub.execute_input":"2023-10-16T13:22:16.034716Z","iopub.status.idle":"2023-10-16T13:22:16.069548Z","shell.execute_reply.started":"2023-10-16T13:22:16.034686Z","shell.execute_reply":"2023-10-16T13:22:16.068610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:22:16.070989Z","iopub.execute_input":"2023-10-16T13:22:16.071530Z","iopub.status.idle":"2023-10-16T13:22:16.399899Z","shell.execute_reply.started":"2023-10-16T13:22:16.071500Z","shell.execute_reply":"2023-10-16T13:22:16.398731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = Image.open('/kaggle/temp/test/test/134190.png')\nim.to_thumb(254,254)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-16T13:22:16.402532Z","iopub.execute_input":"2023-10-16T13:22:16.402862Z","iopub.status.idle":"2023-10-16T13:22:16.411029Z","shell.execute_reply.started":"2023-10-16T13:22:16.402828Z","shell.execute_reply":"2023-10-16T13:22:16.409976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}