{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":70497,"databundleVersionId":7713244,"sourceType":"competition"}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport fastai\nimport seaborn as sns\nimport torch\nfrom PIL import Image\nfrom pathlib import Path\nfrom fastai.vision.all import *\nfrom fastai.callback import *\nfrom fastai.metrics import error_rate\nfrom fastai.callback.tracker import EarlyStoppingCallback\nfrom fastai.vision.all import get_image_files\nfrom sklearn.metrics import classification_report\nfrom sklearn.metrics import confusion_matrix\n\nimport warnings\nwarnings.filterwarnings ('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:12.303177Z","iopub.execute_input":"2024-05-20T05:21:12.304174Z","iopub.status.idle":"2024-05-20T05:21:19.611952Z","shell.execute_reply.started":"2024-05-20T05:21:12.304127Z","shell.execute_reply":"2024-05-20T05:21:19.610985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path(\"/kaggle/input/csc532-fruits-categorical-classification-2024/\")","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:19.614827Z","iopub.execute_input":"2024-05-20T05:21:19.615543Z","iopub.status.idle":"2024-05-20T05:21:19.620734Z","shell.execute_reply.started":"2024-05-20T05:21:19.615502Z","shell.execute_reply":"2024-05-20T05:21:19.619420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path.ls()","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:19.622153Z","iopub.execute_input":"2024-05-20T05:21:19.622619Z","iopub.status.idle":"2024-05-20T05:21:19.637034Z","shell.execute_reply.started":"2024-05-20T05:21:19.622581Z","shell.execute_reply":"2024-05-20T05:21:19.635833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = path / \"csc532_2024_train\" / \"train\"\ntest = path / \"csc532_2024_test\" / \"test\"\nvalid = path / \"csc532_2024_validation\"/ \"validation\"\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:19.639295Z","iopub.execute_input":"2024-05-20T05:21:19.639834Z","iopub.status.idle":"2024-05-20T05:21:19.646209Z","shell.execute_reply.started":"2024-05-20T05:21:19.639791Z","shell.execute_reply":"2024-05-20T05:21:19.645243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fruits = DataBlock(blocks=(ImageBlock, CategoryBlock),\n       get_items=get_image_files,splitter=RandomSplitter(valid_pct=0.2, seed=42),get_y=parent_label,\n       item_tfms=Resize(128))","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:19.649869Z","iopub.execute_input":"2024-05-20T05:21:19.650753Z","iopub.status.idle":"2024-05-20T05:21:19.662494Z","shell.execute_reply.started":"2024-05-20T05:21:19.650708Z","shell.execute_reply":"2024-05-20T05:21:19.661219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = fruits.dataloaders(train)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:19.664127Z","iopub.execute_input":"2024-05-20T05:21:19.664594Z","iopub.status.idle":"2024-05-20T05:21:27.212250Z","shell.execute_reply.started":"2024-05-20T05:21:19.664554Z","shell.execute_reply":"2024-05-20T05:21:27.211145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dls.train_ds)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:27.213644Z","iopub.execute_input":"2024-05-20T05:21:27.214022Z","iopub.status.idle":"2024-05-20T05:21:27.221712Z","shell.execute_reply.started":"2024-05-20T05:21:27.213985Z","shell.execute_reply":"2024-05-20T05:21:27.220437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dls = ImageDataLoaders.from_folder(path, train='train', valid_pct=0.2, \n                                   #item_tfms=Resize(460), batch_tfms=[*aug_transforms(size=224), Normalize.from_stats(*imagenet_stats)])","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:27.223158Z","iopub.execute_input":"2024-05-20T05:21:27.223644Z","iopub.status.idle":"2024-05-20T05:21:27.233593Z","shell.execute_reply.started":"2024-05-20T05:21:27.223607Z","shell.execute_reply":"2024-05-20T05:21:27.232680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dls.valid_ds)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:27.234752Z","iopub.execute_input":"2024-05-20T05:21:27.235727Z","iopub.status.idle":"2024-05-20T05:21:27.248494Z","shell.execute_reply.started":"2024-05-20T05:21:27.235692Z","shell.execute_reply":"2024-05-20T05:21:27.247549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.vocab","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:27.249881Z","iopub.execute_input":"2024-05-20T05:21:27.250217Z","iopub.status.idle":"2024-05-20T05:21:27.261991Z","shell.execute_reply.started":"2024-05-20T05:21:27.250188Z","shell.execute_reply":"2024-05-20T05:21:27.260927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:27.263238Z","iopub.execute_input":"2024-05-20T05:21:27.263655Z","iopub.status.idle":"2024-05-20T05:21:29.212977Z","shell.execute_reply.started":"2024-05-20T05:21:27.263621Z","shell.execute_reply":"2024-05-20T05:21:29.211850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet34, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:29.214470Z","iopub.execute_input":"2024-05-20T05:21:29.215488Z","iopub.status.idle":"2024-05-20T05:21:29.841402Z","shell.execute_reply.started":"2024-05-20T05:21:29.215440Z","shell.execute_reply":"2024-05-20T05:21:29.840288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.model_dir = \"/kaggle/working/\"","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:29.842695Z","iopub.execute_input":"2024-05-20T05:21:29.842982Z","iopub.status.idle":"2024-05-20T05:21:29.847710Z","shell.execute_reply.started":"2024-05-20T05:21:29.842955Z","shell.execute_reply":"2024-05-20T05:21:29.846570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:29.852857Z","iopub.execute_input":"2024-05-20T05:21:29.853774Z","iopub.status.idle":"2024-05-20T05:21:30.819704Z","shell.execute_reply.started":"2024-05-20T05:21:29.853741Z","shell.execute_reply":"2024-05-20T05:21:30.818554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"suggest_funcs = (minimum, steep, valley, slide)\n\nlrs = learn.lr_find(suggest_funcs=suggest_funcs)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:21:30.823456Z","iopub.execute_input":"2024-05-20T05:21:30.823763Z","iopub.status.idle":"2024-05-20T05:22:00.360535Z","shell.execute_reply.started":"2024-05-20T05:21:30.823737Z","shell.execute_reply":"2024-05-20T05:22:00.359272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5, lr_max=slice(lrs.valley, lrs.slide))","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:22:00.362286Z","iopub.execute_input":"2024-05-20T05:22:00.362653Z","iopub.status.idle":"2024-05-20T05:26:13.146901Z","shell.execute_reply.started":"2024-05-20T05:22:00.362622Z","shell.execute_reply":"2024-05-20T05:26:13.145551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:26:13.149108Z","iopub.execute_input":"2024-05-20T05:26:13.149585Z","iopub.status.idle":"2024-05-20T05:26:13.547875Z","shell.execute_reply.started":"2024-05-20T05:26:13.149537Z","shell.execute_reply":"2024-05-20T05:26:13.546786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:26:13.552064Z","iopub.execute_input":"2024-05-20T05:26:13.552501Z","iopub.status.idle":"2024-05-20T05:26:15.461979Z","shell.execute_reply.started":"2024-05-20T05:26:13.552457Z","shell.execute_reply":"2024-05-20T05:26:15.460800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:26:15.463438Z","iopub.execute_input":"2024-05-20T05:26:15.463929Z","iopub.status.idle":"2024-05-20T05:26:36.571576Z","shell.execute_reply.started":"2024-05-20T05:26:15.463893Z","shell.execute_reply":"2024-05-20T05:26:36.570543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid = path / \"csc532_2024_validation\"/ \"validation\"","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:26:36.572980Z","iopub.execute_input":"2024-05-20T05:26:36.573291Z","iopub.status.idle":"2024-05-20T05:26:36.577681Z","shell.execute_reply.started":"2024-05-20T05:26:36.573263Z","shell.execute_reply":"2024-05-20T05:26:36.576802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = get_image_files(valid)\ntest_dl = learn.dls.test_dl(test_files)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:26:36.578730Z","iopub.execute_input":"2024-05-20T05:26:36.579006Z","iopub.status.idle":"2024-05-20T05:26:36.597088Z","shell.execute_reply.started":"2024-05-20T05:26:36.578982Z","shell.execute_reply":"2024-05-20T05:26:36.596299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds, _ = learn.get_preds(dl=test_dl)\npredicted_classes = torch.argmax(preds, dim=1).numpy()","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:26:36.598053Z","iopub.execute_input":"2024-05-20T05:26:36.598280Z","iopub.status.idle":"2024-05-20T05:26:42.388540Z","shell.execute_reply.started":"2024-05-20T05:26:36.598260Z","shell.execute_reply":"2024-05-20T05:26:42.387413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'ID': [f.stem for f in test_files],  \n    'Code': predicted_classes  \n})\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:26:42.389882Z","iopub.execute_input":"2024-05-20T05:26:42.390218Z","iopub.status.idle":"2024-05-20T05:26:42.397243Z","shell.execute_reply.started":"2024-05-20T05:26:42.390166Z","shell.execute_reply":"2024-05-20T05:26:42.396357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:26:42.398747Z","iopub.execute_input":"2024-05-20T05:26:42.399277Z","iopub.status.idle":"2024-05-20T05:26:42.416806Z","shell.execute_reply.started":"2024-05-20T05:26:42.399240Z","shell.execute_reply":"2024-05-20T05:26:42.415792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-20T05:26:42.418024Z","iopub.execute_input":"2024-05-20T05:26:42.418502Z","iopub.status.idle":"2024-05-20T05:26:42.437015Z","shell.execute_reply.started":"2024-05-20T05:26:42.418473Z","shell.execute_reply":"2024-05-20T05:26:42.435920Z"},"trusted":true},"execution_count":null,"outputs":[]}]}