{"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":"# To ignore warinings\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:37:45.307587Z","iopub.execute_input":"2022-11-01T10:37:45.308210Z","iopub.status.idle":"2022-11-01T10:37:45.313927Z","shell.execute_reply.started":"2022-11-01T10:37:45.308167Z","shell.execute_reply":"2022-11-01T10:37:45.312734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Importing required libraries**","metadata":{}},{"cell_type":"code","source":"!pip install -Uqq fastai","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:37:45.316172Z","iopub.execute_input":"2022-11-01T10:37:45.317320Z","iopub.status.idle":"2022-11-01T10:37:55.372342Z","shell.execute_reply.started":"2022-11-01T10:37:45.317284Z","shell.execute_reply":"2022-11-01T10:37:55.371020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai.vision.widgets import *","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:37:55.375257Z","iopub.execute_input":"2022-11-01T10:37:55.375678Z","iopub.status.idle":"2022-11-01T10:37:55.384125Z","shell.execute_reply.started":"2022-11-01T10:37:55.375634Z","shell.execute_reply":"2022-11-01T10:37:55.382925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:37:55.387462Z","iopub.execute_input":"2022-11-01T10:37:55.387806Z","iopub.status.idle":"2022-11-01T10:37:55.394826Z","shell.execute_reply.started":"2022-11-01T10:37:55.387780Z","shell.execute_reply":"2022-11-01T10:37:55.393825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Loading data and splitting it into training and validation set**","metadata":{}},{"cell_type":"code","source":"path = Path('../input/finding-disney/train')","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:37:55.396769Z","iopub.execute_input":"2022-11-01T10:37:55.397180Z","iopub.status.idle":"2022-11-01T10:37:55.405530Z","shell.execute_reply.started":"2022-11-01T10:37:55.397143Z","shell.execute_reply":"2022-11-01T10:37:55.404576Z"},"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":"2022-11-01T10:37:55.408691Z","iopub.execute_input":"2022-11-01T10:37:55.409424Z","iopub.status.idle":"2022-11-01T10:37:57.083412Z","shell.execute_reply.started":"2022-11-01T10:37:55.409396Z","shell.execute_reply":"2022-11-01T10:37:57.082492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Defining our learner (model) which uses a pre-trained ResNet-50**","metadata":{}},{"cell_type":"code","source":"learn = vision_learner(dls, resnet50, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:37:57.084717Z","iopub.execute_input":"2022-11-01T10:37:57.085734Z","iopub.status.idle":"2022-11-01T10:37:57.617166Z","shell.execute_reply.started":"2022-11-01T10:37:57.085699Z","shell.execute_reply":"2022-11-01T10:37:57.616035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training our learner","metadata":{}},{"cell_type":"code","source":"learn.fine_tune(epochs=8)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:37:57.618759Z","iopub.execute_input":"2022-11-01T10:37:57.619189Z","iopub.status.idle":"2022-11-01T10:48:45.458525Z","shell.execute_reply.started":"2022-11-01T10:37:57.619149Z","shell.execute_reply":"2022-11-01T10:48:45.457436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Looking at a few predictions of our model","metadata":{}},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:48:45.460203Z","iopub.execute_input":"2022-11-01T10:48:45.461027Z","iopub.status.idle":"2022-11-01T10:48:48.007956Z","shell.execute_reply.started":"2022-11-01T10:48:45.460969Z","shell.execute_reply":"2022-11-01T10:48:48.007047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Looking at the images for which the losses were high","metadata":{}},{"cell_type":"code","source":"interp = Interpretation.from_learner(learn)\ninterp.plot_top_losses(9, figsize=(15,10))","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:48:48.012955Z","iopub.execute_input":"2022-11-01T10:48:48.014842Z","iopub.status.idle":"2022-11-01T10:49:04.267823Z","shell.execute_reply.started":"2022-11-01T10:48:48.014806Z","shell.execute_reply":"2022-11-01T10:49:04.266927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plotting a confusion-matrix to look at misclassification","metadata":{}},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:49:04.269295Z","iopub.execute_input":"2022-11-01T10:49:04.270396Z","iopub.status.idle":"2022-11-01T10:49:20.136037Z","shell.execute_reply.started":"2022-11-01T10:49:04.270356Z","shell.execute_reply":"2022-11-01T10:49:20.134952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:49:20.137708Z","iopub.execute_input":"2022-11-01T10:49:20.138462Z","iopub.status.idle":"2022-11-01T10:49:35.944737Z","shell.execute_reply.started":"2022-11-01T10:49:20.138417Z","shell.execute_reply":"2022-11-01T10:49:35.943276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Creating the final submission file**","metadata":{}},{"cell_type":"code","source":"test_path = Path('../input/finding-disney/test')\ntest_X = np.array(os.listdir(test_path))","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:50:44.869220Z","iopub.execute_input":"2022-11-01T10:50:44.869836Z","iopub.status.idle":"2022-11-01T10:50:44.877342Z","shell.execute_reply.started":"2022-11-01T10:50:44.869796Z","shell.execute_reply":"2022-11-01T10:50:44.876234Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:50:44.879087Z","iopub.execute_input":"2022-11-01T10:50:44.879524Z","iopub.status.idle":"2022-11-01T10:51:03.189018Z","shell.execute_reply.started":"2022-11-01T10:50:44.879488Z","shell.execute_reply":"2022-11-01T10:51:03.188038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"character_id = np.array([char_id.strip('.jpg') for char_id in test_X])","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:51:03.190691Z","iopub.execute_input":"2022-11-01T10:51:03.191090Z","iopub.status.idle":"2022-11-01T10:51:03.197765Z","shell.execute_reply.started":"2022-11-01T10:51:03.191050Z","shell.execute_reply":"2022-11-01T10:51:03.196805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(data=[character_id, test_y]).T","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:51:03.199623Z","iopub.execute_input":"2022-11-01T10:51:03.199970Z","iopub.status.idle":"2022-11-01T10:51:03.228574Z","shell.execute_reply.started":"2022-11-01T10:51:03.199943Z","shell.execute_reply":"2022-11-01T10:51:03.227462Z"},"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":"2022-11-01T10:51:03.231425Z","iopub.execute_input":"2022-11-01T10:51:03.231743Z","iopub.status.idle":"2022-11-01T10:51:03.247006Z","shell.execute_reply.started":"2022-11-01T10:51:03.231714Z","shell.execute_reply":"2022-11-01T10:51:03.245905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-01T10:51:03.249319Z","iopub.execute_input":"2022-11-01T10:51:03.249715Z","iopub.status.idle":"2022-11-01T10:51:03.257786Z","shell.execute_reply.started":"2022-11-01T10:51:03.249687Z","shell.execute_reply":"2022-11-01T10:51:03.256764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}