{"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-04T09:17:10.338083Z","iopub.execute_input":"2023-10-04T09:17:10.338425Z","iopub.status.idle":"2023-10-04T09:17:10.631910Z","shell.execute_reply.started":"2023-10-04T09:17:10.338396Z","shell.execute_reply":"2023-10-04T09:17:10.631027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:17:30.735903Z","iopub.execute_input":"2023-10-04T09:17:30.736482Z","iopub.status.idle":"2023-10-04T09:17:30.741333Z","shell.execute_reply.started":"2023-10-04T09:17:30.736454Z","shell.execute_reply":"2023-10-04T09:17:30.740104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:17:33.504325Z","iopub.execute_input":"2023-10-04T09:17:33.504839Z","iopub.status.idle":"2023-10-04T09:17:52.060257Z","shell.execute_reply.started":"2023-10-04T09:17:33.504797Z","shell.execute_reply":"2023-10-04T09:17:52.059061Z"},"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-04T09:17:56.503789Z","iopub.execute_input":"2023-10-04T09:17:56.504209Z","iopub.status.idle":"2023-10-04T09:17:56.850592Z","shell.execute_reply.started":"2023-10-04T09:17:56.504175Z","shell.execute_reply":"2023-10-04T09:17:56.849614Z"},"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-04T09:18:01.189773Z","iopub.execute_input":"2023-10-04T09:18:01.190182Z","iopub.status.idle":"2023-10-04T09:19:11.927500Z","shell.execute_reply.started":"2023-10-04T09:18:01.190149Z","shell.execute_reply":"2023-10-04T09:19:11.926538Z"},"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-04T09:19:19.942862Z","iopub.execute_input":"2023-10-04T09:19:19.943267Z","iopub.status.idle":"2023-10-04T09:19:19.999790Z","shell.execute_reply.started":"2023-10-04T09:19:19.943234Z","shell.execute_reply":"2023-10-04T09:19:19.998931Z"},"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-04T09:19:23.851804Z","iopub.execute_input":"2023-10-04T09:19:23.852289Z","iopub.status.idle":"2023-10-04T09:19:32.760635Z","shell.execute_reply.started":"2023-10-04T09:19:23.852246Z","shell.execute_reply":"2023-10-04T09:19:32.759742Z"},"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-04T09:19:51.934170Z","iopub.execute_input":"2023-10-04T09:19:51.934586Z","iopub.status.idle":"2023-10-04T09:20:01.427578Z","shell.execute_reply.started":"2023-10-04T09:19:51.934555Z","shell.execute_reply":"2023-10-04T09:20:01.426533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:20:07.954994Z","iopub.execute_input":"2023-10-04T09:20:07.955393Z","iopub.status.idle":"2023-10-04T09:20:07.969091Z","shell.execute_reply.started":"2023-10-04T09:20:07.955362Z","shell.execute_reply":"2023-10-04T09:20:07.968257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:20:11.907005Z","iopub.execute_input":"2023-10-04T09:20:11.907464Z","iopub.status.idle":"2023-10-04T09:20:13.694374Z","shell.execute_reply.started":"2023-10-04T09:20:11.907431Z","shell.execute_reply":"2023-10-04T09:20:13.693526Z"},"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-04T09:20:31.636637Z","iopub.execute_input":"2023-10-04T09:20:31.637179Z","iopub.status.idle":"2023-10-04T09:20:32.542584Z","shell.execute_reply.started":"2023-10-04T09:20:31.637143Z","shell.execute_reply":"2023-10-04T09:20:32.541581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:20:35.097496Z","iopub.execute_input":"2023-10-04T09:20:35.098251Z","iopub.status.idle":"2023-10-04T09:21:06.636272Z","shell.execute_reply.started":"2023-10-04T09:20:35.098210Z","shell.execute_reply":"2023-10-04T09:21:06.635281Z"},"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-04T09:21:14.171008Z","iopub.execute_input":"2023-10-04T09:21:14.171565Z","iopub.status.idle":"2023-10-04T09:30:47.817975Z","shell.execute_reply.started":"2023-10-04T09:21:14.171525Z","shell.execute_reply":"2023-10-04T09:30:47.816855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:32:44.225298Z","iopub.execute_input":"2023-10-04T09:32:44.225761Z","iopub.status.idle":"2023-10-04T09:32:44.231579Z","shell.execute_reply.started":"2023-10-04T09:32:44.225692Z","shell.execute_reply":"2023-10-04T09:32:44.230741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:32:47.421232Z","iopub.execute_input":"2023-10-04T09:32:47.421634Z","iopub.status.idle":"2023-10-04T09:33:09.436927Z","shell.execute_reply.started":"2023-10-04T09:32:47.421599Z","shell.execute_reply":"2023-10-04T09:33:09.435860Z"},"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-04T09:33:17.137795Z","iopub.execute_input":"2023-10-04T09:33:17.138326Z","iopub.status.idle":"2023-10-04T09:52:50.355521Z","shell.execute_reply.started":"2023-10-04T09:33:17.138290Z","shell.execute_reply":"2023-10-04T09:52:50.354453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from fastai.callback.fp16 import *\n# learn = vision_learner(dls, resnet18, metrics=accuracy)\n# learn.fine_tune(10, freeze_epochs=3)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:10:09.712381Z","iopub.execute_input":"2023-10-04T10:10:09.712774Z","iopub.status.idle":"2023-10-04T10:10:09.717618Z","shell.execute_reply.started":"2023-10-04T10:10:09.712744Z","shell.execute_reply":"2023-10-04T10:10:09.716596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:10:15.889955Z","iopub.execute_input":"2023-10-04T10:10:15.890368Z","iopub.status.idle":"2023-10-04T10:10:16.375831Z","shell.execute_reply.started":"2023-10-04T10:10:15.890334Z","shell.execute_reply":"2023-10-04T10:10:16.374849Z"},"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-04T10:10:21.275584Z","iopub.execute_input":"2023-10-04T10:10:21.276053Z","iopub.status.idle":"2023-10-04T10:27:35.459943Z","shell.execute_reply.started":"2023-10-04T10:10:21.276015Z","shell.execute_reply":"2023-10-04T10:27:35.459004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:30:18.702506Z","iopub.execute_input":"2023-10-04T10:30:18.702899Z","iopub.status.idle":"2023-10-04T10:30:19.235576Z","shell.execute_reply.started":"2023-10-04T10:30:18.702869Z","shell.execute_reply":"2023-10-04T10:30:19.234578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir('/kaggle/temp/test/test')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:30:24.633357Z","iopub.execute_input":"2023-10-04T10:30:24.634017Z","iopub.status.idle":"2023-10-04T10:30:24.638106Z","shell.execute_reply.started":"2023-10-04T10:30:24.633986Z","shell.execute_reply":"2023-10-04T10:30:24.636941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:30:27.982911Z","iopub.execute_input":"2023-10-04T10:30:27.983667Z","iopub.status.idle":"2023-10-04T10:30:28.146366Z","shell.execute_reply.started":"2023-10-04T10:30:27.983633Z","shell.execute_reply":"2023-10-04T10:30:28.144884Z"},"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-04T10:30:31.004950Z","iopub.execute_input":"2023-10-04T10:30:31.005267Z","iopub.status.idle":"2023-10-04T10:30:31.202697Z","shell.execute_reply.started":"2023-10-04T10:30:31.005242Z","shell.execute_reply":"2023-10-04T10:30:31.201736Z"},"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-04T10:30:34.326232Z","iopub.execute_input":"2023-10-04T10:30:34.326632Z","iopub.status.idle":"2023-10-04T10:30:34.344686Z","shell.execute_reply.started":"2023-10-04T10:30:34.326601Z","shell.execute_reply":"2023-10-04T10:30:34.343752Z"},"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-04T10:46:28.096191Z","iopub.execute_input":"2023-10-04T10:46:28.096758Z","iopub.status.idle":"2023-10-04T10:58:28.267431Z","shell.execute_reply.started":"2023-10-04T10:46:28.096715Z","shell.execute_reply":"2023-10-04T10:58:28.266349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:59:22.672023Z","iopub.execute_input":"2023-10-04T10:59:22.672444Z","iopub.status.idle":"2023-10-04T10:59:22.681078Z","shell.execute_reply.started":"2023-10-04T10:59:22.672410Z","shell.execute_reply":"2023-10-04T10:59:22.679972Z"},"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-04T10:59:33.672931Z","iopub.execute_input":"2023-10-04T10:59:33.673351Z","iopub.status.idle":"2023-10-04T10:59:33.769828Z","shell.execute_reply.started":"2023-10-04T10:59:33.673307Z","shell.execute_reply":"2023-10-04T10:59:33.767320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:59:37.537108Z","iopub.execute_input":"2023-10-04T10:59:37.537541Z","iopub.status.idle":"2023-10-04T10:59:37.544717Z","shell.execute_reply.started":"2023-10-04T10:59:37.537505Z","shell.execute_reply":"2023-10-04T10:59:37.543782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:59:41.100064Z","iopub.execute_input":"2023-10-04T10:59:41.100490Z","iopub.status.idle":"2023-10-04T10:59:41.111644Z","shell.execute_reply.started":"2023-10-04T10:59:41.100459Z","shell.execute_reply":"2023-10-04T10:59:41.110734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:59:46.898253Z","iopub.execute_input":"2023-10-04T10:59:46.898754Z","iopub.status.idle":"2023-10-04T10:59:46.914029Z","shell.execute_reply.started":"2023-10-04T10:59:46.898690Z","shell.execute_reply":"2023-10-04T10:59:46.913074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:59:50.218603Z","iopub.execute_input":"2023-10-04T10:59:50.219134Z","iopub.status.idle":"2023-10-04T10:59:50.227501Z","shell.execute_reply.started":"2023-10-04T10:59:50.219094Z","shell.execute_reply":"2023-10-04T10:59:50.226342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:59:53.050124Z","iopub.execute_input":"2023-10-04T10:59:53.050544Z","iopub.status.idle":"2023-10-04T10:59:53.058876Z","shell.execute_reply.started":"2023-10-04T10:59:53.050510Z","shell.execute_reply":"2023-10-04T10:59:53.057771Z"},"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-04T10:59:56.765388Z","iopub.execute_input":"2023-10-04T10:59:56.765873Z","iopub.status.idle":"2023-10-04T10:59:56.771909Z","shell.execute_reply.started":"2023-10-04T10:59:56.765836Z","shell.execute_reply":"2023-10-04T10:59:56.770902Z"},"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-04T11:00:00.123080Z","iopub.execute_input":"2023-10-04T11:00:00.123500Z","iopub.status.idle":"2023-10-04T11:00:00.129096Z","shell.execute_reply.started":"2023-10-04T11:00:00.123466Z","shell.execute_reply":"2023-10-04T11:00:00.128154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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-04T11:00:03.818305Z","iopub.execute_input":"2023-10-04T11:00:03.818795Z","iopub.status.idle":"2023-10-04T11:00:04.033040Z","shell.execute_reply.started":"2023-10-04T11:00:03.818757Z","shell.execute_reply":"2023-10-04T11:00:04.032003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"execution":{"iopub.status.busy":"2023-10-04T11:00:07.770732Z","iopub.execute_input":"2023-10-04T11:00:07.771156Z","iopub.status.idle":"2023-10-04T11:00:07.779130Z","shell.execute_reply.started":"2023-10-04T11:00:07.771122Z","shell.execute_reply":"2023-10-04T11:00:07.778132Z"},"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-04T11:00:27.243497Z","iopub.execute_input":"2023-10-04T11:00:27.244210Z","iopub.status.idle":"2023-10-04T11:00:27.863280Z","shell.execute_reply.started":"2023-10-04T11:00:27.244173Z","shell.execute_reply":"2023-10-04T11:00:27.862282Z"},"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-04T11:00:30.139888Z","iopub.execute_input":"2023-10-04T11:00:30.140354Z","iopub.status.idle":"2023-10-04T11:00:30.875153Z","shell.execute_reply.started":"2023-10-04T11:00:30.140309Z","shell.execute_reply":"2023-10-04T11:00:30.874203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T11:00:44.034580Z","iopub.execute_input":"2023-10-04T11:00:44.035098Z","iopub.status.idle":"2023-10-04T11:00:44.110544Z","shell.execute_reply.started":"2023-10-04T11:00:44.035058Z","shell.execute_reply":"2023-10-04T11:00:44.108861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T11:00:49.291637Z","iopub.execute_input":"2023-10-04T11:00:49.292135Z","iopub.status.idle":"2023-10-04T11:00:50.018001Z","shell.execute_reply.started":"2023-10-04T11:00:49.292096Z","shell.execute_reply":"2023-10-04T11:00:50.016778Z"},"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-04T11:00:56.564196Z","iopub.execute_input":"2023-10-04T11:00:56.564605Z","iopub.status.idle":"2023-10-04T11:00:56.580050Z","shell.execute_reply.started":"2023-10-04T11:00:56.564571Z","shell.execute_reply":"2023-10-04T11:00:56.578665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}