{"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-03T08:29:30.228033Z","iopub.execute_input":"2023-10-03T08:29:30.228547Z","iopub.status.idle":"2023-10-03T08:29:30.522991Z","shell.execute_reply.started":"2023-10-03T08:29:30.228516Z","shell.execute_reply":"2023-10-03T08:29:30.521997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:29:30.524635Z","iopub.execute_input":"2023-10-03T08:29:30.525583Z","iopub.status.idle":"2023-10-03T08:29:36.834866Z","shell.execute_reply.started":"2023-10-03T08:29:30.525549Z","shell.execute_reply":"2023-10-03T08:29:36.833968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:29:36.836121Z","iopub.execute_input":"2023-10-03T08:29:36.837043Z","iopub.status.idle":"2023-10-03T08:29:51.843277Z","shell.execute_reply.started":"2023-10-03T08:29:36.837012Z","shell.execute_reply":"2023-10-03T08:29:51.842054Z"},"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-03T08:29:51.849826Z","iopub.execute_input":"2023-10-03T08:29:51.851924Z","iopub.status.idle":"2023-10-03T08:29:52.040142Z","shell.execute_reply.started":"2023-10-03T08:29:51.851885Z","shell.execute_reply":"2023-10-03T08:29:52.039207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('../input/cifar-10/train.7z', '/kaggle/temp/')","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:29:52.044849Z","iopub.execute_input":"2023-10-03T08:29:52.046917Z","iopub.status.idle":"2023-10-03T08:31:03.695185Z","shell.execute_reply.started":"2023-10-03T08:29:52.046885Z","shell.execute_reply":"2023-10-03T08:31:03.694028Z"},"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-03T08:31:03.696819Z","iopub.execute_input":"2023-10-03T08:31:03.697466Z","iopub.status.idle":"2023-10-03T08:31:03.803343Z","shell.execute_reply.started":"2023-10-03T08:31:03.697436Z","shell.execute_reply":"2023-10-03T08:31:03.802359Z"},"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-03T08:31:03.804556Z","iopub.execute_input":"2023-10-03T08:31:03.805090Z","iopub.status.idle":"2023-10-03T08:31:12.638120Z","shell.execute_reply.started":"2023-10-03T08:31:03.805058Z","shell.execute_reply":"2023-10-03T08:31:12.637112Z"},"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-03T08:31:12.639571Z","iopub.execute_input":"2023-10-03T08:31:12.640203Z","iopub.status.idle":"2023-10-03T08:31:23.612963Z","shell.execute_reply.started":"2023-10-03T08:31:12.640171Z","shell.execute_reply":"2023-10-03T08:31:23.611939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:31:23.617602Z","iopub.execute_input":"2023-10-03T08:31:23.619765Z","iopub.status.idle":"2023-10-03T08:31:23.632126Z","shell.execute_reply.started":"2023-10-03T08:31:23.619732Z","shell.execute_reply":"2023-10-03T08:31:23.630923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:31:23.639158Z","iopub.execute_input":"2023-10-03T08:31:23.641276Z","iopub.status.idle":"2023-10-03T08:31:25.355239Z","shell.execute_reply.started":"2023-10-03T08:31:23.641245Z","shell.execute_reply":"2023-10-03T08:31:25.354345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet50, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:31:25.356205Z","iopub.execute_input":"2023-10-03T08:31:25.356531Z","iopub.status.idle":"2023-10-03T08:31:27.637853Z","shell.execute_reply.started":"2023-10-03T08:31:25.356502Z","shell.execute_reply":"2023-10-03T08:31:27.636877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:31:27.642120Z","iopub.execute_input":"2023-10-03T08:31:27.644531Z","iopub.status.idle":"2023-10-03T08:31:58.997040Z","shell.execute_reply.started":"2023-10-03T08:31:27.644496Z","shell.execute_reply":"2023-10-03T08:31:58.995928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:31:59.001944Z","iopub.execute_input":"2023-10-03T08:31:59.004522Z","iopub.status.idle":"2023-10-03T08:41:31.581997Z","shell.execute_reply.started":"2023-10-03T08:31:59.004484Z","shell.execute_reply":"2023-10-03T08:41:31.580758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:41:31.586714Z","iopub.execute_input":"2023-10-03T08:41:31.589474Z","iopub.status.idle":"2023-10-03T08:41:31.598690Z","shell.execute_reply.started":"2023-10-03T08:41:31.589435Z","shell.execute_reply":"2023-10-03T08:41:31.597677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:41:31.603132Z","iopub.execute_input":"2023-10-03T08:41:31.605934Z","iopub.status.idle":"2023-10-03T08:41:53.339646Z","shell.execute_reply.started":"2023-10-03T08:41:31.605900Z","shell.execute_reply":"2023-10-03T08:41:53.338617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5, lr_max=slice(1e-6,3e-5))","metadata":{"execution":{"iopub.status.busy":"2023-10-03T08:41:53.343953Z","iopub.execute_input":"2023-10-03T08:41:53.346027Z","iopub.status.idle":"2023-10-03T09:00:40.330890Z","shell.execute_reply.started":"2023-10-03T08:41:53.345993Z","shell.execute_reply":"2023-10-03T09:00:40.329830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:00:40.332965Z","iopub.execute_input":"2023-10-03T09:00:40.333628Z","iopub.status.idle":"2023-10-03T09:00:40.655316Z","shell.execute_reply.started":"2023-10-03T09:00:40.333583Z","shell.execute_reply":"2023-10-03T09:00:40.654432Z"},"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-03T09:00:40.656722Z","iopub.execute_input":"2023-10-03T09:00:40.657265Z","iopub.status.idle":"2023-10-03T09:14:14.309904Z","shell.execute_reply.started":"2023-10-03T09:00:40.657231Z","shell.execute_reply":"2023-10-03T09:14:14.308874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:14:14.314476Z","iopub.execute_input":"2023-10-03T09:14:14.316614Z","iopub.status.idle":"2023-10-03T09:14:15.075635Z","shell.execute_reply.started":"2023-10-03T09:14:14.316570Z","shell.execute_reply":"2023-10-03T09:14:15.074715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:14:15.079759Z","iopub.execute_input":"2023-10-03T09:14:15.082094Z","iopub.status.idle":"2023-10-03T09:14:15.302869Z","shell.execute_reply.started":"2023-10-03T09:14:15.082061Z","shell.execute_reply":"2023-10-03T09:14:15.301986Z"},"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-03T09:14:15.306508Z","iopub.execute_input":"2023-10-03T09:14:15.308849Z","iopub.status.idle":"2023-10-03T09:14:15.629762Z","shell.execute_reply.started":"2023-10-03T09:14:15.308815Z","shell.execute_reply":"2023-10-03T09:14:15.628841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl=learn.dls.test_dl(new)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:14:15.633396Z","iopub.execute_input":"2023-10-03T09:14:15.635706Z","iopub.status.idle":"2023-10-03T09:14:15.652007Z","shell.execute_reply.started":"2023-10-03T09:14:15.635672Z","shell.execute_reply":"2023-10-03T09:14:15.651107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score,y=learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:14:15.655616Z","iopub.execute_input":"2023-10-03T09:14:15.657868Z","iopub.status.idle":"2023-10-03T09:27:13.130248Z","shell.execute_reply.started":"2023-10-03T09:14:15.657838Z","shell.execute_reply":"2023-10-03T09:27:13.129077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.135163Z","iopub.execute_input":"2023-10-03T09:27:13.137314Z","iopub.status.idle":"2023-10-03T09:27:13.161932Z","shell.execute_reply.started":"2023-10-03T09:27:13.137263Z","shell.execute_reply":"2023-10-03T09:27:13.161114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score = np.argmax(class_score, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.169856Z","iopub.execute_input":"2023-10-03T09:27:13.171859Z","iopub.status.idle":"2023-10-03T09:27:13.209246Z","shell.execute_reply.started":"2023-10-03T09:27:13.171827Z","shell.execute_reply":"2023-10-03T09:27:13.208421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.213014Z","iopub.execute_input":"2023-10-03T09:27:13.215024Z","iopub.status.idle":"2023-10-03T09:27:13.224156Z","shell.execute_reply.started":"2023-10-03T09:27:13.214993Z","shell.execute_reply":"2023-10-03T09:27:13.223265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.228276Z","iopub.execute_input":"2023-10-03T09:27:13.230346Z","iopub.status.idle":"2023-10-03T09:27:13.239571Z","shell.execute_reply.started":"2023-10-03T09:27:13.230305Z","shell.execute_reply":"2023-10-03T09:27:13.238676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.243683Z","iopub.execute_input":"2023-10-03T09:27:13.245731Z","iopub.status.idle":"2023-10-03T09:27:13.256993Z","shell.execute_reply.started":"2023-10-03T09:27:13.245702Z","shell.execute_reply":"2023-10-03T09:27:13.256164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.261031Z","iopub.execute_input":"2023-10-03T09:27:13.263323Z","iopub.status.idle":"2023-10-03T09:27:13.272265Z","shell.execute_reply.started":"2023-10-03T09:27:13.263276Z","shell.execute_reply":"2023-10-03T09:27:13.271275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.276395Z","iopub.execute_input":"2023-10-03T09:27:13.278458Z","iopub.status.idle":"2023-10-03T09:27:13.286644Z","shell.execute_reply.started":"2023-10-03T09:27:13.278427Z","shell.execute_reply":"2023-10-03T09:27:13.285822Z"},"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'}\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.288380Z","iopub.execute_input":"2023-10-03T09:27:13.290188Z","iopub.status.idle":"2023-10-03T09:27:13.297758Z","shell.execute_reply.started":"2023-10-03T09:27:13.290157Z","shell.execute_reply":"2023-10-03T09:27:13.296818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes=np.empty(shape=300000,dtype=np.dtype('U20'))\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.299129Z","iopub.execute_input":"2023-10-03T09:27:13.300036Z","iopub.status.idle":"2023-10-03T09:27:13.315431Z","shell.execute_reply.started":"2023-10-03T09:27:13.300006Z","shell.execute_reply":"2023-10-03T09:27:13.314422Z"},"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-03T09:27:13.319313Z","iopub.execute_input":"2023-10-03T09:27:13.319919Z","iopub.status.idle":"2023-10-03T09:27:13.514749Z","shell.execute_reply.started":"2023-10-03T09:27:13.319889Z","shell.execute_reply":"2023-10-03T09:27:13.513733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.519089Z","iopub.execute_input":"2023-10-03T09:27:13.521259Z","iopub.status.idle":"2023-10-03T09:27:13.530798Z","shell.execute_reply.started":"2023-10-03T09:27:13.521226Z","shell.execute_reply":"2023-10-03T09:27:13.529819Z"},"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)]\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:13.534607Z","iopub.execute_input":"2023-10-03T09:27:13.536863Z","iopub.status.idle":"2023-10-03T09:27:14.125157Z","shell.execute_reply.started":"2023-10-03T09:27:13.536833Z","shell.execute_reply":"2023-10-03T09:27:14.124162Z"},"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-03T09:27:14.129664Z","iopub.execute_input":"2023-10-03T09:27:14.131776Z","iopub.status.idle":"2023-10-03T09:27:14.830200Z","shell.execute_reply.started":"2023-10-03T09:27:14.131743Z","shell.execute_reply":"2023-10-03T09:27:14.829352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:14.834026Z","iopub.execute_input":"2023-10-03T09:27:14.836084Z","iopub.status.idle":"2023-10-03T09:27:14.902590Z","shell.execute_reply.started":"2023-10-03T09:27:14.836050Z","shell.execute_reply":"2023-10-03T09:27:14.901480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:14.907325Z","iopub.execute_input":"2023-10-03T09:27:14.909415Z","iopub.status.idle":"2023-10-03T09:27:15.528081Z","shell.execute_reply.started":"2023-10-03T09:27:14.909382Z","shell.execute_reply":"2023-10-03T09:27:15.526315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = Image.open('/kaggle/temp/test/test/134190.png')\nim.to_thumb(254,254)","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:27:15.532536Z","iopub.execute_input":"2023-10-03T09:27:15.534921Z","iopub.status.idle":"2023-10-03T09:27:15.546513Z","shell.execute_reply.started":"2023-10-03T09:27:15.534883Z","shell.execute_reply":"2023-10-03T09:27:15.545624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}