{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":40012,"databundleVersionId":4380823,"sourceType":"competition"}],"dockerImageVersionId":30748,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-08-07T12:43:36.988038Z","iopub.execute_input":"2024-08-07T12:43:36.988775Z","iopub.status.idle":"2024-08-07T12:43:40.000722Z","shell.execute_reply.started":"2024-08-07T12:43:36.988742Z","shell.execute_reply":"2024-08-07T12:43:40.000054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ipywidgets","metadata":{"execution":{"iopub.status.busy":"2024-08-07T12:45:50.267412Z","iopub.execute_input":"2024-08-07T12:45:50.267760Z","iopub.status.idle":"2024-08-07T12:45:54.722725Z","shell.execute_reply.started":"2024-08-07T12:45:50.267733Z","shell.execute_reply":"2024-08-07T12:45:54.721653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -Uqq fastai\n\nfrom fastai.vision.all import *\nfrom fastai.vision.widgets import *\n\nfrom PIL import ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T12:46:02.206994Z","iopub.execute_input":"2024-08-07T12:46:02.207750Z","iopub.status.idle":"2024-08-07T12:46:06.361074Z","shell.execute_reply.started":"2024-08-07T12:46:02.207711Z","shell.execute_reply":"2024-08-07T12:46:06.359965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/finding-disney/train')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T12:46:09.728800Z","iopub.execute_input":"2024-08-07T12:46:09.729159Z","iopub.status.idle":"2024-08-07T12:46:09.733030Z","shell.execute_reply.started":"2024-08-07T12:46:09.729114Z","shell.execute_reply":"2024-08-07T12:46:09.732424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = DataBlock(\nblocks=(ImageBlock, CategoryBlock),\nget_items=get_image_files,\nsplitter=RandomSplitter(valid_pct=0.2, seed=42),\nget_y=parent_label,\nitem_tfms=[Resize(192, method='squish')]\n).dataloaders(path)\n\ndls.show_batch(max_n=6)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T12:46:11.874962Z","iopub.execute_input":"2024-08-07T12:46:11.875326Z","iopub.status.idle":"2024-08-07T12:46:16.053851Z","shell.execute_reply.started":"2024-08-07T12:46:11.875295Z","shell.execute_reply":"2024-08-07T12:46:16.053085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet50, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T12:46:20.144945Z","iopub.execute_input":"2024-08-07T12:46:20.145797Z","iopub.status.idle":"2024-08-07T12:46:21.252845Z","shell.execute_reply.started":"2024-08-07T12:46:20.145761Z","shell.execute_reply":"2024-08-07T12:46:21.251845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(epochs=8)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T12:46:23.964571Z","iopub.execute_input":"2024-08-07T12:46:23.965669Z","iopub.status.idle":"2024-08-07T13:09:07.928239Z","shell.execute_reply.started":"2024-08-07T12:46:23.965627Z","shell.execute_reply":"2024-08-07T13:09:07.926910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:19:29.159482Z","iopub.execute_input":"2024-08-07T13:19:29.159918Z","iopub.status.idle":"2024-08-07T13:19:31.690182Z","shell.execute_reply.started":"2024-08-07T13:19:29.159884Z","shell.execute_reply":"2024-08-07T13:19:31.689330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = Interpretation.from_learner(learn)\ninterp.plot_top_losses(9, figsize=(15,10))","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:19:54.212792Z","iopub.execute_input":"2024-08-07T13:19:54.213233Z","iopub.status.idle":"2024-08-07T13:20:06.052412Z","shell.execute_reply.started":"2024-08-07T13:19:54.213198Z","shell.execute_reply":"2024-08-07T13:20:06.051632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:20:29.181180Z","iopub.execute_input":"2024-08-07T13:20:29.181661Z","iopub.status.idle":"2024-08-07T13:20:37.632431Z","shell.execute_reply.started":"2024-08-07T13:20:29.181608Z","shell.execute_reply":"2024-08-07T13:20:37.631552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:20:47.858316Z","iopub.execute_input":"2024-08-07T13:20:47.858744Z","iopub.status.idle":"2024-08-07T13:20:56.944482Z","shell.execute_reply.started":"2024-08-07T13:20:47.858705Z","shell.execute_reply":"2024-08-07T13:20:56.943230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = Path('../input/finding-disney/test')\ntest_X = np.array(os.listdir(test_path))","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:21:01.696371Z","iopub.execute_input":"2024-08-07T13:21:01.696761Z","iopub.status.idle":"2024-08-07T13:21:01.702050Z","shell.execute_reply.started":"2024-08-07T13:21:01.696727Z","shell.execute_reply":"2024-08-07T13:21:01.701360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_y = []\nfor img in test_X:\n    with learn.no_bar(), learn.no_logging():\n        test_y.append(learn.predict(test_path/img)[0])\ntest_y = np.array(test_y)\n\ncharacter_id = np.array([char_id.strip('.jpg') for char_id in test_X])\n\nsubmission = pd.DataFrame(data=[character_id, test_y]).T\n","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:21:04.341634Z","iopub.execute_input":"2024-08-07T13:21:04.342019Z","iopub.status.idle":"2024-08-07T13:21:38.416897Z","shell.execute_reply.started":"2024-08-07T13:21:04.341981Z","shell.execute_reply":"2024-08-07T13:21:38.415847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.columns = ['character_id', 'label']\nsubmission.set_index('character_id', inplace=True)\nsubmission.head(4)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:22:56.844001Z","iopub.execute_input":"2024-08-07T13:22:56.844455Z","iopub.status.idle":"2024-08-07T13:22:56.858766Z","shell.execute_reply.started":"2024-08-07T13:22:56.844416Z","shell.execute_reply":"2024-08-07T13:22:56.857983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:23:14.542618Z","iopub.execute_input":"2024-08-07T13:23:14.543015Z","iopub.status.idle":"2024-08-07T13:23:14.548846Z","shell.execute_reply.started":"2024-08-07T13:23:14.542982Z","shell.execute_reply":"2024-08-07T13:23:14.548191Z"},"trusted":true},"execution_count":null,"outputs":[]}]}