{"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-04T10:37:07.257867Z","iopub.execute_input":"2023-10-04T10:37:07.258683Z","iopub.status.idle":"2023-10-04T10:37:07.559600Z","shell.execute_reply.started":"2023-10-04T10:37:07.258649Z","shell.execute_reply":"2023-10-04T10:37:07.558657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:37:21.951189Z","iopub.execute_input":"2023-10-04T10:37:21.951618Z","iopub.status.idle":"2023-10-04T10:37:27.350096Z","shell.execute_reply.started":"2023-10-04T10:37:21.951591Z","shell.execute_reply":"2023-10-04T10:37:27.349082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:38:01.972656Z","iopub.execute_input":"2023-10-04T10:38:01.973198Z","iopub.status.idle":"2023-10-04T10:38:22.343450Z","shell.execute_reply.started":"2023-10-04T10:38:01.973144Z","shell.execute_reply":"2023-10-04T10:38:22.342182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\n\narchive_path = '../input/cifar-10/train.7z'\nextract_folder = '/kaggle/temp/'\n\nwith py7zr.SevenZipFile(archive_path, mode='r') as archive:\n    archive.extractall(path=extract_folder)\n# Loading your labels CSV\ntrain_labels = pd.read_csv(\"../input/cifar-10/trainLabels.csv\", header=\"infer\")","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:38:56.283085Z","iopub.execute_input":"2023-10-04T10:38:56.283728Z","iopub.status.idle":"2023-10-04T10:40:08.394414Z","shell.execute_reply.started":"2023-10-04T10:38:56.283669Z","shell.execute_reply":"2023-10-04T10:40:08.393439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# List files in '../input/cifar-10/'\nprint(os.listdir('../input/cifar-10/'))\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:40:50.800865Z","iopub.execute_input":"2023-10-04T10:40:50.801189Z","iopub.status.idle":"2023-10-04T10:40:50.808624Z","shell.execute_reply.started":"2023-10-04T10:40:50.801165Z","shell.execute_reply":"2023-10-04T10:40:50.807586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# Parent paths for training and validation sets\nparent_path_train = \"/kaggle/temp/train\"\nparent_path_valid = \"/kaggle/temp/valid\"\n\n# Assuming you have a list of classes like this\nclasses = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\n\n# Create subdirectories for each class under train and valid directories\nfor class1 in classes:\n    # For the training directory\n    path_train = os.path.join(parent_path_train, class1)\n    os.makedirs(path_train, exist_ok=True)  # This will create directories if they don't exist\n    \n    # For the validation directory\n    path_valid = os.path.join(parent_path_valid, class1)\n    os.makedirs(path_valid, exist_ok=True)  # This will create directories if they don't exist\n\n# Moving images to their respective directories based on labels\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    if not os.path.exists(source_path):\n        print(f\"Missing source file: {source_path}\")\n        continue\n    \n    p = np.random.random()\n    if p <= 0.8:\n        target_path = os.path.join(parent_path_train, row[\"label\"], id)\n    else:\n        target_path = os.path.join(parent_path_valid, row[\"label\"], id)\n    \n    os.replace(source_path, target_path)\n\nprint(\"Processing completed!\")\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:40:59.938589Z","iopub.execute_input":"2023-10-04T10:40:59.939075Z","iopub.status.idle":"2023-10-04T10:41:05.895611Z","shell.execute_reply.started":"2023-10-04T10:40:59.939035Z","shell.execute_reply":"2023-10-04T10:41:05.894670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = train_labels['label'].unique()\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:41:35.984067Z","iopub.execute_input":"2023-10-04T10:41:35.984561Z","iopub.status.idle":"2023-10-04T10:41:36.009800Z","shell.execute_reply.started":"2023-10-04T10:41:35.984477Z","shell.execute_reply":"2023-10-04T10:41:36.008992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parent_path_train = \"/kaggle/temp/train\"\nparent_path_valid = \"/kaggle/temp/valid\"\n# parent_path_test = \"/kaggle/temp/test\"","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:41:42.688184Z","iopub.execute_input":"2023-10-04T10:41:42.688574Z","iopub.status.idle":"2023-10-04T10:41:42.692794Z","shell.execute_reply.started":"2023-10-04T10:41:42.688539Z","shell.execute_reply":"2023-10-04T10:41:42.691914Z"},"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-04T10:41:49.411962Z","iopub.execute_input":"2023-10-04T10:41:49.412368Z","iopub.status.idle":"2023-10-04T10:41:58.565580Z","shell.execute_reply.started":"2023-10-04T10:41:49.412337Z","shell.execute_reply":"2023-10-04T10:41:58.564582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/temp')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:43:04.085458Z","iopub.execute_input":"2023-10-04T10:43:04.085901Z","iopub.status.idle":"2023-10-04T10:43:04.098617Z","shell.execute_reply.started":"2023-10-04T10:43:04.085868Z","shell.execute_reply":"2023-10-04T10:43:04.097541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:43:12.397429Z","iopub.execute_input":"2023-10-04T10:43:12.397989Z","iopub.status.idle":"2023-10-04T10:43:14.362469Z","shell.execute_reply.started":"2023-10-04T10:43:12.397951Z","shell.execute_reply":"2023-10-04T10:43:14.361576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet50, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:43:27.416666Z","iopub.execute_input":"2023-10-04T10:43:27.417081Z","iopub.status.idle":"2023-10-04T10:43:29.129003Z","shell.execute_reply.started":"2023-10-04T10:43:27.417047Z","shell.execute_reply":"2023-10-04T10:43:29.127985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:43:33.859925Z","iopub.execute_input":"2023-10-04T10:43:33.860335Z","iopub.status.idle":"2023-10-04T10:44:04.923214Z","shell.execute_reply.started":"2023-10-04T10:43:33.860303Z","shell.execute_reply":"2023-10-04T10:44:04.922118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:44:33.099666Z","iopub.execute_input":"2023-10-04T10:44:33.100215Z","iopub.status.idle":"2023-10-04T10:54:17.931569Z","shell.execute_reply.started":"2023-10-04T10:44:33.100174Z","shell.execute_reply":"2023-10-04T10:54:17.930323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T11:02:47.216532Z","iopub.execute_input":"2023-10-04T11:02:47.216943Z","iopub.status.idle":"2023-10-04T11:02:47.225108Z","shell.execute_reply.started":"2023-10-04T11:02:47.216911Z","shell.execute_reply":"2023-10-04T11:02:47.223745Z"},"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-04T11:02:52.661022Z","iopub.execute_input":"2023-10-04T11:02:52.661594Z","iopub.status.idle":"2023-10-04T11:23:25.526924Z","shell.execute_reply.started":"2023-10-04T11:02:52.661554Z","shell.execute_reply":"2023-10-04T11:23:25.525855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T11:36:30.872450Z","iopub.execute_input":"2023-10-04T11:36:30.872879Z","iopub.status.idle":"2023-10-04T11:36:31.205682Z","shell.execute_reply.started":"2023-10-04T11:36:30.872846Z","shell.execute_reply":"2023-10-04T11:36:31.204679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\n\narchive_path = '/kaggle/input/cifar-10/test.7z'\nextract_folder = '/kaggle/temp/test/'\n\nwith py7zr.SevenZipFile(archive_path, mode='r') as archive:\n    archive.extractall(path=extract_folder)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T11:36:37.039294Z","iopub.execute_input":"2023-10-04T11:36:37.039654Z","iopub.status.idle":"2023-10-04T11:49:47.805121Z","shell.execute_reply.started":"2023-10-04T11:36:37.039624Z","shell.execute_reply":"2023-10-04T11:49:47.803255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/kaggle/working/vision')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T11:52:58.343717Z","iopub.execute_input":"2023-10-04T11:52:58.344096Z","iopub.status.idle":"2023-10-04T11:52:58.893957Z","shell.execute_reply.started":"2023-10-04T11:52:58.344062Z","shell.execute_reply":"2023-10-04T11:52:58.892947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir('/kaggle/temp/test/test')\nlen(os.listdir('/kaggle/temp/test/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-04T11:53:02.867636Z","iopub.execute_input":"2023-10-04T11:53:02.867974Z","iopub.status.idle":"2023-10-04T11:53:03.017204Z","shell.execute_reply.started":"2023-10-04T11:53:02.867947Z","shell.execute_reply":"2023-10-04T11:53:03.016288Z"},"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-04T11:53:17.319798Z","iopub.execute_input":"2023-10-04T11:53:17.320219Z","iopub.status.idle":"2023-10-04T11:53:17.679384Z","shell.execute_reply.started":"2023-10-04T11:53:17.320185Z","shell.execute_reply":"2023-10-04T11:53:17.678369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl=learn.dls.test_dl(new)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T11:53:21.382843Z","iopub.execute_input":"2023-10-04T11:53:21.383260Z","iopub.status.idle":"2023-10-04T11:53:21.400847Z","shell.execute_reply.started":"2023-10-04T11:53:21.383227Z","shell.execute_reply":"2023-10-04T11:53:21.399926Z"},"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-04T11:53:25.759027Z","iopub.execute_input":"2023-10-04T11:53:25.759440Z","iopub.status.idle":"2023-10-04T12:06:44.812567Z","shell.execute_reply.started":"2023-10-04T11:53:25.759406Z","shell.execute_reply":"2023-10-04T12:06:44.811357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-04T12:28:34.197960Z","iopub.execute_input":"2023-10-04T12:28:34.198598Z","iopub.status.idle":"2023-10-04T12:28:34.221894Z","shell.execute_reply.started":"2023-10-04T12:28:34.198538Z","shell.execute_reply":"2023-10-04T12:28:34.220828Z"},"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-04T12:28:40.299510Z","iopub.execute_input":"2023-10-04T12:28:40.299939Z","iopub.status.idle":"2023-10-04T12:28:40.369827Z","shell.execute_reply.started":"2023-10-04T12:28:40.299903Z","shell.execute_reply":"2023-10-04T12:28:40.358078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score[1].item()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T12:28:44.346303Z","iopub.execute_input":"2023-10-04T12:28:44.346840Z","iopub.status.idle":"2023-10-04T12:28:44.354124Z","shell.execute_reply.started":"2023-10-04T12:28:44.346797Z","shell.execute_reply":"2023-10-04T12:28:44.353116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score","metadata":{"execution":{"iopub.status.busy":"2023-10-04T12:28:56.258591Z","iopub.execute_input":"2023-10-04T12:28:56.259022Z","iopub.status.idle":"2023-10-04T12:28:56.269425Z","shell.execute_reply.started":"2023-10-04T12:28:56.258988Z","shell.execute_reply":"2023-10-04T12:28:56.268437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classScore=class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T12:28:59.691807Z","iopub.execute_input":"2023-10-04T12:28:59.692217Z","iopub.status.idle":"2023-10-04T12:28:59.708078Z","shell.execute_reply.started":"2023-10-04T12:28:59.692184Z","shell.execute_reply":"2023-10-04T12:28:59.706984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(classScore)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T12:29:05.629566Z","iopub.execute_input":"2023-10-04T12:29:05.629979Z","iopub.status.idle":"2023-10-04T12:29:05.636907Z","shell.execute_reply.started":"2023-10-04T12:29:05.629947Z","shell.execute_reply":"2023-10-04T12:29:05.636010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-10-04T12:29:08.612804Z","iopub.execute_input":"2023-10-04T12:29:08.613221Z","iopub.status.idle":"2023-10-04T12:29:08.620674Z","shell.execute_reply.started":"2023-10-04T12:29:08.613186Z","shell.execute_reply":"2023-10-04T12:29:08.619659Z"},"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-04T12:29:15.105538Z","iopub.execute_input":"2023-10-04T12:29:15.105967Z","iopub.status.idle":"2023-10-04T12:29:15.115783Z","shell.execute_reply.started":"2023-10-04T12:29:15.105930Z","shell.execute_reply":"2023-10-04T12:29:15.114693Z"},"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-04T12:29:43.706804Z","iopub.execute_input":"2023-10-04T12:29:43.707232Z","iopub.status.idle":"2023-10-04T12:29:43.735358Z","shell.execute_reply.started":"2023-10-04T12:29:43.707198Z","shell.execute_reply":"2023-10-04T12:29:43.734421Z"},"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-04T12:29:50.635876Z","iopub.execute_input":"2023-10-04T12:29:50.636299Z","iopub.status.idle":"2023-10-04T12:29:50.825226Z","shell.execute_reply.started":"2023-10-04T12:29:50.636263Z","shell.execute_reply":"2023-10-04T12:29:50.824261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"execution":{"iopub.status.busy":"2023-10-04T12:29:59.333051Z","iopub.execute_input":"2023-10-04T12:29:59.333465Z","iopub.status.idle":"2023-10-04T12:29:59.340935Z","shell.execute_reply.started":"2023-10-04T12:29:59.333432Z","shell.execute_reply":"2023-10-04T12:29:59.340026Z"},"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-04T12:30:16.644224Z","iopub.execute_input":"2023-10-04T12:30:16.644817Z","iopub.status.idle":"2023-10-04T12:30:17.796772Z","shell.execute_reply.started":"2023-10-04T12:30:16.644776Z","shell.execute_reply":"2023-10-04T12:30:17.794465Z"},"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-04T12:30:22.108477Z","iopub.execute_input":"2023-10-04T12:30:22.108915Z","iopub.status.idle":"2023-10-04T12:30:22.873770Z","shell.execute_reply.started":"2023-10-04T12:30:22.108882Z","shell.execute_reply":"2023-10-04T12:30:22.872888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['id'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T12:30:32.628478Z","iopub.execute_input":"2023-10-04T12:30:32.628916Z","iopub.status.idle":"2023-10-04T12:30:32.694671Z","shell.execute_reply.started":"2023-10-04T12:30:32.628882Z","shell.execute_reply":"2023-10-04T12:30:32.693667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T12:30:36.228578Z","iopub.execute_input":"2023-10-04T12:30:36.228993Z","iopub.status.idle":"2023-10-04T12:30:36.904199Z","shell.execute_reply.started":"2023-10-04T12:30:36.228959Z","shell.execute_reply":"2023-10-04T12:30:36.903202Z"},"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-04T12:30:40.984131Z","iopub.execute_input":"2023-10-04T12:30:40.984622Z","iopub.status.idle":"2023-10-04T12:30:40.995048Z","shell.execute_reply.started":"2023-10-04T12:30:40.984576Z","shell.execute_reply":"2023-10-04T12:30:40.993942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}