{"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-05T02:33:01.529522Z","iopub.execute_input":"2023-10-05T02:33:01.529831Z","iopub.status.idle":"2023-10-05T02:33:01.836918Z","shell.execute_reply.started":"2023-10-05T02:33:01.529805Z","shell.execute_reply":"2023-10-05T02:33:01.836045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T02:33:01.838511Z","iopub.execute_input":"2023-10-05T02:33:01.839448Z","iopub.status.idle":"2023-10-05T02:33:06.681534Z","shell.execute_reply.started":"2023-10-05T02:33:01.839414Z","shell.execute_reply":"2023-10-05T02:33:06.680432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T02:33:06.682758Z","iopub.execute_input":"2023-10-05T02:33:06.683108Z","iopub.status.idle":"2023-10-05T02:33:18.130557Z","shell.execute_reply.started":"2023-10-05T02:33:06.683077Z","shell.execute_reply":"2023-10-05T02:33:18.129442Z"},"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-05T02:33:18.133643Z","iopub.execute_input":"2023-10-05T02:33:18.134014Z","iopub.status.idle":"2023-10-05T02:33:18.256162Z","shell.execute_reply.started":"2023-10-05T02:33:18.133981Z","shell.execute_reply":"2023-10-05T02:33:18.255281Z"},"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-05T02:33:18.257575Z","iopub.execute_input":"2023-10-05T02:33:18.258087Z","iopub.status.idle":"2023-10-05T02:34:01.021583Z","shell.execute_reply.started":"2023-10-05T02:33:18.258056Z","shell.execute_reply":"2023-10-05T02:34:01.020569Z"},"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-05T02:34:01.023137Z","iopub.execute_input":"2023-10-05T02:34:01.023775Z","iopub.status.idle":"2023-10-05T02:34:01.094806Z","shell.execute_reply.started":"2023-10-05T02:34:01.023739Z","shell.execute_reply":"2023-10-05T02:34:01.093793Z"},"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-05T02:34:01.099663Z","iopub.execute_input":"2023-10-05T02:34:01.100303Z","iopub.status.idle":"2023-10-05T02:34:07.136303Z","shell.execute_reply.started":"2023-10-05T02:34:01.100270Z","shell.execute_reply":"2023-10-05T02:34:07.135388Z"},"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-05T02:34:07.137522Z","iopub.execute_input":"2023-10-05T02:34:07.137823Z","iopub.status.idle":"2023-10-05T02:34:13.201916Z","shell.execute_reply.started":"2023-10-05T02:34:07.137795Z","shell.execute_reply":"2023-10-05T02:34:13.200972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-05T02:34:13.203332Z","iopub.execute_input":"2023-10-05T02:34:13.203903Z","iopub.status.idle":"2023-10-05T02:34:13.211494Z","shell.execute_reply.started":"2023-10-05T02:34:13.203870Z","shell.execute_reply":"2023-10-05T02:34:13.210522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-05T02:34:13.215796Z","iopub.execute_input":"2023-10-05T02:34:13.216045Z","iopub.status.idle":"2023-10-05T02:34:14.350894Z","shell.execute_reply.started":"2023-10-05T02:34:13.216024Z","shell.execute_reply":"2023-10-05T02:34:14.350041Z"},"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-05T02:34:14.351850Z","iopub.execute_input":"2023-10-05T02:34:14.352154Z","iopub.status.idle":"2023-10-05T02:34:16.578002Z","shell.execute_reply.started":"2023-10-05T02:34:14.352129Z","shell.execute_reply":"2023-10-05T02:34:16.577087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T02:34:16.579271Z","iopub.execute_input":"2023-10-05T02:34:16.580084Z","iopub.status.idle":"2023-10-05T02:34:44.918234Z","shell.execute_reply.started":"2023-10-05T02:34:16.580052Z","shell.execute_reply":"2023-10-05T02:34:44.917232Z"},"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-05T02:34:44.919859Z","iopub.execute_input":"2023-10-05T02:34:44.920840Z","iopub.status.idle":"2023-10-05T02:43:38.021259Z","shell.execute_reply.started":"2023-10-05T02:34:44.920808Z","shell.execute_reply":"2023-10-05T02:43:38.019955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T02:43:38.025139Z","iopub.execute_input":"2023-10-05T02:43:38.025807Z","iopub.status.idle":"2023-10-05T02:43:38.036620Z","shell.execute_reply.started":"2023-10-05T02:43:38.025774Z","shell.execute_reply":"2023-10-05T02:43:38.035737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T02:43:38.041001Z","iopub.execute_input":"2023-10-05T02:43:38.041684Z","iopub.status.idle":"2023-10-05T02:43:58.270563Z","shell.execute_reply.started":"2023-10-05T02:43:38.041654Z","shell.execute_reply":"2023-10-05T02:43:58.269450Z"},"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-05T02:43:58.272277Z","iopub.execute_input":"2023-10-05T02:43:58.272568Z","iopub.status.idle":"2023-10-05T03:02:43.419615Z","shell.execute_reply.started":"2023-10-05T02:43:58.272547Z","shell.execute_reply":"2023-10-05T03:02:43.418584Z"},"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-05T03:02:43.421220Z","iopub.execute_input":"2023-10-05T03:02:43.422241Z","iopub.status.idle":"2023-10-05T03:02:43.426419Z","shell.execute_reply.started":"2023-10-05T03:02:43.422206Z","shell.execute_reply":"2023-10-05T03:02:43.425351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T03:02:43.427685Z","iopub.execute_input":"2023-10-05T03:02:43.428820Z","iopub.status.idle":"2023-10-05T03:02:43.754202Z","shell.execute_reply.started":"2023-10-05T03:02:43.428763Z","shell.execute_reply":"2023-10-05T03:02:43.753323Z"},"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-05T10:07:30.441934Z","iopub.execute_input":"2023-10-05T10:07:30.442232Z","iopub.status.idle":"2023-10-05T10:07:30.460600Z","shell.execute_reply.started":"2023-10-05T10:07:30.442208Z","shell.execute_reply":"2023-10-05T10:07:30.459372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-10-05T03:15:54.724728Z","iopub.execute_input":"2023-10-05T03:15:54.725051Z","iopub.status.idle":"2023-10-05T03:15:55.455564Z","shell.execute_reply.started":"2023-10-05T03:15:54.725023Z","shell.execute_reply":"2023-10-05T03:15:55.454641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir('/kaggle/temp/test/test')","metadata":{"execution":{"iopub.status.busy":"2023-10-05T03:15:55.456978Z","iopub.execute_input":"2023-10-05T03:15:55.457294Z","iopub.status.idle":"2023-10-05T03:15:55.927105Z","shell.execute_reply.started":"2023-10-05T03:15:55.457264Z","shell.execute_reply":"2023-10-05T03:15:55.925962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-05T03:15:55.928678Z","iopub.execute_input":"2023-10-05T03:15:55.929299Z","iopub.status.idle":"2023-10-05T03:15:56.115308Z","shell.execute_reply.started":"2023-10-05T03:15:55.929266Z","shell.execute_reply":"2023-10-05T03:15:56.114451Z"},"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-05T03:15:56.116731Z","iopub.execute_input":"2023-10-05T03:15:56.117039Z","iopub.status.idle":"2023-10-05T03:15:56.320917Z","shell.execute_reply.started":"2023-10-05T03:15:56.117009Z","shell.execute_reply":"2023-10-05T03:15:56.319957Z"},"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-05T03:15:56.322314Z","iopub.execute_input":"2023-10-05T03:15:56.322935Z","iopub.status.idle":"2023-10-05T03:15:56.333684Z","shell.execute_reply.started":"2023-10-05T03:15:56.322903Z","shell.execute_reply":"2023-10-05T03:15:56.332674Z"},"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-05T03:15:56.335149Z","iopub.execute_input":"2023-10-05T03:15:56.335482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score = np.argmax(class_score, axis=1)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes=np.empty(shape=300000,dtype=np.dtype('U20'))","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}